Commit 4adbaec0 authored by Mark Frohnmayer's avatar Mark Frohnmayer
Browse files

event_connection mostly converted

parent 5a40cdef
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ enum NetClassGroup
   NetClassGroupInvalid = 32, ///< Invalid net class group
};

/// Mask values used to indicate which NetClassGroup(s) a NetObject or NetEvent
/// Mask values used to indicate which NetClassGroup(s) a NetObject or net_event
/// can be transmitted through.
enum NetClassGroupMask
{
+2 −2
Original line number Diff line number Diff line
static PoolAllocator<ConnectionStringTable::PacketEntry> packetEntryFreeList;

/// ConnectionStringTable is a helper class to EventConnection for reducing duplicated string data sends
/// ConnectionStringTable is a helper class to event_connection for reducing duplicated string data sends
class ConnectionStringTable : public StringTableEntryCompressor
{
public:
@@ -25,7 +25,7 @@ public:
      PacketList() { stringHead = stringTail = NULL; }
   };

   /// An entry in the EventConnection's string table
   /// An entry in the event_connection's string table
   struct Entry {
      StringTableEntry string; ///< Global string table entry of this string
                           ///< will be 0 if this string is unused.
+2 −2
Original line number Diff line number Diff line
@@ -84,7 +84,7 @@ enum NetClassGroup {
   NetClassGroupInvalid = NetClassGroupCount,
};

/// Mask values used to indicate which NetClassGroup(s) a NetObject or NetEvent
/// Mask values used to indicate which NetClassGroup(s) a NetObject or net_event
/// can be transmitted through.
enum NetClassMask {
   NetClassGroupGameMask      = 1 << NetClassGroupGame,
@@ -343,7 +343,7 @@ public:

class SafeObjectRef;

/// Base class for all NetObject, NetEvent, NetConnection and NetInterface instances.
/// Base class for all NetObject, net_event, NetConnection and NetInterface instances.
///
/// @section TNLObject_references Ways of Referencing Object
///
+83 −140
Original line number Diff line number Diff line
/// An event to be sent over the network.
///
/// @note TNL implements two methods of network data passing; this is one of them.
/// See GhostConnection for details of the other, which is referred to as ghosting.
///
/// TNL lets you pass NetEvent objects across EventConnection instances. There are three
/// types of events:
///      - <b>Unguaranteed events</b> are events which are sent once. If they don't
///        make it through the link, they are not resent. This is good for quick,
///        frequent status updates which are of transient interest, like voice
///        communication fragments.
///      - <b>Guaranteed events</b> are events which are guaranteed to be
///        delivered. If they don't make it through the link, they are sent as
///        needed. This is good for important, one-time information,
///        like which team a user wants to play on, or the current weather.
///        Guaranteed events are processed when they are received, so they may be processed
///        in a different order than they were sent in.
///      - <b>GuaranteedOrdered events</b> are events which are guaranteed to be
///        delivered, and whose process methods will be executed in the order the events were sent.
///        This is good for information which is not only important, but also order-critical, like
///        chat messages or file transfers.
///
/// There are 3 methods that you need to implement if you want to make a
/// basic NetEvent subclass, and 2 macros you need to call.
/// @note TNL implements two methods of network data passing; this is one of them.  See GhostConnection for details of the other, which is referred to as ghosting.
///
/// TNL lets you pass net_event objects across event_connection instances. There are three types of events:
///      - <b>unguaranteed events</b> are events which are sent once. If they don't make it through the link, they are not resent. This is good for quick, frequent status updates which are of transient interest, like voice communication fragments.
///      - <b>guaranteed events</b> are events which are guaranteed to be delivered. If they don't make it through the link, they are sent as needed. This is good for important, one-time information, like which team a user wants to play on, or the current weather.  guaranteed events are processed when they are received, so they may be processed in a different order than they were sent in.
///      - <b>guaranteed_ordered events</b> are events which are guaranteed to be delivered, and whose process methods will be executed in the order the events were sent.  This is good for information which is not only important, but also order-critical, like chat messages or file transfers.
///
/// There are 3 methods that you need to implement if you want to make a basic net_event subclass, and 2 macros you need to call.
///
/// @code
/// // A simple NetEvent to transmit a string over the network.
/// class SimpleMessageEvent : public NetEvent
/// // A simple net_event to transmit a string over the network.
/// class SimpleMessageEvent : public net_event
/// {
///    typedef NetEvent Parent;
///    typedef net_event Parent;
///    char *msg;
/// public:
///    SimpleMessageEvent(const char *message = NULL);
///    ~SimpleMessageEvent();
///
///    virtual void pack   (EventConnection *conn, BitStream *bstream)
///    virtual void unpack (EventConnection *conn, BitStream *bstream);
///    virtual void process(EventConnection *conn);
///    virtual void pack   (event_connection *conn, bit_stream *bstream)
///    virtual void unpack (event_connection *conn, bit_stream *bstream);
///    virtual void process(event_connection *conn);
///
///    TORQUE_DECLARE_CLASS(SimpleMessageEvent);
/// };
@@ -43,50 +29,29 @@
/// TNL_IMPLEMENT_NETEVENT(SimpleMessageEvent, NetClassGroupGameMask,0);
/// @endcode
///
/// The first macro called, TORQUE_DECLARE_CLASS() registers the static class functions and ClassRep object that will assign
/// this class a network ID and allow instances to be constructed by ID.
///
/// The second, TNL_IMPLEMENT_NETEVENT(), instantiates the ClassRep and
/// tells it that the instances are NetEvent objects in the Game group.  The final
/// parameter to the TNL_IMPLEMENT_NETEVENT macro is the version number of the event
/// class.  Versioning allows a server to offer new event services without forcing
/// older clients to be updated.
///
/// In the constructor for the event the guarantee type of the event and the direction it will be
/// allowed to travel over the connection, must be specified by way of the constructor
/// for the base NetEvent class.  The guarantee type can be one of:
///      - <b>NetEvent::GuaranteedOrdered</b>, for guaranteed, ordered events
///      - <b>NetEvent::Guaranteed</b>, for guaranteed events
///      - <b>NetEvent::Unguaranteed</b>, for unguaranteed events
///
/// It is also a good idea to clearly specify which direction the event is allowed to travel.
/// If the program has a certain set of message events that are only sent from server to client,
/// then the network system can enforce that error checking automatically, in order to prevent hacks that may
/// otherwise crash or compromise the program.  The valid event directions are:
///      - <b>NetEvent::DirAny</b>, this event can be sent from server to client
///        or from client to server
///      - <b>NetEvent::DirServerToClient</b>, this event can only be sent from
///        server to client.  If the server receives an event of this type, it will
///        signal an error on the connection.
///      - <b>NetEvent::DirClientToServer</b>, this event can only be sent from client
///        to server.  If the client receives an event of this type, it will signal an
///        error on the connection.
///
/// @note TNL allows you to call NetConnection::setLastError() on the EventConnection passed to
///       the NetEvent. This will cause the connection to abort if invalid data is received, specifying
///       a reason to the user.
///
/// Of the 5 methods declared above; the constructor and destructor need only do
/// whatever book-keeping is needed for the specific implementation, in addition to calling
/// the NetEvent constructor with the direction and type information that the networking system
/// needs to function. In this case, the SimpleMessageEvent simply allocates/deallocates the space for the string,
/// and specifies the event as guaranteed ordered and bidirectional.
/// The first macro called, TORQUE_DECLARE_CLASS() registers the static class functions and ClassRep object that will assign this class a network ID and allow instances to be constructed by ID.
///
/// The second, TNL_IMPLEMENT_NETEVENT(), instantiates the ClassRep and tells it that the instances are net_event objects in the Game group.  The final parameter to the TNL_IMPLEMENT_NETEVENT macro is the version number of the event class.  Versioning allows a server to offer new event services without forcing older clients to be updated.
///
/// In the constructor for the event the guarantee type of the event and the direction it will be allowed to travel over the connection, must be specified by way of the constructor for the base net_event class.  The guarantee type can be one of:
///      - <b>net_event::guaranteed_ordered</b>, for guaranteed, ordered events
///      - <b>net_event::guaranteed</b>, for guaranteed events
///      - <b>net_event::unguaranteed</b>, for unguaranteed events
///
/// It is also a good idea to clearly specify which direction the event is allowed to travel.  If the program has a certain set of message events that are only sent from server to client, then the network system can enforce that error checking automatically, in order to prevent hacks that may otherwise crash or compromise the program.  The valid event directions are:
///      - <b>net_event::DirAny</b>, this event can be sent from server to client or from client to server
///      - <b>net_event::DirServerToClient</b>, this event can only be sent from server to client.  If the server receives an event of this type, it will signal an error on the connection.
///      - <b>net_event::DirClientToServer</b>, this event can only be sent from client to server.  If the client receives an event of this type, it will signal an error on the connection.
///
/// @note TNL allows you to call NetConnection::setLastError() on the event_connection passed to the net_event. This will cause the connection to abort if invalid data is received, specifying a reason to the user.
///
/// Of the 5 methods declared above; the constructor and destructor need only do whatever book-keeping is needed for the specific implementation, in addition to calling the net_event constructor with the direction and type information that the networking system needs to function. In this case, the SimpleMessageEvent simply allocates/deallocates the space for the string, and specifies the event as guaranteed ordered and bidirectional.
///
/// @code
///    SimpleMessageEvent::SimpleMessageEvent(const char *message = NULL)
///           : NetEvent(NetEvent::GuaranteedOrdered, NetEvent::DirAny)
///           : net_event(net_event::guaranteed_ordered, net_event::DirAny)
///    {
///       // we marked this event as GuaranteedOrdered, and it can be sent in any direction
///       // we marked this event as guaranteed_ordered, and it can be sent in any direction
///       if(message)
///          msg = strDuplicate(message);
///       else
@@ -99,12 +64,12 @@
///    }
/// @endcode
///
/// The 3 other functions that must be overridden for evern NetEvent are pack(), unpack() and process().
/// The 3 other functions that must be overridden for evern net_event are pack(), unpack() and process().
///
/// <b>pack()</b> is responsible for packing the event over the wire:
///
/// @code
/// void SimpleMessageEvent::pack(EventConnection* conn, BitStream *bstream)
/// void SimpleMessageEvent::pack(event_connection* conn, bit_stream *bstream)
/// {
///   bstream->writeString(msg);
/// }
@@ -115,7 +80,7 @@
/// @code
/// // The networking layer takes care of instantiating a new
/// // SimpleMessageEvent, which saves us a bit of effort.
/// void SimpleMessageEvent::unpack(EventConnection *conn, BitStream *bstream)
/// void SimpleMessageEvent::unpack(event_connection *conn, bit_stream *bstream)
/// {
///   char buf[256];
///   bstream->readString(buf);
@@ -123,14 +88,12 @@
/// }
/// @endcode
///
/// <b>process()</b> 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 been process()'d.
/// <b>process()</b> is called when the network layer is finished with things.  A typical case is that a guaranteed_ordered event is unpacked and stored, but not processed until the events preceding it in the sequence have been process()'d.
///
/// @code
/// // This just prints the event in the log. You might
/// // want to do something more clever here.
/// void SimpleMessageEvent::process(EventConnection *conn)
/// void SimpleMessageEvent::process(event_connection *conn)
/// {
///   logprintf("Received a SimpleMessageEvent: %s", msg);
///
@@ -142,97 +105,77 @@
/// Posting an event to the remote host on a connection is simple:
///
/// @code
/// EventConnection *conn; // We assume you have filled this in.
/// event_connection *conn; // We assume you have filled this in.
///
/// conn->postNetEvent(new SimpleMessageEvent("This is a test!"));
/// @endcode
///
/// Finally, for more advanced applications, notifyPosted() is called when the event is posted
/// into the send queue, notifySent() is called whenever the event is
/// sent over the wire, in EventConnection::eventWritePacket(). notifyDelivered() is called
/// when the packet is finally received or (in the case of Unguaranteed packets) dropped.
/// Finally, for more advanced applications, notify_posted() is called when the event is posted into the send queue, notify_sent() is called whenever the event is sent over the wire, in event_connection::eventWritePacket(). notify_delivered() is called when the packet is finally received or (in the case of unguaranteed packets) dropped.
///
/// @note the TNL_IMPLEMENT_NETEVENT groupMask specifies which "group" of EventConnections
/// the event can be sent over.  See TNL::Object for a further discussion of this.
class NetEvent : public Object
/// @note the TNL_IMPLEMENT_NETEVENT groupMask specifies which "group" of EventConnections the event can be sent over.  See TNL::Object for a further discussion of this.
class net_event : public ref_object
{
   friend class EventConnection;
   friend class event_connection;
public:
   enum EventDirection {
      DirUnset,          ///< Default value - NetConnection will Assert if an event is posted without a valid direction set.
      DirAny,            ///< This event can be sent from the server or the client
      DirServerToClient, ///< This event can only be sent from the server to the client
      DirClientToServer, ///< This event can only be sent from the client to the server
   } mEventDirection;   ///< Direction this event is allowed to travel in the network
   enum event_direction {
      bidirectional, ///< This event can be sent from either the initiator or the host of the connection.
      downstream, ///< This event can only be sent from the host to the initiator
      upstream, ///< This event can only be sent from the initiator to the host
   } _event_direction; ///< Direction this event is allowed to travel in the network

