Files
pip/libs/cloud/picloudtcp.cpp
2021-04-06 15:24:58 +03:00

108 lines
2.5 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::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);
}
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 == Server) {
PIString client;
ba >> client;
return ba;
}
if (header.role == Client) {
return ba;
}
return PIByteArray();
}
PIString PICloud::TCP::parseConnect(PIByteArray & ba) {
PIString ret;
ba >> ret;
return ret;
}