diff options
Diffstat (limited to 'tests/sockets')
-rw-r--r-- | tests/sockets/socket_relay.py | 56 | ||||
-rw-r--r-- | tests/sockets/test_enet_client.c | 113 | ||||
-rw-r--r-- | tests/sockets/test_enet_server.c | 112 |
3 files changed, 281 insertions, 0 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; +} + |