• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_IOMGR_UDP_SERVER_H
20 #define GRPC_CORE_LIB_IOMGR_UDP_SERVER_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <vector>
25 
26 #include "src/core/lib/iomgr/endpoint.h"
27 #include "src/core/lib/iomgr/ev_posix.h"
28 #include "src/core/lib/iomgr/resolve_address.h"
29 
30 /* Forward decl of struct grpc_server */
31 /* This is not typedef'ed to avoid a typedef-redefinition error */
32 struct grpc_server;
33 
34 /* Forward decl of grpc_udp_server */
35 typedef struct grpc_udp_server grpc_udp_server;
36 
37 /* An interface associated with a socket. udp server delivers I/O event on that
38  * socket to the subclass of this interface which is created through
39  * GrpcUdpHandlerFactory.
40  * Its implementation should do the real IO work, e.g. read packet and write. */
41 class GrpcUdpHandler {
42  public:
GrpcUdpHandler(grpc_fd *,void *)43   GrpcUdpHandler(grpc_fd* /* emfd */, void* /* user_data */) {}
~GrpcUdpHandler()44   virtual ~GrpcUdpHandler() {}
45 
46   // Interfaces to be implemented by subclasses to do the actual setup/tear down
47   // or I/O.
48 
49   // Called when data is available to read from the socket. Returns true if
50   // there is more data to read after this call.
51   virtual bool Read() = 0;
52   // Called when socket becomes write unblocked. The given closure should be
53   // scheduled when the socket becomes blocked next time.
54   virtual void OnCanWrite(void* user_data,
55                           grpc_closure* notify_on_write_closure) = 0;
56   // Called before the gRPC FD is orphaned. Notify udp server to continue
57   // orphaning fd by scheduling the given closure, afterwards the associated fd
58   // will be closed.
59   virtual void OnFdAboutToOrphan(grpc_closure* orphan_fd_closure,
60                                  void* user_data) = 0;
61 };
62 
63 class GrpcUdpHandlerFactory {
64  public:
~GrpcUdpHandlerFactory()65   virtual ~GrpcUdpHandlerFactory() {}
66   /* Called when start to listen on a socket.
67    * Return an instance of the implementation of GrpcUdpHandler interface which
68    * will process I/O events for this socket from now on. */
69   virtual GrpcUdpHandler* CreateUdpHandler(grpc_fd* emfd, void* user_data) = 0;
70   virtual void DestroyUdpHandler(GrpcUdpHandler* handler) = 0;
71 };
72 
73 /* Create a server, initially not bound to any ports */
74 grpc_udp_server* grpc_udp_server_create(const grpc_channel_args* args);
75 
76 /* Start listening to bound ports. user_data is passed to callbacks. */
77 void grpc_udp_server_start(grpc_udp_server* udp_server,
78                            const std::vector<grpc_pollset*>* pollsets,
79                            void* user_data);
80 
81 int grpc_udp_server_get_fd(grpc_udp_server* s, unsigned port_index);
82 
83 /* Add a port to the server, returning port number on success, or negative
84    on failure.
85 
86    Create |num_listeners| sockets for given address to listen on using
87    SO_REUSEPORT if supported.
88 
89    The :: and 0.0.0.0 wildcard addresses are treated identically, accepting
90    both IPv4 and IPv6 connections, but :: is the preferred style. This usually
91    creates |num_listeners| sockets, but possibly 2 * |num_listeners| on systems
92    which support IPv6, but not dualstack sockets. */
93 
94 /* TODO(ctiller): deprecate this, and make grpc_udp_server_add_ports to handle
95                   all of the multiple socket port matching logic in one place */
96 int grpc_udp_server_add_port(grpc_udp_server* s, grpc_resolved_address* addr,
97                              int rcv_buf_size, int snd_buf_size,
98                              GrpcUdpHandlerFactory* handler_factory,
99                              size_t num_listeners);
100 
101 void grpc_udp_server_destroy(grpc_udp_server* server, grpc_closure* on_done);
102 
103 #endif /* GRPC_CORE_LIB_IOMGR_UDP_SERVER_H */
104