コード例 #1
0
ファイル: KCPSocket.cs プロジェクト: sodatencent/Snake-update
        //=================================================================================
        #region 构造和析构

        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 = IPUtils.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
        }
コード例 #2
0
ファイル: UdpSocket.cs プロジェクト: penguin-ku/SGF
        //------------------------------------------------------------
        #region 绑定端口函数
        public int Bind(int port = 0)
        {
            Debuger.Log(LOG_TAG, "Bind() port = " + port);
            if (m_SystemSocket == null)
            {
                return(0);
            }

            //如果Bind的端口为0,则会随机分配一个端口
            IPEndPoint ipep = IPUtils.GetIPEndPointAny(m_AddrFamily, port);

            m_SystemSocket.Bind(ipep);
            m_IsActive = true;
            return(SelfPort);
        }
コード例 #3
0
        public void Start()
        {
            Debuger.Log();

            m_IsRunning = true;

            m_SystemSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            m_SystemSocket.Bind(IPUtils.GetIPEndPointAny(AddressFamily.InterNetwork, m_port));

            m_ThreadRecv = new Thread(Thread_Recv)
            {
                IsBackground = true
            };
            m_ThreadRecv.Start();
        }
コード例 #4
0
        private void DoReceiveInThread()
        {
            EndPoint remotePoint = IPUtils.GetIPEndPointAny(AddressFamily.InterNetwork, 0);
            int      cnt         = m_SystemSocket.ReceiveFrom(m_RecvBufferTemp, m_RecvBufferTemp.Length, SocketFlags.None, ref remotePoint);

            if (cnt > 0)
            {
                byte[] dst = new byte[cnt];
                Buffer.BlockCopy(m_RecvBufferTemp, 0, dst, 0, cnt);

                lock (m_RecvBufferQueue)
                {
                    m_RecvBufferQueue.Enqueue(dst);
                }
            }
        }
