-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCPSocket.cpp
276 lines (229 loc) · 6.57 KB
/
TCPSocket.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "TCPSocket.h"
#include "Constant.h"
#include "Utilities.h"
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <exception>
#include <iostream>
#include <unistd.h>
#include <set>
using namespace std;
TCPSocket::TCPSocket()
{
socket_Port = TCP_PORT;
socket_Fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(socket_Fd < 0)
{
throw std::runtime_error("Cannot create TCP Socket");
}
/*struct timeval timeout;
timeout.tv_sec = MAX_RECV_TIMEOUT;
timeout.tv_usec = 0;
if (setsockopt (socket_Fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
sizeof(timeout)) < 0){
FILE_LOG(logERROR) << "setsockopt SO_RCVTIMEO failed\n";
}*/
/*if (setsockopt (socket_Fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
sizeof(timeout)) < 0){
FILE_LOG(logERROR) << "setsockopt SO_RCVTIMEO failed\n";
}*/
}
TCPSocket::TCPSocket(int tcp_socket, sockaddr_in* addr)
{
socket_Fd = tcp_socket;
socketInfo.sin_family = addr->sin_family;
socketInfo.sin_port = addr->sin_port;
socketInfo.sin_addr = addr->sin_addr;
isConnected = true;
isAccepted = true; // incoming client socket
serverSocket = NULL;
}
TCPSocket::~TCPSocket()
{
if(isBound)
{
FILE_LOG(logINFO) << "delete " << socketList.size() << " clients sockets";
for(set<TCPSocket*>::iterator itr = socketList.begin(); itr != socketList.end(); ++itr)
{
TCPSocket* pClient = *itr;
delete pClient;
}
socketList.clear();
}
closeSocket();
}
void TCPSocket::bindSocket()
{
if(isClosed)
{
throw std::runtime_error("Bind error, TCP socket is closed");
}
if(isBound)
{
throw std::runtime_error("TCP socket had been already bound");
}
socketInfo.sin_family = AF_INET;
socketInfo.sin_addr.s_addr = INADDR_ANY;
socketInfo.sin_port = htons(socket_Port);
int enable = 1;
if(::setsockopt(socket_Fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)))
{
FILE_LOG(logERROR) << "set SO_REUSEADDR error";
}
if(::setsockopt(socket_Fd, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(enable)))
{
FILE_LOG(logERROR) << "set SO_REUSEPORT error";
}
if(::bind(socket_Fd, (sockaddr*)&socketInfo, sizeof(socketInfo)) < 0)
{
throw std::runtime_error("Cannot bind to port");
}
FILE_LOG(logINFO) << "TCP socket is bound";
::listen(socket_Fd, MAX_LISTEN_COUNT);
isBound = true;
FILE_LOG(logINFO) << "TCP socket is listening";
}
void TCPSocket::closeSocket()
{
if(!isClosed)
{
// u_long iMode = 0;
//::ioctl(socket_Fd, FIONBIO, &iMode);
::shutdown(socket_Fd, SHUT_RDWR);
::close(socket_Fd);
isClosed = true;
FILE_LOG(logINFO) << "TCP socket is closed";
}
}
TCPSocket* TCPSocket::acceptSocket()
{
if(!isBound)
{
throw std::runtime_error("Cannot accept, socket is not bound");
}
if(isClosed)
{
throw std::runtime_error("Cannot accept, socket is closed");
}
int client_fd;
struct sockaddr_in clientAddr;
socklen_t clientSize = sizeof(struct sockaddr_in);
client_fd = ::accept(socket_Fd, (sockaddr*)&clientAddr, &clientSize);
if(client_fd < 0)
{
FILE_LOG(logERROR) << "Cannot accept connection";
isClosed = true;
return NULL;
}
// TCP socket used as Server (isBound == true) should delte it after disconnection
TCPSocket* pClientSocket = new TCPSocket(client_fd, &clientAddr);
pClientSocket->setServerSocket(this);
socketList.insert(pClientSocket);
FILE_LOG(logINFO) << "Server accepted a incoming connection from ip " << inet_ntoa(clientAddr.sin_addr) << ":"
<< htons(clientAddr.sin_port);
return pClientSocket;
}
int TCPSocket::wait()
{
// for TCP, recv is enough to check if connection drop
return 1;
}
void TCPSocket::connect(const char* host, int port)
{
if(isClosed)
{
throw std::runtime_error("Cannot connect, socket is closed");
}
if(isConnected)
{
FILE_LOG(logERROR) << "Already connected to a remote host";
return;
}
struct sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
serverAddr.sin_addr.s_addr = inet_addr(host);
int ret = ::connect(socket_Fd, (sockaddr*)&serverAddr, sizeof(serverAddr));
if(ret < 0)
{
throw std::runtime_error("Cannot connect to remote host");
}
isConnected = true;
socklen_t addrlen = sizeof(struct sockaddr_in);
::getsockname(socket_Fd, (struct sockaddr*)&socketInfo, &addrlen);
FILE_LOG(logINFO) << "TCP socket is connected to remote from " << getSocketInfoStr();
}
int TCPSocket::send(const char* buffer, int buf_size)
{
if(isClosed)
{
FILE_LOG(logERROR) << "Cannot send on TCP, socket is closed";
return -1;
}
if(!isConnected)
{
FILE_LOG(logERROR) << "Cannot send on TCP, socket is not connected";
return -1;
}
if(buffer == NULL || buf_size == 0)
{
FILE_LOG(logERROR) << "send TCP buffer is wrong";
return -2;
}
int sendBytes = ::send(socket_Fd, buffer, buf_size, 0);
if(sendBytes <= 0)
{
FILE_LOG(logERROR) << "send TCP date error, socket drop";
isConnected = false;
return -3;
}
return sendBytes;
}
int TCPSocket::receive(char* buffer, int buf_size)
{
if(isClosed)
{
FILE_LOG(logERROR) << "Cannot recv on TCP, socket is closed";
return -1;
}
if(!isConnected)
{
FILE_LOG(logERROR) << "Cannot recv on TCP, socket is not connected";
return -1;
}
if(buffer == NULL)
{
FILE_LOG(logERROR) << "recv TCP buffer is wrong";
return -2;
}
int recvBytes = ::recv(socket_Fd, buffer, buf_size, 0);
if(recvBytes < 0)
{
FILE_LOG(logERROR) << "recv TCP data error, socket drop";
isConnected = false;
return -3;
}
else if(recvBytes == 0)
{
FILE_LOG(logERROR) << "recv TCP data zero, socket close";
isConnected = false;
}
if(recvBytes < 10)
{
FILE_LOG(logDEBUG1) << "TCP socket recv " << buffer;
}
return recvBytes;
}
void TCPSocket::removeClientSocket(TCPSocket* pClientSocket)
{
assert(pClientSocket != NULL);
assert(socketList.size() != 0);
if(pClientSocket)
{
delete pClientSocket;
int ret = socketList.erase(pClientSocket);
assert(ret == 1);
pClientSocket = NULL;
}
}