Skip to content

Commit

Permalink
Handle the return code of the socket functions and prints an error me…
Browse files Browse the repository at this point in the history
…ssage when relevant.
  • Loading branch information
bcoconni committed May 3, 2024
1 parent 0658867 commit ac0b4c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/input_output/FGfdmSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ string FGfdmSocket::Receive(void)
int flags = fcntl(sckt_in, F_GETFL, 0);
fcntl(sckt_in, F_SETFL, flags | O_NONBLOCK);

Check warning on line 270 in src/input_output/FGfdmSocket.cpp

View check run for this annotation

Codecov / codecov/patch

src/input_output/FGfdmSocket.cpp#L269-L270

Added lines #L269 - L270 were not covered by tests
#endif
send(sckt_in, "Connected to JSBSim server\n\rJSBSim> ", 36, 0);
if (send(sckt_in, "Connected to JSBSim server\n\rJSBSim> ", 36, 0) == SOCKET_ERROR)
PrintSocketError();

Check warning on line 273 in src/input_output/FGfdmSocket.cpp

View check run for this annotation

Codecov / codecov/patch

src/input_output/FGfdmSocket.cpp#L272-L273

Added lines #L272 - L273 were not covered by tests
}
}

Expand All @@ -278,7 +279,9 @@ string FGfdmSocket::Receive(void)

while ((num_chars = recv(sckt_in, buf, sizeof buf, 0)) > 0) {
data.append(buf, num_chars);
}
}

if (num_chars == SOCKET_ERROR) PrintSocketError();

Check warning on line 284 in src/input_output/FGfdmSocket.cpp

View check run for this annotation

Codecov / codecov/patch

src/input_output/FGfdmSocket.cpp#L284

Added line #L284 was not covered by tests

#ifdef _WIN32
// when nothing received and the error isn't "would block"
Expand All @@ -301,6 +304,7 @@ string FGfdmSocket::Receive(void)
socklen_t fromlen = sizeof addr;
int num_chars = recvfrom(sckt, buf, sizeof buf, 0, (struct sockaddr*)&addr, &fromlen);
if (num_chars > 0) data.append(buf, num_chars);
if (num_chars == SOCKET_ERROR) PrintSocketError();

Check warning on line 307 in src/input_output/FGfdmSocket.cpp

View check run for this annotation

Codecov / codecov/patch

src/input_output/FGfdmSocket.cpp#L307

Added line #L307 was not covered by tests
}

return data;
Expand All @@ -314,6 +318,7 @@ int FGfdmSocket::Reply(const string& text)

if (sckt_in != INVALID_SOCKET) {
num_chars_sent = send(sckt_in, text.c_str(), text.size(), 0);
if (num_chars_sent == SOCKET_ERROR) PrintSocketError();

Check warning on line 321 in src/input_output/FGfdmSocket.cpp

View check run for this annotation

Codecov / codecov/patch

src/input_output/FGfdmSocket.cpp#L321

Added line #L321 was not covered by tests
send(sckt_in, "JSBSim> ", 8, 0);
} else {
cerr << "Socket reply must be to a valid socket" << endl;
Expand Down Expand Up @@ -383,11 +388,13 @@ void FGfdmSocket::Send(void)
void FGfdmSocket::Send(const char *data, int length)
{
if (Protocol == ptTCP && sckt_in != INVALID_SOCKET) {
if ((send(sckt_in,data,length,0)) > 0) return;
if ((send(sckt_in, data, length, 0)) == SOCKET_ERROR) PrintSocketError();
return;

Check warning on line 392 in src/input_output/FGfdmSocket.cpp

View check run for this annotation

Codecov / codecov/patch

src/input_output/FGfdmSocket.cpp#L390-L392

Added lines #L390 - L392 were not covered by tests
}

if (Protocol == ptUDP && sckt != INVALID_SOCKET) {
if ((send(sckt,data,length,0)) > 0) return;
if ((send(sckt, data, length, 0)) == SOCKET_ERROR) PrintSocketError();
return;

Check warning on line 397 in src/input_output/FGfdmSocket.cpp

View check run for this annotation

Codecov / codecov/patch

src/input_output/FGfdmSocket.cpp#L395-L397

Added lines #L395 - L397 were not covered by tests
}

cerr << "Data sending must be to a valid socket" << endl;

Check warning on line 400 in src/input_output/FGfdmSocket.cpp

View check run for this annotation

Codecov / codecov/patch

src/input_output/FGfdmSocket.cpp#L400

Added line #L400 was not covered by tests
Expand All @@ -412,6 +419,11 @@ void FGfdmSocket::WaitUntilReadable(void)
} else if (result != SOCKET_ERROR)
return;

Check warning on line 420 in src/input_output/FGfdmSocket.cpp

View check run for this annotation

Codecov / codecov/patch

src/input_output/FGfdmSocket.cpp#L416-L420

Added lines #L416 - L420 were not covered by tests

PrintSocketError();

Check warning on line 422 in src/input_output/FGfdmSocket.cpp

View check run for this annotation

Codecov / codecov/patch

src/input_output/FGfdmSocket.cpp#L422

Added line #L422 was not covered by tests
}

void FGfdmSocket::PrintSocketError(void)

Check warning on line 425 in src/input_output/FGfdmSocket.cpp

View check run for this annotation

Codecov / codecov/patch

src/input_output/FGfdmSocket.cpp#L425

Added line #L425 was not covered by tests
{
// An error has occurred, display the error message.
cerr << "Socket error: ";

Check warning on line 428 in src/input_output/FGfdmSocket.cpp

View check run for this annotation

Codecov / codecov/patch

src/input_output/FGfdmSocket.cpp#L428

Added line #L428 was not covered by tests
#ifdef _WIN32
Expand Down
1 change: 1 addition & 0 deletions src/input_output/FGfdmSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class FGfdmSocket : public FGJSBBase
std::ostringstream buffer;
int precision;
bool connected;
void PrintSocketError(void);
void Debug(int from);
};
}
Expand Down

0 comments on commit ac0b4c7

Please sign in to comment.