Пример #1
0
        public ServerChannel(KcpCon con)
        {
            m_Dispose = false;
            m_Conv    = con.Conv;
            m_ConId   = con.ConId;

            m_Con         = con;
            m_Heartbeat   = new ServerHeartbeatProcessing();
            m_RecvPackets = new HConcurrentQueue <Packet>();
            m_SendPackets = new HConcurrentQueue <Packet>();
        }
Пример #2
0
        public ClientTransport()
        {
            m_ConId   = 0;
            m_Status  = Status.None;
            m_Dispose = false;

            m_Kcp    = null;
            m_Socket = null;

            m_Register    = new CRegister();
            m_SendPackets = new HConcurrentQueue <Packet>();
            m_RecvPackets = new HConcurrentQueue <Packet>();
        }
Пример #3
0
 public void SendPackets(KcpCon kcp, HConcurrentQueue <Packet> packets)
 {
     while (packets.TryDequeue(out Packet packet))
     {
         int size = PacketProcessing.Send(m_Buffer, packet);
         if (size > 0)
         {
             if (packet.Channel == ChannelType.Reliable)
             {
                 kcp.Send(m_Buffer, 0, size);
             }
             else if (packet.Channel == ChannelType.Unreliable)
             {
                 kcp.RawSend(m_Buffer, 0, size);
             }
         }
     }
 }
Пример #4
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);
            }
        }
Пример #5
0
        private void OnConnectLooper(object point)
        {
            try
            {
                int       time        = 0;
                const int tick        = 100;
                const int timeout     = 5000;
                const int pollTimeout = 100000;
                byte[]    rawBuffer   = new byte[KcpConstants.Packet_Length];

                m_Socket.Connect((EndPoint)point);

                while (!m_Dispose && m_Status == Status.Connecting)
                {
                    int size = KcpHelper.Encode32u(rawBuffer, 0, KcpConstants.Flag_Connect);
                    m_Socket.Send(rawBuffer, 0, size, SocketFlags.None);

                    if (!m_Socket.Poll(pollTimeout, SelectMode.SelectRead))
                    {
                        time += tick;
                        if (time >= timeout)
                        {
                            m_Status = Status.Timeout;
                            break;
                        }
                        continue;
                    }

                    if (m_Dispose)
                    {
                        break;
                    }

                    int count = m_Socket.Receive(rawBuffer, 0, rawBuffer.Length, SocketFlags.None);

                    if (count == 37)
                    {
                        uint cid  = KcpHelper.Decode32u(rawBuffer, 0);
                        uint flag = KcpHelper.Decode32u(rawBuffer, 29);
                        uint conv = KcpHelper.Decode32u(rawBuffer, 33);

                        if (flag == KcpConstants.Flag_Connect)
                        {
                            m_Kcp = new KcpConClient(cid, conv, m_Socket);
                            m_Kcp.Input(rawBuffer, KcpConstants.Head_Size, count - KcpConstants.Head_Size);
                            count = m_Kcp.Recv(rawBuffer, 0, rawBuffer.Length);

                            if (count == 8)
                            {
                                KcpHelper.Encode32u(rawBuffer, 0, KcpConstants.Flag_Connect);
                                KcpHelper.Encode32u(rawBuffer, 4, conv);
                                m_Kcp.Send(rawBuffer, 0, 8);
                                m_Kcp.Flush();

                                m_ConId  = cid;
                                m_Status = Status.Success;
                                m_Register.Notify(Msg.Success);

                                KcpHelper.CreateThread(OnHandleLooper);
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(e.ToString());
                m_Status = Status.Error;
            }
            finally
            {
                if (!m_Dispose)
                {
                    if (m_Status == Status.Error)
                    {
                        m_Register.Notify(Msg.Error);
                    }
                    else if (m_Status == Status.Timeout)
                    {
                        m_Register.Notify(Msg.Timeout);
                    }
                }
            }
        }