Beispiel #1
0
        /// <summary>send call request to remote and pending callback
        /// </summary>
        /// <param name="remoteUri"></param>
        /// <param name="operation"></param>
        /// <param name="transportHeaders"></param>
        /// <param name="call"></param>
        /// <param name="callback"></param>
        public void Call(Uri remoteUri
            , ushort operation
            , IDictionary<string, object> transportHeaders
            , MethodCall call
            , RemotingCallback callback)
        {
            var data = this._serializationFactory.Get(callback.SerializationFormat).SerializeMethodCall(call);
            var flag = Interlocked.Increment(ref this._flag);
            callback.Flag = flag;
            transportHeaders.Add(RemotingTransportHeader.Flag, flag);
            transportHeaders.Add(RemotingTransportHeader.Format, callback.SerializationFormat);

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

            this.GetChannel(remoteUri).Send(request.ToArray());
            this._callbacks.Add(flag, callback);

            if (this._log.IsDebugEnabled)
                this._log.DebugFormat("pending methodCall#{0}|{1}", flag, remoteUri);
        }
Beispiel #2
0
 private static void Process(RemotingTcpProtocolHandle handle, ILog log, TcpServerChannel server, TcpClient tcpClient)
 {
     try
     {
         if (handle == null)
             return;
         //TODO:process methodCall
         MethodCall methodCall = Serializer.DeserializeMethodCall(handle.ReadContent());
     }
     catch (Exception e)
     {
         log.Error(e);
         //TODO:return error to client
     }
     finally
     {
         IOWorker(log, server, tcpClient);
     }
 }
Beispiel #3
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); }
        }
Beispiel #4
0
            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());
            }