test-udp-send-immediate.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 sv_recv_cb_called;
  32. static int close_cb_called;
  33. static void alloc_cb(uv_handle_t* handle,
  34. size_t suggested_size,
  35. uv_buf_t* buf) {
  36. static char slab[65536];
  37. CHECK_HANDLE(handle);
  38. ASSERT(suggested_size <= sizeof(slab));
  39. buf->base = slab;
  40. buf->len = sizeof(slab);
  41. }
  42. static void close_cb(uv_handle_t* handle) {
  43. CHECK_HANDLE(handle);
  44. ASSERT(1 == uv_is_closing(handle));
  45. close_cb_called++;
  46. }
  47. static void cl_send_cb(uv_udp_send_t* req, int status) {
  48. ASSERT(req != NULL);
  49. ASSERT(status == 0);
  50. CHECK_HANDLE(req->handle);
  51. cl_send_cb_called++;
  52. }
  53. static void sv_recv_cb(uv_udp_t* handle,
  54. ssize_t nread,
  55. const uv_buf_t* rcvbuf,
  56. const struct sockaddr* addr,
  57. unsigned flags) {
  58. if (nread < 0) {
  59. ASSERT(0 && "unexpected error");
  60. }
  61. if (nread == 0) {
  62. /* Returning unused buffer. Don't count towards sv_recv_cb_called */
  63. ASSERT(addr == NULL);
  64. return;
  65. }
  66. CHECK_HANDLE(handle);
  67. ASSERT(flags == 0);
  68. ASSERT(addr != NULL);
  69. ASSERT(nread == 4);
  70. ASSERT(memcmp("PING", rcvbuf->base, nread) == 0 ||
  71. memcmp("PANG", rcvbuf->base, nread) == 0);
  72. if (++sv_recv_cb_called == 2) {
  73. uv_close((uv_handle_t*) &server, close_cb);
  74. uv_close((uv_handle_t*) &client, close_cb);
  75. }
  76. }
  77. TEST_IMPL(udp_send_immediate) {
  78. struct sockaddr_in addr;
  79. uv_udp_send_t req1, req2;
  80. uv_buf_t buf;
  81. int r;
  82. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
  83. r = uv_udp_init(uv_default_loop(), &server);
  84. ASSERT(r == 0);
  85. r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0);
  86. ASSERT(r == 0);
  87. r = uv_udp_recv_start(&server, alloc_cb, sv_recv_cb);
  88. ASSERT(r == 0);
  89. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  90. r = uv_udp_init(uv_default_loop(), &client);
  91. ASSERT(r == 0);
  92. /* client sends "PING", then "PANG" */
  93. buf = uv_buf_init("PING", 4);
  94. r = uv_udp_send(&req1,
  95. &client,
  96. &buf,
  97. 1,
  98. (const struct sockaddr*) &addr,
  99. cl_send_cb);
  100. ASSERT(r == 0);
  101. buf = uv_buf_init("PANG", 4);
  102. r = uv_udp_send(&req2,
  103. &client,
  104. &buf,
  105. 1,
  106. (const struct sockaddr*) &addr,
  107. cl_send_cb);
  108. ASSERT(r == 0);
  109. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  110. ASSERT(cl_send_cb_called == 2);
  111. ASSERT(sv_recv_cb_called == 2);
  112. ASSERT(close_cb_called == 2);
  113. MAKE_VALGRIND_HAPPY();
  114. return 0;
  115. }