Example #1
0
        public static void Recv(byte[] data, int offset, int size, List <Packet> packets)
        {
            //消息格式
            //2字节消息类型,2字节消息长度,N字节消息数据
            while (size > CmdMsgLength)
            {
                //MsgLength个字节消息长度,CmdLength个字节消息类型长度,N字节消息数据长度
                ByteReadMemory memory = new ByteReadMemory(data, offset, size);

                //1.读取消息类型
                ushort cmd = memory.ReadUShort();
                size -= CmdLength;

                //2.读取消息长度
                ushort length = memory.ReadUShort();
                size -= MsgLength;

                if (length > size)
                {
                    throw new ArgumentException($"PacketProcessing Recv error: {length} > {size}");
                }

                //3.读取消息数据
                ByteBuffer buffer = ByteBuffer.Allocate(length);
                memory.Read(buffer, length);
                size -= length;

                packets.Add(new Packet(cmd, buffer, 0));
            }
        }
        public bool RecvReliablePackets(ServerChannel channel, List <Packet> packets, IChannelListener listener, ServerHeartbeatProcessing heartbeat)
        {
            while (true)
            {
                int size = channel.Recv(m_RecvBuffer, 0, m_RecvBuffer.Length);

                if (size <= 0)
                {
                    break;
                }

                if (size == 8)
                {
                    ByteReadMemory memory = new ByteReadMemory(m_RecvBuffer, 0, size);
                    uint           flag   = memory.ReadUInt();
                    uint           conv   = memory.ReadUInt();

                    if (conv == channel.Conv)
                    {
                        if (flag == KcpConstants.Flag_Connect)
                        {
                            channel.SetConnectedStatus(true);
                            if (listener != null)
                            {
                                listener.OnAddChannel(channel);
                            }
                            continue;
                        }

                        if (flag == KcpConstants.Flag_Disconnect)
                        {
                            channel.SetConnectedStatus(false);
                            if (listener != null)
                            {
                                listener.OnRemoveChannel(channel);
                            }
                            continue;
                        }

                        if (flag == KcpConstants.Flag_Heartbeat)
                        {
                            heartbeat.UpdateHeartbeat(channel, m_RecvBuffer, 0, size);
                            continue;
                        }
                    }
                }

                PacketProcessing.Recv(m_RecvBuffer, 0, size, packets);
            }

            return(packets.Count > 0);
        }
        public void RecvReliablePackets(ClientTransport transport, KcpCon kcp, List <Packet> packets, ClientHeartbeatProcessing heartbeat)
        {
            while (true)
            {
                int size = kcp.Recv(m_Buffer, 0, m_Buffer.Length);

                if (size <= 0)
                {
                    break;
                }

                if (size == 8)
                {
                    ByteReadMemory memory = new ByteReadMemory(m_Buffer, 0, 8);
                    uint           flag   = memory.ReadUInt();
                    uint           conv   = memory.ReadUInt();

                    if (conv == kcp.Conv)
                    {
                        if (flag == KcpConstants.Flag_Heartbeat)
                        {
                            heartbeat.RecvHeartbeat();
                            continue;
                        }

                        if (flag == KcpConstants.Flag_Disconnect)
                        {
                            transport.Disconnect(false, true);
                            continue;
                        }
                    }
                }

                PacketProcessing.Recv(m_Buffer, 0, size, packets);
            }
        }