1 /*
2  *
3  * Copyright 2018 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_TCP_CUSTOM_H
20 #define GRPC_CORE_LIB_IOMGR_TCP_CUSTOM_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include "src/core/lib/iomgr/endpoint.h"
25 #include "src/core/lib/iomgr/sockaddr.h"
26 
27 typedef struct grpc_tcp_listener grpc_tcp_listener;
28 typedef struct grpc_custom_tcp_connect grpc_custom_tcp_connect;
29 
30 typedef struct grpc_custom_socket {
31   // Implementation defined
32   void* impl;
33   grpc_endpoint* endpoint;
34   grpc_tcp_listener* listener;
35   grpc_custom_tcp_connect* connector;
36   int refs;
37 } grpc_custom_socket;
38 
39 typedef void (*grpc_custom_connect_callback)(grpc_custom_socket* socket,
40                                              grpc_error* error);
41 typedef void (*grpc_custom_write_callback)(grpc_custom_socket* socket,
42                                            grpc_error* error);
43 typedef void (*grpc_custom_read_callback)(grpc_custom_socket* socket,
44                                           size_t nread, grpc_error* error);
45 typedef void (*grpc_custom_accept_callback)(grpc_custom_socket* socket,
46                                             grpc_custom_socket* client,
47                                             grpc_error* error);
48 typedef void (*grpc_custom_close_callback)(grpc_custom_socket* socket);
49 
50 typedef struct grpc_socket_vtable {
51   grpc_error* (*init)(grpc_custom_socket* socket, int domain);
52   void (*connect)(grpc_custom_socket* socket, const grpc_sockaddr* addr,
53                   size_t len, grpc_custom_connect_callback cb);
54   void (*destroy)(grpc_custom_socket* socket);
55   void (*shutdown)(grpc_custom_socket* socket);
56   void (*close)(grpc_custom_socket* socket, grpc_custom_close_callback cb);
57   void (*write)(grpc_custom_socket* socket, grpc_slice_buffer* slices,
58                 grpc_custom_write_callback cb);
59   void (*read)(grpc_custom_socket* socket, char* buffer, size_t length,
60                grpc_custom_read_callback cb);
61   grpc_error* (*getpeername)(grpc_custom_socket* socket,
62                              const grpc_sockaddr* addr, int* len);
63   grpc_error* (*getsockname)(grpc_custom_socket* socket,
64                              const grpc_sockaddr* addr, int* len);
65   grpc_error* (*bind)(grpc_custom_socket* socket, const grpc_sockaddr* addr,
66                       size_t len, int flags);
67   grpc_error* (*listen)(grpc_custom_socket* socket);
68   void (*accept)(grpc_custom_socket* socket, grpc_custom_socket* client,
69                  grpc_custom_accept_callback cb);
70 } grpc_socket_vtable;
71 
72 /* Internal APIs */
73 void grpc_custom_endpoint_init(grpc_socket_vtable* impl);
74 
75 void grpc_custom_close_server_callback(grpc_tcp_listener* listener);
76 
77 grpc_endpoint* custom_tcp_endpoint_create(grpc_custom_socket* socket,
78                                           grpc_resource_quota* resource_quota,
79                                           char* peer_string);
80 
81 #endif /* GRPC_CORE_LIB_IOMGR_TCP_CUSTOM_H */
82