Blob Blame Raw

#include "protocol.h"
#include "server.h"
#include "connection.h"


Socket::Socket(Protocol &protocol, Connection *connection, Server *server, const Address &address):
	closed(), prev(), next(), protocol(protocol), connection(connection), server(server), address(address)
{
	Protocol::Lock lock(protocol.mutex);
	prev = protocol.last;
	(prev ? prev->next : protocol.first) = this;
}

Socket::~Socket()
	{ assert(closed); }

void Socket::onClose()
	{ }

void Socket::close() {
	Protocol::Lock lock(protocol.mutex);
	onClose();
	(prev ? prev->next : protocol.first) = next;
	(next ? next->prev : protocol.last) = prev;
	closed = true;
	delete this;
}



Protocol::Protocol():
	first(), last() { }
Protocol::~Protocol()
	{ assert(!first); }


void Protocol::closeAll() {
	Lock lock(mutex);
	while(first) first->close();
}


ErrorCode Protocol::connect(Connection&, const Address&)
	{ return ERR_CONNECTION_FAILED; }
ErrorCode Protocol::listen(Server&, const Address&)
	{ return ERR_SERVER_LISTENING_FAILED; }