Home | History | Annotate | Download | only in iomgr
      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 "src/core/lib/gprpp/abstract.h"
     25 #include "src/core/lib/iomgr/endpoint.h"
     26 #include "src/core/lib/iomgr/ev_posix.h"
     27 #include "src/core/lib/iomgr/resolve_address.h"
     28 
     29 /* Forward decl of struct grpc_server */
     30 /* This is not typedef'ed to avoid a typedef-redefinition error */
     31 struct grpc_server;
     32 
     33 /* Forward decl of grpc_udp_server */
     34 typedef struct grpc_udp_server grpc_udp_server;
     35 
     36 /* An interface associated with a socket. udp server delivers I/O event on that
     37  * socket to the subclass of this interface which is created through
     38  * GrpcUdpHandlerFactory.
     39  * Its implementation should do the real IO work, e.g. read packet and write. */
     40 class GrpcUdpHandler {
     41  public:
     42   GrpcUdpHandler(grpc_fd* emfd, void* user_data) {}
     43   virtual ~GrpcUdpHandler() {}
     44 
     45   // Interfaces to be implemented by subclasses to do the actual setup/tear down
     46   // or I/O.
     47 
     48   // Called when data is available to read from the socket. Returns true if
     49   // there is more data to read after this call.
     50   virtual bool Read() GRPC_ABSTRACT;
     51   // Called when socket becomes write unblocked. The given closure should be
     52   // scheduled when the socket becomes blocked next time.
     53   virtual void OnCanWrite(void* user_data,
     54                           grpc_closure* notify_on_write_closure) GRPC_ABSTRACT;
     55   // Called before the gRPC FD is orphaned. Notify udp server to continue
     56   // orphaning fd by scheduling the given closure, afterwards the associated fd
     57   // will be closed.
     58   virtual void OnFdAboutToOrphan(grpc_closure* orphan_fd_closure,
     59                                  void* user_data) GRPC_ABSTRACT;
     60 
     61   GRPC_ABSTRACT_BASE_CLASS
     62 };
     63 
     64 class GrpcUdpHandlerFactory {
     65  public:
     66   virtual ~GrpcUdpHandlerFactory() {}
     67   /* Called when start to listen on a socket.
     68    * Return an instance of the implementation of GrpcUdpHandler interface which
     69    * will process I/O events for this socket from now on. */
     70   virtual GrpcUdpHandler* CreateUdpHandler(grpc_fd* emfd,
     71                                            void* user_data) GRPC_ABSTRACT;
     72   virtual void DestroyUdpHandler(GrpcUdpHandler* handler) GRPC_ABSTRACT;
     73 
     74   GRPC_ABSTRACT_BASE_CLASS
     75 };
     76 
     77 /* Create a server, initially not bound to any ports */
     78 grpc_udp_server* grpc_udp_server_create(const grpc_channel_args* args);
     79 
     80 /* Start listening to bound ports. user_data is passed to callbacks. */
     81 void grpc_udp_server_start(grpc_udp_server* udp_server, grpc_pollset** pollsets,
     82                            size_t pollset_count, void* user_data);
     83 
     84 int grpc_udp_server_get_fd(grpc_udp_server* s, unsigned port_index);
     85 
     86 /* Add a port to the server, returning port number on success, or negative
     87    on failure.
     88 
     89    Create |num_listeners| sockets for given address to listen on using
     90    SO_REUSEPORT if supported.
     91 
     92    The :: and 0.0.0.0 wildcard addresses are treated identically, accepting
     93    both IPv4 and IPv6 connections, but :: is the preferred style. This usually
     94    creates |num_listeners| sockets, but possibly 2 * |num_listeners| on systems
     95    which support IPv6, but not dualstack sockets. */
     96 
     97 /* TODO(ctiller): deprecate this, and make grpc_udp_server_add_ports to handle
     98                   all of the multiple socket port matching logic in one place */
     99 int grpc_udp_server_add_port(grpc_udp_server* s,
    100                              const grpc_resolved_address* addr,
    101                              int rcv_buf_size, int snd_buf_size,
    102                              GrpcUdpHandlerFactory* handler_factory,
    103                              size_t num_listeners);
    104 
    105 void grpc_udp_server_destroy(grpc_udp_server* server, grpc_closure* on_done);
    106 
    107 #endif /* GRPC_CORE_LIB_IOMGR_UDP_SERVER_H */
    108