/// <summary> /// Sets the target identifier /// </summary> public UpdateMessage( Guid targetId, uint sequence, Message payload ) { m_TargetId = targetId; m_Payload = payload; m_Sequence = sequence; }
/// <summary> /// Called when this object receives a message of UpdateMessageType /// </summary> private MessageRecipientResult ReceivedMessage( Message msg ) { // If the message came from an update handler (should be - with the same host ID), then just ignore the message. Why? // Because that means that the handler received a message from the update source, which is also the target for this update // provider's messages, meaning the message will be endlessly circulating if they aren't ignored here if ( ( m_IgnoreUpdateHandlerMessages ) && ( msg.Sender is IUpdateHandler ) ) { return MessageRecipientResult.DeliverToNext; } // Add the message to the message buffer, and don't let the rest of the chain handle the message m_Buffer.AddMessage( new UpdateMessage( Id, m_Sequence, msg ), m_Sequence ); if ( m_RemoveBufferedMessages ) { return MessageRecipientResult.RemoveFromChain; } return MessageRecipientResult.DeliverToNext; }
/// <summary> /// Adds a message to the outgoing message queue. On the next network tick, all queued messages will be sent /// </summary> public void DeliverMessage( Message msg ) { if ( IsConnected ) { // TODO: AP: Use a network stream? Requires stream sockets // TODO: AP: Cache messages in a memory stream? MemoryStream messageStore = new MemoryStream( ); m_Formatter.Serialize( messageStore, msg ); messageStore.Close( ); byte[] messageBytes = messageStore.ToArray( ); m_Socket.Send( messageBytes ); } }
/// <summary> /// Called when the specified connection receives a message /// </summary> private void OnReceivedMessage( IConnection connection, Message msg ) { if ( msg is TargetSequenceMessage ) { foreach ( TargetConnection target in m_Targets ) { if ( target.Connection == connection ) { target.Sequence = ( ( TargetSequenceMessage )msg ).Sequence; return; } } } }
private static void MessageChecker( IConnection connection, Message msg ) { Assert.AreEqual( ( ( TestMessage )msg ).m_Content.m_Value, 10 ); }
/// <summary> /// Handles messages sent over a specified connection /// </summary> private void OnReceivedMessage( IConnection connection, Message msg ) { if ( !( msg is UpdateMessageBatch ) ) { // Not interested return; } UpdateMessageBatch batchMsg = ( UpdateMessageBatch )msg; if ( batchMsg.Sequence < m_Sequence ) // TODO: Should be <=. Just ordering issues between update message creation and sequence increment { return; } // Message away if ( batchMsg.Messages != null ) { foreach ( UpdateMessage updateMsg in batchMsg.Messages ) { if ( updateMsg.Sequence >= m_Sequence ) // TODO: Should be >. Just ordering issues between update message creation and sequence increment { IUpdateHandler handler = m_HandlerMap[ updateMsg.TargetId ]; handler.Handle( updateMsg ); } } } m_Sequence = batchMsg.Sequence; // Let's let the source know that we got an update! yay! // TODO: Should this be sent at every frame? // TODO: If there's an update source, then the information about the target sequence can be piggy-backed in an UpdateMessageBatch connection.DeliverMessage( new TargetSequenceMessage( m_Sequence ) ); }