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_ENDPOINT_H 20 #define GRPC_CORE_LIB_IOMGR_ENDPOINT_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include "absl/strings/string_view.h" 25 26 #include <grpc/slice.h> 27 #include <grpc/slice_buffer.h> 28 #include <grpc/support/time.h> 29 #include "src/core/lib/iomgr/pollset.h" 30 #include "src/core/lib/iomgr/pollset_set.h" 31 #include "src/core/lib/iomgr/resource_quota.h" 32 33 /* An endpoint caps a streaming channel between two communicating processes. 34 Examples may be: a tcp socket, <stdin+stdout>, or some shared memory. */ 35 36 typedef struct grpc_endpoint grpc_endpoint; 37 typedef struct grpc_endpoint_vtable grpc_endpoint_vtable; 38 39 struct grpc_endpoint_vtable { 40 void (*read)(grpc_endpoint* ep, grpc_slice_buffer* slices, grpc_closure* cb, 41 bool urgent); 42 void (*write)(grpc_endpoint* ep, grpc_slice_buffer* slices, grpc_closure* cb, 43 void* arg); 44 void (*add_to_pollset)(grpc_endpoint* ep, grpc_pollset* pollset); 45 void (*add_to_pollset_set)(grpc_endpoint* ep, grpc_pollset_set* pollset); 46 void (*delete_from_pollset_set)(grpc_endpoint* ep, grpc_pollset_set* pollset); 47 void (*shutdown)(grpc_endpoint* ep, grpc_error* why); 48 void (*destroy)(grpc_endpoint* ep); 49 grpc_resource_user* (*get_resource_user)(grpc_endpoint* ep); 50 absl::string_view (*get_peer)(grpc_endpoint* ep); 51 absl::string_view (*get_local_address)(grpc_endpoint* ep); 52 int (*get_fd)(grpc_endpoint* ep); 53 bool (*can_track_err)(grpc_endpoint* ep); 54 }; 55 56 /* When data is available on the connection, calls the callback with slices. 57 Callback success indicates that the endpoint can accept more reads, failure 58 indicates the endpoint is closed. 59 Valid slices may be placed into \a slices even when the callback is 60 invoked with error != GRPC_ERROR_NONE. */ 61 void grpc_endpoint_read(grpc_endpoint* ep, grpc_slice_buffer* slices, 62 grpc_closure* cb, bool urgent); 63 64 absl::string_view grpc_endpoint_get_peer(grpc_endpoint* ep); 65 66 absl::string_view grpc_endpoint_get_local_address(grpc_endpoint* ep); 67 68 /* Get the file descriptor used by \a ep. Return -1 if \a ep is not using an fd. 69 */ 70 int grpc_endpoint_get_fd(grpc_endpoint* ep); 71 72 /* Write slices out to the socket. 73 74 If the connection is ready for more data after the end of the call, it 75 returns GRPC_ENDPOINT_DONE. 76 Otherwise it returns GRPC_ENDPOINT_PENDING and calls cb when the 77 connection is ready for more data. 78 \a slices may be mutated at will by the endpoint until cb is called. 79 No guarantee is made to the content of slices after a write EXCEPT that 80 it is a valid slice buffer. 81 \a arg is platform specific. It is currently only used by TCP on linux 82 platforms as an argument that would be forwarded to the timestamps callback. 83 */ 84 void grpc_endpoint_write(grpc_endpoint* ep, grpc_slice_buffer* slices, 85 grpc_closure* cb, void* arg); 86 87 /* Causes any pending and future read/write callbacks to run immediately with 88 success==0 */ 89 void grpc_endpoint_shutdown(grpc_endpoint* ep, grpc_error* why); 90 void grpc_endpoint_destroy(grpc_endpoint* ep); 91 92 /* Add an endpoint to a pollset or pollset_set, so that when the pollset is 93 polled, events from this endpoint are considered */ 94 void grpc_endpoint_add_to_pollset(grpc_endpoint* ep, grpc_pollset* pollset); 95 void grpc_endpoint_add_to_pollset_set(grpc_endpoint* ep, 96 grpc_pollset_set* pollset_set); 97 98 /* Delete an endpoint from a pollset_set */ 99 void grpc_endpoint_delete_from_pollset_set(grpc_endpoint* ep, 100 grpc_pollset_set* pollset_set); 101 102 grpc_resource_user* grpc_endpoint_get_resource_user(grpc_endpoint* ep); 103 104 bool grpc_endpoint_can_track_err(grpc_endpoint* ep); 105 106 struct grpc_endpoint { 107 const grpc_endpoint_vtable* vtable; 108 }; 109 110 #endif /* GRPC_CORE_LIB_IOMGR_ENDPOINT_H */ 111