test-ping-pong.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. static int completed_pingers = 0;
  26. #define NUM_PINGS 1000
  27. /* 64 bytes is enough for a pinger */
  28. #define BUFSIZE 10240
  29. static char PING[] = "PING\n";
  30. static int pinger_on_connect_count;
  31. typedef struct {
  32. int pongs;
  33. int state;
  34. union {
  35. uv_tcp_t tcp;
  36. uv_pipe_t pipe;
  37. } stream;
  38. uv_connect_t connect_req;
  39. char read_buffer[BUFSIZE];
  40. } pinger_t;
  41. static uv_buf_t alloc_cb(uv_handle_t* handle, size_t size) {
  42. return uv_buf_init(malloc(size), size);
  43. }
  44. static void pinger_on_close(uv_handle_t* handle) {
  45. pinger_t* pinger = (pinger_t*)handle->data;
  46. ASSERT(NUM_PINGS == pinger->pongs);
  47. free(pinger);
  48. completed_pingers++;
  49. }
  50. static void pinger_after_write(uv_write_t *req, int status) {
  51. ASSERT(status == 0);
  52. free(req);
  53. }
  54. static void pinger_write_ping(pinger_t* pinger) {
  55. uv_write_t *req;
  56. uv_buf_t buf;
  57. buf = uv_buf_init(PING, sizeof(PING) - 1);
  58. req = malloc(sizeof(*req));
  59. if (uv_write(req, (uv_stream_t*)&pinger->stream.tcp, &buf, 1, pinger_after_write)) {
  60. FATAL("uv_write failed");
  61. }
  62. puts("PING");
  63. }
  64. static void pinger_read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t buf) {
  65. ssize_t i;
  66. pinger_t* pinger;
  67. pinger = (pinger_t*)stream->data;
  68. if (nread < 0) {
  69. ASSERT(uv_last_error(uv_default_loop()).code == UV_EOF);
  70. puts("got EOF");
  71. free(buf.base);
  72. uv_close((uv_handle_t*)(&pinger->stream.tcp), pinger_on_close);
  73. return;
  74. }
  75. /* Now we count the pings */
  76. for (i = 0; i < nread; i++) {
  77. ASSERT(buf.base[i] == PING[pinger->state]);
  78. pinger->state = (pinger->state + 1) % (sizeof(PING) - 1);
  79. if (pinger->state != 0)
  80. continue;
  81. printf("PONG %d\n", pinger->pongs);
  82. pinger->pongs++;
  83. if (pinger->pongs < NUM_PINGS) {
  84. pinger_write_ping(pinger);
  85. } else {
  86. uv_close((uv_handle_t*)(&pinger->stream.tcp), pinger_on_close);
  87. break;
  88. }
  89. }
  90. free(buf.base);
  91. }
  92. static void pinger_on_connect(uv_connect_t *req, int status) {
  93. pinger_t *pinger = (pinger_t*)req->handle->data;
  94. pinger_on_connect_count++;
  95. ASSERT(status == 0);
  96. ASSERT(uv_is_readable(req->handle));
  97. ASSERT(uv_is_writable(req->handle));
  98. ASSERT(!uv_is_closing((uv_handle_t *)req->handle));
  99. pinger_write_ping(pinger);
  100. uv_read_start((uv_stream_t*)(req->handle), alloc_cb, pinger_read_cb);
  101. }
  102. /* same ping-pong test, but using IPv6 connection */
  103. static void tcp_pinger_v6_new(void) {
  104. int r;
  105. struct sockaddr_in6 server_addr = uv_ip6_addr("::1", TEST_PORT);
  106. pinger_t *pinger;
  107. pinger = (pinger_t*)malloc(sizeof(*pinger));
  108. pinger->state = 0;
  109. pinger->pongs = 0;
  110. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  111. r = uv_tcp_init(uv_default_loop(), &pinger->stream.tcp);
  112. pinger->stream.tcp.data = pinger;
  113. ASSERT(!r);
  114. /* We are never doing multiple reads/connects at a time anyway. */
  115. /* so these handles can be pre-initialized. */
  116. r = uv_tcp_connect6(&pinger->connect_req, &pinger->stream.tcp, server_addr,
  117. pinger_on_connect);
  118. ASSERT(!r);
  119. /* Synchronous connect callbacks are not allowed. */
  120. ASSERT(pinger_on_connect_count == 0);
  121. }
  122. static void tcp_pinger_new(void) {
  123. int r;
  124. struct sockaddr_in server_addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  125. pinger_t *pinger;
  126. pinger = (pinger_t*)malloc(sizeof(*pinger));
  127. pinger->state = 0;
  128. pinger->pongs = 0;
  129. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  130. r = uv_tcp_init(uv_default_loop(), &pinger->stream.tcp);
  131. pinger->stream.tcp.data = pinger;
  132. ASSERT(!r);
  133. /* We are never doing multiple reads/connects at a time anyway. */
  134. /* so these handles can be pre-initialized. */
  135. r = uv_tcp_connect(&pinger->connect_req, &pinger->stream.tcp, server_addr,
  136. pinger_on_connect);
  137. ASSERT(!r);
  138. /* Synchronous connect callbacks are not allowed. */
  139. ASSERT(pinger_on_connect_count == 0);
  140. }
  141. static void pipe_pinger_new(void) {
  142. int r;
  143. pinger_t *pinger;
  144. pinger = (pinger_t*)malloc(sizeof(*pinger));
  145. pinger->state = 0;
  146. pinger->pongs = 0;
  147. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  148. r = uv_pipe_init(uv_default_loop(), &pinger->stream.pipe, 0);
  149. pinger->stream.pipe.data = pinger;
  150. ASSERT(!r);
  151. /* We are never doing multiple reads/connects at a time anyway. */
  152. /* so these handles can be pre-initialized. */
  153. uv_pipe_connect(&pinger->connect_req, &pinger->stream.pipe, TEST_PIPENAME,
  154. pinger_on_connect);
  155. /* Synchronous connect callbacks are not allowed. */
  156. ASSERT(pinger_on_connect_count == 0);
  157. }
  158. TEST_IMPL(tcp_ping_pong) {
  159. tcp_pinger_new();
  160. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  161. ASSERT(completed_pingers == 1);
  162. MAKE_VALGRIND_HAPPY();
  163. return 0;
  164. }
  165. TEST_IMPL(tcp_ping_pong_v6) {
  166. tcp_pinger_v6_new();
  167. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  168. ASSERT(completed_pingers == 1);
  169. MAKE_VALGRIND_HAPPY();
  170. return 0;
  171. }
  172. TEST_IMPL(pipe_ping_pong) {
  173. pipe_pinger_new();
  174. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  175. ASSERT(completed_pingers == 1);
  176. MAKE_VALGRIND_HAPPY();
  177. return 0;
  178. }