-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtcp.h
150 lines (120 loc) · 4.1 KB
/
tcp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef _PROTO_TCP_H_
#define _PROTO_TCP_H_
#include "pub/type.h"
#include "proto/common.h"
#define TCP_SOCKET_HEADER \
tcp_socket_read_t read_func; \
tcp_socket_try_read_t try_read_func; \
tcp_socket_write_t write_func; \
tcp_socket_bind_t bind_func; \
tcp_socket_listen_t listen_func; \
tcp_socket_accept_t accept_func; \
tcp_socket_handshake_t handshake_func; \
tcp_socket_connect_t connect_func; \
tcp_socket_revent_t revent_func; \
tcp_socket_close_t close_func; \
tcp_socket_free_t free_func; \
tcp_socket_target_t target_func; /* optional*/
struct tcp_socket_t_tag;
// these three functions should be thread-safe
typedef ssize_t (*tcp_socket_read_t)(struct tcp_socket_t_tag *sock, byte_t *buf, size_t size);
typedef ssize_t (*tcp_socket_try_read_t)(struct tcp_socket_t_tag *sock, byte_t *buf, size_t size);
typedef ssize_t (*tcp_socket_write_t)(struct tcp_socket_t_tag *sock, const byte_t *buf, size_t size);
typedef int (*tcp_socket_bind_t)(struct tcp_socket_t_tag *sock, const char *node, const char *port);
typedef int (*tcp_socket_listen_t)(struct tcp_socket_t_tag *sock, int backlog);
typedef struct tcp_socket_t_tag *(*tcp_socket_accept_t)(struct tcp_socket_t_tag *sock);
typedef int (*tcp_socket_handshake_t)(struct tcp_socket_t_tag *sock);
typedef int (*tcp_socket_connect_t)(struct tcp_socket_t_tag *sock, const char *node, const char *port);
typedef fd_t (*tcp_socket_revent_t)(struct tcp_socket_t_tag *sock);
typedef target_id_t *(*tcp_socket_target_t)(struct tcp_socket_t_tag *sock);
typedef int (*tcp_socket_close_t)(struct tcp_socket_t_tag *sock); // NOTE: close is similar to shutdown(SHUT_WR)
typedef void (*tcp_socket_free_t)(struct tcp_socket_t_tag *sock); // and free is similar to close
// an abstract layer for tcp connection
typedef struct tcp_socket_t_tag {
TCP_SOCKET_HEADER
} tcp_socket_t;
// wrappers
INLINE ssize_t
tcp_socket_read(void *sock, byte_t *buf, size_t size)
{
return ((tcp_socket_t *)sock)->read_func(sock, buf, size);
}
// non-blocking read
INLINE ssize_t
tcp_socket_try_read(void *sock, byte_t *buf, size_t size)
{
return ((tcp_socket_t *)sock)->try_read_func(sock, buf, size);
}
INLINE ssize_t
tcp_socket_write(void *sock, const byte_t *buf, size_t size)
{
return ((tcp_socket_t *)sock)->write_func(sock, buf, size);
}
INLINE int
tcp_socket_bind(void *sock, const char *node, const char *port)
{
return ((tcp_socket_t *)sock)->bind_func(sock, node, port);
}
INLINE int
tcp_socket_listen(void *sock, int backlog)
{
return ((tcp_socket_t *)sock)->listen_func(sock, backlog);
}
INLINE void *
tcp_socket_accept(void *sock)
{
return ((tcp_socket_t *)sock)->accept_func(sock);
}
// original accept is splitted into accept and handshake
INLINE int
tcp_socket_handshake(void *sock)
{
return ((tcp_socket_t *)sock)->handshake_func(sock);
}
INLINE int
tcp_socket_connect(void *sock, const char *node, const char *port)
{
return ((tcp_socket_t *)sock)->connect_func(sock, node, port);
}
// return a fd for listening to read event
INLINE fd_t
tcp_socket_revent(void *sock)
{
return ((tcp_socket_t *)sock)->revent_func(sock);
}
INLINE int
tcp_socket_close(void *sock)
{
return ((tcp_socket_t *)sock)->close_func(sock);
}
INLINE void
tcp_socket_free(void *sock)
{
((tcp_socket_t *)sock)->free_func(sock);
}
// only used for proxy protocols
INLINE target_id_t *
tcp_socket_target(void *sock)
{
ASSERT(((tcp_socket_t *)sock)->target_func, "target function not implemented");
return ((tcp_socket_t *)sock)->target_func(sock);
}
INLINE int
tcp_socket_bind_target(void *sock, const target_id_t *target)
{
char node_buf[TARGET_ID_MAX_DOMAIN + 1];
char serv_buf[TARGET_ID_MAX_PORT + 1];
target_id_node(target, node_buf);
target_id_port(target, serv_buf);
return tcp_socket_bind(sock, node_buf, serv_buf);
}
INLINE int
tcp_socket_connect_target(void *sock, const target_id_t *target)
{
char node_buf[TARGET_ID_MAX_DOMAIN + 1];
char serv_buf[TARGET_ID_MAX_PORT + 1];
target_id_node(target, node_buf);
target_id_port(target, serv_buf);
return tcp_socket_connect(sock, node_buf, serv_buf);
}
#endif