Optimizing Network Usage
Providing the best gaming experience normally means sending updates and other information as rapidly as possible without flooding the target with more messages than it can handle. The Microsoft?DirectPlay?protocol combined with asynchronous messaging enables you to dynamically optimize your messaging strategy to provide your users with the best possible game experience.
The bulk of your messaging will use the IDirectPlay8Peer::SendTo, IDirectPlay8Client::Send, or IDirectPlay8Server::SendTo methods. These methods normally work asynchronously for all message categories. They return immediately, and your message handler receives a DPN_MSGID_SEND_COMPLETE message when the message is actually sent. You can choose to send messages synchronously by setting the DPNSEND_SYNC flag. If you do so, the method will block until the message is actually sent.
The DirectPlay protocol's throttling mechanism guarantees that the client will not receive messages faster than they can be handled. However, the throttling protocol does not control how frequently you submit messages to the outgoing queue. You can easily end up with a large backlog of messages in your unsent message queues. You can avoid this situation by sending messages as infrequently as possible, but then you might unnecessarily degrade the user's experience. An optimal messaging strategy sends messages as fast as possible without exceeding the target's ability to handle them.
The following are tips for optimizing your messaging strategy.
- Send most if not all of your messages asynchronously. If you send a message synchronously, the method will block until the throttling mechanism allows the message to be sent.
- Monitor the pending message queues and the network statistics. If there are few or no messages in the queue, you can increase your transmission rate. If the queues are large or growing rapidly, decrease your transmission rate and perhaps cancel some messages. See the discussion of send timeouts in Send Prioritization for more information.
- Analyze the pending message queues on a player-by-player basis. Some players might be able to receive messages at a much higher rate than others. The bulk statistics might be misleading. Consider using directed sends rather than group sends.
- Choose the appropriate category for each message. Reserve the categories with the highest overhead for the most important messages.
- Prioritize your messages, so that the most important are assured of being sent promptly and not delayed by relatively unimportant messages.
- Do not let the pending message queues grow too large. In addition to delaying the transmission of your messages, a large number of pending messages might consume significant memory resources.
- Use the timeout feature of the
Send and
SendTo methods to automatically clear outdated messages from the pending message queue.
- Minimize the amount of data per message. It is usually better to send frequent small messages than a smaller number of large messages.
- Do not loop tightly when checking the pending message queue. Doing so wastes CPU cycles. Instead, use a sleep period based on how long it typically takes the queue to get down to the level that it will be ready for another send.