|
|
f07ad6 |
#ifndef PROTOCOL_H
|
|
|
f07ad6 |
#define PROTOCOL_H
|
|
|
f07ad6 |
|
|
|
f07ad6 |
|
|
|
f07ad6 |
#include <mutex></mutex>
|
|
|
f07ad6 |
|
|
|
541903 |
#include "common.h"
|
|
|
f07ad6 |
#include "address.h"
|
|
|
541903 |
#include "connection.h"
|
|
|
541903 |
#include "server.h"
|
|
|
541903 |
|
|
|
541903 |
|
|
|
541903 |
enum : ErrorCode {
|
|
|
541903 |
ERR_PROTOCOL_COMMON = ERR_SERVER,
|
|
|
541903 |
ERR_PROTOCOL_NOT_INITIALIZED,
|
|
|
541903 |
};
|
|
|
f07ad6 |
|
|
|
f07ad6 |
|
|
|
f07ad6 |
class Protocol;
|
|
|
f07ad6 |
|
|
|
f07ad6 |
|
|
|
f07ad6 |
class Socket {
|
|
|
f07ad6 |
public:
|
|
|
541903 |
enum : unsigned int {
|
|
|
541903 |
CHANGED = 1 << 0,
|
|
|
541903 |
CREATED = 1 << 1,
|
|
|
541903 |
REMOVED = 1 << 2,
|
|
|
541903 |
WANTREAD = 1 << 3,
|
|
|
541903 |
WANTWRITE = 1 << 4, };
|
|
|
541903 |
|
|
|
f07ad6 |
Protocol &protocol;
|
|
|
541903 |
const Connection::Handle connection;
|
|
|
541903 |
const Server::Handle server;
|
|
|
f07ad6 |
const Address &address;
|
|
|
f07ad6 |
|
|
|
541903 |
private:
|
|
|
541903 |
friend class Protocol;
|
|
|
541903 |
|
|
|
541903 |
// these vars protected by protocol.mutex
|
|
|
541903 |
unsigned int flags;
|
|
|
541903 |
Socket *prev, *next;
|
|
|
541903 |
Socket *chPrev, *chNext;
|
|
|
541903 |
|
|
|
541903 |
// protocol.mutex must be already locked
|
|
|
541903 |
void setChanged(bool changed);
|
|
|
541903 |
|
|
|
f07ad6 |
protected:
|
|
|
541903 |
// protocol.mutex must be already locked
|
|
|
f07ad6 |
Socket(const Socket&) = delete;
|
|
|
541903 |
Socket(
|
|
|
541903 |
Protocol &protocol,
|
|
|
541903 |
const Connection::Handle &connection,
|
|
|
541903 |
const Server::Handle &server,
|
|
|
541903 |
const Address &address );
|
|
|
541903 |
~Socket();
|
|
|
f07ad6 |
|
|
|
f07ad6 |
public:
|
|
|
541903 |
// thread-saef methods
|
|
|
541903 |
void setWantRead(bool want);
|
|
|
541903 |
void setWantWrite(bool want);
|
|
|
541903 |
void finalize();
|
|
|
f07ad6 |
};
|
|
|
f07ad6 |
|
|
|
f07ad6 |
|
|
|
541903 |
class Protocol: public Shared {
|
|
|
f07ad6 |
public:
|
|
|
98bb38 |
typedef THandle<protocol> Handle;</protocol>
|
|
|
541903 |
|
|
|
f07ad6 |
typedef std::recursive_mutex Mutex;
|
|
|
f07ad6 |
typedef std::lock_guard<mutex> Lock;</mutex>
|
|
|
f07ad6 |
|
|
|
f07ad6 |
private:
|
|
|
f07ad6 |
friend class Socket;
|
|
|
f07ad6 |
Socket *first, *last;
|
|
|
541903 |
Socket *chFirst, *chLast;
|
|
|
541903 |
|
|
|
541903 |
inline void socketCheck(Socket *socket, bool mustBeChanged = false) {
|
|
|
541903 |
assert(socket);
|
|
|
541903 |
assert(&socket->protocol == this);
|
|
|
541903 |
assert(!mustBeChanged || (socket->flags & Socket::CHANGED));
|
|
|
541903 |
}
|
|
|
541903 |
|
|
|
541903 |
protected:
|
|
|
541903 |
Mutex mutex;
|
|
|
541903 |
bool allowNewConnections;
|
|
|
541903 |
|
|
|
541903 |
// any class derived from Protocol may access to Socket private members via these methods
|
|
|
541903 |
// mutex must be locked before call
|
|
|
541903 |
inline Socket* socketFirst()
|
|
|
541903 |
{ return first; }
|
|
|
541903 |
inline Socket* socketNext(Socket *socket)
|
|
|
541903 |
{ socketCheck(socket); return socket->next; }
|
|
|
541903 |
inline Socket* socketChFirst()
|
|
|
541903 |
{ return chFirst; }
|
|
|
541903 |
inline Socket* socketChNext(Socket *socket)
|
|
|
541903 |
{ socketCheck(socket, true); return socket->chNext; }
|
|
|
f07ad6 |
|
|
|
541903 |
inline unsigned int socketFlags(Socket *socket)
|
|
|
541903 |
{ socketCheck(socket); return socket->flags; }
|
|
|
541903 |
|
|
|
541903 |
inline Socket* socketRemove(Socket *socket)
|
|
|
541903 |
{ socketCheck(socket); Socket *next = socket->next; delete socket; return next; }
|
|
|
541903 |
inline Socket* socketRemoveChanged(Socket *socket)
|
|
|
541903 |
{ socketCheck(socket, true); Socket *chNext = socket->chNext; delete socket; return chNext; }
|
|
|
541903 |
inline Socket* socketSetUnchanged(Socket *socket)
|
|
|
541903 |
{ socketCheck(socket, true); Socket *chNext = socket->chNext; socket->setChanged(false); return chNext; }
|
|
|
541903 |
|
|
|
2a209b |
|
|
|
2a209b |
// thread-safe methods
|
|
|
2a209b |
void connectionOpen(const Connection::Handle &connection, Socket *socket);
|
|
|
541903 |
virtual void wakeup();
|
|
|
541903 |
|
|
|
f07ad6 |
public:
|
|
|
541903 |
// thread-safe methods
|
|
|
f07ad6 |
Protocol();
|
|
|
f07ad6 |
virtual ~Protocol();
|
|
|
541903 |
virtual ErrorCode resolve(Address &address);
|
|
|
541903 |
virtual ErrorCode connect(const Connection::Handle &connection, const Address &address);
|
|
|
541903 |
virtual ErrorCode listen(const Server::Handle &server, const Address &address);
|
|
|
f07ad6 |
void closeAll();
|
|
|
f07ad6 |
};
|
|
|
f07ad6 |
|
|
|
f07ad6 |
|
|
|
f07ad6 |
#endif
|