예제 #1
0
        private async ValueTask HandleMessageReqPacketAsync()
        {
            var wrapper = await _messageReqChannel.Reader.ReadAsync(_cts.Token);

            if (wrapper.Packet == null)
            {
                _logger.LogError("Read packet is null.");
                return;
            }

            if (!_requestMessageHandlerDict.TryGetValue(wrapper.Packet.Code, out IRequestMessageHandler requestMessageHandler))
            {
                _logger.LogError("Can't find any 'IRequestMessageHandler' from the dict by code '{0}'! ", wrapper.Packet.Code);
                return;
            }

            var request = new RequestMessage()
            {
                Code = wrapper.Packet.Code,
                Body = wrapper.Packet.Body
            };

            var response = await requestMessageHandler.HandleRequestAsync(wrapper.Session, request);

            if (!_channelDict.TryGetValue(wrapper.Session.SessionId, out IChannel channel))
            {
                _logger.LogError("Can't find any channel by session {0}", wrapper?.Session.SessionId);
                return;
            }

            var messageRespPacket = new MessageRespPacket()
            {
                Sequence = wrapper.Packet.Sequence,
                Code     = response.Code,
                Body     = response.Body
            };

            if (!channel.IsWritable)
            {
                _logger.LogInformation("Channel not writeable!");
            }

            await channel.WriteAndFlushAsync(messageRespPacket);
        }
예제 #2
0
        /// <summary>Handle the 'RespMessagePacket' received from server
        /// </summary>
        private void SetMessageRespPacket(MessageRespPacket packet)
        {
            if (_responseFutureDict.TryRemove(packet.Sequence, out ResponseFuture responseFuture))
            {
                var responseMessage = new ResponseMessage()
                {
                    Code = packet.Code,
                    Body = packet.Body
                };

                if (!responseFuture.SetResponse(responseMessage))
                {
                    _logger.LogDebug("Set remoting response failed,Sequence: '{0}'.", packet.Sequence);
                }
            }
            else
            {
                _logger.LogDebug("Try remove  from responseFuture dict fail!");
            }
        }
예제 #3
0
        internal void DoDecode(IChannelHandlerContext context, IByteBuffer buffer, List <object> output)
        {
            ////1 byte type, 4 byte sequence, 4 byte length
            //if (buffer.ReadableBytes < 9)
            //{
            //    return;
            //}
            ////var lengthBuffer = buffer.Slice(5, 4);
            ////// is that right?
            ////var length = lengthBuffer.ReadInt();

            //var length = buffer.GetInt(5);

            //// buffer is smaller than packet length
            //if (buffer.ReadableBytes < 9 + length)
            //{
            //    return;
            //}

            var packetType = (PacketType)buffer.ReadByte();
            var sequence   = buffer.ReadInt();

            switch (packetType)
            {
            case PacketType.PINGREQ:
                var pingCode   = buffer.ReadByte();
                var pingPacket = new PingPacket()
                {
                    Sequence = sequence,
                    PingCode = pingCode
                };
                output.Add(pingPacket);
                break;

            case PacketType.PINGRESP:
                var pongCode   = buffer.ReadByte();
                var pongPacket = new PongPacket()
                {
                    Sequence = sequence,
                    PongCode = pongCode
                };
                output.Add(pongPacket);
                break;

            case PacketType.MESSAGEREQ:
                var req_code         = buffer.ReadShort();
                var req_length       = buffer.ReadInt();
                var messageReqPacket = new MessageReqPacket()
                {
                    Sequence = sequence,
                    Code     = req_code,
                    Body     = new byte[req_length]
                };
                buffer.ReadBytes(messageReqPacket.Body);
                output.Add(messageReqPacket);
                break;

            case PacketType.MESSAGERESP:
                var resp_code         = buffer.ReadShort();
                var resp_length       = buffer.ReadInt();
                var messageRespPacket = new MessageRespPacket()
                {
                    Sequence = sequence,
                    Code     = resp_code,
                    Body     = new byte[resp_length]
                };
                buffer.ReadBytes(messageRespPacket.Body);
                output.Add(messageRespPacket);
                break;

            case PacketType.PUSHREQ:
                var reqPush_type   = buffer.ReadByte();
                var reqPush_code   = buffer.ReadShort();
                var reqPush_length = buffer.ReadInt();
                var reqPushPacket  = new PushReqPacket()
                {
                    Sequence = sequence,
                    Code     = reqPush_code,
                    PushType = (PushType)reqPush_type,
                    Body     = new byte[reqPush_length]
                };
                buffer.ReadBytes(reqPushPacket.Body);
                output.Add(reqPushPacket);
                break;

            case PacketType.PUSHRESP:
                var respPush_type   = buffer.ReadByte();
                var respPush_Code   = buffer.ReadShort();
                var respPush_length = buffer.ReadInt();
                var respPushPacket  = new PushRespPacket()
                {
                    Sequence = sequence,
                    Code     = respPush_Code,
                    PushType = (PushType)respPush_type,
                    Body     = new byte[respPush_length]
                };
                buffer.ReadBytes(respPushPacket.Body);
                output.Add(respPushPacket);
                break;

            default:
                throw new ArgumentException("Invalid packet type!");
            }
        }