   enum GuaranteeType {
      GuaranteedOrdered = 0, ///< Event delivery is guaranteed and will be processed in the order it
                             ///  was sent relative to other ordered events.
      Guaranteed = 1,        ///< Event delivery is guaranteed and will be processed in the order it
                             ///  was received.
      Unguaranteed = 2       ///< Event delivery is not guaranteed - however, the event will remain
                             ///  ordered relative to other unguaranteed events.
   } mGuaranteeType;        ///< Type of data guarantee this event supports
   enum guarantee_type {
      guaranteed_ordered = 0, ///< Event delivery is guaranteed and will be processed in the order it was sent relative to other ordered events.
      guaranteed = 1, ///< Event delivery is guaranteed and will be processed in the order it was received.
      unguaranteed = 2 ///< Event delivery is not guaranteed - however, the event will remain ordered relative to other unguaranteed events.
   } _guarantee_type; ///< Type of data guarantee this event supports

   /// Constructor - should always be called by subclasses.
   ///
   /// Subclasses MUST pass in an event direction and guarantee type, or else the network
   /// system will error on the event. Events are by default GuaranteedOrdered, however,
   /// the default direction is unset which will result in asserts.
   NetEvent(GuaranteeType gType = GuaranteedOrdered, EventDirection evDir = DirUnset)
   /// Subclasses MUST pass in an event direction and guarantee type, or else the network system will error on the event. Events are by default guaranteed_ordered, however, the default direction is unset which will result in asserts.
   net_event(guarantee_type the_type = guaranteed_ordered, event_direction the_direction = bidirectional)
   {
      mGuaranteeType = gType;
      mEventDirection = evDir;
      _guarantee_type = the_type;
      _event_direction = the_direction;
   }

