Provides fast but unreliable transfer of messages. On a LAN, LightweightConduit will probably never drop messages but you might get your messages out of order. On an internet connection it might drop messages altogether. Messages are never corrupted, however. LightweightConduit requires a little less setup and overhead than ReliableConduit. ReliableConduit guarantees message delivery and order but requires a persistent connection.
To set up a LightweightConduit (assuming you have already made subclasses of G3D::NetMessage based on your application's pcommunication protocol):
[Server Side]
-
Call LightweightConduit::create(port, true, false), where port is the port on which you will receive messages.
-
Poll LightweightConduit::messageWaiting from your main loop. When it is true (or, equivalently, when LightweightConduit::waitingMessageType is non-zero) there is an incoming message.
-
To read the incoming message, call LightweightConduit::receive with the appropriate class type, which mist have a deserialize method. LightweightConduit::waitingMessageType tells you what class is needed (you make up your own message constants for your program; numbers under 1000 are reserved for G3D's internal use).
-
When done, simply set the G3D::LightweightConduitRef to NULL or let it go out of scope and the conduit cleans itself up automatically.
[Client Side]
-
Call G3D::LightweightConduit::create(). If you will broadcast to all servers on a LAN, set the third optional argument to true (the default is false for no broadcast). You can also set up the receive port as if it was a server to send and receive from a single LightweightConduit.
-
To send, call G3D::LightweightConduit::send with the target address and a pointer to an instance of the message you want to send.
-
When done, simply set the G3D::LightweightConduitRef to NULL or let it go out of scope and the conduit cleans itself up automatically.
- Deprecated:
uint32 G3D::LightweightConduit::waitingMessageType |
( |
| ) |
|
|
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.
1103 int iRemoteAddrLen =
sizeof(sockaddr);
1107 (socklen_t*)&iRemoteAddrLen);
1109 if (ret == SOCKET_ERROR) {
1111 "LightweightConduit::waitingMessageType().");
1113 nd->closesocket(
sock);
1132 for (
int i = 0; i < 4; ++i) {
friend class NetworkDevice
Definition: NetworkDevice.h:409
void resize(size_t n, bool shrinkIfNecessary=true)
Definition: Array.h:490
static std::string socketErrorCode(int code)
Definition: networkHelpers.h:64
T * getCArray()
Definition: Array.h:256
virtual bool messageWaiting()
Definition: NetworkDevice.cpp:1087
SOCKET sock
Definition: NetworkDevice.h:67
Array< uint8 > messageBuffer
Definition: NetworkDevice.h:430
const bool DONT_SHRINK_UNDERLYING_ARRAY
Definition: Array.h:43
NetAddress messageSender
Definition: NetworkDevice.h:420
uint32_t uint32
Definition: Define.h:150
int size() const
Definition: Array.h:430
uint32 messageType
Definition: NetworkDevice.h:425
static G3DEndian machineEndian()
Definition: System.h:219
uint64 bReceived
Definition: NetworkDevice.h:65
static Log * common()
Definition: Log.cpp:100
void println(const std::string &s)
Definition: Log.cpp:144
uint64 mReceived
Definition: NetworkDevice.h:63
static NetworkDevice * instance()
Definition: NetworkDevice.cpp:109
bool alreadyReadMessage
Definition: NetworkDevice.h:415