Skip to content

Commit 45d8158

Browse files
committed
[net] Make TSocket::Recv slightly more readable
1 parent e8410af commit 45d8158

1 file changed

Lines changed: 44 additions & 34 deletions

File tree

net/net/src/TSocket.cxx

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -804,67 +804,77 @@ Int_t TSocket::Recv(Int_t &status, Int_t &kind)
804804
/// Returns length of message in bytes (can be 0 if other side of connection
805805
/// is closed) or -1 in case of error or -4 in case a non-blocking socket
806806
/// would block (i.e. there is nothing to be read) or -5 if pipe broken
807-
/// or reset by peer (EPIPE || ECONNRESET). In those case mess == 0.
807+
/// or reset by peer (EPIPE || ECONNRESET). In those case mess == nullptr.
808808

809809
Int_t TSocket::Recv(TMessage *&mess)
810810
{
811811
TSystem::ResetErrno();
812812

813813
if (!IsValid()) {
814-
mess = 0;
814+
mess = nullptr;
815815
return -1;
816816
}
817817

818-
oncemore:
819-
ResetBit(TSocket::kBrokenConn);
820818
Int_t n;
821-
UInt_t len;
822-
if ((n = gSystem->RecvRaw(fSocket, &len, sizeof(UInt_t), 0)) <= 0) {
823-
if (n == 0 || n == -5) {
824-
// Connection closed, reset or broken
825-
MarkBrokenConnection();
819+
while (1) {
820+
ResetBit(TSocket::kBrokenConn);
821+
UInt_t len;
822+
if ((n = gSystem->RecvRaw(fSocket, &len, sizeof(UInt_t), 0)) <= 0) {
823+
if (n == 0 || n == -5) {
824+
// Connection closed, reset or broken
825+
MarkBrokenConnection();
826+
}
827+
mess = nullptr;
828+
return n;
826829
}
827-
mess = 0;
828-
return n;
829-
}
830-
len = net2host(len); //from network to host byte order
830+
len = net2host(len); //from network to host byte order
831831

832-
ResetBit(TSocket::kBrokenConn);
833-
char *buf = new char[len+sizeof(UInt_t)];
834-
if ((n = gSystem->RecvRaw(fSocket, buf+sizeof(UInt_t), len, 0)) <= 0) {
835-
if (n == 0 || n == -5) {
836-
// Connection closed, reset or broken
837-
MarkBrokenConnection();
832+
ResetBit(TSocket::kBrokenConn);
833+
char *buf = new char[len+sizeof(UInt_t)];
834+
if ((n = gSystem->RecvRaw(fSocket, buf+sizeof(UInt_t), len, 0)) <= 0) {
835+
if (n == 0 || n == -5) {
836+
// Connection closed, reset or broken
837+
MarkBrokenConnection();
838+
}
839+
delete [] buf;
840+
mess = nullptr;
841+
return n;
838842
}
839-
delete [] buf;
840-
mess = 0;
841-
return n;
842-
}
843843

844-
fBytesRecv += n + sizeof(UInt_t);
845-
fgBytesRecv += n + sizeof(UInt_t);
844+
fBytesRecv += n + sizeof(UInt_t);
845+
fgBytesRecv += n + sizeof(UInt_t);
846+
847+
// `buf` becomes owned by the TMessage.
848+
mess = new TMessage(buf, len+sizeof(UInt_t));
846849

847-
mess = new TMessage(buf, len+sizeof(UInt_t));
850+
// receive any streamer infos
851+
bool streamerInfoReceived = RecvStreamerInfos(mess);
852+
if (streamerInfoReceived) {
853+
// do another loop. No need to delete `mess` because RecvStreamerInfos already did it.
854+
continue;
855+
}
848856

849-
// receive any streamer infos
850-
if (RecvStreamerInfos(mess))
851-
goto oncemore;
857+
// receive any process ids
858+
bool processIdReceived = RecvProcessIDs(mess);
859+
if (processIdReceived) {
860+
// do another loop. No need to delete `mess` because RecvProcessIDs already did it.
861+
continue;
862+
}
852863

853-
// receive any process ids
854-
if (RecvProcessIDs(mess))
855-
goto oncemore;
864+
break;
865+
}
856866

857867
if (mess->What() & kMESS_ACK) {
858868
ResetBit(TSocket::kBrokenConn);
859-
char ok[2] = { 'o', 'k' };
869+
const char ok[2] = { 'o', 'k' };
860870
Int_t n2 = 0;
861871
if ((n2 = gSystem->SendRaw(fSocket, ok, sizeof(ok), 0)) < 0) {
862872
if (n2 == -5) {
863873
// Connection reset or broken
864874
MarkBrokenConnection();
865875
}
866876
delete mess;
867-
mess = 0;
877+
mess = nullptr;
868878
return n2;
869879
}
870880
mess->SetWhat(mess->What() & ~kMESS_ACK);

0 commit comments

Comments
 (0)