   /// Pack is called on the origin side of the connection to write an event's
   /// data into a packet.
   virtual void pack(EventConnection *ps, BitStream *bstream) = 0;
   /// Pack is called on the origin side of the connection to write an event's data into a packet.
   virtual void pack(event_connection *ps, bit_stream *bstream) = 0;

   /// Unpack is called on the destination side of the connection to read an event's
   /// data out of a packet.
   virtual void unpack(EventConnection *ps, BitStream *bstream) = 0;
   /// Unpack is called on the destination side of the connection to read an event's data out of a packet.
   virtual void unpack(event_connection *ps, bit_stream *bstream) = 0;

   /// Process is called to process the event data when it has been unpacked.
   ///
   /// For a guaranteed, ordered event, process is called only once all prior events
   /// have been received and processed.  For unguaranteed events, process is called
   /// immediately after unpack.
   virtual void process(EventConnection *ps) = 0;
   /// For a guaranteed, ordered event, process is called only once all prior events have been received and processed.  For unguaranteed events, process is called immediately after unpack.
   virtual void process(event_connection *ps) = 0;

   /// notifyPosted is called on an event when it is posted to a particular EventConnection, before it is added to the send queue.  
   /// This allows events to post additional events to the connection that will be send _before_ this event
   virtual void notifyPosted(EventConnection *ps) {}
   /// notify_posted is called on an event when it is posted to a particular event_connection, before it is added to the send queue.    This allows events to post additional events to the connection that will be send _before_ this event
   virtual void notify_posted(event_connection *ps) {}

