Blob Blame Raw
#ifndef CONNECTION_H
#define CONNECTION_H


#include "socket.h"


class Server;


class Connection: public Socket {
public:
	struct Task {
		void *data;
		size_t size;
		size_t completion;
		Task *next;
		
		inline explicit Task(
			void *data = nullptr,
			size_t size = 0,
			size_t completion = 0,
			Task *next = nullptr
		):
			data(data), size(size), completion(completion), next(next)
			{ assert(!!data == !!size); }
	};
	typedef CounteredQueueMISO<Task, &Task::next> TaskQueue;
	
private:
	friend class Protocol;
	friend class Server;
	
	Server *server;
	Connection *srvPrev, *srvNext;
	bool errorLocal;
	
	TaskQueue readQueue;
	TaskQueue writeQueue;
	
	bool open(Protocol &protocol, void *address, size_t addressSize, int sockId, Server *server);
	
public:
	Connection();
	~Connection();
	
	bool open(Protocol &protocol, void *address, size_t addressSize);
	
	bool read(Task &task);
	bool write(Task &task);
	
private:
	void handleState() override;
	void handleEvents(unsigned int events) override;
	
protected:
	virtual void onReadReady(Task &task, bool error);
	virtual void onWriteReady(Task &task, bool error);
};


#endif