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