コード例 #5
0
ファイル: FSPClient.cs プロジェクト: zj831007/SGF
        public bool Connect(string ip, int port)
        {
            if (m_SystemSocket != null)
            {
                Debuger.LogError("无法建立连接,需要先关闭上一次连接!");
                return(false);
            }

            Debuger.Log("建立基础连接, host = {0}, port = {1}", ip, port);
            m_ip   = ip;
            m_port = port;
            m_lastRecvTimestamp = (uint)TimeUtils.GetTotalMillisecondsSince1970();

            try
            {
                m_RemoteEndPoint = IPUtils.GetHostEndPoint(m_ip, m_port);
                if (m_RemoteEndPoint == null)
                {
                    Debuger.LogError("无法将Host解析为IP!");
                    Close();
                    return(false);
                }
                Debuger.Log("HostEndPoint = {0}", m_RemoteEndPoint.ToString());

                //创建Socket
                Debuger.Log("创建Socket, AddressFamily = {0}", m_RemoteEndPoint.AddressFamily);
                m_SystemSocket = new Socket(m_RemoteEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
                m_SystemSocket.Bind(IPUtils.GetIPEndPointAny(AddressFamily.InterNetwork, 0));


                m_IsRunning = true;

                m_ThreadRecv = new Thread(Thread_Recv)
                {
                    IsBackground = true
                };
                m_ThreadRecv.Start();
            }
            catch (Exception e)
            {
                Debuger.LogError(e.Message + e.StackTrace);
                Close();
                return(false);
            }

            return(true);
        }
コード例 #6
0
ファイル: FSPClient.cs プロジェクト: zj831007/SGF
        private void DoReceiveInThread()
        {
            EndPoint remotePoint = IPUtils.GetIPEndPointAny(AddressFamily.InterNetwork, 0);
            int      cnt         = m_SystemSocket.ReceiveFrom(m_RecvBufferTemp, m_RecvBufferTemp.Length, SocketFlags.None, ref remotePoint);

            if (cnt > 0)
            {
                if (!m_RemoteEndPoint.Equals(remotePoint))
                {
                    Debuger.LogError("收到非目标服务器的数据!");
                    return;
                }

                byte[] dst = new byte[cnt];
                Buffer.BlockCopy(m_RecvBufferTemp, 0, dst, 0, cnt);
                m_RecvBufQueue.Push(dst);
            }
        }
コード例 #7
0
ファイル: KCPConnection.cs プロジェクト: zj831007/SGF
        //======================================================================
        //接收数据
        //======================================================================
        protected void DoReceiveInThread()
        {
            //子线程
            this.LogVerbose();

            bool result = false;

            try
            {
                EndPoint remotePoint = IPUtils.GetIPEndPointAny(AddressFamily.InterNetwork, 0);
                int      cnt         = m_socket.ReceiveFrom(m_RecvBufferTemp, m_RecvBufferTemp.Length, SocketFlags.None, ref remotePoint);

                if (cnt > 0)
                {
                    if (!RemoteEndPoint.Equals(remotePoint))
                    {
                        Debuger.LogVerbose("收到非目标服务器的数据!");
                        return;
                    }

                    m_bufferReceive.Attach(m_RecvBufferTemp, cnt);
                    byte[] m_32b = new byte[4];
                    m_bufferReceive.ReadBytes(m_32b, 0, 4);
                    uint sid = BitConverter.ToUInt32(m_32b, 0);

                    if (sid == 0)
                    {
                        //Session过期了
                        HandleServerError((int)NetErrorCode.SessionExpire);
                        return;
                    }

                    byte[] dst = new byte[cnt];
                    Buffer.BlockCopy(m_RecvBufferTemp, 0, dst, 0, cnt);

                    m_RecvBufQueue.Push(dst);
                }
            }
            catch (Exception ex)
            {
                this.LogWarning("接收数据出错:{0}", ex.Message);
                onReceiveError.InvokeSafe(this, (int)NetErrorCode.UnkownError, ex.Message);
            }
        }
コード例 #8
0
        private void DoReceiveInThread()
        {
            EndPoint remotePoint = IPUtils.GetIPEndPointAny(AddressFamily.InterNetwork, 0);
            int      cnt         = m_socket.ReceiveFrom(m_RecvBufferTemp, m_RecvBufferTemp.Length, SocketFlags.None, ref remotePoint);

            if (cnt > 0)
            {
                m_bufferReceive.Attach(m_RecvBufferTemp, cnt);
                byte[] m_32b = new byte[4];
                m_bufferReceive.ReadBytes(m_32b, 0, 4);
                uint sid = BitConverter.ToUInt32(m_32b, 0);

                lock (m_mapSession)
                {
                    KcpSession session = null;

                    if (sid == 0)
                    {
                        //来自Client的第1个包,只能是鉴权包
                        session = new KcpSession(NewSessionID(), m_listener);
                        m_mapSession.Add(session.Id, session);
                    }
                    else
                    {
                        session = m_mapSession[sid];
                    }


                    if (session != null)
                    {
                        session.Active(m_socket, remotePoint as IPEndPoint);
                        session.DoReceiveInGateway(m_RecvBufferTemp, cnt);
                    }
                    else
                    {
                        this.LogWarning("无效的包! sid:{0}", sid);
                        //需要返回给客户端,Session无效了,直接返回一个Sid为0的包
                        //当客户端收到包好,会抛出Session过期事件
                        //因为正常情况下,客户端收到的Session肯定不为0
                        m_socket.SendTo(new byte[4], remotePoint);
                    }
                }
            }
        }
コード例 #9
0
ファイル: UdpSocket.cs プロジェクト: penguin-ku/SGF
        //------------------------------------------------------------
        #region ReceiveFrom和SendTo函数
        public int ReceiveFrom(byte[] buffer, int maxsize, ref IPEndPoint remoteEP)
        {
            int cnt = 0;

            EndPoint ip = null;

            if (!m_EnableBlockOnRecv)
            {
                if (m_SystemSocket.Available <= 0)
                {
                    return(0);
                }
            }


            if (m_AddrFamily == AddressFamily.InterNetwork)
            {
                //如果是IPv4环境,则(与Android的处理一样)
                ip  = IPUtils.GetIPEndPointAny(AddressFamily.InterNetwork, 0);
                cnt = m_SystemSocket.ReceiveFrom(buffer, maxsize, SocketFlags.None, ref ip);

                if (cnt > 0 && remoteEP != null && !remoteEP.Equals(ip))
                {
                    Debuger.LogWarning(LOG_TAG, "ReceiveFrom() 收到一个自来陌生IP:Port(" + ip + ")的数据包!");
                    return(0);
                }
            }
            else
            {
                //如果是IPv6环境,则:
                ip  = remoteEP;
                cnt = m_SystemSocket.ReceiveFrom(buffer, maxsize, SocketFlags.None, ref ip);
            }

            remoteEP = ip as IPEndPoint;

            if (NetDebuger.IsPacketLoss())
            {
                return(0);
            }

            return(cnt);
        }
コード例 #10
0
        private void DoReceiveInThread()
        {
            EndPoint remotePoint = IPUtils.GetIPEndPointAny(AddressFamily.InterNetwork, 0);
            int      cnt         = m_SystemSocket.ReceiveFrom(m_RecvBufferTemp, m_RecvBufferTemp.Length, SocketFlags.None, ref remotePoint);

            if (cnt > 0)
            {
                m_RecvBufferTempReader.Attach(m_RecvBufferTemp, cnt);
                byte[] m_32b = new byte[4];
                m_RecvBufferTempReader.ReadBytes(m_32b, 0, 4);
                uint sid = BitConverter.ToUInt32(m_32b, 0);
                //uint sid = m_RecvBufferTempReader.ReadUInt();


                lock (m_mapSession)
                {
                    ISession session = null;

                    if (sid == 0)
                    {
                        //来自Client的第1个包,只能是鉴权包
                        sid     = SessionID.NewID();
                        session = new KCPSession(sid, HandleSessionSend, m_listener);
                        m_mapSession.Add(session.id, session);
                    }
                    else
                    {
                        session = m_mapSession[sid];
                    }


                    if (session != null)
                    {
                        session.Active(remotePoint as IPEndPoint);
                        session.DoReceiveInGateway(m_RecvBufferTemp, cnt);
                    }
                    else
                    {
                        Debuger.LogWarning("无效的包! sid:{0}", sid);
                    }
                }
            }
        }
コード例 #11
0
        public void Start()
        {
            try
            {
                m_SystemSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                m_SystemSocket.Bind(IPUtils.GetIPEndPointAny(AddressFamily.InterNetwork, m_port));

                m_IsRunning = true;

                m_ThreadRecv = new Thread(Thread_Recv)
                {
                    IsBackground = true
                };
                m_ThreadRecv.Start();
            }
            catch (Exception e)
            {
                Debuger.LogError(e.Message + e.StackTrace);
                Stop();
            }
        }
コード例 #12
0
        private void DoReceiveInThread()
        {
            EndPoint remotePoint = IPUtils.GetIPEndPointAny(AddressFamily.InterNetwork, 0);
            int      cnt         = m_SystemSocket.ReceiveFrom(m_RecvBufferTemp, m_RecvBufferTemp.Length, SocketFlags.None, ref remotePoint);

            if (cnt > 0)
            {
                m_RecvBufferTempReader.Attach(m_RecvBufferTemp, cnt);
                byte[] m_32b = new byte[4];
                m_RecvBufferTempReader.ReadBytes(m_32b, 0, 4);
                uint sid = BitConverter.ToUInt32(m_32b, 0);

                lock (m_mapSession)
                {
                    FSPSession session = null;
                    if (sid == 0)
                    {
                        Debuger.LogError("基于KCP的Sid为0,该包需要被丢掉");
                    }
                    else
                    {
                        session = m_mapSession[sid];
                    }

                    if (session != null)
                    {
                        session.Active(remotePoint as IPEndPoint);
                        session.DoReceiveInGateway(m_RecvBufferTemp, cnt);
                    }
                    else
                    {
                        Debuger.LogWarning("无效的包! sid:{0}", sid);
                    }
                }
            }
        }