-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Previously, if an emulated TCP socket is connect()ed and one attempts…
… to send() back to back right after connecting, the send would fail because the socket is not yet actually connected. It is a bit hard to imagine where that behavior would be useful. So this PR changes this behavior to be identical to how connectionless UDP sockets are emulated: instead, connect() calls are always pretended to succeed (since we cannot synchronously establish if the connect would fail, so presume it'll work), and all send() calls that are issued while the socket is connecting will be queued to be sent after the socket actually connects.
- Loading branch information
Showing
3 changed files
with
90 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// This test verifies that calling send() back to back right after calling | ||
// connect() succeeds. | ||
|
||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <arpa/inet.h> | ||
#include <emscripten/html5.h> | ||
#include <string.h> | ||
#include <stdio.h> | ||
|
||
int sock; | ||
|
||
EM_BOOL wait_for_recv(double, void *) { | ||
// Poll read from the socket to see what we have received | ||
char buf[1024] = {}; | ||
recv(sock, buf, sizeof(buf)-1, 0); | ||
if (strlen(buf) > 0) | ||
{ | ||
printf("%s\n", buf); | ||
if (!strcmp(buf, "Hello")) | ||
{ | ||
printf("Got hello, test finished.\n"); | ||
#ifdef REPORT_RESULT | ||
REPORT_RESULT(0); | ||
#endif | ||
} | ||
} | ||
return EM_TRUE; | ||
} | ||
|
||
int main() { | ||
// Connect socket to a WebSocket echo server | ||
sockaddr_in addr = { | ||
.sin_family = AF_INET, | ||
.sin_port = htons(8089) | ||
}; | ||
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); | ||
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | ||
if (sock < 0) | ||
{ | ||
printf("socket() failed to error %d\n", sock); | ||
return sock; | ||
} | ||
|
||
// Connect to echo server. | ||
int error = connect(sock, (sockaddr*)&addr, sizeof(addr)); | ||
if (error) | ||
{ | ||
printf("connect() failed to error %d\n", error); | ||
return error; | ||
} | ||
|
||
// Immediately send a message back-to-back from connecting to the socket | ||
const char *msg = "Hello"; | ||
ssize_t bytes = send(sock, msg, strlen(msg), 0); | ||
if (bytes != strlen(msg)) | ||
{ | ||
printf("send() failed to send %d bytes. Return value: %d\n", (int)strlen(msg), (int)bytes); | ||
return bytes; | ||
} | ||
|
||
// Asynchronously wait until we get the message echoed back | ||
emscripten_set_timeout_loop(wait_for_recv, 0, 0); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters