-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKTcpClient.cpp
401 lines (340 loc) · 8.9 KB
/
KTcpClient.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
*
* Copyright (C) 2015 Jürg Müller, CH-5524
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation version 3 of the License.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/ .
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(__WINDOWS__)
// __MINGW__
#if defined(__CYGWIN__)
#include <winsock.h>
#endif
#include <windows.h>
#endif
#if defined(__LINUX__)
// Definition von sockaddr_in (Socket address, internet style)
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include "NTypes.h"
#include "KTcpClient.h"
/////////////////////////////////////////////////////////////////////////
//
// Klasse KTcpClient
//
/////////////////////////////////////////////////////////////////////////
#if !defined(SD_BOTH)
#define SD_BOTH 2
#endif
KTcpClient::KTcpClient() //: KIpSocket()
{
mConnected = false;
mReadTimeout = 2000;
mSendTimeout = 4000; // 1 Sek. Schreiben auf Flash (ARM-Board) braucht lange
mTrace = false;
mRemotePath[0] = 0;
}
KTcpClient::~KTcpClient()
{
Disconnect();
}
void KTcpClient::Disconnect()
{
if (mConnected)
{
// unter Windows SD_BOTH == 2
// unter Linux SHUT_RDWR == 2
if (mActive)
ErrorCheck(shutdown(mSocket, SD_BOTH), "shutdown");
mConnected = false;
}
Close();
}
bool KTcpClient::CheckConnection(int aTimeout)
{
bool Result;
TSocketBlockModeEnum SaveBlockMode;
SaveBlockMode = GetBlockMode();
SetBlockMode(bmNonBlocking);
Result = Connect(aTimeout);
Disconnect();
SetBlockMode(SaveBlockMode);
return Result;
}
bool KTcpClient::Connect(int aTimeout)
{
sockaddr_in addr;
KIpSocket::Open();
if (mActive && !mConnected)
{
GetSocketAddr(addr, mRemoteHost, mRemotePort);
mConnected = ErrorCheck(connect(mSocket, (sockaddr *)(&addr), sizeof(addr)),
"connect") == 0;
if (!mConnected && mBlockMode == bmNonBlocking && aTimeout > 0)
Select(NULL, &mConnected, NULL, aTimeout);
if (!mConnected)
{
#if defined(__CONSOLE__)
printf("connect error 0x%x\n", mLastError);
#endif
Close();
}
}
return mConnected;
}
bool KTcpClient::ReceiveBuffer(char * Buffer, int BufferLen)
{
bool Ok = true;
while (Ok && BufferLen > 0)
{
int getl = ReceiveBuf(Buffer, BufferLen, 0, 2000);
Ok = getl > 0;
if (Ok && mTrace)
for (int i = 0; i < getl; i++)
{
char c = Buffer[i];
if (c != '\n' && (c < ' ' || c >= 127))
c = '.';
printf("%c", c);
}
Buffer += getl;
BufferLen -= getl;
}
return Ok;
}
bool KTcpClient::ReadBuffer(char* aBuffer, int aToRead)
{
int Read, ToRead;
bool Ok;
ToRead = aToRead;
do
{
Read = ReceiveBuf(aBuffer, ToRead, 0, mReadTimeout);
Ok = Read > 0;
if (Ok)
{
aBuffer += Read;
ToRead -= Read;
}
} while (Ok && mActive && ToRead > 0);
#if defined(__CONSOLE__) && defined(__WINDOWS__)
if (!Ok || !mActive)
{
printf("ReadBuffer error 0x%x (bytes to read %d of %d; timeout: %d)\n",
mLastError, ToRead, aToRead, mReadTimeout);
}
#endif
aBuffer[0] = 0; // aBuffer muss länger als aToRead sein!
// closed by peer
if (!mActive)
mConnected = false;
return Ok;
}
bool KTcpClient::SendBuffer(const char * aBuffer, int aToSend, int TimeOut)
{
int Sent, ToSend;
bool Ok;
if (!aBuffer || !aToSend)
return true;
ToSend = aToSend;
do
{
Sent = SendBuf(aBuffer, ToSend > 10240 ? 10240 : ToSend, 0, TimeOut);
Ok = Sent > 0;
if (Ok)
{
aBuffer += Sent;
ToSend -= Sent;
}
} while (Ok && mActive && ToSend > 0);
#if defined(__CONSOLE__)
if (!Ok || !mActive)
{
printf("SendBuffer error 0x%x (bytes to send %d of %d; timeout: %d)\n",
mLastError, ToSend, aToSend, TimeOut);
}
#endif
// closed by peer
if (!mActive)
mConnected = false;
return Ok;
}
bool KTcpClient::ReadLine(char * aString, int aLength)
{
const char * First = aString;
aString[0] = 0;
int Len = 0;
char Temp[4];
bool Ok = false;
bool CR = false;
while (!Ok &&
ReceiveBuf(Temp, 1, 0, Len ? 100 : 2000) == 1)
{
if (Temp[0] == 13)
{
CR = true;
} else
if (Temp[0] == 10 && CR)
{
Ok = true;
} else
if (aLength <= 1)
{
CR = false;
Ok = false;
} else {
*aString = Temp[0];
aString++;
*aString = 0;
aLength--;
Len++;
CR = false;
}
}
if (Ok && mTrace)
printf("Tcp recv: %s\n", First);
return Ok;
}
bool KTcpClient::WriteString(const char * aBuffer)
{
if (!aBuffer || !*aBuffer)
return true;
return SendBuffer(aBuffer, (int)strlen(aBuffer));
}
bool KTcpClient::WriteLine(const char * aBuffer)
{
if (mTrace && aBuffer)
{
const char * Ptr = aBuffer;
do
{
const char * Next = strstr(Ptr, "\r\n");
printf("Tcp send: ");
if (Next)
{
for (int i = 0; i < Next-Ptr; i++)
printf("%c", Ptr[i]);
Ptr = Next + 2;
} else {
printf("%s", Ptr);
Ptr = NULL;
}
printf("\n");
} while(Ptr);
}
int len = aBuffer ? (int) strlen(aBuffer) : 0;
if (len)
if (!SendBuffer(aBuffer, len, mSendTimeout))
return false;
return SendBuffer("\r\n", 2, len ? mSendTimeout : 100);
}
//////////////////////////////////////////////////////////////////////////////
//
// KHttp
//
//////////////////////////////////////////////////////////////////////////////
const char * KHttp::Methods[htUnknown] =
{
"OPTIONS",
"GET",
"HEAD",
"POST",
"PUT",
"DELETE",
"TRACE",
"CONNECT"
};
KHttp::KHttp()
{
mIpError = false;
mShowLevel = 0;
}
const char * KHttp::StatusCode(int aCode)
{
// Hypertext Transfer Protocol -- HTTP/1.1
// rfc2616
switch (aCode)
{
case 100: return "Continue";
case 101: return "Switching Protocols";
// 2XX: Success
case 200: return "OK";
case 201: return "Created";
case 202: return "Accepted";
case 203: return "Non-Authoritative Information";
case 204: return "No Content";
case 205: return "Reset Content";
case 206: return "Partial Content";
// 3XX: Redirections
case 300: return "Multiple Choices";
case 301: return "Moved Permanently";
case 302: return "Found";
case 303: return "See Other";
case 304: return "Not Modified";
case 305: return "Use Proxy";
case 307: return "Temporary Redirect";
// 4XX Client Errors
case 400: return "Bad Request";
case 401: return "Unauthorized";
case 402: return "Payment Required";
case 403: return "Forbidden";
case 404: return "Not Found";
case 405: return "Method Not Allowed";
case 406: return "Not Acceptable";
case 407: return "Proxy Authentication Required";
case 408: return "Request Time-out";
case 409: return "Conflict";
case 410: return "Gone";
case 411: return "Required";
case 412: return "Precondition Failed";
case 413: return "Request Entity Too Large";
case 414: return "Request-URI Too Large";
case 415: return "Unsupported Media Type";
case 416: return "Requested range not satisfiable";
case 417: return "Expectation Failed";
// 5XX Server errors
case 500: return "Internal Server Error";
case 501: return "Not Implemented";
case 502: return "Bad Gateway";
case 503: return "Service Unavailable";
case 504: return "Gateway Time-out";
case 505: return "HTTP Version not supported";
default: return "unknown";
}
}
bool KHttp::ReadLine(KTcpClient & aClientSocket, char * aString, int aLength, bool aShow)
{
aString[0] = 0;
if (mIpError)
return false;
mIpError = !aClientSocket.ReadLine(aString, aLength);
#if defined(__CONSOLE__)
if ((aShow || mShowLevel >= 3) && !mIpError)
printf("%s\n", aString);
#endif
return !mIpError;
}
bool KHttp::WriteLine(KTcpClient & aClientSocket, const char * aString, bool aShow)
{
if (mIpError)
return false;
#if defined(__CONSOLE__)
if (aShow || mShowLevel >= 3)
printf("%s\n", aString);
#endif
mIpError = !aClientSocket.WriteLine(aString);
return !mIpError;
}