Blob Blame Raw
#ifndef SERVER_H
#define SERVER_H


#include <mutex>

#include "common.h"
#include "address.h"


class Socket;
class Protocol;
class Connection;


enum : ErrorCode  {
	ERR_SERVER_COMMON = ERR_SERVER,
	ERR_SERVER_LISTENING_FAILED,
	ERR_SERVER_UNFINISHED_CONNECTIONS,
	ERR_SERVER_LISTENING_LOST,
};


class Server: public Shared {
public:
	typedef ::Handle<Server> Handle;
	
	typedef std::recursive_mutex Mutex;
	typedef std::lock_guard<Mutex> Lock;
	
private:
	Mutex mutex;
	Socket *socket;
	
	friend class Protocol;
	void open(Socket *socket);
	
public:
	Server();
	~Server();
	
	void close(ErrorCode errorCode = ERR_NONE);
	
protected:
	Protocol& getProtocol() const;
	const Address& getLocalAddress() const;
	
	void onOpen();
	void onClose(ErrorCode errorCode);
	::Handle<Connection> onConnect(const Address &remoteAddres);
	void onDisconnect(const ::Handle<Connection> &connection);
};


#endif