-
Notifications
You must be signed in to change notification settings - Fork 27
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.
typedef void (*Callback)(QObject *receiver, const QVariant &data);
void messageReady(const QByteArray &message);
void unknownPacket(const QByteArray &packet);
void replyReady();
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);
The signal is emitted when a new packet in string representation is ready to be sent.
The signal is emitted when an invalid string representation of a packet is received.
The signal is emmited when a reply packet is received.
Creates a packet router for *receiver, with socket to send and receive packets and parser to convert between packets and string representations.
Destroys a packet router.
Returns the pointer of socket.
Returns the pointer of parser.
Sets a new CTcpSocket, disconnects and destroys the previous socket if it exists.
Sets the callback functions for sychronized communication.
Sets the callback functions for asychronized communication.
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.
Used in sychronized communication.
Reply to the peer with command and data.
Used in asychronized communication.
Send a command with parameter data to the peer.
Returns the request timeout of the last packet received from the peer.
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.
Used after sending a request. The caller will pause until an expected reply is received.
Used after sending a request. The caller will pause until an expected reply is received or timeout seconds pass.