diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-08-10 18:44:42 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-08-10 18:44:42 -0700 |
commit | 3ecb9d7f45e8af5a980940af3ed8bc17efd1a300 (patch) | |
tree | c80166f322c334ceff4cc20332cb276b24d66be8 /tests/sockets | |
parent | ed84a998aeeb7930bdfa9935f3fdeaf9cf7c0cf2 (diff) | |
parent | 1cac914671205affad037b6777378d876cf6e36a (diff) |
Merge pull request #1495 from inolen/websocket_tests
more websocket test updates
Diffstat (limited to 'tests/sockets')
19 files changed, 922 insertions, 862 deletions
diff --git a/tests/sockets/socket_relay.py b/tests/sockets/socket_relay.py new file mode 100644 index 00000000..5b6403f9 --- /dev/null +++ b/tests/sockets/socket_relay.py @@ -0,0 +1,56 @@ +''' +Listens on 2 ports and relays between them. + +Listens to ports A and B. When someone connects to port A, and then +sends some data to port A, that data is sent to someone who +connected to socket B. And so forth. + +This is different than say socat which will listen to one port +and then make a connection to another port, and do bidirectional +communication. We need to actually listen on both ports. +''' + +import os, sys, socket, time, threading, signal +from subprocess import Popen, PIPE, STDOUT + +ports = [int(sys.argv[1]), int(sys.argv[2])] + +class Listener(threading.Thread): + def run(self): + self.conn = None + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + global ports + port = ports[0] + ports = ports[1:] + print 'listener binding to ', port + s.bind(('127.0.0.1', port)) + s.listen(1) + print 'listener', port, 'waiting for connection' + conn, addr = s.accept() + self.conn = conn + while 1: + time.sleep(0.5) + print 'listener', port, 'waiting for data' + data = conn.recv(20*1024) + if not data: + continue + while not self.other.conn: + print 'listener', port, 'waiting for other connection in order to send data' + time.sleep(1) + print 'listener', port, 'sending data', len(data) + self.other.conn.send(data) + +in_listener = Listener() +in_listener.daemon = True +in_listener.start() + +out_listener = Listener() +out_listener.daemon = True +out_listener.start() + +in_listener.other = out_listener +out_listener.other = in_listener + +while 1: + time.sleep(1) + diff --git a/tests/sockets/test_enet_client.c b/tests/sockets/test_enet_client.c new file mode 100644 index 00000000..bf14375c --- /dev/null +++ b/tests/sockets/test_enet_client.c @@ -0,0 +1,113 @@ +#include <stdio.h> +#include <string.h> +#include <enet/enet.h> +#if EMSCRIPTEN +#include <emscripten.h> +#endif + +ENetHost * host; + +void main_loop() { +#if EMSCRIPTEN + static int counter = 0; + counter++; + if (counter == 100) { + printf("stop!\n"); + emscripten_cancel_main_loop(); + return; + } +#endif + + ENetEvent event; + if (enet_host_service (host, & event, 0) == 0) return; + switch (event.type) + { + case ENET_EVENT_TYPE_CONNECT: + printf ("Connection succeeded!\n"); + + break; + case ENET_EVENT_TYPE_RECEIVE: + printf ("A packet of length %u containing %s was received from %s on channel %u.\n", + event.packet -> dataLength, + event.packet -> data, + event.peer -> data, + event.channelID); + + int result = strcmp("packetfoo", event.packet->data); +#if EMSCRIPTEN + REPORT_RESULT(); +#else + exit(EXIT_SUCCESS); +#endif + + /* Clean up the packet now that we're done using it. */ + enet_packet_destroy (event.packet); + break; + case ENET_EVENT_TYPE_DISCONNECT: + printf ("%s disconected.\n", event.peer -> data); + /* Reset the peer's client information. */ + event.peer -> data = NULL; + enet_host_destroy(host); + break; + default: + printf("whaaa? %d\n", event.type); + } +} + +int main (int argc, char ** argv) +{ + if (enet_initialize () != 0) + { + fprintf (stderr, "An error occurred while initializing ENet.\n"); + return EXIT_FAILURE; + } + atexit (enet_deinitialize); + + printf("creating host\n"); + + host = enet_host_create (NULL /* create a client host */, + 1 /* only allow 1 outgoing connection */, + 2 /* allow up 2 channels to be used, 0 and 1 */, + 57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */, + 14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */); + if (host == NULL) + { + fprintf (stderr, + "An error occurred while trying to create an ENet client host.\n"); + exit (EXIT_FAILURE); + } + + ENetAddress address; + enet_address_set_host (& address, "localhost"); + address.port = SOCKK; + + printf("connecting to server...\n"); + + ENetPeer *peer = enet_host_connect (host, & address, 2, 0); + + if (peer == NULL) + { + fprintf (stderr, + "No available peers for initiating an ENet connection.\n"); + exit (EXIT_FAILURE); + } + +#if EMSCRIPTEN + emscripten_run_script("console.log('adding iframe');" + "var iframe = document.createElement('iframe');" + "iframe.src = 'server.html';" + "iframe.width = '100%';" + "iframe.height = '33%';" + "document.body.appendChild(iframe);" + "console.log('added.');"); +#endif + +#if EMSCRIPTEN + emscripten_set_main_loop(main_loop, 3, 1); +#else + while (1) main_loop(); +#endif + + return 1; +} + diff --git a/tests/sockets/test_enet_server.c b/tests/sockets/test_enet_server.c new file mode 100644 index 00000000..9a4518ac --- /dev/null +++ b/tests/sockets/test_enet_server.c @@ -0,0 +1,112 @@ +// g++ -fpermissive ../enet_server.c -I/home/alon/Dev/emscripten/system/include/emscripten/ -Iinclude/ -fpermissive .libs/libenet.a -o enet_server ; g++ ../enet_client.c -I/home/alon/Dev/emscripten/system/include/emscripten/ -Iinclude/ -fpermissive .libs/libenet.a -o enet_client + +#include <stdio.h> +#include <string.h> +#include <enet/enet.h> + +#if EMSCRIPTEN +#include <emscripten.h> +#endif + +ENetHost *host; + +void send_msg(ENetPeer *peer) { + /* Create a reliable packet of size 7 containing "packet\0" */ + ENetPacket * packet = enet_packet_create ("packet", + strlen ("packet") + 1, + ENET_PACKET_FLAG_RELIABLE); + /* Extend the packet so and append the string "foo", so it now */ + /* contains "packetfoo\0" */ + enet_packet_resize (packet, strlen ("packetfoo") + 1); + strcpy (& packet -> data [strlen ("packet")], "foo"); + /* Send the packet to the peer over channel id 0. */ + /* One could also broadcast the packet by */ + /* enet_host_broadcast (host, 0, packet); */ + enet_peer_send (peer, 0, packet); + /* One could just use enet_host_service() instead. */ + enet_host_flush (host); +} + +void main_loop() { + static int counter = 0; +#if EMSCRIPTEN + counter++; +#endif + if (counter == 100) { + printf("stop!\n"); +#if EMSCRIPTEN + emscripten_cancel_main_loop(); +#endif + return; + } + + ENetEvent event; +//printf("enet host?\n"); + if (enet_host_service (host, & event, 0) == 0) return; +printf("enet host, got event of type %d\n", event.type); + switch (event.type) + { + case ENET_EVENT_TYPE_CONNECT: + printf ("A new client connected from %x:%u.\n", + event.peer -> address.host, + event.peer -> address.port); + /* Store any relevant client information here. */ + event.peer -> data = "Client information"; + + send_msg(event.peer); + + break; + case ENET_EVENT_TYPE_RECEIVE: + printf ("A packet of length %u containing %s was received from %s on channel %u.\n", + event.packet -> dataLength, + event.packet -> data, + event.peer -> data, + event.channelID); + /* Clean up the packet now that we're done using it. */ + enet_packet_destroy (event.packet); + break; + case ENET_EVENT_TYPE_DISCONNECT: + printf ("%s disconected.\n", event.peer -> data); + /* Reset the peer's client information. */ + event.peer -> data = NULL; + enet_host_destroy(host); + break; + default: + printf("whaaa? %d\n", event.type); + } +} + +int main (int argc, char ** argv) +{ + if (enet_initialize () != 0) + { + fprintf (stderr, "An error occurred while initializing ENet.\n"); + return EXIT_FAILURE; + } + atexit (enet_deinitialize); + + ENetAddress address; + address.host = ENET_HOST_ANY; + address.port = SOCKK; + printf("create!\n"); + host = enet_host_create (& address /* the address to bind the server host to */, + 32 /* allow up to 32 clients and/or outgoing connections */, + 2 /* allow up to 2 channels to be used, 0 and 1 */, + 0 /* assume any amount of incoming bandwidth */, + 0 /* assume any amount of outgoing bandwidth */); + if (host == NULL) + { + fprintf (stderr, + "An error occurred while trying to create an ENet server host.\n"); + exit (EXIT_FAILURE); + } + +#if EMSCRIPTEN + emscripten_set_main_loop(main_loop, 3, 1); +#else + while (1) main_loop(); +#endif + + return 1; +} + diff --git a/tests/sockets/test_sockets.c b/tests/sockets/test_sockets.c deleted file mode 100644 index 8845ef43..00000000 --- a/tests/sockets/test_sockets.c +++ /dev/null @@ -1,143 +0,0 @@ -#include <errno.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/ioctl.h> -#include <assert.h> -#if EMSCRIPTEN -#include <emscripten.h> -#endif - -#define EXPECTED_BYTES 5 - -int sockfd; -int not_always_data = 0; - -void finish(int result) { - close(sockfd); -#if EMSCRIPTEN - REPORT_RESULT(); -#endif - exit(result); -} - -unsigned int get_all_buf(int sock, char* output, unsigned int maxsize) { - // select check for IO - fd_set sett; - FD_ZERO(&sett); - assert(select(64, &sett, NULL, NULL, NULL) == 0); // empty set - FD_SET(sock, &sett); - assert(select(0, &sett, NULL, NULL, NULL) == 0); // max FD to check is 0 - assert(FD_ISSET(sock, &sett) == 0); - FD_SET(sock, &sett); - int select_says_yes = select(64, &sett, NULL, NULL, NULL); - - // ioctl check for IO - int bytes; - if (ioctl(sock, FIONREAD, &bytes) || bytes == 0) { - not_always_data = 1; - printf("ioctl says 0, FD_ISSET says %ld\n", FD_ISSET(sock, &sett)); - assert(FD_ISSET(sock, &sett) == 0); - return 0; - } - - assert(FD_ISSET(sock, &sett)); - assert(select_says_yes); // ioctl must agree with select - - char buffer[1024]; - int n; - unsigned int offset = 0; - while((errno = 0, (n = recv(sock, buffer, sizeof(buffer), 0))>0) || - errno == EINTR) { - if(n > 0) { - if (((unsigned int) n)+offset > maxsize) { - fprintf(stderr, "too much data!"); - finish(EXIT_FAILURE); - } - memcpy(output+offset, buffer, n); - offset += n; - } - } - - if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { - fprintf(stderr, "error in get_all_buf! %d", errno); - finish(EXIT_FAILURE); - } - return offset; -} - -void iter(void *arg) { - static char out[1024*2]; - static int pos = 0; - fd_set fdr; - int res; - - // make sure that sockfd is ready to read - FD_ZERO(&fdr); - FD_SET(sockfd, &fdr); - res = select(64, &fdr, NULL, NULL, NULL); - if (res == -1) { - perror("select failed"); - finish(EXIT_FAILURE); - } else if (!FD_ISSET(sockfd, &fdr)) { - return; - } - - // perform read write operations ... - int n = get_all_buf(sockfd, out+pos, 1024-pos); - if (n) printf("read! %d\n", n); - pos += n; - if (pos >= EXPECTED_BYTES) { - int i, sum = 0; - for (i=0; i < pos; i++) { - printf("%x\n", out[i]); - sum += out[i]; - } - - shutdown(sockfd, SHUT_RDWR); - - close(sockfd); - - printf("sum: %d\n", sum); - finish(sum); - } -} - -void main() { - struct sockaddr_in addr; - int res; - - sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); - if (sockfd == -1) { - perror("cannot create socket"); - finish(EXIT_FAILURE); - } - fcntl(sockfd, F_SETFL, O_NONBLOCK); - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(SOCKK); - if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) { - perror("inet_pton failed"); - finish(EXIT_FAILURE); - } - - res = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)); - if (res == -1 && errno != EINPROGRESS) { - perror("connect failed"); - finish(EXIT_FAILURE); - } - -#if EMSCRIPTEN - emscripten_set_main_loop(iter, 0, 0); -#else - while (1) iter(NULL); -#endif -} - diff --git a/tests/sockets/test_sockets_bi.c b/tests/sockets/test_sockets_bi.c deleted file mode 100644 index 4266d20c..00000000 --- a/tests/sockets/test_sockets_bi.c +++ /dev/null @@ -1,138 +0,0 @@ -#include <errno.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/ioctl.h> -#if EMSCRIPTEN -#include <emscripten.h> -#endif - -#define EXPECTED_BYTES 28 - -int sockfd; - -void finish(int result) { - close(sockfd); -#if EMSCRIPTEN - REPORT_RESULT(); -#endif - exit(result); -} - -unsigned int get_all_buf(int sock, char* output, unsigned int maxsize) { - int bytes; - if (ioctl(sock, FIONREAD, &bytes)) return 0; - if (bytes == 0) return 0; - - char buffer[1024]; - int n; - unsigned int offset = 0; -#if TEST_FILE_OPS - while((errno = 0, (n = read(sock, buffer, sizeof(buffer)))>0) || -#else - while((errno = 0, (n = recv(sock, buffer, sizeof(buffer), 0))>0) || -#endif - errno == EINTR) { - if(n > 0) { - if (((unsigned int) n)+offset > maxsize) { - fprintf(stderr, "too much data!"); - finish(EXIT_FAILURE); - } - memcpy(output+offset, buffer, n); - offset += n; - } - } - - if(n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { - fprintf(stderr, "error in get_all_buf!"); - finish(EXIT_FAILURE); - } - return offset; -} - -void iter(void *arg) { - static char out[1024*2]; - static int pos = 0; - fd_set fdr; - int res; - - // make sure that sockfd is ready to read - FD_ZERO(&fdr); - FD_SET(sockfd, &fdr); - res = select(64, &fdr, NULL, NULL, NULL); - if (res == -1) { - perror("select failed"); - finish(EXIT_FAILURE); - } else if (!FD_ISSET(sockfd, &fdr)) { - return; - } - - // perform read write operations ... - int n = get_all_buf(sockfd, out+pos, 1024-pos); - if (n) printf("read! %d\n", n); - pos += n; - if (pos >= EXPECTED_BYTES) { - int i, sum = 0; - for (i=0; i < pos; i++) { - printf("%x\n", out[i]); - sum += out[i]; - } - - shutdown(sockfd, SHUT_RDWR); - close(sockfd); - - printf("sum: %d\n", sum); - finish(sum); - } -} - -int main() { - struct sockaddr_in addr; - int res; - - printf("hello from main page\n"); - -#if !TEST_DGRAM - sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); -#else - sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); -#endif - if (sockfd == -1) { - perror("cannot create socket"); - finish(EXIT_FAILURE); - } - fcntl(sockfd, F_SETFL, O_NONBLOCK); - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(SOCKK); - if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) { - perror("inet_pton failed"); - finish(EXIT_FAILURE); - } - - res = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)); - if (res == -1 && errno != EINPROGRESS) { - perror("connect failed"); - finish(EXIT_FAILURE); - } - -#if EMSCRIPTEN - emscripten_run_script("console.log('adding iframe');" - "var iframe = document.createElement('iframe');" - "iframe.src = 'side.html';" - "document.body.appendChild(iframe);" - "console.log('added.');"); - emscripten_set_main_loop(iter, 0, 0); -#else - while (1) iter(NULL); -#endif - - return EXIT_SUCCESS; -} diff --git a/tests/sockets/test_sockets_bi_bigdata.c b/tests/sockets/test_sockets_bi_bigdata.c deleted file mode 100644 index c1d8100e..00000000 --- a/tests/sockets/test_sockets_bi_bigdata.c +++ /dev/null @@ -1,138 +0,0 @@ -#include <errno.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/ioctl.h> -#if EMSCRIPTEN -#include <emscripten.h> -#endif - -#include "test_sockets_bigdata.h" - -#define EXPECTED_BYTES DATA_SIZE - -int sockfd; - -void finish(int result) { - close(sockfd); -#if EMSCRIPTEN - REPORT_RESULT(); -#endif - exit(result); -} - -unsigned int get_all_buf(int sock, char* output, unsigned int maxsize) { - int bytes; - if (ioctl(sock, FIONREAD, &bytes)) return 0; - if (bytes == 0) return 0; - - char buffer[EXPECTED_BYTES]; - int n; - unsigned int offset = 0; - while((errno = 0, (n = recv(sock, buffer, sizeof(buffer), 0))>0) || - errno == EINTR) { - if (n > 0) { - if (((unsigned int) n)+offset > maxsize) { - fprintf(stderr, "too much data!"); - finish(EXIT_FAILURE); - } - memcpy(output+offset, buffer, n); - offset += n; - } - } - - if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { - fprintf(stderr, "error in get_all_buf!"); - finish(EXIT_FAILURE); - } - return offset; -} - -void iter(void *arg) { - static char out[EXPECTED_BYTES]; - static int pos = 0; - fd_set fdr; - int res; - - // make sure that sockfd has finished connecting and is ready to read - FD_ZERO(&fdr); - FD_SET(sockfd, &fdr); - res = select(64, &fdr, NULL, NULL, NULL); - if (res == -1) { - perror("select failed"); - finish(EXIT_FAILURE); - return; - } else if (!FD_ISSET(sockfd, &fdr)) { - return; - } - - // perform read write operations ... - printf("so far %d, expecting up to %d\n", pos, EXPECTED_BYTES-pos); - res = get_all_buf(sockfd, out+pos, EXPECTED_BYTES-pos); - if (res) printf("read! %d\n", res); - pos += res; - if (pos >= EXPECTED_BYTES) { - shutdown(sockfd, SHUT_RDWR); - - close(sockfd); - - char *comp = generateData(); - int result = strcmp(comp, out); - if (result != 0) { - for (int i = 0; i < DATA_SIZE; i++) { - printf("%d:%d\n", comp[i], out[i]); - } - } - finish(result); - } -} - -int main() { - struct sockaddr_in addr; - int res; - - printf("hello from main page\n"); - - sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); - if (sockfd == -1) { - perror("cannot create socket"); - finish(EXIT_FAILURE); - } - fcntl(sockfd, F_SETFL, O_NONBLOCK); - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(SOCKK); - if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) { - perror("inet_pton failed"); - finish(EXIT_FAILURE); - } - - res = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)); - if (res == -1 && errno != EINPROGRESS) { - perror("connect failed"); - finish(EXIT_FAILURE); - } - -#if EMSCRIPTEN - emscripten_run_script("console.log('adding iframe');" - "var iframe = document.createElement('iframe');" - "iframe.src = 'side.html';" - "iframe.width = '100%';" - "iframe.width = '40%';" - "document.body.appendChild(iframe);" - "console.log('added.');"); - emscripten_set_main_loop(iter, 3, 0); -#else - while (1) iter(NULL); -#endif - - return EXIT_SUCCESS; -} - diff --git a/tests/sockets/test_sockets_bi_side.c b/tests/sockets/test_sockets_bi_side.c deleted file mode 100644 index b8910632..00000000 --- a/tests/sockets/test_sockets_bi_side.c +++ /dev/null @@ -1,93 +0,0 @@ -#include <errno.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <fcntl.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/ioctl.h> -#if EMSCRIPTEN -#include <emscripten.h> -#endif - -#define EXPECTED_BYTES 5 - -int sockfd = -1; - -void finish(int result) { - close(sockfd); - exit(result); -} - -void loop() { - fd_set fdw; - int res; - - // Make sure that sockfd has actually finished connecting - // and is ready to read. - FD_ZERO(&fdw); - FD_SET(sockfd, &fdw); - res = select(64, NULL, &fdw, NULL, NULL); - if (res == -1) { - perror("select failed"); - finish(EXIT_FAILURE); - } else if (!FD_ISSET(sockfd, &fdw)) { - return; - } - - char data[] = "hello from the other siide\n"; - - printf("send..\n"); -#if TEST_FILE_OPS - res = write(sockfd, data, sizeof(data)); -#else - res = send(sockfd, data, sizeof(data), 0); -#endif - if (res == -1) { - if (errno != EAGAIN) { - perror("send error"); - finish(EXIT_FAILURE); - } - return; - } - - finish(EXIT_SUCCESS); -} - -int main() { - struct sockaddr_in addr; - int res; -#if !TEST_DGRAM - sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); -#else - sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); -#endif - if (sockfd == -1) { - perror("cannot create socket"); - finish(EXIT_FAILURE); - } - fcntl(sockfd, F_SETFL, O_NONBLOCK); - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(SOCKK); - if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) { - perror("inet_pton failed"); - finish(EXIT_FAILURE); - } - - res = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)); - if (res == -1 && errno != EINPROGRESS) { - perror("connect failed"); - finish(EXIT_FAILURE); - } - - emscripten_set_main_loop(loop, 0, 0); - - return EXIT_SUCCESS; -} - diff --git a/tests/sockets/test_sockets_bi_side_bigdata.c b/tests/sockets/test_sockets_bi_side_bigdata.c deleted file mode 100644 index e31029b6..00000000 --- a/tests/sockets/test_sockets_bi_side_bigdata.c +++ /dev/null @@ -1,90 +0,0 @@ -#include <errno.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/ioctl.h> -#if EMSCRIPTEN -#include <emscripten.h> -#endif - -#include "test_sockets_bigdata.h" - -#define EXPECTED_BYTES 5 - -int sockfd = -1; -char *data = NULL; - -void finish(int result) { - close(sockfd); - exit(result); -} - -void loop() { - fd_set fdw; - int res; - - // make sure that sockfd has finished connecting and is ready to write - FD_ZERO(&fdw); - FD_SET(sockfd, &fdw); - res = select(64, NULL, &fdw, NULL, NULL); - if (res == -1) { - perror("select failed"); - finish(EXIT_FAILURE); - return; - } else if (!FD_ISSET(sockfd, &fdw)) { - return; - } - - printf("send..\n"); - - res = send(sockfd, data, DATA_SIZE, 0); - if (res == -1) { - if (errno != EAGAIN) { - perror("send error"); - finish(EXIT_FAILURE); - } - return; - } - - finish(EXIT_SUCCESS); -} - -int main() { - struct sockaddr_in addr; - int res; - - sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); - if (sockfd == -1) { - perror("cannot create socket"); - exit(EXIT_FAILURE); - } - fcntl(sockfd, F_SETFL, O_NONBLOCK); - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(SOCKK); - if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) { - perror("inet_pton failed"); - finish(EXIT_FAILURE); - } - - printf("connect..\n"); - - res = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)); - if (res == -1 && errno != EINPROGRESS) { - perror("connect failed"); - finish(EXIT_FAILURE); - } - - data = generateData(); - - emscripten_set_main_loop(loop, 1, 0); - - return EXIT_SUCCESS; -}
\ No newline at end of file diff --git a/tests/sockets/test_sockets_bigdata.h b/tests/sockets/test_sockets_bigdata.h deleted file mode 100644 index 17149ad6..00000000 --- a/tests/sockets/test_sockets_bigdata.h +++ /dev/null @@ -1,20 +0,0 @@ - -#include <stdlib.h> - -#define DATA_SIZE (256*256*2) -// 1500 fails - -char *generateData() { - char *ret = malloc(256*256*2); - char *curr = ret; - for (int i = 0; i < 256; i++) { - for (int j = 0; j < 256; j++) { - *curr = i; - curr++; - *curr = j; - curr++; - } - } - return ret; -} - diff --git a/tests/sockets/test_sockets_echo_client.c b/tests/sockets/test_sockets_echo_client.c new file mode 100644 index 00000000..6b3ccef3 --- /dev/null +++ b/tests/sockets/test_sockets_echo_client.c @@ -0,0 +1,148 @@ +#include <errno.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/ioctl.h> +#include <assert.h> +#if EMSCRIPTEN +#include <emscripten.h> +#endif + +#include "test_sockets_msg.h" + +// message to send to the server +#ifndef MESSAGE +#define MESSAGE "pingtothepong" +#endif + +typedef enum { + MSG_READ, + MSG_WRITE +} msg_state_t; + +typedef struct { + int fd; + msg_t msg; + msg_state_t state; +} server_t; + +server_t server; +msg_t echo_msg; +int echo_read; +int echo_wrote; + +void finish(int result) { + close(server.fd); +#if EMSCRIPTEN + REPORT_RESULT(); +#endif + exit(result); +} + +void main_loop(void *arg) { + static char out[1024*2]; + static int pos = 0; + fd_set fdr; + fd_set fdw; + int res; + + // make sure that server.fd is ready to read / write + FD_ZERO(&fdr); + FD_ZERO(&fdw); + FD_SET(server.fd, &fdr); + FD_SET(server.fd, &fdw); + res = select(64, &fdr, &fdw, NULL, NULL); + if (res == -1) { + perror("select failed"); + finish(EXIT_FAILURE); + } |