#include <protocol.h>
Public Types | |
enum | connectionType { None, Server, Client } |
Public Member Functions | |
Protocol () | |
virtual | ~Protocol () |
connectionType | getConnectionType (void) const |
int | listen (unsigned int port=0) |
nodeFD | accept (void) |
nodeFD | connect (const char *host, unsigned int port=0) |
void | disconnect (nodeFD node=-1) |
bool | send (Packet &p) |
int | recv (Packet &p) |
virtual nodeId | getUniqueId (void)=0 |
Protected Member Functions | |
void | setOwner (const Glass *g) |
nodeFD | IdToFD (nodeId id) |
nodeId | FDToId (nodeFD fd) |
nodeId | registerNode (nodeFD fd) |
bool | unregisterNode (nodeId id) |
bool | unregisterNode (nodeFD fd) |
virtual int | _listen (unsigned int port)=0 |
virtual nodeFD | _accept (void)=0 |
virtual nodeFD | _connect (const char *host, unsigned int port)=0 |
virtual void | _disconnect (nodeFD node)=0 |
virtual bool | _send (Packet &p)=0 |
virtual int | _recv (Packet &p)=0 |
Protected Attributes | |
connectionType | type |
Friends | |
class | Glass |
Definition at line 35 of file protocol.h.
libglass::Protocol::Protocol | ( | ) |
Constructor.
virtual libglass::Protocol::~Protocol | ( | ) | [virtual] |
Virtual destructor. Responsible for closing all connections.
virtual nodeFD libglass::Protocol::_accept | ( | void | ) | [protected, pure virtual] |
This is the derived class implementation of accept().
Implemented in libglass::TCP, and libglass::UDP.
virtual nodeFD libglass::Protocol::_connect | ( | const char * | host, | |
unsigned int | port | |||
) | [protected, pure virtual] |
This is the derived class implementation of connect().
Implemented in libglass::TCP, and libglass::UDP.
virtual void libglass::Protocol::_disconnect | ( | nodeFD | node | ) | [protected, pure virtual] |
This is the derived class implementation of disconnect().
Implemented in libglass::TCP, and libglass::UDP.
virtual int libglass::Protocol::_listen | ( | unsigned int | port | ) | [protected, pure virtual] |
This is the derived class implementation of listen().
Implemented in libglass::TCP, and libglass::UDP.
virtual int libglass::Protocol::_recv | ( | Packet & | p | ) | [protected, pure virtual] |
This is the derived class implementation of recv().
Implemented in libglass::TCP, and libglass::UDP.
virtual bool libglass::Protocol::_send | ( | Packet & | p | ) | [protected, pure virtual] |
This is the derived class implementation of send().
Implemented in libglass::TCP, and libglass::UDP.
nodeFD libglass::Protocol::accept | ( | void | ) |
Waits for a connection.
When the connection is successful, you must register the new FD by calling registerNode().
-1 | if protocol type is not server. | |
-2 | if could not open connection. | |
>=0 | otherwise, a glass node file descriptor to the node connected. |
nodeFD libglass::Protocol::connect | ( | const char * | host, | |
unsigned int | port = 0 | |||
) |
void libglass::Protocol::disconnect | ( | nodeFD | node = -1 |
) |
Closes the connection to a given node. Derived classes may choose exactly what to do.
node | The glass node descriptor |
Converts a nodeFD to a nodeId.
Wrapper to the real call, which is a member of Glass().
fd | The node fd. |
connectionType libglass::Protocol::getConnectionType | ( | void | ) | const |
Gets connection type.
virtual nodeId libglass::Protocol::getUniqueId | ( | void | ) | [pure virtual] |
Converts a nodeId to a nodeFD.
Wrapper to the real call, which is a member of Glass().
id | The node id. |
int libglass::Protocol::listen | ( | unsigned int | port = 0 |
) |
int libglass::Protocol::recv | ( | Packet & | p | ) |
Receives a message.
This function gets the first packet in queue, and fills the Packet structure with the data.
p | The packet to store the message. |
3 | Invalid packet format. | |
2 | Timeout (may not be implemented by the derived class). This is not an error, and ought to be handled properly by the caller. | |
1 | Connection closed. |
-1 | Protocol not initialized, or error reading. | |
-2 | Invalid "from" descriptor/unregistered node |
Registers a node, by getting its descriptor.
Wrapper to the real call, which is a member of Glass().
fd | A valid node descriptor. |
bool libglass::Protocol::send | ( | Packet & | p | ) |
Sends a message.
This function must be reentrant, that is, it may be called simultaneously by two or more threads. It's up to the implementation to make it truly concurrent (the preferred alternative) or use some kind of locking system, such as a mutex.
p | The packet to send. |
true | Ok. | |
false | Protocol not initialized or transmission error. |
void libglass::Protocol::setOwner | ( | const Glass * | g | ) | [protected] |
bool libglass::Protocol::unregisterNode | ( | nodeFD | fd | ) | [protected] |
bool libglass::Protocol::unregisterNode | ( | nodeId | id | ) | [protected] |
Unregisters a id/fd association.
Wrapper to the real call, which is a member of Glass().
id | The node id. |
true | This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. |
id | The node id. |
true |
friend class Glass [friend] |
Definition at line 48 of file protocol.h.
connectionType libglass::Protocol::type [protected] |
This instance type.
Definition at line 46 of file protocol.h.