-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCPReaderWriterClient.cpp
241 lines (207 loc) · 5.33 KB
/
TCPReaderWriterClient.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
/*
* Copyright (C) 2010-2013 by Jonathan Naylor G4KLX
* Copyright (C) 2019 by Thomas A. Early N7TAE
*
* 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 "TCPReaderWriterClient.h"
#include <cstdio>
#include <cerrno>
#include <cassert>
#include <cstring>
CTCPReaderWriterClient::CTCPReaderWriterClient(const std::string &address, int family, const std::string &port) :
m_address(address),
m_family(family),
m_port(port),
m_fd(-1)
{
}
CTCPReaderWriterClient::CTCPReaderWriterClient() : m_fd(-1)
{
}
CTCPReaderWriterClient::~CTCPReaderWriterClient()
{
}
bool CTCPReaderWriterClient::Open(const std::string &address, int family, const std::string &port)
{
m_address = address;
m_family = family;
m_port = port;
return Open();
}
bool CTCPReaderWriterClient::Open()
{
if (m_fd != -1)
{
fprintf(stderr, "ERROR: port for '%s' is already open!\n", m_address.c_str());
return true;
}
if (0 == m_address.size() || 0 == m_port.size() || 0 == std::stoul(m_port))
{
fprintf(stderr, "ERROR: '[%s]:%s' is malformed!\n", m_address.c_str(), m_port.c_str());
return true;
}
if (AF_INET!=m_family && AF_INET6!=m_family && AF_UNSPEC!=m_family)
{
fprintf(stderr, "ERROR: family must be AF_INET, AF_INET6 or AF_UNSPEC\n");
return true;
}
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
//hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = IPPROTO_TCP;
struct addrinfo *res;
int s = EAI_AGAIN;
int count = 0;
while (EAI_AGAIN==s and count++<20)
{
// connecting to a server, so we can wait until it's ready
s = getaddrinfo(m_address.c_str(), m_port.c_str(), &hints, &res);
if (s && s != EAI_AGAIN)
{
fprintf(stderr, "ERROR: getaddrinfo of %s: %s\n", m_address.c_str(), gai_strerror(s));
return true;
}
std::this_thread::sleep_for(std::chrono::seconds(3));
}
if (EAI_AGAIN == s)
{
fprintf(stderr, "ERROR getaddrinfo of %s failed 20 times\n", m_address.c_str());
return true;
}
struct addrinfo *rp;
for (rp = res; rp != NULL; rp = rp->ai_next)
{
m_fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (m_fd == -1)
continue;
if (connect(m_fd, rp->ai_addr, rp->ai_addrlen))
{
Close();
continue;
}
else
{
char buf[INET6_ADDRSTRLEN];
void *addr;
if (AF_INET == rp->ai_family)
{
struct sockaddr_in *addr4 = (struct sockaddr_in *)rp->ai_addr;
addr = &(addr4->sin_addr);
}
else
{
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)rp->ai_addr;
addr = &(addr6->sin6_addr);
}
if (inet_ntop(rp->ai_family, addr, buf, INET6_ADDRSTRLEN))
fprintf(stderr, "Successfully connected to %s at [%s]:%s\n", m_address.c_str(), buf, m_port.c_str());
break;
}
}
freeaddrinfo(res);
if (rp == NULL)
{
fprintf(stderr, "Could not connect to any system returned by %s\n", m_address.c_str());
m_fd = -1;
return true;
}
return false;
}
int CTCPReaderWriterClient::ReadExact(unsigned char *buf, const unsigned int length)
{
unsigned int offset = 0U;
do
{
int n = Read(buf + offset, length - offset);
if (n < 0)
return n;
offset += n;
}
while ((length - offset) > 0U);
return length;
}
int CTCPReaderWriterClient::Read(unsigned char* buffer, const unsigned int length)
{
assert(buffer != NULL);
assert(length > 0U);
assert(m_fd != -1);
ssize_t len = recv(m_fd, buffer, length, 0);
if (len <= 0)
{
if (len < 0)
fprintf(stderr, "Error returned from recv, err=%d\n", errno);
return -1;
}
return len;
}
int CTCPReaderWriterClient::ReadLine(std::string& line)
{
unsigned char c;
int resultCode;
int len = 0;
line = "";
do
{
resultCode = Read(&c, 1);
if(resultCode == 1)
{
line += c;
len++;
}
}
while(c != '\n' && resultCode == 1);
return resultCode <= 0 ? resultCode : len;
}
bool CTCPReaderWriterClient::Write(const unsigned char *buffer, const 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 (ret < 0)
fprintf(stderr, "Error returned from send, err=%s\n", strerror(errno));
else
fprintf(stderr, "Error only wrote %d of %d bytes\n", int(ret), int(length));
return true;
}
return false;
}
bool CTCPReaderWriterClient::WriteLine(const std::string& line)
{
std::string lineCopy(line);
if(lineCopy.size() > 0 && lineCopy.at(lineCopy.size() - 1) != '\n')
lineCopy.append("\n");
size_t len = lineCopy.size();
bool result = true;
for(size_t i = 0; i < len && result; i++)
{
unsigned char c = lineCopy.at(i);
result = Write(&c, 1);
}
return result;
}
void CTCPReaderWriterClient::Close()
{
if (m_fd != -1)
{
close(m_fd);
m_fd = -1;
}
}