Esempio n. 1
0
        private void Send(byte flag, uint rpcId, IMessage message)
        {
            OpcodeTypeComponent opcodeTypeComponent = (this.Network.Parent as Entity).GetComponent <OpcodeTypeComponent>();
            ushort opcode = opcodeTypeComponent.GetOpcode(message.GetType());

            byte[] bytes = this.Network.MessagePacker.Serialize(message);
            Send(flag, opcode, rpcId, bytes);
        }
Esempio n. 2
0
        private void Execute(Packet packet)
        {
            if (packet.Length < Packet.MinSize)
            {
                this.Network.Remove(this.id);
                return;
            }

            byte   flag   = packet.Flag;
            ushort opcode = packet.Opcode;
            uint   rpcId  = packet.RpcId;

            if ((flag & 0x01) == 0)
            {
                //flag为0表示rpc请求
                this.Network.MessageDispatcher.Dispatch(this, packet);
                return;
            }

            //flag为1表示rpc返回消息
            OpcodeTypeComponent opcodeTypeComponent = (this.Network.Parent as Entity).GetComponent <OpcodeTypeComponent>();
            Type   responseType = opcodeTypeComponent.GetResponseType(opcode);
            object message      = this.Network.MessagePacker.DeserializeFrom(responseType, packet.Bytes, Packet.Index, packet.Length);

            IResponse response = message as IResponse;

            if (response == null)
            {
                throw new Exception($"flag is response, but message is not! {opcode}");
            }

            Action <IResponse> action;

            if (!this.requestCallback.TryGetValue(rpcId, out action))
            {
                return;
            }

            this.requestCallback.Remove(rpcId);

            action(response);
        }