1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
/* Copyright libuv project contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "task.h"
static uv_tcp_t server;
static uv_tcp_t connection;
static int read_cb_called = 0;
static uv_tcp_t client;
static uv_connect_t connect_req;
static void on_read2(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);
static void on_write_close_immediately(uv_write_t* req, int status) {
ASSERT_OK(status);
uv_close((uv_handle_t*)req->handle, NULL); /* Close immediately */
free(req);
}
static void on_write(uv_write_t* req, int status) {
ASSERT_OK(status);
free(req);
}
static void do_write(uv_stream_t* stream, uv_write_cb cb) {
uv_write_t* req = malloc(sizeof(*req));
uv_buf_t buf;
buf.base = "1234578";
buf.len = 8;
ASSERT_OK(uv_write(req, stream, &buf, 1, cb));
}
static void on_alloc(uv_handle_t* handle,
size_t suggested_size,
uv_buf_t* buf) {
static char slab[65536];
buf->base = slab;
buf->len = sizeof(slab);
}
static void on_read1(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
ASSERT_GE(nread, 0);
/* Do write on a half open connection to force WSAECONNABORTED (on Windows)
* in the subsequent uv_read_start()
*/
do_write(stream, on_write);
ASSERT_OK(uv_read_stop(stream));
ASSERT_OK(uv_read_start(stream, on_alloc, on_read2));
read_cb_called++;
}
static void on_read2(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
ASSERT_LT(nread, 0);
uv_close((uv_handle_t*)stream, NULL);
uv_close((uv_handle_t*)&server, NULL);
read_cb_called++;
}
static void on_connection(uv_stream_t* server, int status) {
ASSERT_OK(status);
ASSERT_OK(uv_tcp_init(server->loop, &connection));
ASSERT_OK(uv_accept(server, (uv_stream_t* )&connection));
ASSERT_OK(uv_read_start((uv_stream_t*)&connection, on_alloc, on_read1));
}
static void on_connect(uv_connect_t* req, int status) {
ASSERT_OK(status);
do_write((uv_stream_t*)&client, on_write_close_immediately);
}
TEST_IMPL(tcp_read_stop_start) {
uv_loop_t* loop = uv_default_loop();
{ /* Server */
struct sockaddr_in addr;
ASSERT_OK(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
ASSERT_OK(uv_tcp_init(loop, &server));
ASSERT_OK(uv_tcp_bind(&server, (struct sockaddr*) & addr, 0));
ASSERT_OK(uv_listen((uv_stream_t*)&server, 10, on_connection));
}
{ /* Client */
struct sockaddr_in addr;
ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
ASSERT_OK(uv_tcp_init(loop, &client));
ASSERT_OK(uv_tcp_connect(&connect_req, &client,
(const struct sockaddr*) & addr, on_connect));
}
ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT));
ASSERT_GE(read_cb_called, 2);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
|