Blob Blame Raw
#ifndef PROTOCOL_H
#define PROTOCOL_H


#include <thread>

#include "utils.h"
#include "connection.h"
#include "server.h"



class Protocol {
private:
	friend class Socket;
	friend class Server;
	friend class Connection;
	
	std::thread *thread;
	int epollFd;
	int eventFd;
	
	std::atomic<State> state;
	std::atomic<State> stateWanted;
	ReadProtector stateProtector;

	std::atomic<int> closeWaiters;
	std::atomic<int> finishWaiters;
	std::condition_variable closeCondition;
	
	Socket *sockFirst, *sockLast;
	Connection::Queue sockQueue;

	void wakeup();
	void wantState(State state);
	
	void threadRun();
	
public:
	Protocol();
	virtual ~Protocol();
	
	bool open();
	void closeReq();
	void close();
	void closeWait(unsigned long long timeoutUs = 0, bool withReq = true);
};


#endif