This repository has been archived by the owner on Jan 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
socket.cc
210 lines (166 loc) · 3.43 KB
/
socket.cc
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
/*
* sockets - streams sockets - implementation
* Copyright(c) 2003-2018 of wave++ (Yuri D'Elia)
* Distributed under GNU LGPL without ANY warranty.
*/
// local headers
#include "socket.hh"
// system headers
#include <stdexcept>
// c system headers
#include <unistd.h>
#include <string.h>
#include <errno.h>
// implementation
Socket::~Socket() throw()
{
if(conn)
{
close();
if(timeout)
delete timeout;
}
}
void
Socket::open(const addrinfo& ai, const timeval* timeout)
{
if(conn)
close();
fd = socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol);
if(fd == -1)
throw std::runtime_error(strerror(errno));
if(::connect(fd, ai.ai_addr, ai.ai_addrlen) == -1)
throw std::runtime_error(strerror(errno));
if(!timeout)
this->timeout = NULL;
else
{
this->timeout = new timeval;
*this->timeout = *timeout;
}
conn = true;
}
void
Socket::open(const char* host, const char* port, const timeval* timeout)
{
// resolve the hostname
addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
addrinfo* ai;
int r = getaddrinfo(host, port, &hints, &ai);
if(r)
throw std::runtime_error(gai_strerror(r));
// connect
try { open(*ai, timeout); }
catch(...)
{
freeaddrinfo(ai);
throw;
}
freeaddrinfo(ai);
}
void
Socket::close(const int how)
{
// do _not_ check return status of close: as close is called under
// the destructor it could be fatal.
if(how)
{
::shutdown(fd, how);
if(how == SHUT_RDWR)
conn = false;
}
else
{
::close(fd);
conn = false;
}
}
size_t
Socket::read(char* buffer, const size_t lenght)
{
if(timeout)
{
// reinitialize for threading
timeval t = *timeout;
fd_set fdSet;
FD_ZERO(&fdSet);
FD_SET(fd, &fdSet);
int rt = select(fd + 1, &fdSet, NULL, NULL, &t);
if(rt < 0)
throw std::runtime_error(strerror(errno));
if(!rt)
throw std::runtime_error("select timeout");
}
ssize_t b = ::recv(fd, buffer, lenght, 0);
if(b == -1)
throw std::runtime_error(strerror(errno));
return b;
}
void
Socket::readn(char* buffer, const size_t lenght)
{
size_t res = lenght;
while(res)
{
size_t n = read(buffer, res);
if(!n)
throw std::runtime_error("connection terminated prematurely");
res -= n;
buffer += n;
}
}
size_t
Socket::gets(char* buffer, const size_t lenght, const char term)
{
// gets works under a non-buffered stream, so it's particularly slow
char* w = buffer;
size_t pos = 0;
char b;
while(pos != lenght)
{
if(read(&b, 1) != 1)
break;
*w++ = b;
++pos;
if(b == term)
break;
}
return pos;
}
size_t
Socket::write(const char* buffer, const size_t lenght)
{
if(timeout)
{
// reinitialize for threading
timeval t = *timeout;
fd_set fdSet;
FD_ZERO(&fdSet);
FD_SET(fd, &fdSet);
int rt = select(fd + 1, NULL, &fdSet, NULL, &t);
if(rt < 0)
throw std::runtime_error(strerror(errno));
if(!rt)
throw std::runtime_error("select timeout");
}
ssize_t b = ::send(fd, buffer, lenght, 0);
if(b == -1)
throw std::runtime_error(strerror(errno));
return b;
}
void
Socket::writen(const char* buffer, const size_t lenght)
{
size_t res = lenght;
while(res)
{
size_t n = write(buffer, res);
if(!n)
throw std::runtime_error("connection terminated prematurely");
res -= n;
buffer += n;
}
}