Ejemplo n.º 1
0
 public EndpointContext(ChannelContext channelContext
     , EndpointHandler handler
     , Identity messageFrom
     , int flag
     , string token)
 {
     this._channelContext = channelContext;
     this._handler = handler;
     this.MessageFrom = messageFrom;
     this._flag = flag;
     this._token = token;
 }
Ejemplo n.º 2
0
        private void OnMessage(object sender, ChannelContext ctx)
        {
            var protocol = new RemotingTcpProtocolHandle(new MemoryStream((byte[])ctx.Message));
            protocol.ReadPreamble();
            protocol.ReadMajorVersion();
            protocol.ReadMinorVersion();

            ushort operation = protocol.ReadOperation();
            if (operation != TcpOperations.Reply)
                return;

            protocol.ReadContentDelimiter();
            protocol.ReadContentLength();

            IDictionary<String, object> transportHeaders = protocol.ReadTransportHeaders();
            if (!transportHeaders.ContainsKey(RemotingTransportHeader.Flag))
                return;

            var flag = (int)transportHeaders[RemotingTransportHeader.Flag];

            if (this._log.IsDebugEnabled)
                this._log.DebugFormat("receive methodReturn of methodCall#{0}", flag);

            if (!this._callbacks.ContainsKey(flag))
                return;

            RemotingCallback callback = this._callbacks[flag];
            this.Cancel(callback);

            var statusCode = transportHeaders.ContainsKey(TcpTransportHeader.StatusCode)
                ? (int)transportHeaders[TcpTransportHeader.StatusCode]
                : 0;
            var statusPhrase = transportHeaders.ContainsKey(TcpTransportHeader.StatusPhrase)
                ? (string)transportHeaders[TcpTransportHeader.StatusPhrase]
                : null;
            if (statusCode > 0 || !string.IsNullOrEmpty(statusPhrase))
            {
                callback.OnException(new RemotingException(string.Format(
                    "remote reutrn error#%s: %s"
                    , statusCode
                    , statusPhrase)));
                return;
            }

            MethodReturn methodReturn = null;
            try
            {
                methodReturn = this._serializationFactory
                    .Get(callback.SerializationFormat)
                    .DeserializeMethodReturn(protocol.ReadContent(), callback.ReturnType);
            }
            catch (Exception e)
            {
                callback.OnException(e);
                return;
            }

            try { callback.OnMethodReturn(methodReturn); }
            catch (Exception e) { this._log.Error(e); }
        }
Ejemplo n.º 3
0
        private void OnMessage(object sender, ChannelContext ctx)
        {
            Message msg = MessageIO.ReadMessage(new MemoryStream((byte[])ctx.Message));
            SendCallback callback = this._callbacks.ContainsKey(msg.Flag)
                ? this._callbacks[msg.Flag]
                : null;

            if (msg.MessageType == MessageType.CONNECTACK)
            {
                this.HandleConnectAck(callback, msg);
                return;
            }

            Identity msgFrom = msg.Token != null && this._idByToken.ContainsKey(msg.Token)
                ? this._idByToken[msg.Token]
                : null;
            // must CONNECT/CONNECTACK for got token before SEND
            if (msgFrom == null)
            {
                var error = new LinkException(Text.E_UNKNOWN_MSG_FROM);
                if (callback == null)
                    throw error;
                callback.Error = error;
                return;
            }

            #region raise callback of client
            if (callback != null)
            {
                this.HandleCallback(callback, msg, msgFrom);
                return;
            }
            else if (this.IsError(msg))
            {
                this._log.ErrorFormat(Text.E_GOT_ERROR, msg.StatusCode, msg.StatusPhase);
                return;
            }
            #endregion

            #region raise event
            if (msg.MessageType == MessageType.SENDACK)
            {
                if (this.AckMessageHandler != null)
                    this.AckMessageHandler(msg.Content, msgFrom);
                return;
            }

            if (this.MessageHandler == null)
                return;
            EndpointContext endpointContext = new EndpointContext(ctx, this, msgFrom, msg.Flag, msg.Token);
            endpointContext.Message = msg.Content;
            try
            {
                this.MessageHandler(endpointContext);
            }
            catch (Exception e)
            {
                // onMessage error should be reply to client
                if (e is LinkException)
                    endpointContext.Error(
                            ((LinkException)e).ErrorCode,
                            ((LinkException)e).Message);
                else
                    endpointContext.Error(0, e.Message);
            }
            #endregion
        }