-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_connection.cpp
290 lines (186 loc) · 5.16 KB
/
tcp_connection.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
//-----------------------------------------------------------------------------
/*!
\file
\brief Non blocking TCP connection class
*/
//-----------------------------------------------------------------------------
/* -- Includes ------------------------------------------------------------ */
#include <iostream>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netdb.h>
#include <fcntl.h>
#include "tcp_connection.h"
/* -- Defines ------------------------------------------------------------- */
using namespace std;
/* -- Types --------------------------------------------------------------- */
/* -- (Module) Global Variables ------------------------------------------- */
/* -- Module Global Function Prototypes ----------------------------------- */
/* -- Implementation ------------------------------------------------------ */
NbTcpConnection::NbTcpConnection()
{
this->sock = -1;
memset(&this->address, 0, sizeof(this->address));
}
NbTcpConnection::NbTcpConnection(int sock, const struct sockaddr_in * address)
{
this->sock = sock;
this->address = *address;
}
bool NbTcpConnection::isOpen()
{
return (this->sock >= 0);
}
int NbTcpConnection::recv(uint8_t * buffer, size_t bufferLen)
{
int status;
//check
if (this->sock < 0)
{
cout << "Failed to receive from closed connection!" << endl;
return -1;
}
//Receive a reply from the server
status = ::recv(this->sock, buffer, bufferLen, 0);
if ((status == -1) && (errno == EWOULDBLOCK)) //noting received
{
return 0;
}
//receiving 0 on a non blocking socket means, the socket was closed remotely
//receiveing a negative value (other than EWOULDBLOCK indicates an error)
if (status <= 0)
{
this->close();
return -1;
}
//otherwise - N bytes of data has been received
return status;
}
int NbTcpConnection::send(const uint8_t * data, size_t dataLen, bool more)
{
int flags = (more ? MSG_MORE : 0);
int status;
//check
if (this->sock < 0)
{
cout << "Failed to send to closed connection!" << endl;
return -1;
}
//Send some data
status = ::send(this->sock, data, dataLen, flags);
if (status < 0)
{
cout << "Failed to send data to server!" << endl;
return -1;
}
return status;
}
void NbTcpConnection::close()
{
if (this->sock >= 0)
{
::shutdown(this->sock, SHUT_RD); //block until all queued bytes are sent!!!
::close(this->sock);
this->sock = -1;
memset(&this->address, 0, sizeof(this->address));
}
}
//-----------------------------------------------------------------------------------
NbTcpServer::NbTcpServer()
{
//nothing special todo here
}
int NbTcpServer::open(const uint16_t port)
{
struct sockaddr_in address = { 0 };
int status;
int sock;
//create TCP socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock >= 0)
{
//bind socket to given port
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = INADDR_ANY;
status = ::bind(sock, (struct sockaddr *)&address, sizeof(address));
if (status >= 0)
{
//start to listen
status = ::listen(sock, 3); //backlog of 3
if (status >= 0)
{
//make socket non-blocking
fcntl(sock, F_SETFL, O_NONBLOCK);
//
this->sock = sock;
this->address = address;
return sock;
}
}
//something went wrong ...
::close(sock);
}
return -1;
}
NbTcpConnection * NbTcpServer::serve()
{
struct sockaddr_in address = { 0 };
socklen_t addressSize = sizeof(address);
int connection;
//check
if (this->sock < 0)
{
cout << "Failed to serve on closed connection!" << endl;
return NULL;
}
//test for incomming connections
connection = ::accept(this->sock, (struct sockaddr *)&address, &addressSize);
if (connection >= 0)
{
//make socket non-blocking
fcntl(connection, F_SETFL, O_NONBLOCK);
//return connection instance
return new NbTcpConnection(connection, &address);
}
return NULL;
}
//-----------------------------------------------------------------------------------
NbTcpClient::NbTcpClient()
{
//nothing special todo here
}
int NbTcpClient::open(const char * ip, const uint16_t port)
{
struct sockaddr_in address = { 0 };
int status;
int sock;
//create TCP socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock >= 0)
{
//connect to remote host
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = inet_addr(ip);
status = ::connect(sock , (struct sockaddr *)&address , sizeof(address));
if (status >= 0)
{
//make socket non-blocking
fcntl(sock, F_SETFL, O_NONBLOCK);
//
this->sock = sock;
this->address = address;
return sock;
}
//something went wrong ...
::close(sock);
}
return -1;
}