-
Notifications
You must be signed in to change notification settings - Fork 42
/
tcpstack.cpp
93 lines (74 loc) · 2.45 KB
/
tcpstack.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
/*************************************************************************
* UrBackup - Client/Server backup system
* Copyright (C) 2011-2015 Martin Raiber
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#ifdef _WIN32
#pragma warning (disable: 4996)
#endif
#include "tcpstack.h"
#include <memory.h>
#include "stringtools.h"
const unsigned int max_packet_size = 1 * 1024 * 1024;
void CTCPStack::AddData(char* buf, size_t datasize)
{
if(datasize>0)
{
size_t osize=buffer.size();
buffer.resize(osize+datasize);
memcpy(&buffer[osize], buf, datasize);
}
}
size_t CTCPStack::Send(wxSocketBase* p, char* buf, size_t msglen)
{
char* buffer=new char[msglen+sizeof(MAX_PACKETSIZE)];
MAX_PACKETSIZE len=(MAX_PACKETSIZE)msglen;
memcpy(buffer, &len, sizeof(MAX_PACKETSIZE) );
memcpy(&buffer[sizeof(MAX_PACKETSIZE)], buf, msglen);
p->Write(buffer, (wxUint32)(msglen+sizeof(MAX_PACKETSIZE)));
delete[] buffer;
return msglen;
}
size_t CTCPStack::Send(wxSocketBase* p, const std::string &msg)
{
return Send(p, (char*)msg.c_str(), msg.size());
}
char* CTCPStack::getPacket(size_t* packetsize)
{
if (buffer.size() >= sizeof(MAX_PACKETSIZE))
{
MAX_PACKETSIZE len;
memcpy(&len, &buffer[0], sizeof(MAX_PACKETSIZE));
len = little_endian(len);
if (len<max_packet_size && buffer.size() >= (size_t)len + sizeof(MAX_PACKETSIZE))
{
char* buf = new char[len + 1];
if (len>0)
{
memcpy(buf, &buffer[sizeof(MAX_PACKETSIZE)], len);
}
(*packetsize) = len;
buffer.erase(buffer.begin(), buffer.begin() + len + sizeof(MAX_PACKETSIZE));
buf[len] = 0;
return buf;
}
}
*packetsize = 0;
return NULL;
}
void CTCPStack::reset(void)
{
buffer.clear();
}