示例#1
0
        public void HandleRecv(byte[] date, int offset, int length)
        {
            if (this.IsDisposed)
            {
                return;
            }

            this.IsConnected = true;

            Kcp.KcpInput(this.kcp, date, offset, length);
            this.Service.AddToUpdateNextTime(0, this.Id);

            while (true)
            {
                if (this.IsDisposed)
                {
                    break;
                }
                int n = Kcp.KcpPeeksize(this.kcp);
                if (n < 0)
                {
                    break;
                }
                if (n == 0)
                {
                    this.OnError((int)SocketError.NetworkReset);
                    break;
                }

                MemoryStream ms = MessageSerializeHelper.GetStream(n);

                ms.SetLength(n);
                ms.Seek(0, SeekOrigin.Begin);
                byte[] buffer = ms.GetBuffer();
                int    count  = Kcp.KcpRecv(this.kcp, buffer, n);
                if (n != count)
                {
                    break;
                }

                switch (this.Service.ServiceType)
                {
                case ServiceType.Inner:
                    ms.Seek(Packet.ActorIdLength + Packet.OpcodeLength, SeekOrigin.Begin);
                    break;

                case ServiceType.Outer:
                    ms.Seek(Packet.OpcodeLength, SeekOrigin.Begin);
                    break;
                }
                this.lastRecvTime = this.Service.TimeNow;
                this.OnRead(ms);
            }
        }
        public static (ushort, MemoryStream) MessageToStream(object message, int count = 0)
        {
            MemoryStream stream = GetStream(Packet.OpcodeLength + count);

            ushort opcode = OpcodeTypeComponent.Instance.GetOpcode(message.GetType());

            stream.Seek(Packet.OpcodeLength, SeekOrigin.Begin);
            stream.SetLength(Packet.OpcodeLength);

            stream.GetBuffer().WriteTo(0, opcode);

            MessageSerializeHelper.SerializeTo(opcode, message, stream);

            stream.Seek(0, SeekOrigin.Begin);
            return(opcode, stream);
        }
        public static (ushort, MemoryStream) MessageToStream(long actorId, object message, int count = 0)
        {
            int          actorSize = sizeof(long);
            MemoryStream stream    = GetStream(actorSize + Packet.OpcodeLength + count);

            ushort opcode = OpcodeTypeComponent.Instance.GetOpcode(message.GetType());

            stream.Seek(actorSize + Packet.OpcodeLength, SeekOrigin.Begin);
            stream.SetLength(actorSize + Packet.OpcodeLength);

            // 写入actorId
            stream.GetBuffer().WriteTo(0, actorId);
            stream.GetBuffer().WriteTo(actorSize, opcode);

            MessageSerializeHelper.SerializeTo(opcode, message, stream);

            stream.Seek(0, SeekOrigin.Begin);
            return(opcode, stream);
        }
示例#4
0
        public void Send(IMessage message)
        {
            switch (this.AService.ServiceType)
            {
            case ServiceType.Inner:
            {
                (ushort opcode, MemoryStream stream) = MessageSerializeHelper.MessageToStream(0, message);
                OpcodeHelper.LogMsg(this.DomainZone(), opcode, message);
                this.Send(0, stream);
                break;
            }

            case ServiceType.Outer:
            {
                (ushort opcode, MemoryStream stream) = MessageSerializeHelper.MessageToStream(message);
                OpcodeHelper.LogMsg(this.DomainZone(), opcode, message);
                this.Send(0, stream);
                break;
            }
            }
        }
示例#5
0
 public void Send(long actorId, IMessage message)
 {
     (ushort opcode, MemoryStream stream) = MessageSerializeHelper.MessageToStream(actorId, message);
     OpcodeHelper.LogMsg(this.DomainZone(), opcode, message);
     this.Send(actorId, stream);
 }
示例#6
0
        public bool Parse()
        {
            while (true)
            {
                switch (this.state)
                {
                case ParserState.PacketSize:
                {
                    if (this.service.ServiceType == ServiceType.Inner)
                    {
                        if (this.buffer.Length < InnerPacketSizeLength)
                        {
                            return(false);
                        }

                        this.buffer.Read(this.cache, 0, InnerPacketSizeLength);

                        this.packetSize = BitConverter.ToInt32(this.cache, 0);
                        if (this.packetSize > ushort.MaxValue * 16 || this.packetSize < Packet.MinPacketSize)
                        {
                            throw new Exception($"recv packet size error, 可能是外网探测端口: {this.packetSize}");
                        }
                    }
                    else
                    {
                        if (this.buffer.Length < OuterPacketSizeLength)
                        {
                            return(false);
                        }

                        this.buffer.Read(this.cache, 0, OuterPacketSizeLength);

                        this.packetSize = BitConverter.ToUInt16(this.cache, 0);
                        if (this.packetSize < Packet.MinPacketSize)
                        {
                            throw new Exception($"recv packet size error, 可能是外网探测端口: {this.packetSize}");
                        }
                    }

                    this.state = ParserState.PacketBody;
                    break;
                }

                case ParserState.PacketBody:
                {
                    if (this.buffer.Length < this.packetSize)
                    {
                        return(false);
                    }

                    MemoryStream memoryStream = MessageSerializeHelper.GetStream(this.packetSize);
                    this.buffer.Read(memoryStream, this.packetSize);
                    //memoryStream.SetLength(this.packetSize - Packet.MessageIndex);
                    this.MemoryStream = memoryStream;

                    if (this.service.ServiceType == ServiceType.Inner)
                    {
                        memoryStream.Seek(Packet.MessageIndex, SeekOrigin.Begin);
                    }
                    else
                    {
                        memoryStream.Seek(Packet.OpcodeLength, SeekOrigin.Begin);
                    }

                    this.state = ParserState.PacketSize;
                    return(true);
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }