示例#1
0
        //=================================================================================
        #region 管理KCP

        private KCPProxy GetKcp(IPEndPoint ipep)
        {
            if (ipep == null || ipep.Port == 0 ||
                ipep.Address.Equals(IPAddress.Any) ||
                ipep.Address.Equals(IPAddress.IPv6Any))
            {
                return(null);
            }

            KCPProxy proxy;
            int      cnt = m_ListKcp.Count;

            for (int i = 0; i < cnt; i++)
            {
                proxy = m_ListKcp[i];
                if (proxy.RemotePoint.Equals(ipep))
                {
                    return(proxy);
                }
            }

            proxy = new KCPProxy(m_KcpKey, ipep, m_SystemSocket);
            proxy.AddReceiveListener(OnReceiveAny);
            m_ListKcp.Add(proxy);
            return(proxy);
        }
示例#2
0
        private KCPProxy GetKcp(IPEndPoint ipep, bool autoAdd = true)
        {
            if (!IsValidKcpIPEndPoint(ipep))
            {
                return(null);
            }

            KCPProxy proxy = null;
            int      cnt   = m_ListKcp.Count;

            for (int i = 0; i < cnt; i++)
            {
                proxy = m_ListKcp[i];
                if (proxy.RemotePoint.Equals(ipep))
                {
                    return(proxy);
                }
            }

            if (autoAdd)
            {
                proxy = new KCPProxy(m_KcpKey, ipep, m_SystemSocket);
                proxy.AddReceiveListener(OnReceiveAny);
                m_ListKcp.Add(proxy);
            }
            return(proxy);
        }
示例#3
0
        public void RemoveReceiveListener(IPEndPoint remotePoint, KCPReceiveListener listener)
        {
            KCPProxy proxy = GetKcp(remotePoint);

            if (proxy != null)
            {
                proxy.RemoveReceiveListener(listener);
            }
            else
            {
                m_AnyEPListener -= listener;
            }
        }
示例#4
0
        //=================================================================================
        #region 主线程驱动

        public void Update()
        {
            if (m_IsRunning)
            {
                //获取时钟
                uint current = GetClockMS();

                int cnt = m_ListKcp.Count;
                for (int i = 0; i < cnt; i++)
                {
                    KCPProxy proxy = m_ListKcp[i];
                    proxy.Update(current);
                }
            }
        }
示例#5
0
        private void DoReceive()
        {
            EndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);
            int      cnt         = m_SystemSocket.ReceiveFrom(m_RecvBufferTemp, m_RecvBufferTemp.Length,
                                                              SocketFlags.None, ref remotePoint);

            if (cnt > 0)
            {
                KCPProxy proxy = GetKcp((IPEndPoint)remotePoint);
                if (proxy != null)
                {
                    proxy.DoReceiveInThread(m_RecvBufferTemp, cnt);
                }
            }
        }
示例#6
0
        public void CloseKcp(IPEndPoint ipep)
        {
            KCPProxy proxy = null;
            int      cnt   = m_ListKcp.Count;

            for (int i = 0; i < cnt; i++)
            {
                proxy = m_ListKcp[i];
                if (proxy.RemotePoint.Equals(ipep))
                {
                    m_ListKcp.RemoveAt(i);
                    break;
                }
            }
        }
示例#7
0
        //=================================================================================
        #region 发送逻辑
        public bool SendTo(byte[] buffer, int size, IPEndPoint remotePoint)
        {
            if (remotePoint.Address == IPAddress.Broadcast)
            {
                int cnt = m_SystemSocket.SendTo(buffer, size, SocketFlags.None, remotePoint);
                return(cnt > 0);
            }
            else
            {
                KCPProxy proxy = GetKcp(remotePoint);
                if (proxy != null)
                {
                    return(proxy.DoSend(buffer, size));
                }
            }

            return(false);
        }
示例#8
0
        private void DoReceive()
        {
            if (m_SystemSocket.Available <= 0)
            {
                return;
            }

            EndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);
            int      cnt         = m_SystemSocket.ReceiveFrom(m_RecvBuffer.GetBytes(), m_RecvBuffer.Capacity,
                                                              SocketFlags.None, ref remotePoint);

            if (cnt > 0)
            {
                m_RecvBuffer.AddLength(cnt, 0);
                m_RecvBuffer.Position = 0;

                KCPProxy proxy = GetKcp((IPEndPoint)remotePoint);
                if (proxy != null)
                {
                    proxy.DoReceive(m_RecvBuffer);
                }
            }
        }