/*! \file picloudtcp.h
 * \brief PICloud TCP transport
*/
/*
	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/>.
*/

#ifndef PICLOUDTCP_H
#define PICLOUDTCP_H

#include "pip_cloud_export.h"
#include "pistring.h"


class PIEthernet;
class PIStreamPacker;

namespace PICloud {



class PIP_CLOUD_EXPORT TCP {
public:
	enum Version {
		Version_1 = 1,
		Version_2 = 2,
	};

	enum Role {
		InvalidRole = 0,
		Server = 1,
		Client = 2,
	};

	enum Type {
		InvalidType = 0,
		Connect = 1,
		Disconnect = 2,
		Data = 3,
	};

	TCP(PIStreamPacker * s);
	void setRole(Role r);
	Role role() const {return (Role)header.role;}
	void setServerName(const PIString & server_name);

	void sendStart();
	void sendConnected(uint client_id);
	void sendDisconnected(uint client_id);
	int sendData(const PIByteArray & data);
	int sendData(const PIByteArray & data, uint client_id);
	PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba);
	PIByteArray parseData(PIByteArray & ba);
	PIPair<uint, PIByteArray> parseDataServer(PIByteArray & ba);
	PIByteArray parseConnect_d(PIByteArray & ba);
	uint parseConnect(PIByteArray & ba);
	uint parseDisconnect(PIByteArray & ba);

private:
	struct Header {
		Header();
		uchar version; // PICloud::Version
		uchar type; // PICloud::Type
		uchar role; // PICloud::Role
	};

	Header header;
	PIByteArray suuid;
	PIStreamPacker * streampacker;
};

}

#endif // PICLOUDTCP_H
