Пример #1
0
        private void CheckTransport()
        {
            var transportState = _transport.State;

            if (transportState == TcpTransportState.Closed)
            {
                throw new RpcException("SimpleRpcClient CheckTransport failed,connection has been closed");
            }

            if (transportState == TcpTransportState.Uninit)
            {
                try
                {
                    _transport.Init();
                }
                catch (Exception ex)
                {
                    try
                    {
                        _transport.Close();
                    }
                    catch
                    {
                    }

                    LogAgent.Warn("close network in SimpleRpcClient CheckTransport,transport init error", ex);
                    throw;
                }
            }
        }
Пример #2
0
        private void TransportKeepAlive()
        {
            if (_transport.IsSocketConnected == false)
            {
                try
                {
                    _transport.Close();
                }
                catch
                {
                }
            }

            var transportState = _transport.State;

            if (transportState == -1)
            {
                _transport = new TcpTransport(this, ServerIP, ServerPort);
                _transport.Init();
            }
            if (transportState == 0)
            {
                _transport.Init();
            }
        }
Пример #3
0
        private async Task <FrameData> QueryAsync(string title, byte[] contentBytes, bool throwIfErrorResponseCode = false)
        {
            if (_transport == null)
            {
                throw new Exception("RpcClient has been closed");
            }

            if (string.IsNullOrEmpty(title))
            {
                throw new Exception();
            }

            if (title.Length > 65535)
            {
                throw new Exception();
            }

            var messageId = Interlocked.Increment(ref _messageId);

            lock (_queryKey)
            {
                CheckConnection();

                _queryContext.Reset(messageId);

                //if (NetworkSettings.TcpRequestSendMode == TcpSendMode.Async)
                //    _transport.AsyncSend(title, contentBytes, 0, messageId);
                //else
                _transport.Send(title, contentBytes, 0, messageId);
            }

            var receiveData = await _queryContext.WaitForResultAsync(messageId);

            if (receiveData == null)
            {
                _transport.Close();
                throw new Exception("query timeout");
            }

            if (throwIfErrorResponseCode)
            {
                var stateCode = receiveData.StateCode;
                if (stateCode != 0)
                {
                    throw new Exception("query error:" + stateCode);
                }
            }

            return(receiveData);
        }
        private void BreakConnection()
        {
            TcpTransport transport = this.connection.ITransport.Narrow(typeof(TcpTransport)) as TcpTransport;

            Assert.IsNotNull(transport);
            transport.Close();
        }
Пример #5
0
        private void CheckConnection()
        {
            if (_transport.IsSocketConnected == false)
            {
                try
                {
                    _transport.Close();
                }
                catch
                {
                }
            }

            if (_transport.State == -1)
            {
                _transport = new TcpTransport(this, ServerIP, ServerPort);
                _transport.Init();
            }

            if (_transport.State == 0)
            {
                _transport.Init();
            }
        }
Пример #6
0
        public void FailOnCommitTransportHook(ITransport transport, Command command)
        {
            if (commitFailed)
            {
                return;
            }

            if (command is TransactionInfo)
            {
                TransactionInfo txInfo = command as TransactionInfo;
                if (txInfo.Type == (byte)TransactionType.CommitOnePhase)
                {
                    Tracer.Debug("Exception from the Commit to simulate an connection drop.");
                    commitFailed = true;
                    TcpTransport tcpTransport = transport as TcpTransport;
                    tcpTransport.Close();
                }
            }
        }
Пример #7
0
        private static async void ProcessReceive(TcpTransport serverTransport, FrameData frameData,
                                                 IMessageProcessor messageProcessor)
        {
            ResponseBase response = null;

            if (frameData.TitleBytes == null || frameData.TitleBytes.Length == 0)
            {
                response = new ErrorResponse((byte)ResponseCode.SERVICE_TITLE_ERROR);
            }

            if (messageProcessor == null)
            {
                response = new ErrorResponse((byte)ResponseCode.SERVICE_NOT_FOUND);
            }

            try
            {
                if (response == null)
                {
                    if (NetworkSettings.ServerProcessMode == CommunicationMode.Sync)
                    {
                        var responseTask = messageProcessor.Process(frameData);
                        responseTask.Wait();
                        response = responseTask.Result;
                    }
                    else
                    {
                        response = await messageProcessor.Process(frameData);
                    }
                }
            }
            catch
            {
                response = new ErrorResponse((byte)ResponseCode.SERVER_INTERNAL_ERROR);
            }

            var responseExtention = response.HeaderExtentionBytes ?? FrameFormat.EmptyBytes;
            var responseContent   = response.ContentBytes ?? FrameFormat.EmptyBytes;
            var responseCode      = response.Code;

            try
            {
                var messageByteCount = FrameFormat.ComputeFrameByteCount(responseExtention, FrameFormat.EmptyBytes, responseContent);
                var sendBuffer       = serverTransport.SendBufferCache.Get(messageByteCount);

                FrameFormat.FillFrame(sendBuffer, responseExtention, FrameFormat.EmptyBytes, responseContent, responseCode, frameData.MessageId);

                //if (NetworkSettings.ServerTcpSendMode == TcpSendMode.Async)
                //{
                //    serverTransport.SendAsync(sendBuffer, messageByteCount);
                //}
                //else
                //{
                serverTransport.Send(sendBuffer, messageByteCount);
                //}

                serverTransport.SendBufferCache.Cache(sendBuffer);
            }
            catch
            {
                serverTransport.Close();
            }
        }
