1
0

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

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