/
abyss_socket.h
155 lines (119 loc) · 4.84 KB
/
abyss_socket.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
151
152
153
154
155
#ifndef SOCKET_H_INCLUDED
#define SOCKET_H_INCLUDED
#include <netinet/in.h>
#include <xmlrpc-c/abyss.h>
#include <netinet/in.h>
#define IPB1(x) (((unsigned char *)(&x))[0])
#define IPB2(x) (((unsigned char *)(&x))[1])
#define IPB3(x) (((unsigned char *)(&x))[2])
#define IPB4(x) (((unsigned char *)(&x))[3])
typedef struct in_addr TIPAddr;
typedef void SocketCloseImpl(TSocket * const socketP);
typedef void SocketDestroyImpl(TSocket * const socketP);
typedef void SocketWriteImpl(TSocket * const socketP,
const unsigned char * const buffer,
uint32_t const len,
abyss_bool * const failedP);
typedef uint32_t SocketReadImpl(TSocket * const socketP,
char * const buffer,
uint32_t const len);
typedef abyss_bool SocketConnectImpl(TSocket * const socketP,
TIPAddr * const addrP,
uint16_t const portNumber);
typedef abyss_bool SocketBindImpl(TSocket * const socketP,
TIPAddr * const addrP,
uint16_t const portNumber);
typedef abyss_bool SocketListenImpl(TSocket * const socketP,
uint32_t const backlog);
typedef void SocketAcceptImpl(TSocket * const listenSocketP,
abyss_bool * const connectedP,
abyss_bool * const failedP,
TSocket ** const acceptedSocketPP,
TIPAddr * const ipAddrP);
typedef uint32_t SocketErrorImpl(TSocket * const socketP);
typedef uint32_t SocketWaitImpl(TSocket * const socketP,
abyss_bool const rd,
abyss_bool const wr,
uint32_t const timems);
typedef uint32_t SocketAvailableReadBytesImpl(TSocket * const socketP);
typedef void SocketGetPeerNameImpl(TSocket * const socketP,
TIPAddr * const ipAddrP,
uint16_t * const portNumberP,
abyss_bool * const successP);
struct TSocketVtbl {
SocketCloseImpl * close;
SocketDestroyImpl * destroy;
SocketWriteImpl * write;
SocketReadImpl * read;
SocketConnectImpl * connect;
SocketBindImpl * bind;
SocketListenImpl * listen;
SocketAcceptImpl * accept;
SocketErrorImpl * error;
SocketWaitImpl * wait;
SocketAvailableReadBytesImpl * availableReadBytes;
SocketGetPeerNameImpl * getPeerName;
};
struct _TSocket {
uint signature;
/* With both background and foreground use of sockets, and
background being both fork and pthread, it is very easy to
screw up socket lifetime and try to destroy twice. We use
this signature to help catch such bugs.
*/
void * implP;
struct TSocketVtbl vtbl;
};
#define TIME_INFINITE 0xffffffff
extern abyss_bool SocketTraceIsActive;
abyss_bool
SocketInit(void);
void
SocketTerm(void);
void
SocketClose(TSocket * const socketP);
void
SocketCreate(const struct TSocketVtbl * const vtblP,
void * const implP,
TSocket ** const socketPP);
void
SocketWrite(TSocket * const socketP,
const unsigned char * const buffer,
uint32_t const len,
abyss_bool * const failedP);
uint32_t
SocketRead(TSocket * const socketP,
unsigned char * const buffer,
uint32_t const len);
abyss_bool
SocketConnect(TSocket * const socketP,
TIPAddr * const addrP,
uint16_t const portNumber);
abyss_bool
SocketBind(TSocket * const socketP,
TIPAddr * const addrP,
uint16_t const portNumber);
abyss_bool
SocketListen(TSocket * const socketP,
uint32_t const backlog);
void
SocketAccept(TSocket * const listenSocketP,
abyss_bool * const connectedP,
abyss_bool * const failedP,
TSocket ** const acceptedSocketP,
TIPAddr * const ipAddrP);
uint32_t
SocketWait(TSocket * const socketP,
abyss_bool const rd,
abyss_bool const wr,
uint32_t const timems);
uint32_t
SocketAvailableReadBytes(TSocket * const socketP);
void
SocketGetPeerName(TSocket * const socketP,
TIPAddr * const ipAddrP,
uint16_t * const portNumberP,
abyss_bool * const successP);
uint32_t
SocketError(TSocket * const socketP);
#endif