예제 #1
0
        private void RunDecompressedBytes(ushort opcode, byte[] messageBytes, int offset)
        {
            Type   messageType = this.network.Owner.GetComponent <OpcodeTypeComponent>().GetType(opcode);
            object message     = messagePacker.DeserializeFrom(messageType, messageBytes, offset, messageBytes.Length - offset);


            // 普通消息或者是Rpc请求消息
            if (message is AMessage || message is ARequest)
            {
                MessageInfo messageInfo = new MessageInfo(opcode, message);
                Game.Scene.GetComponent <CrossComponent>().Run(CrossIdType.MessageDeserializeFinish, messageInfo);
                return;
            }

            AResponse response = message as AResponse;

            Log.Debug($"aaaaaaaaaaaaaaaaaaaaaaaaaaa {JsonHelper.ToJson(response)}");
            if (response != null)
            {
                // rpcFlag>0 表示这是一个rpc响应消息
                // Rpc回调有找不着的可能,因为client可能取消Rpc调用
                Action <object> action;
                if (!this.requestCallback.TryGetValue(response.RpcId, out action))
                {
                    return;
                }
                this.requestCallback.Remove(response.RpcId);
                action(message);
                return;
            }

            throw new Exception($"message type error: {message.GetType().FullName}");
        }
예제 #2
0
        private void RunDecompressedBytes(ushort opcode, byte[] messageBytes, int offset)
        {
            Type   messageType = this.network.Owner.GetComponent <OpcodeTypeComponent>().GetType(opcode);
            object message     = messagePacker.DeserializeFrom(messageType, messageBytes, offset, messageBytes.Length - offset);

            if (message is AActorMessage)
            {
                this.network.Owner.GetComponent <ActorMessageDispatherComponent>().Handle(this, message);
                return;
            }
            if (message is AMessage || message is ARequest)
            {
                this.network.Owner.GetComponent <MessageDispatherComponent>().Handle(this, message);
                return;
            }

            if (message is AResponse response)
            {
                // rpcFlag>0 表示这是一个rpc响应消息
                // Rpc回调有找不着的可能,因为client可能取消Rpc调用
                if (!this.requestCallback.TryGetValue(response.RpcId, out Action <object> action))
                {
                    return;
                }
                this.requestCallback.Remove(response.RpcId);
                action(message);
                return;
            }

            throw new Exception($"message type error: {message.GetType().FullName}");
        }
예제 #3
0
        private void OnRecvPacket(MemoryStream memoryStream)
        {
            INetworkMessage message = null;

            try
            {
#if SERVER
                var instance = MessagePool.Instance.Fetch <C2S>();
#else
                var instance = MessagePool.Instance.Fetch <S2C>();
#endif
                message = _msgPacker.DeserializeFrom(instance, memoryStream);
            }
            catch (Exception e)
            {
                // 出现任何消息解析异常都要断开Session,防止客户端伪造消息
                Log.Error($"command: {message.Cmd}, error: {e}");
                Error = ErrorCode.ERR_PacketParserError;
                _network.RemoveSession(Id);
                return;
            }

            _lastRecvTime = TimeHelper.Now();
            if (_requestCallback.TryGetValue(message.ReqId, out var action))
            {
                action(message);
                _requestCallback.Remove(message.ReqId);
            }
            else
            {
                _msgDispatcher?.Dispatch(this, message);
            }
        }