1
0

test-ping-pong.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
  42. buf->base = malloc(size);
  43. buf->len = size;
  44. }
  45. static void pinger_on_close(uv_handle_t* handle) {
  46. pinger_t* pinger = (pinger_t*)handle->data;
  47. ASSERT(NUM_PINGS == pinger->pongs);
  48. free(pinger);
  49. completed_pingers++;
  50. }
  51. static void pinger_after_write(uv_write_t *req, int status) {
  52. ASSERT(status == 0);
  53. free(req);
  54. }
  55. static void pinger_write_ping(pinger_t* pinger) {
  56. uv_write_t *req;
  57. uv_buf_t buf;
  58. buf = uv_buf_init(PING, sizeof(PING) - 1);
  59. req = malloc(sizeof(*req));
  60. if (uv_write(req,
  61. (uv_stream_t*) &pinger->stream.tcp,
  62. &buf,
  63. 1,
  64. pinger_after_write)) {
  65. FATAL("uv_write failed");
  66. }
  67. puts("PING");
  68. }
  69. static void pinger_read_cb(uv_stream_t* stream,
  70. ssize_t nread,
  71. const uv_buf_t* buf) {
  72. ssize_t i;
  73. pinger_t* pinger;
  74. pinger = (pinger_t*)stream->data;
  75. if (nread < 0) {
  76. ASSERT(nread == UV_EOF);
  77. puts("got EOF");
  78. free(buf->base);
  79. uv_close((uv_handle_t*)(&pinger->stream.tcp), pinger_on_close);
  80. return;
  81. }
  82. /* Now we count the pings */
  83. for (i = 0; i < nread; i++) {
  84. ASSERT(buf->base[i] == PING[pinger->state]);
  85. pinger->state = (pinger->state + 1) % (sizeof(PING) - 1);
  86. if (pinger->state != 0)
  87. continue;
  88. printf("PONG %d\n", pinger->pongs);
  89. pinger->pongs++;
  90. if (pinger->pongs < NUM_PINGS) {
  91. pinger_write_ping(pinger);
  92. } else {
  93. uv_close((uv_handle_t*)(&pinger->stream.tcp), pinger_on_close);
  94. break;
  95. }
  96. }
  97. free(buf->base);
  98. }
  99. static void pinger_on_connect(uv_connect_t *req, int status) {
  100. pinger_t *pinger = (pinger_t*)req->handle->data;
  101. pinger_on_connect_count++;
  102. ASSERT(status == 0);
  103. ASSERT(1 == uv_is_readable(req->handle));
  104. ASSERT(1 == uv_is_writable(req->handle));
  105. ASSERT(0 == uv_is_closing((uv_handle_t *) req->handle));
  106. pinger_write_ping(pinger);
  107. uv_read_start((uv_stream_t*)(req->handle), alloc_cb, pinger_read_cb);
  108. }
  109. /* same ping-pong test, but using IPv6 connection */
  110. static void tcp_pinger_v6_new(void) {
  111. int r;
  112. struct sockaddr_in6 server_addr;
  113. pinger_t *pinger;
  114. ASSERT(0 ==uv_ip6_addr("::1", TEST_PORT, &server_addr));
  115. pinger = malloc(sizeof(*pinger));
  116. pinger->state = 0;
  117. pinger->pongs = 0;
  118. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  119. r = uv_tcp_init(uv_default_loop(), &pinger->stream.tcp);
  120. pinger->stream.tcp.data = pinger;
  121. ASSERT(!r);
  122. /* We are never doing multiple reads/connects at a time anyway. */
  123. /* so these handles can be pre-initialized. */
  124. r = uv_tcp_connect(&pinger->connect_req,
  125. &pinger->stream.tcp,
  126. (const struct sockaddr*) &server_addr,
  127. pinger_on_connect);
  128. ASSERT(!r);
  129. /* Synchronous connect callbacks are not allowed. */
  130. ASSERT(pinger_on_connect_count == 0);
  131. }
  132. static void tcp_pinger_new(void) {
  133. int r;
  134. struct sockaddr_in server_addr;
  135. pinger_t *pinger;
  136. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr));
  137. pinger = malloc(sizeof(*pinger));
  138. pinger->state = 0;
  139. pinger->pongs = 0;
  140. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  141. r = uv_tcp_init(uv_default_loop(), &pinger->stream.tcp);
  142. pinger->stream.tcp.data = pinger;
  143. ASSERT(!r);
  144. /* We are never doing multiple reads/connects at a time anyway. */
  145. /* so these handles can be pre-initialized. */
  146. r = uv_tcp_connect(&pinger->connect_req,
  147. &pinger->stream.tcp,
  148. (const struct sockaddr*) &server_addr,
  149. pinger_on_connect);
  150. ASSERT(!r);
  151. /* Synchronous connect callbacks are not allowed. */
  152. ASSERT(pinger_on_connect_count == 0);
  153. }
  154. static void pipe_pinger_new(void) {
  155. int r;
  156. pinger_t *pinger;
  157. pinger = (pinger_t*)malloc(sizeof(*pinger));
  158. pinger->state = 0;
  159. pinger->pongs = 0;
  160. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  161. r = uv_pipe_init(uv_default_loop(), &pinger->stream.pipe, 0);
  162. pinger->stream.pipe.data = pinger;
  163. ASSERT(!r);
  164. /* We are never doing multiple reads/connects at a time anyway. */
  165. /* so these handles can be pre-initialized. */
  166. uv_pipe_connect(&pinger->connect_req, &pinger->stream.pipe, TEST_PIPENAME,
  167. pinger_on_connect);
  168. /* Synchronous connect callbacks are not allowed. */
  169. ASSERT(pinger_on_connect_count == 0);
  170. }
  171. TEST_IMPL(tcp_ping_pong) {
  172. tcp_pinger_new();
  173. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  174. ASSERT(completed_pingers == 1);
  175. MAKE_VALGRIND_HAPPY();
  176. return 0;
  177. }
  178. TEST_IMPL(tcp_ping_pong_v6) {
  179. tcp_pinger_v6_new();
  180. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  181. ASSERT(completed_pingers == 1);
  182. MAKE_VALGRIND_HAPPY();
  183. return 0;
  184. }
  185. TEST_IMPL(pipe_ping_pong) {
  186. pipe_pinger_new();
  187. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  188. ASSERT(completed_pingers == 1);
  189. MAKE_VALGRIND_HAPPY();
  190. return 0;
  191. }