-
Notifications
You must be signed in to change notification settings - Fork 22
/
TCPSocket.cpp
238 lines (205 loc) · 5.68 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
/*
* Copyright (C) 2010-2013,2016,2018 by Jonathan Naylor G4KLX
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "TCPSocket.h"
#include "UDPSocket.h"
#include "Log.h"
#include <cstdio>
#include <cassert>
#include <cstring>
#if defined(_WIN32) || defined(_WIN64)
typedef int ssize_t;
#else
#include <cerrno>
#endif
CTCPSocket::CTCPSocket(const std::string& address, unsigned int port) :
m_address(address),
m_port(port),
m_fd(-1)
{
assert(!address.empty());
assert(port > 0U);
#if defined(_WIN32) || defined(_WIN64)
WSAData data;
int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data);
if (wsaRet != 0)
LogError("Error from WSAStartup");
#endif
}
CTCPSocket::~CTCPSocket()
{
#if defined(_WIN32) || defined(_WIN64)
::WSACleanup();
#endif
}
bool CTCPSocket::open()
{
if (m_fd != -1)
return true;
if (m_address.empty() || m_port == 0U)
return false;
/* to determine protocol family, call lookup() first.*/
sockaddr_storage addr;
unsigned int addrlen;
if (CUDPSocket::lookup(m_address, m_port, addr, addrlen) != 0)
return false;
m_fd = ::socket(addr.ss_family, SOCK_STREAM, 0);
if (m_fd < 0) {
#if defined(_WIN32) || defined(_WIN64)
LogError("Cannot create the TCP client socket, err=%d", ::GetLastError());
#else
LogError("Cannot create the TCP client socket, err=%d", errno);
#endif
return false;
}
if (::connect(m_fd, (sockaddr*)&addr, addrlen) == -1) {
#if defined(_WIN32) || defined(_WIN64)
LogError("Cannot connect the TCP client socket, err=%d", ::GetLastError());
#else
LogError("Cannot connect the TCP client socket, err=%d", errno);
#endif
close();
return false;
}
int noDelay = 1;
if (::setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, (char *)&noDelay, sizeof(noDelay)) == -1) {
#if defined(_WIN32) || defined(_WIN64)
LogError("Cannot set the TCP client socket option for TCP_NODELAY, err=%d", ::GetLastError());
#else
LogError("Cannot set the TCP client socket option for TCP_NODELAY, err=%d", errno);
#endif
close();
return false;
}
int keepAlive = 1;
if (::setsockopt(m_fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&keepAlive, sizeof(keepAlive)) == -1) {
#if defined(_WIN32) || defined(_WIN64)
LogError("Cannot set the TCP client socket option for SO_KEEPALIVE, err=%d", ::GetLastError());
#else
LogError("Cannot set the TCP client socket option for SO_KEEPALIVE, err=%d", errno);
#endif
close();
return false;
}
return true;
}
int CTCPSocket::read(unsigned char* buffer, unsigned int length, unsigned int secs, unsigned int msecs)
{
assert(buffer != NULL);
assert(length > 0U);
assert(m_fd != -1);
// Check that the recv() won't block
fd_set readFds;
FD_ZERO(&readFds);
#if defined(_WIN32) || defined(_WIN64)
FD_SET((unsigned int)m_fd, &readFds);
#else
FD_SET(m_fd, &readFds);
#endif
// Return after timeout
timeval tv;
tv.tv_sec = secs;
tv.tv_usec = msecs * 1000;
int ret = ::select(m_fd + 1, &readFds, NULL, NULL, &tv);
if (ret < 0) {
#if defined(_WIN32) || defined(_WIN64)
LogError("Error returned from TCP client select, err=%d", ::GetLastError());
#else
LogError("Error returned from TCP client select, err=%d", errno);
#endif
return -1;
}
#if defined(_WIN32) || defined(_WIN64)
if (!FD_ISSET((unsigned int)m_fd, &readFds))
return 0;
#else
if (!FD_ISSET(m_fd, &readFds))
return 0;
#endif
ssize_t len = ::recv(m_fd, (char*)buffer, length, 0);
if (len == 0) {
return -2;
} else if (len < 0) {
#if defined(_WIN32) || defined(_WIN64)
LogError("Error returned from recv, err=%d", ::GetLastError());
#else
LogError("Error returned from recv, err=%d", errno);
#endif
return -1;
}
return len;
}
int CTCPSocket::readLine(std::string& line, unsigned int secs)
{
// Maybe there is a better way to do this like reading blocks, pushing them for later calls
// Nevermind, we'll read one char at a time for the time being.
int resultCode;
int len = 0;
line.clear();
char c[2U];
c[1U] = 0x00U;
do
{
resultCode = read((unsigned char*)c, 1U, secs);
if (resultCode == 1){
line.append(c);
len++;
}
} while (c[0U] != '\n' && resultCode == 1);
return resultCode <= 0 ? resultCode : len;
}
bool CTCPSocket::write(const unsigned char* buffer, unsigned int length)
{
assert(buffer != NULL);
assert(length > 0U);
assert(m_fd != -1);
ssize_t ret = ::send(m_fd, (char *)buffer, length, 0);
if (ret != ssize_t(length)) {
#if defined(_WIN32) || defined(_WIN64)
LogError("Error returned from send, err=%d", ::GetLastError());
#else
LogError("Error returned from send, err=%d", errno);
#endif
return false;
}
return true;
}
bool CTCPSocket::writeLine(const std::string& line)
{
std::string lineCopy(line);
if (lineCopy.length() > 0 && lineCopy.at(lineCopy.length() - 1) != '\n')
lineCopy.append("\n");
//stupidly write one char after the other
size_t len = lineCopy.length();
bool result = true;
for (size_t i = 0U; i < len && result; i++){
unsigned char c = lineCopy.at(i);
result = write(&c , 1);
}
return result;
}
void CTCPSocket::close()
{
if (m_fd != -1) {
#if defined(_WIN32) || defined(_WIN64)
::closesocket(m_fd);
#else
::close(m_fd);
#endif
m_fd = -1;
}
}