Skip to content

CPacketRouter

Takashiro edited this page Aug 20, 2015 · 1 revision
Cardirector Documentation: Network

CPacketRouter

The CPacketRouter class inherits QObject. It acts like a proxy of CTcpSocket, converts CPacket objects and string representations with a CAbstractPacketParser, sends and receives string representations of packets via CTcpSocket. To make use of CPacketRouter, you define your own Callback functions for each command, put each pair of command and callback into a QHash map, and finally call setInteractions or setCallbacks to apply them for sychronized and asychronized communication respectively.

Public Types

typedef void (*Callback)(QObject *receiver, const QVariant &data);

Signals

void messageReady(const QByteArray &message);
void unknownPacket(const QByteArray &packet);
void replyReady();

Public Functions

CPacketRouter(QObject *receiver, CTcpSocket *socket, CAbstractPacketParser *parser);
~CPacketRouter();

CTcpSocket *socket();
CAbstractPacketParser *parser();

void setSocket(CTcpSocket *socket);

void setInteractions(const QHash<int, Callback> *interactions);
void setCallbacks(const QHash<int, Callback> *callbacks);

void request(int command, const QVariant &data = QVariant(), int timeout = -1);
void reply(int command, const QVariant &data = QVariant());
void notify(int command, const QVariant &data = QVariant());

int requestTimeout() const;

void cancelRequest();
QVariant waitForReply();
QVariant waitForReply(int timeout);

void messageReady(const QByteArray &message);

The signal is emitted when a new packet in string representation is ready to be sent.

void unknownPacket(const QByteArray &packet);

The signal is emitted when an invalid string representation of a packet is received.

void replyReady();

The signal is emmited when a reply packet is received.

CPacketRouter(QObject *receiver, CTcpSocket *socket, CAbstractPacketParser *parser);

Creates a packet router for *receiver, with socket to send and receive packets and parser to convert between packets and string representations.

~CPacketRouter();

Destroys a packet router.

CTcpSocket *socket();

Returns the pointer of socket.

CAbstractPacketParser *parser();

Returns the pointer of parser.

void setSocket(CTcpSocket *socket);

Sets a new CTcpSocket, disconnects and destroys the previous socket if it exists.

void setInteractions(const QHash<int, Callback> *interactions);

Sets the callback functions for sychronized communication.

void setCallbacks(const QHash<int, Callback> *callbacks);

Sets the callback functions for asychronized communication.

void request(int command, const QVariant &data = QVariant(), int timeout = -1);

Used in sychronized communication.

Requests the peer with command and the corresponding parameter data. The peer is notified to reply in timeout seconds, where -1 represents unlimited time.

void reply(int command, const QVariant &data = QVariant());

Used in sychronized communication.

Reply to the peer with command and data.

void notify(int command, const QVariant &data = QVariant());

Used in asychronized communication.

Send a command with parameter data to the peer.

int requestTimeout() const;

Returns the request timeout of the last packet received from the peer.

void cancelRequest();

Cancel the current request and the router won't expect a reply corresponding to the canceled request. And waitForReply() will be aborted, the caller of which will continue to run.

QVariant waitForReply();

Used after sending a request. The caller will pause until an expected reply is received.

QVariant waitForReply(int timeout);

Used after sending a request. The caller will pause until an expected reply is received or timeout seconds pass.