about summary refs log tree commit diff
path: root/src/rt/libuv/test/echo-server.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rt/libuv/test/echo-server.c')
-rw-r--r--src/rt/libuv/test/echo-server.c60
1 files changed, 47 insertions, 13 deletions
diff --git a/src/rt/libuv/test/echo-server.c b/src/rt/libuv/test/echo-server.c
index a3f8f29ed10..9addc546c1e 100644
--- a/src/rt/libuv/test/echo-server.c
+++ b/src/rt/libuv/test/echo-server.c
@@ -19,7 +19,7 @@
  * IN THE SOFTWARE.
  */
 
-#include "../uv.h"
+#include "uv.h"
 #include "task.h"
 #include <stdio.h>
 #include <stdlib.h>
@@ -34,12 +34,15 @@ typedef struct {
 static int server_closed;
 static uv_tcp_t server;
 
+static int server6_closed;
+static uv_tcp_t server6;
+
 
 static void after_write(uv_req_t* req, int status);
-static void after_read(uv_tcp_t*, ssize_t nread, uv_buf_t buf);
+static void after_read(uv_stream_t*, ssize_t nread, uv_buf_t buf);
 static void on_close(uv_handle_t* peer);
 static void on_server_close(uv_handle_t* handle);
-static void on_connection(uv_tcp_t*, int status);
+static void on_connection(uv_handle_t*, int status);
 
 
 static void after_write(uv_req_t* req, int status) {
@@ -65,7 +68,7 @@ static void after_shutdown(uv_req_t* req, int status) {
 }
 
 
-static void after_read(uv_tcp_t* handle, ssize_t nread, uv_buf_t buf) {
+static void after_read(uv_stream_t* handle, ssize_t nread, uv_buf_t buf) {
   int i;
   write_req_t *wr;
   uv_req_t* req;
@@ -79,7 +82,7 @@ static void after_read(uv_tcp_t* handle, ssize_t nread, uv_buf_t buf) {
     }
 
     req = (uv_req_t*) malloc(sizeof *req);
-    uv_req_init(req, (uv_handle_t*)handle, after_shutdown);
+    uv_req_init(req, (uv_handle_t*)handle, (void *(*)(void *))after_shutdown);
     uv_shutdown(req);
 
     return;
@@ -97,13 +100,15 @@ static void after_read(uv_tcp_t* handle, ssize_t nread, uv_buf_t buf) {
       if (buf.base[i] == 'Q') {
         uv_close((uv_handle_t*)&server, on_server_close);
         server_closed = 1;
+        uv_close((uv_handle_t*)&server6, on_server_close);
+        server6_closed = 1;
       }
     }
   }
 
   wr = (write_req_t*) malloc(sizeof *wr);
 
-  uv_req_init(&wr->req, (uv_handle_t*)handle, after_write);
+  uv_req_init(&wr->req, (uv_handle_t*)handle, (void *(*)(void *))after_write);
   wr->buf.base = buf.base;
   wr->buf.len = nread;
   if (uv_write(&wr->req, &wr->buf, 1)) {
@@ -117,7 +122,7 @@ static void on_close(uv_handle_t* peer) {
 }
 
 
-static uv_buf_t echo_alloc(uv_tcp_t* handle, size_t suggested_size) {
+static uv_buf_t echo_alloc(uv_stream_t* handle, size_t suggested_size) {
   uv_buf_t buf;
   buf.base = (char*) malloc(suggested_size);
   buf.len = suggested_size;
@@ -125,10 +130,13 @@ static uv_buf_t echo_alloc(uv_tcp_t* handle, size_t suggested_size) {
 }
 
 
-static void on_connection(uv_tcp_t* server, int status) {
+static void on_connection(uv_handle_t* server, int status) {
   uv_tcp_t* handle;
   int r;
 
+  if (status != 0) {
+    fprintf(stderr, "Connect error %d\n", uv_last_error().code);
+  }
   ASSERT(status == 0);
 
   handle = (uv_tcp_t*) malloc(sizeof *handle);
@@ -136,21 +144,25 @@ static void on_connection(uv_tcp_t* server, int status) {
 
   uv_tcp_init(handle);
 
-  r = uv_accept(server, handle);
+  /* associate server with stream */
+  handle->data = server;
+
+  r = uv_accept(server, (uv_stream_t*)handle);
   ASSERT(r == 0);
 
-  r = uv_read_start(handle, echo_alloc, after_read);
+  r = uv_read_start((uv_stream_t*)handle, echo_alloc, after_read);
   ASSERT(r == 0);
 }
 
 
 static void on_server_close(uv_handle_t* handle) {
-  ASSERT(handle == (uv_handle_t*)&server);
+  ASSERT(handle == (uv_handle_t*)&server || handle == (uv_handle_t*)&server6);
 }
 
 
 static int echo_start(int port) {
   struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", port);
+  struct sockaddr_in6 addr6 = uv_ip6_addr("::1", port);
   int r;
 
   r = uv_tcp_init(&server);
@@ -160,20 +172,42 @@ static int echo_start(int port) {
     return 1;
   }
 
-  r = uv_bind(&server, addr);
+  r = uv_tcp_bind(&server, addr);
   if (r) {
     /* TODO: Error codes */
     fprintf(stderr, "Bind error\n");
     return 1;
   }
 
-  r = uv_listen(&server, 128, on_connection);
+  r = uv_tcp_listen(&server, 128, on_connection);
   if (r) {
     /* TODO: Error codes */
     fprintf(stderr, "Listen error\n");
     return 1;
   }
 
+  r = uv_tcp_init(&server6);
+  if (r) {
+    /* TODO: Error codes */
+    fprintf(stderr, "Socket creation error\n");
+    return 1;
+  }
+
+  /* IPv6 is optional as not all platforms support it */
+  r = uv_tcp_bind6(&server6, addr6);
+  if (r) {
+    /* show message but return OK */
+    fprintf(stderr, "IPv6 not supported\n");
+    return 0;
+  }
+
+  r = uv_tcp_listen(&server6, 128, on_connection);
+  if (r) {
+    /* TODO: Error codes */
+    fprintf(stderr, "Listen error on IPv6\n");
+    return 1;
+  }
+
   return 0;
 }