コード例 #1
0
ファイル: RemotingHandler.cs プロジェクト: wsky/top-link
        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); }
        }
コード例 #2
0
ファイル: DynamicProxyTest.cs プロジェクト: wsky/top-link
            protected override void OnMessage(object sender, MessageEventArgs e)
            {
                var protocol = new RemotingTcpProtocolHandle(new MemoryStream(e.RawData));
                protocol.ReadPreamble();
                protocol.ReadMajorVersion();
                protocol.ReadMinorVersion();
                ushort operation = protocol.ReadOperation();
                protocol.ReadContentDelimiter();
                protocol.ReadContentLength();
                IDictionary<string, Object> transportHeaders = protocol.ReadTransportHeaders();
                var flag = (int)transportHeaders[RemotingTransportHeader.Flag];
                ISerializer serializer = serializationFactory.Get(null);
                transportHeaders.Clear();
                transportHeaders.Add(RemotingTransportHeader.Flag, flag);

                MethodCall methodCall = serializer.DeserializeMethodCall(protocol.ReadContent());
                var methodReturn = new MethodReturn() { ReturnValue = methodCall.Args[0] };

                byte[] data = serializer.SerializeMethodReturn(methodReturn);

                var response = new MemoryStream();
                var handle = new RemotingTcpProtocolHandle(response);
                handle.WritePreamble();
                handle.WriteMajorVersion();
                handle.WriteMinorVersion();
                handle.WriteOperation(TcpOperations.Reply);
                handle.WriteContentDelimiter(TcpContentDelimiter.ContentLength);
                handle.WriteContentLength(data.Length);
                handle.WriteTransportHeaders(transportHeaders);
                handle.WriteContent(data);

                this.Send(response.ToArray());
            }