Ejemplo n.º 1
0
        private void DoReceive()
        {
            if (m_SystemSocket.Available <= 0)
            {
                return;
            }

            EndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);

            try
            {
                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);
                    }
                }
            }
            catch (Exception ex)
            {
                //接收出错,用户主动关闭。
            }
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public void RemoveReceiveListener(IPEndPoint remotePoint, KCPReceiveListener listener)
        {
            KCPProxy proxy = GetKcp(remotePoint);

            if (proxy != null)
            {
                proxy.RemoveReceiveListener(listener);
            }
            else
            {
                m_AnyEPListener -= listener;
            }
        }
Ejemplo n.º 4
0
        public void Update()
        {
            if (m_IsRunning)
            {
                //获取时钟
                long current = KCPProxy.GetClockMS();

                int cnt = m_ListKcp.Count;
                for (int i = 0; i < cnt; i++)
                {
                    KCPProxy proxy = m_ListKcp[i];
                    proxy.Update(current);
                }
            }
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        public KCPSocket(int bindPort, uint kcpKey, AddressFamily family = AddressFamily.InterNetwork)
        {
            m_AddrFamily = family;
            m_KcpKey     = kcpKey;
            m_ListKcp    = new List <KCPProxy>();

            m_SystemSocket = new Socket(m_AddrFamily, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint ipep = KCPProxy.GetIPEndPointAny(m_AddrFamily, bindPort);

            m_SystemSocket.Bind(ipep);

            bindPort = (m_SystemSocket.LocalEndPoint as IPEndPoint).Port;
            LOG_TAG  = "KCPSocket[" + bindPort + "-" + kcpKey + "]";

            m_IsRunning  = true;
            m_ThreadRecv = new Thread(Thread_Recv)
            {
                IsBackground = true
            };
            m_ThreadRecv.Start();



#if UNITY_EDITOR_WIN
            uint IOC_IN            = 0x80000000;
            uint IOC_VENDOR        = 0x18000000;
            uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
            m_SystemSocket.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);
#endif


#if UNITY_EDITOR
            UnityEditor.EditorApplication.playmodeStateChanged -= OnEditorPlayModeChanged;
            UnityEditor.EditorApplication.playmodeStateChanged += OnEditorPlayModeChanged;
#endif
        }