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_SURFACE_SERVER_H 20 #define GRPC_CORE_LIB_SURFACE_SERVER_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <grpc/grpc.h> 25 #include "src/core/lib/channel/channel_stack.h" 26 #include "src/core/lib/channel/channelz.h" 27 #include "src/core/lib/debug/trace.h" 28 #include "src/core/lib/transport/transport.h" 29 30 extern const grpc_channel_filter grpc_server_top_filter; 31 32 /** Lightweight tracing of server channel state */ 33 extern grpc_core::TraceFlag grpc_server_channel_trace; 34 35 namespace grpc_core { 36 37 /// Interface for listeners. 38 /// Implementations must override the Orphan() method, which should stop 39 /// listening and initiate destruction of the listener. 40 class ServerListenerInterface : public Orphanable { 41 public: 42 virtual ~ServerListenerInterface() = default; 43 44 /// Starts listening. This listener may refer to the pollset object beyond 45 /// this call, so it is a pointer rather than a reference. 46 virtual void Start(grpc_server* server, 47 const std::vector<grpc_pollset*>* pollsets) = 0; 48 49 /// Returns the channelz node for the listen socket, or null if not 50 /// supported. 51 virtual channelz::ListenSocketNode* channelz_listen_socket_node() const = 0; 52 53 /// Sets a closure to be invoked by the listener when its destruction 54 /// is complete. 55 virtual void SetOnDestroyDone(grpc_closure* on_destroy_done) = 0; 56 }; 57 58 } // namespace grpc_core 59 60 /* Add a listener to the server: when the server starts, it will call Start(), 61 and when it shuts down, it will orphan the listener. */ 62 void grpc_server_add_listener( 63 grpc_server* server, 64 grpc_core::OrphanablePtr<grpc_core::ServerListenerInterface> listener); 65 66 /* Setup a transport - creates a channel stack, binds the transport to the 67 server */ 68 void grpc_server_setup_transport( 69 grpc_server* server, grpc_transport* transport, 70 grpc_pollset* accepting_pollset, const grpc_channel_args* args, 71 const grpc_core::RefCountedPtr<grpc_core::channelz::SocketNode>& 72 socket_node, 73 grpc_resource_user* resource_user = nullptr); 74 75 grpc_core::channelz::ServerNode* grpc_server_get_channelz_node( 76 grpc_server* server); 77 78 const grpc_channel_args* grpc_server_get_channel_args(grpc_server* server); 79 80 grpc_resource_user* grpc_server_get_default_resource_user(grpc_server* server); 81 82 bool grpc_server_has_open_connections(grpc_server* server); 83 84 // Do not call this before grpc_server_start. Returns the pollsets. The vector 85 // itself is immutable, but the pollsets inside are mutable. The result is valid 86 // for the lifetime of the server. 87 const std::vector<grpc_pollset*>& grpc_server_get_pollsets(grpc_server* server); 88 89 namespace grpc_core { 90 91 // An object to represent the most relevant characteristics of a newly-allocated 92 // call object when using an AllocatingRequestMatcherBatch 93 struct ServerBatchCallAllocation { 94 grpc_experimental_completion_queue_functor* tag; 95 grpc_call** call; 96 grpc_metadata_array* initial_metadata; 97 grpc_call_details* details; 98 }; 99 100 // An object to represent the most relevant characteristics of a newly-allocated 101 // call object when using an AllocatingRequestMatcherRegistered 102 struct ServerRegisteredCallAllocation { 103 grpc_experimental_completion_queue_functor* tag; 104 grpc_call** call; 105 grpc_metadata_array* initial_metadata; 106 gpr_timespec* deadline; 107 grpc_byte_buffer** optional_payload; 108 }; 109 110 // Functions to specify that a specific registered method or the unregistered 111 // collection should use a specific allocator for request matching. 112 void SetServerRegisteredMethodAllocator( 113 grpc_server* server, grpc_completion_queue* cq, void* method_tag, 114 std::function<ServerRegisteredCallAllocation()> allocator); 115 void SetServerBatchMethodAllocator( 116 grpc_server* server, grpc_completion_queue* cq, 117 std::function<ServerBatchCallAllocation()> allocator); 118 119 } // namespace grpc_core 120 121 #endif /* GRPC_CORE_LIB_SURFACE_SERVER_H */ 122