libuv-svace-build
149 строк · 4.4 Кб
1/* Copyright libuv 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
22#include "uv.h"
23#include "task.h"
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#define CHECK_HANDLE(handle) \
30ASSERT_NE((uv_udp_t*)(handle) == &recver || (uv_udp_t*)(handle) == &sender, 0)
31
32#define BUFFER_MULTIPLIER 20
33#define MAX_DGRAM_SIZE (64 * 1024)
34#define NUM_SENDS 40
35#define EXPECTED_MMSG_ALLOCS (NUM_SENDS / BUFFER_MULTIPLIER)
36
37static uv_udp_t recver;
38static uv_udp_t sender;
39static int recv_cb_called;
40static int received_datagrams;
41static int close_cb_called;
42static int alloc_cb_called;
43
44
45static void alloc_cb(uv_handle_t* handle,
46size_t suggested_size,
47uv_buf_t* buf) {
48size_t buffer_size;
49CHECK_HANDLE(handle);
50
51/* Only allocate enough room for multiple dgrams if we can actually recv them */
52buffer_size = MAX_DGRAM_SIZE;
53if (uv_udp_using_recvmmsg((uv_udp_t*)handle))
54buffer_size *= BUFFER_MULTIPLIER;
55
56/* Actually malloc to exercise free'ing the buffer later */
57buf->base = malloc(buffer_size);
58ASSERT_NOT_NULL(buf->base);
59buf->len = buffer_size;
60alloc_cb_called++;
61}
62
63
64static void close_cb(uv_handle_t* handle) {
65CHECK_HANDLE(handle);
66ASSERT(uv_is_closing(handle));
67close_cb_called++;
68}
69
70
71static void recv_cb(uv_udp_t* handle,
72ssize_t nread,
73const uv_buf_t* rcvbuf,
74const struct sockaddr* addr,
75unsigned flags) {
76ASSERT_GE(nread, 0);
77
78/* free and return if this is a mmsg free-only callback invocation */
79if (flags & UV_UDP_MMSG_FREE) {
80ASSERT_OK(nread);
81ASSERT_NULL(addr);
82free(rcvbuf->base);
83return;
84}
85
86if (nread == 0) {
87/* There can be no more available data for the time being. */
88ASSERT_NULL(addr);
89} else {
90ASSERT_EQ(4, nread);
91ASSERT_NOT_NULL(addr);
92ASSERT_MEM_EQ("PING", rcvbuf->base, nread);
93received_datagrams++;
94}
95
96recv_cb_called++;
97if (received_datagrams == NUM_SENDS) {
98uv_close((uv_handle_t*) handle, close_cb);
99uv_close((uv_handle_t*) &sender, close_cb);
100}
101
102/* Don't free if the buffer could be reused via mmsg */
103if (rcvbuf && !(flags & UV_UDP_MMSG_CHUNK))
104free(rcvbuf->base);
105}
106
107
108TEST_IMPL(udp_mmsg) {
109struct sockaddr_in addr;
110uv_buf_t buf;
111int i;
112
113ASSERT_OK(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
114
115ASSERT_OK(uv_udp_init_ex(uv_default_loop(), &recver,
116AF_UNSPEC | UV_UDP_RECVMMSG));
117
118ASSERT_OK(uv_udp_bind(&recver, (const struct sockaddr*) &addr, 0));
119
120ASSERT_OK(uv_udp_recv_start(&recver, alloc_cb, recv_cb));
121
122ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
123
124ASSERT_OK(uv_udp_init(uv_default_loop(), &sender));
125
126buf = uv_buf_init("PING", 4);
127for (i = 0; i < NUM_SENDS; i++) {
128ASSERT_EQ(4, uv_udp_try_send(&sender, &buf, 1, (const struct sockaddr*) &addr));
129}
130
131ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
132
133ASSERT_EQ(2, close_cb_called);
134ASSERT_EQ(received_datagrams, NUM_SENDS);
135
136ASSERT_OK(sender.send_queue_size);
137ASSERT_OK(recver.send_queue_size);
138
139printf("%d allocs for %d recvs\n", alloc_cb_called, recv_cb_called);
140
141/* On platforms that don't support mmsg, each recv gets its own alloc */
142if (uv_udp_using_recvmmsg(&recver))
143ASSERT_EQ(alloc_cb_called, EXPECTED_MMSG_ALLOCS);
144else
145ASSERT_EQ(alloc_cb_called, recv_cb_called);
146
147MAKE_VALGRIND_HAPPY(uv_default_loop());
148return 0;
149}
150