Пример #1
0
        private void SendInternal(string title, byte[] contentBytes, byte stateCode, int messageId, TcpSendMode sendMode)
        {
            if (_state != 1)
            {
                throw new Exception();
            }

            if (title == null)
            {
                title = string.Empty;
            }

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

            if (contentBytes == null)
            {
                contentBytes = FrameFormat.EmptyBytes;
            }

            var titleBytes       = FrameFormat.GetTitleBytes(title);
            var messageByteCount = FrameFormat.ComputeFrameByteCount(titleBytes, contentBytes);
            var sendBuffer       = GetSendBuffer(messageByteCount);

            FrameFormat.FillFrameHeader(sendBuffer, titleBytes, contentBytes, stateCode, messageId);

            if (title.Length + contentBytes.Length > 0)
            {
                FrameFormat.FillFrameBody(sendBuffer, titleBytes, contentBytes);
            }

            if (sendMode == TcpSendMode.Async)
            {
                try
                {
                    _socket.BeginSend(sendBuffer, 0, messageByteCount, SocketFlags.None, SendCallback, this);
                }
                catch
                {
                    Close();
                    throw;
                }
            }
            else
            {
                try
                {
                    _socket.Send(sendBuffer, messageByteCount, SocketFlags.None);
                }
                catch
                {
                    Close();
                    throw;
                }
            }
        }
Пример #2
0
        public async Task <FrameData> QueryAsyncInternal(byte[] titleBytes, byte[] contentBytes, byte[] extentionBytes = null, bool throwIfErrorResponseCode = false)
        {
            if (titleBytes == null)
            {
                throw new ArgumentNullException("titleBytes");
            }

            if (titleBytes.Length > 65535)
            {
                throw new ArgumentOutOfRangeException("titleBytes");
            }

            _isSyncQuery = false;

            int messageId = GetMessageId();

            var messageByteCount = FrameFormat.ComputeFrameByteCount(extentionBytes, titleBytes, contentBytes);
            var sendBuffer       = _transport.SendBufferCache.Get(messageByteCount);

            FrameFormat.FillFrame(sendBuffer, extentionBytes, titleBytes, contentBytes, 0, messageId);

            _asyncQueryContext.Reset(messageId);

            CheckConnection();

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

            _transport.SendBufferCache.Cache(sendBuffer);

            var receiveData = await _asyncQueryContext.WaitForResult(messageId, NetworkSettings.ReceiveTimeout);

            if (receiveData == null)
            {
                _transport.Close();
                throw new Exception("queryAsync failed, time is out");
            }

            if (throwIfErrorResponseCode)
            {
                var stateCode = receiveData.StateCode;
                if (stateCode != (byte)ResponseCode.OK)
                {
                    throw new Exception("queryAsync failed,error code:" + stateCode);
                }
            }

            return(receiveData);
        }
Пример #3
0
        internal void ConstructCurrentMessage(byte[] titleBytes, byte[] contentBytes, byte[] extentionBytes = null, bool throwIfErrorResponseCode = true)
        {
            MessageId++;
            if (MessageId >= 100000000)
            {
                MessageId = 1;
            }

            MessageByteCount = FrameFormat.ComputeFrameByteCount(extentionBytes, titleBytes, contentBytes);
            SendBuffer       = _sendBufferCache.Get(MessageByteCount);
            FrameFormat.FillFrame(SendBuffer, extentionBytes, titleBytes, contentBytes, 0, MessageId);

            ThrowIfErrorResponseCode = throwIfErrorResponseCode;
        }
Пример #4
0
        private FrameData QueryInternal(byte[] titleBytes, byte[] contentBytes, byte[] extentionBytes, bool throwIfErrorResponseCode)
        {
            if (titleBytes == null)
            {
                throw new ArgumentNullException("titleBytes");
            }

            if (titleBytes.Length > 65535)
            {
                throw new ArgumentOutOfRangeException("titleBytes");
            }

            int messageId        = GenerateMessageId();
            var messageByteCount = FrameFormat.ComputeFrameByteCount(extentionBytes, titleBytes, contentBytes);
            var sendBuffer       = _transport.SendBufferCache.Get(messageByteCount);

            FrameFormat.FillFrame(sendBuffer, extentionBytes, titleBytes, contentBytes, 0, messageId);

            TransportKeepAlive();

            _syncQueryContext.Reset(messageId);
            _transport.Send(sendBuffer, messageByteCount);
            _transport.SendBufferCache.Cache(sendBuffer);
            var receiveData = _syncQueryContext.WaitForResult(messageId, NetworkSettings.ReceiveTimeout).Result;

            if (receiveData == null)
            {
                _transport.Close();
                throw new RpcException("query failed,time is out");
            }

            if (throwIfErrorResponseCode)
            {
                var stateCode = receiveData.StateCode;
                if (stateCode != (byte)ResponseCode.OK)
                {
                    throw new RpcException("query failed,error code:" + stateCode);
                }
            }

            return(receiveData);
        }
Пример #5
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();
            }
        }