Beispiel #1
0
        public void ReceiveMessage(Message message)
        {
            ASSERT( message != null, "Missing parameter 'message'" );
            LOG( "ReceiveMessage(" + message + ") - Start" );
            #if DEBUG
                MessageReceived = true;
            #endif

            if( Router != null )
            {
                var triggerMessageKeys = Router.TriggerMessageKeys;
                var n = triggerMessageKeys.Length;
                for( int i=0; i<n; ++i )
                {
                    var triggerKey = triggerMessageKeys[ i ];
                    if( message.ContainsKey(triggerKey) )
                    {
                        LOG( "ReceiveMessage(" + message + ") - Invoke router for this message" );
                        Router.ReceiveTriggeredMessageFromConnection( message );
                        LOG( "ReceiveMessage(" + message + ") - Router returned" );
                        goto EXIT;
                    }
                }
            }

            CallbackItem callbackItem;
            if(! HandlerCallbacks.TryGetValue(message.HandlerType, out callbackItem) )
                throw new NotImplementedException( "There is no message handler defined for messages of type '" + message.HandlerType + "'" );

            var messageContext = new MessageContext( this, callbackItem, message );
            if(! callbackItem.IsThreaded )
            {
                // Process message right now
                HandleMessageThread( messageContext, /*taskEntry=*/null );
            }
            else
            {
                // Handle the message inside its own thread
                TaskQueue.CreateTask( (taskEntry)=>{ HandleMessageThread(messageContext, taskEntry); } );
            }

            EXIT:
            LOG( "ReceiveMessage(" + message + ") - Exit" );
        }