   /// notifySent is called on each event after all of the events for a packet have
   /// been written into the packet stream.
   virtual void notifySent(EventConnection *ps) {}
   /// notify_sent is called on each event after all of the events for a packet have been written into the packet stream.
   virtual void notify_sent(event_connection *ps) {}

   /// notifyDelivered is called on the source event after it has been received
   /// and processed by the other side of the connection.
   /// notify_delivered is called on the source event after it has been received and processed by the other side of the connection.
   ///
   /// If the packet delivery fails on an unguaranteed event, madeIt will be
   /// false, otherwise it will be true.
   virtual void notifyDelivered(EventConnection *ps, bool madeIt) {}
   /// If the packet delivery fails on an unguaranteed event, madeIt will be false, otherwise it will be true.
   virtual void notify_delivered(event_connection *ps, bool made_it) {}

   /// getEventDirection returns the direction this event is allowed to travel in on a connection
   EventDirection getEventDirection()
   /// get_event_direction returns the direction this event is allowed to travel in on a connection
	event_direction get_event_direction()
	{
      return mEventDirection;
		return _event_direction;
	}

   /// getDebugName is used to construct event names for packet
   /// logging in debug mode.
   virtual const char *getDebugName()
   /// get_debug_name is used to construct event names for packet logging in debug mode.
	virtual const char *get_debug_name()
	{
      return getClassName();
		//return get_class_name();
	}
};


/// The IMPLEMENT_NETEVENT macro is used for implementing events
/// that can be sent from server to client or from client to server
#define TNL_IMPLEMENT_NETEVENT(className,groupMask,classVersion) \
   TORQUE_IMPLEMENT_NETWORK_CLASS(className, Torque::NetClassTypeEvent, groupMask, classVersion)
/// The IMPLEMENT_NETEVENT macro is used for implementing events that can be sent from server to client or from client to server
#define TNL_IMPLEMENT_NETEVENT(class_name,group_mask,class_version) \
   TORQUE_IMPLEMENT_NETWORK_CLASS(class_name, Torque::NetClassTypeEvent, group_mask, class_version)

+226 −238

File changed.

Preview size limit exceeded, changes collapsed.

Loading