146 lines
3.3 KiB
C++
146 lines
3.3 KiB
C++
/*
|
|
PIP - Platform Independent Primitives
|
|
PICloud TCP transport
|
|
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
|
|
|
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, 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 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 "picloudtcp.h"
|
|
#include "picrypt.h"
|
|
#include "pichunkstream.h"
|
|
#include "piethernet.h"
|
|
|
|
|
|
const char hash_def_key[] = "_picrypt_";
|
|
|
|
|
|
PICloud::TCP::Header::Header() {
|
|
version = Version_1;
|
|
}
|
|
|
|
|
|
PICloud::TCP::TCP() {
|
|
}
|
|
|
|
|
|
void PICloud::TCP::setRole(PICloud::TCP::Role r) {
|
|
header.role = r;
|
|
}
|
|
|
|
|
|
void PICloud::TCP::setServerName(const PIString & server_name) {
|
|
sname = server_name;
|
|
}
|
|
|
|
|
|
void PICloud::TCP::sendStart(PIEthernet * eth) {
|
|
header.type = PICloud::TCP::Connect;
|
|
PIByteArray ba;
|
|
ba << header << sname;
|
|
eth->send(ba);
|
|
}
|
|
|
|
|
|
void PICloud::TCP::sendConnected(PIEthernet * eth, uint client_id) {
|
|
header.type = PICloud::TCP::Connect;
|
|
PIByteArray ba;
|
|
ba << header << client_id;
|
|
eth->send(ba);
|
|
}
|
|
|
|
|
|
void PICloud::TCP::sendDisconnected(PIEthernet * eth, uint client_id) {
|
|
header.type = PICloud::TCP::Disconnect;
|
|
PIByteArray ba;
|
|
ba << header << client_id;
|
|
eth->send(ba);
|
|
}
|
|
|
|
|
|
void PICloud::TCP::sendData(PIEthernet * eth, const PIByteArray & data) {
|
|
header.type = PICloud::TCP::Data;
|
|
PIByteArray ba;
|
|
ba << header;
|
|
ba.append(data);
|
|
// piCout << "sendData" << ba.toHex();
|
|
eth->send(ba);
|
|
}
|
|
|
|
|
|
int PICloud::TCP::sendData(PIEthernet * eth, const PIByteArray & data, uint client_id) {
|
|
header.type = PICloud::TCP::Data;
|
|
PIByteArray ba;
|
|
ba << header << client_id;
|
|
ba.append(data);
|
|
if (eth->send(ba)) return data.size_s();
|
|
else return -1;
|
|
}
|
|
|
|
|
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteArray & ba) {
|
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> ret;
|
|
ret.first = Invalid;
|
|
if (ba.size() < sizeof(Header)) return ret;
|
|
PICloud::TCP::Header hdr;
|
|
ba >> hdr;
|
|
if (hdr.version != header.version) {
|
|
piCout << "[PICloud]" << "invalid TCP version!";
|
|
return ret;
|
|
}
|
|
ret.first = (Type)hdr.type;
|
|
ret.second = (Role)hdr.role;
|
|
return ret;
|
|
}
|
|
|
|
|
|
PIByteArray PICloud::TCP::parseData(PIByteArray & ba) {
|
|
if (header.role == Client) {
|
|
return ba;
|
|
}
|
|
return PIByteArray();
|
|
}
|
|
|
|
|
|
PIPair<uint, PIByteArray> PICloud::TCP::parseDataServer(PIByteArray & ba) {
|
|
PIPair<uint, PIByteArray> ret;
|
|
ret.first = 0;
|
|
if (header.role == Server) {
|
|
ba >> ret.first;
|
|
ret.second = ba;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
|
|
PIString PICloud::TCP::parseConnect_d(PIByteArray & ba) {
|
|
PIString ret;
|
|
ba >> ret;
|
|
return ret;
|
|
}
|
|
|
|
|
|
uint PICloud::TCP::parseConnect(PIByteArray & ba) {
|
|
uint ret;
|
|
ba >> ret;
|
|
return ret;
|
|
}
|
|
|
|
|
|
uint PICloud::TCP::parseDisconnect(PIByteArray & ba) {
|
|
uint ret;
|
|
ba >> ret;
|
|
return ret;
|
|
}
|