forked from augcampos/asterisk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPSocket.cpp
More file actions
221 lines (173 loc) · 6.35 KB
/
TCPSocket.cpp
File metadata and controls
221 lines (173 loc) · 6.35 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
221
/*
* TCPSocket.cpp
*
* Created on: 29 de Jun de 2011
* Author: augcampos
*/
//http://tldp.org/LDP/LG/issue74/tougher.html
#include "asteriskcpp/net/TCPSocket.h"
#include "asteriskcpp/exceptions/IOException.h"
#define RCVBUFSIZE 65536
namespace asteriskcpp {
TCPSocket::TCPSocket(const int fd) :
socketFD(fd), releaseForced(false) {
timeout.tv_usec = NO_TIMEOUT;
timeout.tv_sec = NO_TIMEOUT;
this->resolvePeerAddr();
this->resolveLocalAddr();
}
TCPSocket::TCPSocket(const IPAddress& ipAddress) :
releaseForced(false), peerAddress(ipAddress) {
struct sockaddr_in addr;
if ((socketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Throw(SocketException(std::string("Error creating the socket - ").append(strerror(errno))));
}
memset(&addr, 0, sizeof (addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(peerAddress.getPort());
addr.sin_addr.s_addr = htonl(peerAddress.getIP());
//Open the socket
if (::connect(this->socketFD, (struct sockaddr *) &addr, sizeof (addr)) != 0) {
Throw(SocketException(std::string("Error creating the socket - ").append(strerror(errno))));
}
timeout.tv_usec = NO_TIMEOUT;
timeout.tv_sec = NO_TIMEOUT;
this->resolveLocalAddr();
}
TCPSocket::~TCPSocket() {
boost::mutex::scoped_lock lockRead(this->mutRead);
boost::mutex::scoped_lock lockWrite(this->mutWrite);
this->release();
this->close();
}
int TCPSocket::readData(char* buf, const unsigned int length) {
boost::mutex::scoped_lock lock(this->mutRead);
fd_set fdList;
FD_ZERO(&fdList);
FD_SET(socketFD, &fdList);
int value;
struct timeval timeout = this->timeout;
do {
if (!timeout.tv_usec && !timeout.tv_sec)
value = ::select(socketFD + 1, &fdList, NULL, NULL, NULL);
else
value = ::select(socketFD + 1, &fdList, NULL, NULL, &timeout);
} while (value == -1 && errno == EINTR);
if (value == 0) {
Throw(SocketException(std::string("Error reading from socket - Timeout exception")));
} else if (value == -1) {
Throw(SocketException(std::string("Error reading from socket - ").append(strerror(errno))));
}
if (this->releaseForced)
return (0);
#ifndef _WIN32
int readSize = ::recv(socketFD, buf, length, MSG_NOSIGNAL);
#else
int readSize = ::recv(socketFD, buf, length, 0);
#endif
if (readSize == 0) {
Throw(SocketException(std::string("Error reading from socket - Disconnected")));
} else if (readSize == -1) {
Throw(SocketException(std::string("Error reading from socket - ").append(strerror(errno))));
}
return (readSize);
}
std::string TCPSocket::readData() {
char buffer[(RCVBUFSIZE + 1)] = "\0";
int bytesRead = this->readData(buffer, RCVBUFSIZE);
if (bytesRead > 0) {
std::string rsv(buffer, bytesRead);
return (rsv);
}
return ("");
}
void TCPSocket::writeData(const char* buf, const unsigned int length) {
if (!length) {
return;
}
boost::mutex::scoped_lock lock(this->mutWrite);
#ifndef _WIN32
int writeSize = ::send(socketFD, buf, length, MSG_NOSIGNAL);
#else
int writeSize = ::send(socketFD, buf, length, 0);
#endif
if (writeSize <= 0) {
Throw(SocketException(std::string("Error writing to socket - ").append(strerror(errno))));
}
}
void TCPSocket::writeData(const std::string& data) {
this->writeData(data.c_str(), (unsigned int) (data.length()));
}
void TCPSocket::setTimeout(const unsigned long timeout) {
this->timeout.tv_usec = (timeout % 1000) * 1000;
this->timeout.tv_sec = timeout / 1000;
}
unsigned long TCPSocket::getTimeout() {
unsigned long timeout;
timeout = this->timeout.tv_sec * 1000;
timeout += this->timeout.tv_usec / 1000;
return (timeout);
}
void TCPSocket::release() {
this->releaseForced = true;
#ifdef _WIN32
shutdown(socketFD, SD_BOTH);
#else
::shutdown(this->socketFD, SHUT_RDWR);
#endif
}
void TCPSocket::close() {
#ifdef _WIN32
closesocket(socketFD);
#else
::close(this->socketFD);
#endif
}
bool TCPSocket::check4readData(const unsigned long timeout) {
boost::mutex::scoped_lock lock(this->mutRead);
struct timeval time;
fd_set fdList;
time.tv_usec = (timeout % 1000) * 1000;
time.tv_sec = timeout / 1000;
FD_ZERO(&fdList);
FD_SET(socketFD, &fdList);
if (select(this->socketFD + 1, &fdList, NULL, NULL, &time) <= 0)
return (false);
else
return (true);
}
IPAddress TCPSocket::getLocalAddress() {
return (this->ipAddress);
}
IPAddress TCPSocket::getPeerAddress() {
return (this->peerAddress);
}
void TCPSocket::resolvePeerAddr() {
struct sockaddr_in client_addr;
#ifdef _WIN32
int size = sizeof (client_addr);
#else
socklen_t size = sizeof (client_addr);
#endif
if (::getpeername(this->socketFD, (struct sockaddr *) &client_addr, &size) == -1) {
Throw(SocketException(std::string("Error getting socket addr - ").append(strerror(errno))));
}
this->peerAddress.setIP(ntohl(client_addr.sin_addr.s_addr));
this->peerAddress.setPort(ntohs(client_addr.sin_port));
}
int TCPSocket::getSocketFD() {
return (this->socketFD);
}
void TCPSocket::resolveLocalAddr() {
struct sockaddr_in local_addr;
#ifdef _WIN32
int size = sizeof (local_addr);
#else
socklen_t size = sizeof (local_addr);
#endif
if (::getsockname(this->socketFD, (struct sockaddr *) &local_addr, &size) == -1) {
Throw(SocketException(std::string("Error getting socket addr - ").append(strerror(errno))));
}
this->ipAddress.setPort(ntohs(local_addr.sin_port));
}
}