Blame protocol.cpp

f07ad6
f07ad6
#include "protocol.h"
f07ad6
#include "server.h"
f07ad6
#include "connection.h"
f07ad6
f07ad6
541903
541903
Socket::Socket(
541903
	Protocol &protocol,
541903
	const Connection::Handle &connection,
541903
	const Server::Handle &server,
541903
	const Address &address
541903
):
541903
	protocol(protocol), connection(connection), server(server), address(address),
541903
	flags(CREATED), prev(), next(), chPrev(), chNext()
f07ad6
{
f07ad6
	prev = protocol.last;
f07ad6
	(prev ? prev->next : protocol.first) = this;
541903
	setChanged(true);
f07ad6
}
f07ad6
f07ad6
541903
Socket::~Socket() {
541903
	setChanged(false);
f07ad6
	(prev ? prev->next : protocol.first) = next;
f07ad6
	(next ? next->prev : protocol.last) = prev;
541903
	prev = next = nullptr;
541903
}
541903
541903
541903
void Socket::setChanged(bool changed) {
541903
	if ((bool)(flags & CHANGED) == changed) return;
541903
	if (changed) {
541903
		flags |= CHANGED;
541903
		chPrev = protocol.chLast;
541903
		(chPrev ? chPrev->chNext : protocol.chFirst) = this;
541903
		protocol.wakeup();
541903
	} else {
541903
		flags &= ~CHANGED;
541903
		(chPrev ? chPrev->chNext : protocol.chFirst) = chNext;
541903
		(chNext ? chNext->chPrev : protocol.chLast) = chPrev;
541903
		chPrev = chNext = nullptr;
541903
	}
541903
}
541903
541903
541903
void Socket::setWantRead(bool want) {
541903
	Protocol::Lock lock(protocol.mutex);
541903
	if ((bool)(flags & WANTREAD) == want) return;
541903
	flags = want ? flags | WANTREAD : flags & ~WANTREAD;
541903
	setChanged(true);
541903
}
541903
541903
541903
void Socket::setWantWrite(bool want) {
541903
	Protocol::Lock lock(protocol.mutex);
541903
	if ((bool)(flags & WANTWRITE) == want) return;
541903
	flags = want ? flags | WANTWRITE : flags & ~WANTWRITE;
541903
	setChanged(true);
541903
}
541903
541903
541903
void Socket::finalize() {
541903
	Protocol::Lock lock(protocol.mutex);
541903
	if (flags & REMOVED) return;
541903
	flags |= REMOVED;
541903
	setChanged(true);
f07ad6
}
f07ad6
f07ad6
f07ad6
f07ad6
Protocol::Protocol():
541903
	first(), last(), chFirst(), chLast() { }
f07ad6
Protocol::~Protocol()
f07ad6
	{ assert(!first); }
f07ad6
f07ad6
2a209b
void Protocol::connectionOpen(const Connection::Handle &connection, Socket *socket) {
2a209b
	assert(connection && socket);
2a209b
	connection->open(socket);
2a209b
}
2a209b
2a209b
f07ad6
void Protocol::closeAll() {
f07ad6
	Lock lock(mutex);
541903
	while(first) 
541903
		if (first->connection) first->connection->close(); else
541903
			if (first->server) first->server->close(); else
541903
				first->finalize();
f07ad6
}
f07ad6
f07ad6
541903
void Protocol::wakeup() 
541903
	{ }
541903
ErrorCode Protocol::resolve(Address&) 
541903
	{ return ERR_ADDRESS_INCORRECT; }
541903
ErrorCode Protocol::connect(const Connection::Handle&, const Address&)
f07ad6
	{ return ERR_CONNECTION_FAILED; }
541903
ErrorCode Protocol::listen(const Server::Handle&, const Address&)
f07ad6
	{ return ERR_SERVER_LISTENING_FAILED; }