![]() TGE Version 1.5.2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
NetEvent Class Reference#include <netConnection.h>
Inheritance diagram for NetEvent: ![]() Detailed DescriptionAn event to be sent over the network.
There are 6 methods that you need to implement if you want to make a basic NetEvent subclass, and 2 macros you need to call.
// A simple NetEvent to transmit a string over the network. // This is based on the code in netTest.cc class SimpleMessageEvent : public NetEvent { typedef NetEvent Parent; char *msg; public: SimpleMessageEvent(const char *message = NULL); ~SimpleMessageEvent(); virtual void pack (NetConnection *conn, BitStream *bstream); virtual void write (NetConnection *conn, BitStream *bstream); virtual void unpack (NetConnection *conn, BitStream *bstream); virtual void process(NetConnection *conn); DECLARE_CONOBJECT(SimpleMessageEvent); }; IMPLEMENT_CO_NETEVENT_V1(SimpleMessageEvent); Notice the two macros which we call. The first, DECLARE_CONOBJECT() is there because we're a ConsoleObject. The second, IMPLEMENT_CO_NETEVENT_V1(), is there to register this event type with Torque's networking layer, so that it can be properly transmitted over the wire. There are three macros which you might use:
Choosing the right macro is a good way to make your game more resistant to hacking; for instance, PathManager events are marked as CLIENTEVENTs, because they would cause the server to crash if a client sent them.
SimpleMessageEvent::SimpleMessageEvent(const char *message = NULL) { // If we wanted to make this not be a GuaranteedOrdered event, we'd // put a line like this in the constructor: // mGuaranteeType = Guaranteed; // (or whatever type you wanted.) if(message) msg = dStrdup(message); else msg = NULL; } SimpleMessageEvent::~SimpleMessageEvent() { dFree(msg); } Simple as that! Now, onto pack(), write(), unpack(), process(). pack() is responsible for packing the event over the wire:
void SimpleMessageEvent::pack(NetConnection* conn, BitStream *bstream) { bstream->writeString(msg); } unpack() is responsible for unpacking the event on the other end:
// The networking layer takes care of instantiating a new // SimpleMessageEvent, which saves us a bit of effort. void SimpleMessageEvent::unpack(NetConnection *conn, BitStream *bstream) { char buf[256]; bstream->readString(buf); msg = dStrdup(buf); } process() is called when the network layer is finished with things. A typical case is that a GuaranteedOrdered event is unpacked and stored, but not processed until the events preceding it in the sequence have also been dealt with.
// This just prints the event in the console. You might // want to do something more clever here -- BJG void SimpleMessageEvent::process(NetConnection *conn) { Con::printf("RMSG %d %s", mSourceId, msg); } write() is called if a demo recording is started, and the event has not yet been processed, but it has been unpacked. It should be identical in its output to the bitstream compared to pack(), but since it is called after unpack() some lookups may not need to be performed. In normal demo recording, whole network packets are recorded, meaning that most of the time write() will not be called. In our case, it's entirely identical to pack():
virtual void write(NetConnection*, BitStream *bstream) { bstream->writeString(msg); } The NetEvent is sent over the wire in a straightforward way (assuming you have a handle to a NetConnection):
NetConnection *conn; // We assume you have filled this in. con->postNetEvent(new SimpleMessageEvent("This is a test!")); Finally, for more advanced applications, notifySent() is called whenever the event is sent over the wire, in NetConnection::eventWritePacket(). notifyDelivered() is called when the packet is finally received or (in the case of Unguaranteed packets) dropped.
Member Typedef Documentation
Reimplemented in WeatherLightningStrikeEvent.
Member Enumeration Documentation
Constructor & Destructor Documentation
Member Function Documentation
Implemented in WeatherLightningStrikeEvent, SimDataBlockEvent, Sim2DAudioEvent, Sim3DAudioEvent, and SetMissionCRCEvent.
Implemented in WeatherLightningStrikeEvent, SimDataBlockEvent, Sim2DAudioEvent, Sim3DAudioEvent, and SetMissionCRCEvent.
Implemented in WeatherLightningStrikeEvent, SimDataBlockEvent, Sim2DAudioEvent, Sim3DAudioEvent, and SetMissionCRCEvent.
Implemented in WeatherLightningStrikeEvent, SimDataBlockEvent, Sim2DAudioEvent, Sim3DAudioEvent, and SetMissionCRCEvent.
Reimplemented in SimDataBlockEvent.
Field Documentation
|