#ifndef DISPATCHERCLIENT_H
#define DISPATCHERCLIENT_H

#include "picloudtcp.h"
#include "piethernet.h"
#include "pistreampacker.h"


class DispatcherClient: public PIObject {
	PIOBJECT(DispatcherClient)

public:
	DispatcherClient(PIEthernet * eth_, int id);
	~DispatcherClient();
	void start();
	void close();
	void stop();
	void sendConnected(uint client_id);
	void sendDisconnected(uint client_id);
	void sendData(const PIByteArray & data);
	void sendDataToClient(const PIByteArray & data, uint client_id);
	void authorise(bool ok);
	bool isAuthorised() const { return authorised; }
	PICloud::TCP::Role role() const { return tcp.role(); }
	PIString address();
	uint clientId() const { return client_id; }
	EVENT1(disconnectEvent, DispatcherClient *, client)
	EVENT2(registerServer, const PIByteArray &, sname, DispatcherClient *, client)
	EVENT2(registerClient, const PIByteArray &, sname, DispatcherClient *, client)
	EVENT1(dataReaded, PIByteArray &, ba)
	EVENT2(dataReadedServer, uint, id, PIByteArray &, ba)
	EVENT0(pingReceived)

	PIObject::Connection connect_link;

private:
	EVENT_HANDLER1(void, readed, PIByteArray &, data);
	EVENT_HANDLER1(void, disconnected, bool, withError);

	std::atomic_bool authorised;
	PIEthernet * eth;
	PIStreamPacker streampacker;
	PICloud::TCP tcp;
	PIMutex mutex_send;
	uint client_id;
};


#endif // DISPATCHERCLIENT_H
