This repository has been archived by the owner on Jan 29, 2019. It is now read-only.
/
socket.cc
175 lines (136 loc) · 2.92 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
/*
* sockets - streams sockets - implementation
* Copyright(c) 2003-2005 of wave++ (Yuri D'Elia)
* Distributed under GNU LGPL without ANY warranty.
*/
// local headers
#include "socket.hh"
#include "resolver.hh"
// system headers
#include <stdexcept>
// c system headers
#include <netinet/in.h>
#include <unistd.h>
#include <errno.h>
// implementation
Socket::~Socket()
{
if(conn)
close();
}
void
Socket::open(const in_addr_t& host, const int port)
{
if(conn)
close();
fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd == -1)
throw std::runtime_error(strerror(errno));
// bind the socket
sockaddr_in addr;
addr.sin_family = AF_INET;
// fetch the port number
addr.sin_port = htons(port);
addr.sin_addr.s_addr = host;
memset(&(addr.sin_zero), 0, sizeof(addr.sin_zero));
if(::connect(fd, reinterpret_cast<struct sockaddr*>(&addr),
sizeof(struct sockaddr)) == -1)
throw std::runtime_error(strerror(errno));
conn = true;
}
void
Socket::open(const char* host, const int port)
{
open(resolve(host), port);
}
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, const timeval* timeout)
{
if(timeout)
{
// reinitialize for linux
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, const timeval* timeout)
{
size_t res = lenght;
while(res)
{
size_t n = read(buffer, res, timeout);
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, const timeval* timeout)
{
// gets works under a non-buffered stream, so it's particularly slow
char* w(buffer);
char b;
size_t pos(0);
while(pos != lenght)
{
if(read(&b, 1, timeout) != 1)
break;
*w++ = b;
++pos;
if(b == term)
break;
}
return pos;
}
size_t
Socket::write(const char* buffer, const size_t lenght)
{
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;
}
}