-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsocketio.cpp
More file actions
220 lines (197 loc) · 4.66 KB
/
Copy pathsocketio.cpp
File metadata and controls
220 lines (197 loc) · 4.66 KB
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
217
218
219
220
/* Copyright (C) 2013 Calpont Corp.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
#include <unistd.h>
#ifdef _MSC_VER
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <icmpapi.h>
#include <stdio.h>
#else
#include <poll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include <cerrno>
#include <sstream>
using namespace std;
#include <boost/scoped_array.hpp>
using namespace boost;
#ifndef _MSC_VER
#include "config.h"
#if defined(__linux__) && !defined(STRERROR_R_CHAR_P)
#define STRERROR_R_CHAR_P 1
#endif
#endif
#include "exceptclasses.h"
#include "socketio.h"
namespace qfe
{
namespace socketio
{
#ifndef _MSC_VER
void readn(int fd, void* buf, const size_t wanted)
{
size_t needed = wanted;
size_t sofar = 0;
char* p = static_cast<char*>(buf);
ssize_t rrc = -1;
pollfd fds[1];
int en = 0;
fds[0].fd = fd;
fds[0].events = POLLIN;
while (wanted > sofar)
{
fds[0].revents = 0;
poll(fds, 1, -1);
errno = 0;
rrc = read(fd, (p + sofar), needed);
en = errno;
if (rrc < 0)
{
if (en == EAGAIN || en == EINTR || en == 512)
continue;
ostringstream oss;
oss << "qfe: readn: read() returned " << rrc << " (" << strerror(en) << ")";
idbassert_s(0, oss.str());
}
needed -= rrc;
sofar += rrc;
}
}
size_t writen(int fd, const void* data, const size_t nbytes)
{
size_t nleft;
ssize_t nwritten;
const char* bufp = static_cast<const char*>(data);
nleft = nbytes;
while (nleft > 0)
{
// the O_NONBLOCK flag is not set, this is a blocking I/O.
if ((nwritten = ::write(fd, bufp, nleft)) < 0)
{
if (errno == EINTR)
nwritten = 0;
else {
// save the error no first
int e = errno;
string errorMsg = "qfe: writen: write() error: ";
scoped_array<char> buf(new char[80]);
#if STRERROR_R_CHAR_P
const char* p;
if ((p = strerror_r(e, buf.get(), 80)) != 0)
errorMsg += p;
#else
int p;
if ((p = strerror_r(e, buf.get(), 80)) == 0)
errorMsg += buf.get();
#endif
idbassert_s(0, errorMsg);
}
}
nleft -= nwritten;
bufp += nwritten;
}
return nbytes;
}
#else
const size_t MAX_RECV_BYTES=64*1024;
void reads(SOCKET fd, void* buf, const size_t wanted)
{
size_t needed = wanted;
size_t sofar = 0;
char* p = reinterpret_cast<char*>(buf);
ssize_t rrc = -1;
pollfd fds[1];
int en = 0;
fds[0].fd = fd;
fds[0].events = POLLIN;
while (wanted > sofar)
{
fds[0].revents = 0;
poll(fds, 1, -1);
errno = 0;
//Windows recv() can only read so much at a time...
int thisrecv = static_cast<int>(std::min(needed, MAX_RECV_BYTES));
rrc = ::recv(fd, (p + sofar), thisrecv, 0);
en = errno;
if (rrc < 0)
{
if (en == EAGAIN || en == EINTR)
continue;
ostringstream oss;
oss << "qfe: reads: read() returned " << rrc << " (" << strerror(en) << ")";
idbassert_s(0, oss.str());
}
needed -= rrc;
sofar += rrc;
}
}
size_t writes(SOCKET fd, const void* data, const size_t nbytes)
{
size_t nleft;
ssize_t nwritten;
const char* bufp = static_cast<const char*>(data);
nleft = nbytes;
while (nleft > 0)
{
int thissend = static_cast<int>(std::min(nleft, MAX_RECV_BYTES));
nwritten = ::send(fd, bufp, thissend, 0);
int en = errno;
if (nwritten == SOCKET_ERROR)
{
int wsaerrno = WSAGetLastError();
if (en == EINTR)
nwritten = 0;
else {
ostringstream oss;
oss << "qfe: writes: send() returned " << nwritten << " (WSA: " << wsaerrno << ")";
idbassert_s(0, oss.str());
}
}
nleft -= nwritten;
bufp += nwritten;
}
return nbytes;
}
#endif
uint32_t readNumber32(SockType fd)
{
uint32_t np;
SockReadFcn(fd, &np, 4);
return np;
}
string readString(SockType fd)
{
string s;
uint32_t len = readNumber32(fd);
if (len > 0)
{
scoped_array<char> buf(new char[len+1]);
SockReadFcn(fd, buf.get(), len);
buf[len] = 0;
s = buf.get();
}
return s;
}
void writeString(SockType fd, const string& data)
{
uint32_t len=data.length();
SockWriteFcn(fd, &len, 4);
SockWriteFcn(fd, data.c_str(), len);
}
} //namespace qfe::socketio
} //namespace qfe