DDispatcher inherits from DClient, and extends it by separating the TCP data into distinct messages. DDispatcher will call an event handler whenever a complete message is received, removing the need to deal with partial messages. (See the Event Handling section for more.) This comes at a small overhead; the message size, and a 3-byte header that allows us to recover if the TCP stream is corrupted.
Furthermore, the DDispatcher allows us to split a single TCP connection into seperate 'channels'. We can address messages with a 1-byte type and a 2-byte subtype, giving us over 16 million possible identifiers. The addHandler functions let you specify which event handler should run whenever a message with a given type is received.
If no type is specified, the DDispatcher won't send any type information over the network, to maintain minimal overhead per message. Likewise, if the subtype isn't specified we don't bother sending it.
The default constructor creates a DDispatcher that's registered with the given engine. This constructor should be used when connecting to a server.
Same as above, but allows us to set the size of the DDispatcher's send and receive buffers to bufferSize bytes. Larger buffer sizes can result in better efficiency. The default size is 16 kilobytes, which is already quite large for most networks.
This constructor should be used in DServer's connectHandler function. newSocket is a connection that was accepted by the DServer, and will be used by the created DDispatcher.
Same as above, but allows us to set the size of the DDispatcher's send and receive buffers to bufferSize bytes. Larger buffer sizes can result in better efficiency. The default size is 16 kilobytes, which is already quite large for most networks.
Adds an event handler that will be called whenever any message is received. It no longer makes sense to call the other versions of addHandler; this version will treat their type and subtype headers as the first part of the received message, and their event handlers will never be called. This function throws an exception if the other versions of addHandler have been called already.
Add an event handler for all messages with the given messageType. Throws an exception if you've previously set subtype handlers for this messageType.
addHandler(1, 12, &aHandler); addHandler(1, 13, &bHandler); addHandler(1, &cHandler);//throws exception, message type 1 has subtypes addHandler(2, &dHandler);//this is fine
Add an event handler for all messages with the given messageType and subType. Throws an exception if you've previously set a handler for this messageType that did not take a subType.
Immediately sends any buffered data over the network. Returns false if there was no data to be flushed. flush has no effect if Nagle's algorithm is enabled. It's automatically called along with the DEngine's update function.
Sends a generic message to the remote host.
Sends a message with the specified messageType to the remote host.
Sends a message with the specified messageType and subType to the remote host.
Returns the number of bytes remaining to be sent.