TrinityCore
|
#include <NetworkDevice.h>
Public Member Functions | |
~ReliableConduit () | |
virtual bool | messageWaiting () |
template<typename T > | |
void | send (uint32 type, const T &message) |
void | send (uint32 type) |
virtual uint32 | waitingMessageType () |
template<typename T > | |
bool | receive (T &message) |
void | receive () |
NetAddress | address () const |
Public Member Functions inherited from G3D::Conduit | |
virtual | ~Conduit () |
uint64 | bytesSent () const |
uint64 | messagesSent () const |
uint64 | bytesReceived () const |
uint64 | messagesReceived () const |
bool | ok () const |
Public Member Functions inherited from G3D::ReferenceCountedObject | |
virtual | ~ReferenceCountedObject () |
Static Public Member Functions | |
static ReliableConduitRef | create (const NetAddress &address) |
template<typename T > | |
static void | multisend (const Array< ReliableConduitRef > &array, uint32 type, const T &m) |
Private Types | |
enum | State { RECEIVING, HOLDING, NO_MESSAGE } |
Private Member Functions | |
ReliableConduit (const NetAddress &addr) | |
ReliableConduit (const SOCKET &sock, const NetAddress &addr) | |
void | sendBuffer (const BinaryOutput &b) |
void | receiveIntoBuffer () |
void | receiveHeader () |
Static Private Member Functions | |
template<typename T > | |
static void | serializeMessage (uint32 t, const T &m, BinaryOutput &b) |
Private Attributes | |
enum G3D::ReliableConduit::State | state |
NetAddress | addr |
uint32 | messageType |
uint32 | messageSize |
void * | receiveBuffer |
size_t | receiveBufferTotalSize |
size_t | receiveBufferUsedSize |
Friends | |
class | NetworkDevice |
class | NetListener |
Additional Inherited Members | |
Protected Member Functions inherited from G3D::Conduit | |
Conduit () | |
Protected Attributes inherited from G3D::Conduit | |
uint64 | mSent |
uint64 | mReceived |
uint64 | bSent |
uint64 | bReceived |
SOCKET | sock |
BinaryOutput | binaryOutput |
A conduit that guarantees messages will arrive, intact and in order. Create on the client using NetworkDevice::createReliableConduit and on the server using NetListener::waitForConnection. Set the reference counted pointer to NULL to disconnect.
To construct a ReliableConduit:
|
private |
|
private |
|
private |
G3D::ReliableConduit::~ReliableConduit | ( | ) |
Closes the socket.
NetAddress G3D::ReliableConduit::address | ( | ) | const |
The address of the other end of the conduit
|
static |
Client invokes this to connect to a server. The call blocks until the conduit is opened. The conduit will not be ok() if it fails.
|
virtual |
The message is actually copied from the socket to an internal buffer during this call. Receive only deserializes.
Reimplemented from G3D::Conduit.
|
inlinestatic |
Send the same message to a number of conduits. Useful for sending data from a server to many clients (only serializes once).
|
inline |
If a message is waiting, deserializes the waiting message into message and returns true, otherwise returns false. You can determine the type of the message (and therefore, the class of message) using G3D::ReliableConduit::waitingMessageType().
|
inline |
Removes the current message from the queue.
|
private |
Receives the messageType and messageSize from the socket.
|
private |
Accumulates whatever part of the message (not the header) is still waiting on the socket into the receiveBuffer during state = RECEIVING mode. Closes the socket if anything goes wrong. When receiveBufferUsedSize == messageSize, the entire message has arrived.
Serializes the message and schedules it to be sent as soon as possible, and then returns immediately. The message can be any class with a serialize and deserialize method. On the receiving side, use G3D::ReliableConduit::waitingMessageType() to detect the incoming message and then invoke G3D::ReliableConduit::receive(msg) where msg is of the same class as the message that was sent.
The actual data sent across the network is preceeded by the message type and the size of the serialized message as a 32-bit integer. The size is sent because TCP is a stream protocol and doesn't have a concept of discrete messages.
void G3D::ReliableConduit::send | ( | uint32 | type | ) |
Sends an empty message with the given type. Useful for sending commands that have no parameters.
|
private |
|
inlinestaticprivate |
|
virtual |
Returns the type of the waiting message (i.e. the type supplied with send). The return value is zero when there is no message waiting.
One way to use this is to have a Table mapping message types to pre-allocated subclasses so receiving looks like:
// My base class for messages. class Message { virtual void serialize(BinaryOutput&) const; virtual void deserialize(BinaryInput&); virtual void process() = 0; };
Message* m = table[conduit->waitingMessageType()]; conduit->receive(m); m->process();
Another is to simply switch on the message type:
switch (conduit->waitingMessageType()) { case 0: // No message break;
case ENTITY_SPAWN_MSG: { EntitySpawnMsg m; condiut->receive(m); spawnEntity(m.id, m.position, m.modelID); } break; ... }
Implements G3D::Conduit.
|
friend |
|
friend |
|
private |
|
private |
Total size of the incoming message (read from the header).
|
private |
Type of the incoming message.
|
private |
Shared buffer for receiving messages.
|
private |
Total size of the receiveBuffer.
|
private |
Size occupied by the current message... so far. This will be equal to messageSize when the whole message has arrived.
|
private |