|
1 | 1 | /*
|
2 | 2 | *
|
3 |
| - * Copyright 2015, Google Inc. |
| 3 | + * Copyright 2015-2016, Google Inc. |
4 | 4 | * All rights reserved.
|
5 | 5 | *
|
6 | 6 | * Redistribution and use in source and binary forms, with or without
|
|
34 | 34 | #ifndef GRPC_INTERNAL_CORE_IOMGR_TCP_SERVER_H
|
35 | 35 | #define GRPC_INTERNAL_CORE_IOMGR_TCP_SERVER_H
|
36 | 36 |
|
| 37 | +#include "src/core/iomgr/closure.h" |
37 | 38 | #include "src/core/iomgr/endpoint.h"
|
38 | 39 |
|
39 | 40 | /* Forward decl of grpc_tcp_server */
|
40 | 41 | typedef struct grpc_tcp_server grpc_tcp_server;
|
41 | 42 |
|
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 | +}; |
44 | 52 |
|
45 | 53 | /* Called for newly connected TCP connections. */
|
46 | 54 | 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); |
48 | 57 |
|
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); |
51 | 62 |
|
52 | 63 | /* Start listening to bound ports */
|
53 | 64 | void grpc_tcp_server_start(grpc_exec_ctx *exec_ctx, grpc_tcp_server *server,
|
54 | 65 | grpc_pollset **pollsets, size_t pollset_count,
|
55 | 66 | grpc_tcp_server_cb on_accept_cb, void *cb_arg);
|
56 | 67 |
|
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. |
59 | 70 |
|
60 | 71 | The :: and 0.0.0.0 wildcard addresses are treated identically, accepting
|
61 | 72 | both IPv4 and IPv6 connections, but :: is the preferred style. This usually
|
62 | 73 | creates one socket, but possibly two on systems which support IPv6,
|
63 | 74 | but not dualstack sockets. */
|
64 | 75 | /* TODO(ctiller): deprecate this, and make grpc_tcp_server_add_ports to handle
|
65 | 76 | 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); |
68 | 79 |
|
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); |
71 | 83 |
|
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); |
75 | 90 |
|
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); |
78 | 93 |
|
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); |
82 | 103 |
|
83 | 104 | #endif /* GRPC_INTERNAL_CORE_IOMGR_TCP_SERVER_H */
|
0 commit comments