blackhole-server.c
Go to the documentation of this file.
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 
22 #include "uv.h"
23 #include "task.h"
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 
28 typedef struct {
31 } conn_rec;
32 
34 
35 static void connection_cb(uv_stream_t* stream, int status);
36 static void alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
37 static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);
38 static void shutdown_cb(uv_shutdown_t* req, int status);
39 static void close_cb(uv_handle_t* handle);
40 
41 
42 static void connection_cb(uv_stream_t* stream, int status) {
43  conn_rec* conn;
44  int r;
45 
46  ASSERT(status == 0);
48 
49  conn = malloc(sizeof *conn);
50  ASSERT(conn != NULL);
51 
52  r = uv_tcp_init(stream->loop, &conn->handle);
53  ASSERT(r == 0);
54 
55  r = uv_accept(stream, (uv_stream_t*)&conn->handle);
56  ASSERT(r == 0);
57 
59  ASSERT(r == 0);
60 }
61 
62 
64  size_t suggested_size,
65  uv_buf_t* buf) {
66  static char slab[65536];
67  buf->base = slab;
68  buf->len = sizeof(slab);
69 }
70 
71 
72 static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
73  conn_rec* conn;
74  int r;
75 
76  if (nread >= 0)
77  return;
78 
79  ASSERT(nread == UV_EOF);
80 
82 
83  r = uv_shutdown(&conn->shutdown_req, stream, shutdown_cb);
84  ASSERT(r == 0);
85 }
86 
87 
88 static void shutdown_cb(uv_shutdown_t* req, int status) {
90  uv_close((uv_handle_t*)&conn->handle, close_cb);
91 }
92 
93 
94 static void close_cb(uv_handle_t* handle) {
96  free(conn);
97 }
98 
99 
100 HELPER_IMPL(tcp4_blackhole_server) {
101  struct sockaddr_in addr;
102  uv_loop_t* loop;
103  int r;
104 
105  loop = uv_default_loop();
106  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
107 
109  ASSERT(r == 0);
110 
111  r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
112  ASSERT(r == 0);
113 
115  ASSERT(r == 0);
116 
118  ASSERT(0 && "Blackhole server dropped out of event loop.");
119 
120  return 0;
121 }
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
slab
static char slab[1]
Definition: test-watcher-cross-stop.c:37
task.h
shutdown_cb
static void shutdown_cb(uv_shutdown_t *req, int status)
Definition: blackhole-server.c:88
conn_rec
Definition: blackhole-server.c:28
uv_shutdown_s
Definition: uv.h:417
HELPER_IMPL
HELPER_IMPL(tcp4_blackhole_server)
Definition: blackhole-server.c:100
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
uv_listen
UV_EXTERN int uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb)
Definition: unix/stream.c:656
ASSERT
#define ASSERT(expr)
Definition: task.h:102
status
absl::Status status
Definition: rls.cc:251
conn_rec::shutdown_req
uv_shutdown_t shutdown_req
Definition: blackhole-server.c:30
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
container_of
#define container_of(ptr, type, member)
Definition: uv-common.h:57
shutdown_req
static uv_shutdown_t shutdown_req
Definition: benchmark-tcp-write-batch.c:40
uv_tcp_bind
UV_EXTERN int uv_tcp_bind(uv_tcp_t *handle, const struct sockaddr *addr, unsigned int flags)
Definition: uv-common.c:277
close_cb
static void close_cb(uv_handle_t *handle)
Definition: blackhole-server.c:94
TEST_PORT
#define TEST_PORT
Definition: task.h:53
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_stream_s
Definition: uv.h:491
uv_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
ssize_t
intptr_t ssize_t
Definition: win.h:27
req
static uv_connect_t req
Definition: test-connection-fail.c:30
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_shutdown
UV_PRIVATE_REQ_TYPES UV_EXTERN int uv_shutdown(uv_shutdown_t *req, uv_stream_t *handle, uv_shutdown_cb cb)
Definition: unix/stream.c:1259
uv_read_start
UV_EXTERN int uv_read_start(uv_stream_t *, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: unix/stream.c:1555
read_cb
static void read_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
Definition: blackhole-server.c:72
uv_tcp_init
UV_EXTERN int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: unix/tcp.c:143
uv_accept
UV_EXTERN int uv_accept(uv_stream_t *server, uv_stream_t *client)
Definition: unix/stream.c:591
uv_tcp_s
Definition: uv.h:544
http2_server_health_check.conn
conn
Definition: http2_server_health_check.py:29
uv.h
tcp_server
static uv_tcp_t tcp_server
Definition: blackhole-server.c:33
conn_rec::handle
uv_tcp_t handle
Definition: blackhole-server.c:29
uv_buf_t
Definition: unix.h:121
fix_build_deps.r
r
Definition: fix_build_deps.py:491
handle
static csh handle
Definition: test_arm_regression.c:16
alloc_cb
static void alloc_cb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
Definition: blackhole-server.c:63
uv_handle_s
Definition: uv.h:441
uv_loop_s
Definition: uv.h:1767
conn_rec
struct conn_rec_s conn_rec
connection_cb
static void connection_cb(uv_stream_t *stream, int status)
Definition: blackhole-server.c:42
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
stream
voidpf stream
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:48