test-udp-send-and-recv.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #define CHECK_HANDLE(handle) \
  27. ASSERT((uv_udp_t*)(handle) == &server || (uv_udp_t*)(handle) == &client)
  28. static uv_udp_t server;
  29. static uv_udp_t client;
  30. static int cl_send_cb_called;
  31. static int cl_recv_cb_called;
  32. static int sv_send_cb_called;
  33. static int sv_recv_cb_called;
  34. static int close_cb_called;
  35. static void alloc_cb(uv_handle_t* handle,
  36. size_t suggested_size,
  37. uv_buf_t* buf) {
  38. static char slab[65536];
  39. CHECK_HANDLE(handle);
  40. ASSERT(suggested_size <= sizeof(slab));
  41. buf->base = slab;
  42. buf->len = sizeof(slab);
  43. }
  44. static void close_cb(uv_handle_t* handle) {
  45. CHECK_HANDLE(handle);
  46. ASSERT(1 == uv_is_closing(handle));
  47. close_cb_called++;
  48. }
  49. static void cl_recv_cb(uv_udp_t* handle,
  50. ssize_t nread,
  51. const uv_buf_t* buf,
  52. const struct sockaddr* addr,
  53. unsigned flags) {
  54. CHECK_HANDLE(handle);
  55. ASSERT(flags == 0);
  56. if (nread < 0) {
  57. ASSERT(0 && "unexpected error");
  58. }
  59. if (nread == 0) {
  60. /* Returning unused buffer. Don't count towards cl_recv_cb_called */
  61. ASSERT(addr == NULL);
  62. return;
  63. }
  64. ASSERT(addr != NULL);
  65. ASSERT(nread == 4);
  66. ASSERT(!memcmp("PONG", buf->base, nread));
  67. cl_recv_cb_called++;
  68. uv_close((uv_handle_t*) handle, close_cb);
  69. }
  70. static void cl_send_cb(uv_udp_send_t* req, int status) {
  71. int r;
  72. ASSERT(req != NULL);
  73. ASSERT(status == 0);
  74. CHECK_HANDLE(req->handle);
  75. r = uv_udp_recv_start(req->handle, alloc_cb, cl_recv_cb);
  76. ASSERT(r == 0);
  77. cl_send_cb_called++;
  78. }
  79. static void sv_send_cb(uv_udp_send_t* req, int status) {
  80. ASSERT(req != NULL);
  81. ASSERT(status == 0);
  82. CHECK_HANDLE(req->handle);
  83. uv_close((uv_handle_t*) req->handle, close_cb);
  84. free(req);
  85. sv_send_cb_called++;
  86. }
  87. static void sv_recv_cb(uv_udp_t* handle,
  88. ssize_t nread,
  89. const uv_buf_t* rcvbuf,
  90. const struct sockaddr* addr,
  91. unsigned flags) {
  92. uv_udp_send_t* req;
  93. uv_buf_t sndbuf;
  94. int r;
  95. if (nread < 0) {
  96. ASSERT(0 && "unexpected error");
  97. }
  98. if (nread == 0) {
  99. /* Returning unused buffer. Don't count towards sv_recv_cb_called */
  100. ASSERT(addr == NULL);
  101. return;
  102. }
  103. CHECK_HANDLE(handle);
  104. ASSERT(flags == 0);
  105. ASSERT(addr != NULL);
  106. ASSERT(nread == 4);
  107. ASSERT(!memcmp("PING", rcvbuf->base, nread));
  108. /* FIXME? `uv_udp_recv_stop` does what it says: recv_cb is not called
  109. * anymore. That's problematic because the read buffer won't be returned
  110. * either... Not sure I like that but it's consistent with `uv_read_stop`.
  111. */
  112. r = uv_udp_recv_stop(handle);
  113. ASSERT(r == 0);
  114. req = malloc(sizeof *req);
  115. ASSERT(req != NULL);
  116. sndbuf = uv_buf_init("PONG", 4);
  117. r = uv_udp_send(req, handle, &sndbuf, 1, addr, sv_send_cb);
  118. ASSERT(r == 0);
  119. sv_recv_cb_called++;
  120. }
  121. TEST_IMPL(udp_send_and_recv) {
  122. struct sockaddr_in addr;
  123. uv_udp_send_t req;
  124. uv_buf_t buf;
  125. int r;
  126. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
  127. r = uv_udp_init(uv_default_loop(), &server);
  128. ASSERT(r == 0);
  129. r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0);
  130. ASSERT(r == 0);
  131. r = uv_udp_recv_start(&server, alloc_cb, sv_recv_cb);
  132. ASSERT(r == 0);
  133. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  134. r = uv_udp_init(uv_default_loop(), &client);
  135. ASSERT(r == 0);
  136. /* client sends "PING", expects "PONG" */
  137. buf = uv_buf_init("PING", 4);
  138. r = uv_udp_send(&req,
  139. &client,
  140. &buf,
  141. 1,
  142. (const struct sockaddr*) &addr,
  143. cl_send_cb);
  144. ASSERT(r == 0);
  145. ASSERT(close_cb_called == 0);
  146. ASSERT(cl_send_cb_called == 0);
  147. ASSERT(cl_recv_cb_called == 0);
  148. ASSERT(sv_send_cb_called == 0);
  149. ASSERT(sv_recv_cb_called == 0);
  150. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  151. ASSERT(cl_send_cb_called == 1);
  152. ASSERT(cl_recv_cb_called == 1);
  153. ASSERT(sv_send_cb_called == 1);
  154. ASSERT(sv_recv_cb_called == 1);
  155. ASSERT(close_cb_called == 2);
  156. ASSERT(client.send_queue_size == 0);
  157. ASSERT(server.send_queue_size == 0);
  158. MAKE_VALGRIND_HAPPY();
  159. return 0;
  160. }