forked from kzahel/utp
/
utpsocket.cpp
216 lines (194 loc) · 5.14 KB
/
utpsocket.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include "utp.h"
#include "utpsocket.h"
namespace UTP {
struct UTPFunctionTable Socket::utp_funcs {
Socket::_UTPOnReadProc,
Socket::_UTPOnWriteProc,
Socket::_UTPGetRBSize,
Socket::_UTPOnStateChange,
Socket::_UTPOnError,
Socket::_UTPOnOverhead
};
Socket::Socket(int af) :
address_family(af),
bound(false),
iswritable(false),
isclosed(false),
udp_sock(-1),
utp_sock(NULL),
next(NULL),
inbuffer(NULL),
outbuffer(NULL),
insize(0),
outsize(0)
{
}
Socket::Socket(struct UTPSocket *s)
{
utp_sock = s;
}
Socket::~Socket()
{
if (bound && udp_sock != -1)
close(udp_sock);
if (utp_sock)
UTP_Close(utp_sock);
}
int Socket::listen()
{
if (bindsocket() == -1)
return -1;
}
int Socket::connect(struct sockaddr *sa, socklen_t salen)
{
if (bindsocket() == -1)
return -1;
if (!utp_sock) {
utp_sock = UTP_Create(_SendToProc, this, sa, salen);
UTP_SetCallbacks(utp_sock, &utp_funcs, this);
}
UTP_Connect(utp_sock);
}
int Socket::bindsocket()
{
if (!bound) {
struct sockaddr sa;
memset(&sa, 0, sizeof(sa));
sa.sa_family = address_family;
if (udp_sock == -1)
udp_sock = socket(address_family, SOCK_DGRAM, 0);
if (bind(udp_sock, &sa, sizeof(sa)) == -1) {
close(udp_sock);
return -1;
}
int nonblock = 1;
ioctl(udp_sock, FIONBIO, &nonblock);
bound = true;
}
return 0;
}
void Socket::check_timeouts()
{
UTP_CheckTimeouts();
}
void Socket::_SendToProc(void *userdata, const byte *p, size_t len, const struct sockaddr *to, socklen_t tolen)
{
Socket *self = (Socket*)userdata;
sendto(self->udp_sock, p, len, 0, to, tolen);
}
void Socket::_UTPGotIncomingConnection(void *userdata, struct UTPSocket *s)
{
Socket *self = (Socket*)userdata;
Socket *nextSocket = new Socket(s);
nextSocket->next = self->next;
self->next = nextSocket;
nextSocket->address_family = self->address_family;
nextSocket->udp_sock = self->udp_sock;
UTP_SetCallbacks(nextSocket->utp_sock, &nextSocket->utp_funcs, nextSocket);
}
int Socket::recv(char *buf, size_t len)
{
if (udp_sock == -1 || utp_sock == NULL) return -1;
if (insize > len) {
memcpy(buf, inbuffer, len);
memmove(buf, buf + len, insize - len);
insize -= len;
return len;
} else {
memcpy(buf, inbuffer, insize);
free(inbuffer);
len = insize;
insize = 0;
inbuffer = NULL;
return len;
}
}
int Socket::send(const char *buf, size_t len)
{
if (len + outsize > MAXBUFFER)
len = MAXBUFFER - outsize;
if (!len) return 0;
char *newbuf = (char *)realloc((void *)outbuffer, len + outsize);
if (newbuf == NULL) return -1;
memcpy(newbuf + outsize, buf, len);
outbuffer = newbuf;
outsize += len;
writable(UTP_Write(utp_sock, len));
return len;
}
void Socket::handle_readable()
{
char buf[2048];
struct sockaddr sa;
do {
socklen_t salen = sizeof(sa);
int res = recvfrom(udp_sock, buf, sizeof(buf), 0, &sa, &salen);
if (res == -1) return;
UTP_IsIncomingUTP(_UTPGotIncomingConnection, _SendToProc, this, (const byte *)buf, res, (const struct sockaddr *)&sa, salen);
} while (true);
}
Socket *Socket::accept(struct sockaddr *sa, socklen_t *salen)
{
Socket *newsock = next;
if (newsock) {
next = newsock->next;
newsock->next = NULL;
UTP_GetPeerName(newsock->utp_sock, sa, salen);
}
return newsock;
}
void Socket::_UTPOnReadProc(void *userdata, const byte *bytes, size_t count)
{
Socket *self = (Socket *)userdata;
char *newbuf = (char *)realloc((void *)self->inbuffer, count + self->insize);
if (newbuf) {
memcpy(newbuf + self->insize, bytes, count);
self->insize += count;
self->inbuffer = newbuf;
}
}
void Socket::_UTPOnWriteProc(void *userdata, byte *bytes, size_t count)
{
Socket *self = (Socket *)userdata;
memcpy(bytes, self->outbuffer, count);
if (self->outsize - count) {
memmove(self->outbuffer, self->outbuffer + count, self->outsize - count);
self->outsize -= count;
} else {
free(self->outbuffer);
self->outbuffer = NULL;
self->outsize = 0;
}
}
size_t Socket::_UTPGetRBSize(void *userdata)
{
Socket *self = (Socket *)userdata;
return self->insize;
}
void Socket::_UTPOnStateChange(void *userdata, int state)
{
Socket *self = (Socket *)userdata;
self->iswritable =
((state == UTP_STATE_CONNECT) ||
(state == UTP_STATE_WRITABLE));
self->isclosed = (state == UTP_STATE_EOF);
}
void Socket::_UTPOnError(void *userdata, int errorcode)
{
Socket *self = (Socket *)userdata;
self->error(errorcode);
}
void Socket::_UTPOnOverhead(void *userdata, bool send, size_t count, int type)
{
// XXX investigate call after delete
// Socket *self = (Socket *)userdata;
// self->overhead(send, count, type);
}
}