Пример #1
0
        /// <summary>
        /// Ends the handling of a message and returns the memory stream to send back to the client.
        /// </summary>
        /// <param name="asyncResult">The <see cref="IAsyncResult"/> returned from <see cref="BeginHandleMessage"/>.</param>
        /// <returns>Retuns a <see cref="MemoryStream"/> to send back to the client, if the stream is <see langword="null"/> an empty response is sent.</returns>
        public MemoryStream EndHandleMessage(IAsyncResult asyncResult)
        {
            MemoryStream             replyStream       = null;
            SocketHandlerAsyncResult socketAsyncResult = (SocketHandlerAsyncResult)asyncResult;

            if (socketAsyncResult.Exception != null)
            {
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat("Exception handling async message: {0}", socketAsyncResult.Exception);
                }
                throw socketAsyncResult.Exception;
            }

            //only 1 should be not null
            int replyCount = 0;

            if (socketAsyncResult.ReplyMessage != null)
            {
                replyCount++;
            }
            if (socketAsyncResult.ReplyMessages != null)
            {
                replyCount++;
            }
            if (socketAsyncResult.RuntimeInfo != null)
            {
                replyCount++;
            }
            if (replyCount > 1)
            {
                throw new InvalidOperationException(
                          string.Format("Only 1 reply at a time is supported. ReplyMessage: {0}, ReplyMessages: {1}, RuntimeInfo: {2}",
                                        socketAsyncResult.ReplyMessage,
                                        socketAsyncResult.ReplyMessages,
                                        socketAsyncResult.RuntimeInfo));
            }

            if (socketAsyncResult.ReplyMessage != null)
            {
                replyStream = RelayMessageFormatter.WriteRelayMessage(socketAsyncResult.ReplyMessage);
            }
            else if (socketAsyncResult.ReplyMessages != null)
            {
                replyStream = RelayMessageFormatter.WriteRelayMessageList(socketAsyncResult.ReplyMessages);
            }
            else if (socketAsyncResult.RuntimeInfo != null)
            {
                replyStream = (MemoryStream)RelayMessageFormatter.WriteRuntimeInfo(socketAsyncResult.RuntimeInfo);
            }

            return(replyStream);
        }
        /// <summary>
        /// Handles a new message from a <see cref="MemoryStream"/> and if appropriate translates
        /// and passes the message to the contained <see cref="RelayNode"/>.
        /// </summary>
        /// <param name="commandID"></param>
        /// <param name="messageStream"></param>
        /// <param name="messageLength"></param>
        /// <returns></returns>
        public MemoryStream HandleMessage(int commandID, MemoryStream messageStream, int messageLength)
        {
            SocketCommand       command     = SocketCommand.Unknown;
            RelayMessage        message     = null;
            List <RelayMessage> messages    = null;
            MemoryStream        replyStream = null;

            try
            {
                command = (SocketCommand)commandID;
            }
            catch
            {
                if (RelayNode.log.IsErrorEnabled)
                {
                    RelayNode.log.ErrorFormat("Unrecognized commandID {0} sent to Relay Service via socket transport", commandID);
                }
            }

            Stream reply;

            switch (command)
            {
            case SocketCommand.Unknown:
                if (RelayNode.log.IsErrorEnabled)
                {
                    RelayNode.log.Error("SocketCommand.Unknown received");
                }
                break;

            case SocketCommand.HandleOneWayMessage:
                message = RelayMessageFormatter.ReadRelayMessage(messageStream);
                _dataHandler.HandleMessage(message);
                break;

            case SocketCommand.HandleSyncMessage:
                message = RelayMessageFormatter.ReadRelayMessage(messageStream);
                message.ResultOutcome = RelayOutcome.Received;
                _dataHandler.HandleMessage(message);
                reply = RelayMessageFormatter.WriteRelayMessage(message);
                if (reply != null && reply != Stream.Null)
                {
                    replyStream = (MemoryStream)reply;
                }
                break;

            case SocketCommand.HandleOneWayMessages:
                messages = RelayMessageFormatter.ReadRelayMessageList(messageStream);
                _dataHandler.HandleMessages(messages);
                break;

            case SocketCommand.HandleSyncMessages:
                messages = RelayMessageFormatter.ReadRelayMessageList(messageStream, msg => msg.ResultOutcome = RelayOutcome.Received);
                _dataHandler.HandleMessages(messages);
                reply = RelayMessageFormatter.WriteRelayMessageList(messages);
                if (reply != null && reply != Stream.Null)
                {
                    replyStream = (MemoryStream)reply;
                }
                break;

            case SocketCommand.GetRuntimeInfo:
                ComponentRuntimeInfo[] runtimeInfo = _relayNode.GetComponentsRuntimeInfo();
                reply = RelayMessageFormatter.WriteRuntimeInfo(runtimeInfo);
                if (reply != null && reply != Stream.Null)
                {
                    replyStream = (MemoryStream)reply;
                }
                break;

            default:
                if (RelayNode.log.IsErrorEnabled)
                {
                    RelayNode.log.ErrorFormat("Unhandled command {0} sent to Relay Service via socket transport", command);
                }
                break;
            }


            return(replyStream);
        }