예제 #1
0
파일: Session.cs 프로젝트: ndhisrfzs/GFrame
        public void Send(byte flag, 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, bytes);
        }
예제 #2
0
파일: Session.cs 프로젝트: ndhisrfzs/GFrame
        private void Execute(Packet packet)
        {
            if (packet.Length < Packet.MinSize)
            {
                this.Network.Remove(this.id);
                return;
            }

            byte   flag   = packet.Flag;
            ushort opcode = packet.Opcode;

            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.GetType(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(response.RpcId, out action))
            {
                return;
            }

            this.requestCallback.Remove(response.RpcId);

            action(response);
        }