Imported Upstream version 1.27.0
[platform/upstream/grpc.git] / src / core / lib / iomgr / tcp_custom.h
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 // Same number as the micro of SO_REUSEPORT in kernel
28 #define GRPC_CUSTOM_SOCKET_OPT_SO_REUSEPORT (0x00000200u)
29
30 typedef struct grpc_tcp_listener grpc_tcp_listener;
31 typedef struct grpc_custom_tcp_connect grpc_custom_tcp_connect;
32
33 typedef struct grpc_custom_socket {
34   // Implementation defined
35   void* impl;
36   grpc_endpoint* endpoint;
37   grpc_tcp_listener* listener;
38   grpc_custom_tcp_connect* connector;
39   int refs;
40 } grpc_custom_socket;
41
42 typedef void (*grpc_custom_connect_callback)(grpc_custom_socket* socket,
43                                              grpc_error* error);
44 typedef void (*grpc_custom_write_callback)(grpc_custom_socket* socket,
45                                            grpc_error* error);
46 typedef void (*grpc_custom_read_callback)(grpc_custom_socket* socket,
47                                           size_t nread, grpc_error* error);
48 typedef void (*grpc_custom_accept_callback)(grpc_custom_socket* socket,
49                                             grpc_custom_socket* client,
50                                             grpc_error* error);
51 typedef void (*grpc_custom_close_callback)(grpc_custom_socket* socket);
52
53 typedef struct grpc_socket_vtable {
54   grpc_error* (*init)(grpc_custom_socket* socket, int domain);
55   void (*connect)(grpc_custom_socket* socket, const grpc_sockaddr* addr,
56                   size_t len, grpc_custom_connect_callback cb);
57   void (*destroy)(grpc_custom_socket* socket);
58   void (*shutdown)(grpc_custom_socket* socket);
59   void (*close)(grpc_custom_socket* socket, grpc_custom_close_callback cb);
60   void (*write)(grpc_custom_socket* socket, grpc_slice_buffer* slices,
61                 grpc_custom_write_callback cb);
62   void (*read)(grpc_custom_socket* socket, char* buffer, size_t length,
63                grpc_custom_read_callback cb);
64   grpc_error* (*getpeername)(grpc_custom_socket* socket,
65                              const grpc_sockaddr* addr, int* len);
66   grpc_error* (*getsockname)(grpc_custom_socket* socket,
67                              const grpc_sockaddr* addr, int* len);
68   grpc_error* (*bind)(grpc_custom_socket* socket, const grpc_sockaddr* addr,
69                       size_t len, int flags);
70   grpc_error* (*listen)(grpc_custom_socket* socket);
71   void (*accept)(grpc_custom_socket* socket, grpc_custom_socket* client,
72                  grpc_custom_accept_callback cb);
73 } grpc_socket_vtable;
74
75 /* Internal APIs */
76 void grpc_custom_endpoint_init(grpc_socket_vtable* impl);
77
78 void grpc_custom_close_server_callback(grpc_tcp_listener* listener);
79
80 grpc_endpoint* custom_tcp_endpoint_create(grpc_custom_socket* socket,
81                                           grpc_resource_quota* resource_quota,
82                                           char* peer_string);
83
84 #endif /* GRPC_CORE_LIB_IOMGR_TCP_CUSTOM_H */