Skip to content

Commit 15c8ec3

Browse files
committed
Merge pull request grpc#4680 from daniel-j-born/tcp_listener
Make grpc_tcp_listener private.
2 parents eced528 + 25fe36a commit 15c8ec3

File tree

10 files changed

+470
-205
lines changed

10 files changed

+470
-205
lines changed

src/core/iomgr/tcp_server.h

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright 2015, Google Inc.
3+
* Copyright 2015-2016, Google Inc.
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
@@ -34,50 +34,71 @@
3434
#ifndef GRPC_INTERNAL_CORE_IOMGR_TCP_SERVER_H
3535
#define GRPC_INTERNAL_CORE_IOMGR_TCP_SERVER_H
3636

37+
#include "src/core/iomgr/closure.h"
3738
#include "src/core/iomgr/endpoint.h"
3839

3940
/* Forward decl of grpc_tcp_server */
4041
typedef struct grpc_tcp_server grpc_tcp_server;
4142

42-
/* Forward decl of grpc_tcp_listener */
43-
typedef struct grpc_tcp_listener grpc_tcp_listener;
43+
typedef struct grpc_tcp_server_acceptor grpc_tcp_server_acceptor;
44+
struct grpc_tcp_server_acceptor {
45+
/* grpc_tcp_server_cb functions share a ref on from_server that is valid
46+
until the function returns. */
47+
grpc_tcp_server *from_server;
48+
/* Indices that may be passed to grpc_tcp_server_port_fd(). */
49+
unsigned port_index;
50+
unsigned fd_index;
51+
};
4452

4553
/* Called for newly connected TCP connections. */
4654
typedef void (*grpc_tcp_server_cb)(grpc_exec_ctx *exec_ctx, void *arg,
47-
grpc_endpoint *ep);
55+
grpc_endpoint *ep,
56+
grpc_tcp_server_acceptor *acceptor);
4857

49-
/* Create a server, initially not bound to any ports */
50-
grpc_tcp_server *grpc_tcp_server_create(void);
58+
/* Create a server, initially not bound to any ports. The caller owns one ref.
59+
If shutdown_complete is not NULL, it will be used by
60+
grpc_tcp_server_unref(). */
61+
grpc_tcp_server *grpc_tcp_server_create(grpc_closure *shutdown_complete);
5162

5263
/* Start listening to bound ports */
5364
void grpc_tcp_server_start(grpc_exec_ctx *exec_ctx, grpc_tcp_server *server,
5465
grpc_pollset **pollsets, size_t pollset_count,
5566
grpc_tcp_server_cb on_accept_cb, void *cb_arg);
5667

57-
/* Add a port to the server, returning the newly created listener on success,
58-
or a null pointer on failure.
68+
/* Add a port to the server, returning the newly allocated port on success, or
69+
-1 on failure.
5970
6071
The :: and 0.0.0.0 wildcard addresses are treated identically, accepting
6172
both IPv4 and IPv6 connections, but :: is the preferred style. This usually
6273
creates one socket, but possibly two on systems which support IPv6,
6374
but not dualstack sockets. */
6475
/* TODO(ctiller): deprecate this, and make grpc_tcp_server_add_ports to handle
6576
all of the multiple socket port matching logic in one place */
66-
grpc_tcp_listener *grpc_tcp_server_add_port(grpc_tcp_server *s,
67-
const void *addr, size_t addr_len);
77+
int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr,
78+
size_t addr_len);
6879

69-
/* Returns the file descriptor of the Nth listening socket on this server,
70-
or -1 if the index is out of bounds.
80+
/* Number of fds at the given port_index, or 0 if port_index is out of
81+
bounds. */
82+
unsigned grpc_tcp_server_port_fd_count(grpc_tcp_server *s, unsigned port_index);
7183

72-
The file descriptor remains owned by the server, and will be cleaned
73-
up when grpc_tcp_server_destroy is called. */
74-
int grpc_tcp_server_get_fd(grpc_tcp_server *s, unsigned index);
84+
/* Returns the file descriptor of the Mth (fd_index) listening socket of the Nth
85+
(port_index) call to add_port() on this server, or -1 if the indices are out
86+
of bounds. The file descriptor remains owned by the server, and will be
87+
cleaned up when grpc_tcp_server_destroy is called. */
88+
int grpc_tcp_server_port_fd(grpc_tcp_server *s, unsigned port_index,
89+
unsigned fd_index);
7590

76-
void grpc_tcp_server_destroy(grpc_exec_ctx *exec_ctx, grpc_tcp_server *server,
77-
grpc_closure *closure);
91+
/* Ref s and return s. */
92+
grpc_tcp_server *grpc_tcp_server_ref(grpc_tcp_server *s);
7893

79-
int grpc_tcp_listener_get_port(grpc_tcp_listener *listener);
80-
void grpc_tcp_listener_ref(grpc_tcp_listener *listener);
81-
void grpc_tcp_listener_unref(grpc_tcp_listener *listener);
94+
/* shutdown_starting is called when ref count has reached zero and the server is
95+
about to be destroyed. The server will be deleted after it returns. Calling
96+
grpc_tcp_server_ref() from it has no effect. */
97+
void grpc_tcp_server_shutdown_starting_add(grpc_tcp_server *s,
98+
grpc_closure *shutdown_starting);
99+
100+
/* If the recount drops to zero, delete s, and call (exec_ctx==NULL) or enqueue
101+
a call (exec_ctx!=NULL) to shutdown_complete. */
102+
void grpc_tcp_server_unref(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s);
82103

83104
#endif /* GRPC_INTERNAL_CORE_IOMGR_TCP_SERVER_H */

0 commit comments

Comments
 (0)