Пример #8
0
        private void AcceptSocketCallback(IAsyncResult ar)
        {
            if (_serverState != 2)
            {
                return;
            }

            var server = (Socket)ar.AsyncState;

            Socket     client     = null;
            IPEndPoint ipEndPoint = null;

            try
            {
                client     = server.EndAccept(ar);
                ipEndPoint = (IPEndPoint)client.RemoteEndPoint;
            }
            catch (Exception ex)
            {
                LogAgent.Warn("RpcServer AcceptSocketCallback failed,EndAccept error", ex);
            }

            try
            {
                server.BeginAccept(AcceptSocketCallback, server);
            }
            catch (Exception ex)
            {
                Close();
                try
                {
                    if (client != null)
                    {
                        client.Close();
                    }
                }
                catch
                {
                }

                LogAgent.Warn("close network in RpcServer AcceptSocketCallback,BeginAccept error", ex);
                return;
            }

            if (ipEndPoint == null)
            {
                try
                {
                    if (client != null)
                    {
                        client.Close();
                    }
                }
                catch
                {
                }

                LogAgent.Warn("close network in RpcServer AcceptSocketCallback,client RemoteEndPoint is null");
                return;
            }

            var tcpTransport = new TcpTransport(this, ipEndPoint.Address, ipEndPoint.Port);

            try
            {
                tcpTransport.Init(client);
            }
            catch (Exception ex)
            {
                try
                {
                    tcpTransport.Close();
                }
                catch
                {
                }

                LogAgent.Warn("close network in RpcServer AcceptSocketCallback,tcpTransport Init error", ex);
                return;
            }

            _clientTransportDictionary[tcpTransport.Key] = tcpTransport;
        }
Пример #9
0
        private void AcceptSocketCallback(IAsyncResult ar)
        {
            if (_serverState != 1)
            {
                return;
            }

            var server = (TcpListener)ar.AsyncState;

            Socket     client     = null;
            IPEndPoint ipEndPoint = null;

            try
            {
                client     = server.EndAcceptSocket(ar);
                ipEndPoint = (IPEndPoint)client.RemoteEndPoint;
            }
            catch
            {
            }

            try
            {
                server.BeginAcceptSocket(AcceptSocketCallback, server);
            }
            catch
            {
                Close();
                return;
            }

            if (client == null)
            {
                return;
            }

            if (ipEndPoint == null)
            {
                try
                {
                    client.Close();
                }
                catch
                {
                }

                return;
            }

            var tcpTransport = new TcpTransport(this, ipEndPoint.Address, ipEndPoint.Port);

            try
            {
                tcpTransport.Init(client);
            }
            catch
            {
                try
                {
                    tcpTransport.Close();
                }
                catch
                {
                }
                return;
            }
        }
Пример #10
0
        private void AcceptSocketCallback(IAsyncResult ar)
        {
            if (_serverState != 1)
            {
                return;
            }

            var server = (Socket)ar.AsyncState;

            Socket     client     = null;
            IPEndPoint ipEndPoint = null;

            try
            {
                client     = server.EndAccept(ar);
                ipEndPoint = (IPEndPoint)client.RemoteEndPoint;
            }
            catch (Exception ex)
            {
                LogAgent.Error(ex.Message);
            }

            try
            {
                server.BeginAccept(AcceptSocketCallback, server);
            }
            catch (Exception ex)
            {
                Close();
                try
                {
                    if (client != null)
                    {
                        client.Close();
                    }
                }
                catch
                {
                }

                LogAgent.Error(ex.Message);

                return;
            }

            if (ipEndPoint == null)
            {
                try
                {
                    if (client != null)
                    {
                        client.Close();
                    }
                }
                catch
                {
                }

                return;
            }

            var tcpTransport = new TcpTransport(this, ipEndPoint.Address, ipEndPoint.Port);

            try
            {
                tcpTransport.Init(client);
            }
            catch (Exception ex)
            {
                try
                {
                    tcpTransport.Close();
                }
                catch
                {
                }

                LogAgent.Error(ex.Message);
                return;
            }

            _clientTransportDictionary[tcpTransport.Key] = tcpTransport;
        }