Esempio n. 1
0
        public void Close()
        {
            MyLogger.Log(LOG_TAG_MAIN, "Close()");

            isRunning = false;

            if (mGame != null)
            {
                mGame.Dispose();
                mGame = null;
            }

            if (mRoom != null)
            {
                mRoom.Dispose();
                mRoom    = null;
                mRoomRPC = null;
            }

            if (mGameSocket != null)
            {
                mGameSocket.Dispose();
                mGameSocket = null;
            }

            if (mThreadMain != null)
            {
                mThreadMain.Interrupt();
                mThreadMain = null;
            }

            DelAllSession();
        }
Esempio n. 2
0
    public bool Connect(string host, int port)
    {
        if (m_Socket != null)
        {
            return(false);
        }

        m_Host = host;
        m_Port = port;

        try
        {
            m_HostEndPoint = IPUtils.GetHostEndPoint(m_Host, m_Port);
            if (m_HostEndPoint == null)
            {
                Close();
                return(false);
            }

            m_IsRunning = true;
            m_Socket    = new KCPSocket(0, 1);
            m_Socket.AddReceiveListener(m_HostEndPoint, OnReceive);
        }
        catch (Exception e)
        {
            Close();
            return(false);
        }
        return(true);
    }
Esempio n. 3
0
        public void Close()
        {
            Debuger.Log(LOG_TAG_MAIN, "Close()");

            m_IsRunning = false;

            if (m_Game != null)
            {
                m_Game.Dispose();
                m_Game = null;
            }

            if (m_Room != null)
            {
                m_Room.Dispose();
                m_Room    = null;
                m_RoomRPC = null;
            }

            if (m_GameSocket != null)
            {
                m_GameSocket.Dispose();
                m_GameSocket = null;
            }

            if (m_ThreadMain != null)
            {
                m_ThreadMain.Interrupt();
                m_ThreadMain = null;
            }

            DelAllSession();
        }
Esempio n. 4
0
    public void Close()
    {
        m_bIsRunning = false;

        if (m_aGame != null)
        {
            m_aGame.Dispose();
            m_aGame = null;
        }

        if (m_aRoom != null)
        {
            m_aRoom.Dispose();
            m_aRoom = null;
        }

        if (m_aGameSocket != null)
        {
            m_aGameSocket.Dispose();
            m_aGameSocket = null;
        }

        if (m_aThreadMain != null)
        {
            m_aThreadMain.Interrupt();
            m_aThreadMain = null;
        }

        DelAllSession();
    }
Esempio n. 5
0
    public bool Start(int port)
    {
        if (m_bIsRunning)
        {
            return(false);
        }

        DelAllSession();
        try
        {
            m_nLogicLastTicks  = DateTime.Now.Ticks;
            m_RealTicksAtStart = m_nLogicLastTicks;

            m_aGameSocket = new KCPSocket(0, 1);
            m_aGameSocket.AddReceiveListener(OnReceive);
            m_bIsRunning = true;

            m_aRoom = new FSPRoom();
            m_aRoom.Create();

            m_aThreadMain = new Thread(Thread_Main)
            {
                IsBackground = true
            };
            m_aThreadMain.Start();
        }
        catch (Exception e)
        {
            Close();
            return(false);
        }

        return(true);
    }
Esempio n. 6
0
 public virtual void Close()
 {
     if (m_Socket != null)
     {
         m_Socket.CloseKcp(EndPoint);
         m_Socket = null;
     }
 }
Esempio n. 7
0
        public void Init(IPEndPoint localPoint, IPEndPoint remotePoint)
        {
            LOG_TAG = "KCPPlayer[" + remotePoint + "]";

            m_RemotePoint = remotePoint;

            m_Socket = new KCPSocket(localPoint.Port, 1, AddressFamily.InterNetwork);
            //m_Socket.AddReceiveListener(KCPProxy.IPEP_Any, OnReceiveAny);
            m_Socket.AddReceiveListener(remotePoint, OnReceive);

            NetworkDebuger.Log(LOG_TAG, " 连接.");
        }
Esempio n. 8
0
            //public void Init(string name, IPAddress ipa, int localPort, int remotePort)
            //{
            //    m_Name = name;
            //    LOG_TAG = "KCPPlayer[" + m_Name + "]";

            //    m_RemotePoint = new IPEndPoint(ipa, remotePort);

            //    m_Socket = new KCPSocket(localPort, 1, AddressFamily.InterNetwork);
            //    //m_Socket.AddReceiveListener(KCPProxy.IPEP_Any, OnReceiveAny);
            //    m_Socket.AddReceiveListener(m_RemotePoint, OnReceive);

            //    NetworkDebuger.Log("Init()", "name:{0}, localPort:{1}, remotePort:{2}", name, localPort, remotePort);
            //}
            public void Init(IPEndPoint localPoint, IPEndPoint remotePoint)
            {
                m_Name  = "Player";
                LOG_TAG = "KCPPlayer[" + m_Name + "]";

                m_RemotePoint = remotePoint;

                m_Socket = new KCPSocket(localPoint.Port, 1, AddressFamily.InterNetwork);
                //m_Socket.AddReceiveListener(KCPProxy.IPEP_Any, OnReceiveAny);
                m_Socket.AddReceiveListener(m_RemotePoint, OnReceive);

                NetworkDebuger.Log("Init()", "name:{0}, localPort:{1}, remotePort:{2}", m_Name, localPoint, remotePoint);
            }
Esempio n. 9
0
    private void Disconnect()
    {
        m_IsRunning = false;

        if (m_Socket != null)
        {
            m_Socket.Dispose();
            m_Socket = null;
        }


        m_HostEndPoint = null;
    }
Esempio n. 10
0
        //=================================================================================
        #region 构造和析构

        public RPCService(int port = 0)
        {
            m_MapRPCBind = new Dictionary <string, RPCMethodHelper> ();

            //创建Socket
            m_Socket = new KCPSocket(port, 1);
            m_Socket.AddReceiveListener(OnReceive);
            m_Socket.EnableBroadcast = true;
            m_IsRunning = true;

            port    = m_Socket.SelfPort;
            LOG_TAG = LOG_TAG + "[" + port + "]";
            Debuger.Log(LOG_TAG, "RPCSocket() port:{0}", port);
        }
Esempio n. 11
0
        public virtual void Dispose()
        {
            Debuger.Log(LOG_TAG, "Dispose()");

            m_IsRunning = false;

            if (m_Socket != null)
            {
                m_Socket.Dispose();
                m_Socket = null;
            }

            m_MapRPCBind.Clear();
        }
Esempio n. 12
0
        void Disconnect()
        {
            Debuger.Log(LOG_TAG_MAIN, "Disconnect()");

            // 断开就不跑了
            m_IsRunning = false;

            if (m_Socket != null)
            {
                m_Socket.Dispose();
                m_Socket = null;
            }
            m_HostEndPoint = null;
        }
Esempio n. 13
0
        //------------------------------------------------------------

        #region 启动
        public bool Start(int port)
        {
            if (m_IsRunning)
            {
                Debuger.LogWarning(LOG_TAG_MAIN, "Start() 不能重复创建启动Server!");
                return(false);
            }
            Debuger.Log(LOG_TAG_MAIN, "Start()  port = {0}", port);

            DelAllSession();

            try
            {
                m_LogicLastTicks   = DateTime.Now.Ticks;
                m_RealTicksAtStart = m_LogicLastTicks;

                //创建Game Socket
                m_GameSocket = new KCPSocket(0, 1);
                m_GameSocket.AddReceiveListener(OnReceive);
                m_IsRunning = true;

                //一个简单通用的房间模块
                m_Room = new FSPRoom();
                m_Room.Create();
                m_RoomRPC = m_Room;

                //创建线程
                Debuger.Log(LOG_TAG_MAIN, "Start()  创建服务器线程");
                m_ThreadMain = new Thread(Thread_Main)
                {
                    IsBackground = true
                };
                m_ThreadMain.Start();
            }
            catch (Exception e)
            {
                Debuger.LogError(LOG_TAG_MAIN, "Start() " + e.Message);
                Close();
                return(false);
            }

            //当用户直接用UnityEditor上的停止按钮退出游戏时,会来不及走完整的析构流程。
            //这里做一个监听保护
#if UNITY_EDITOR
            UnityEditor.EditorApplication.playmodeStateChanged -= OnEditorPlayModeChanged;
            UnityEditor.EditorApplication.playmodeStateChanged += OnEditorPlayModeChanged;
#endif
            return(true);
        }
Esempio n. 14
0
        //------------------------------------------------------------

        #region start server
        public bool Start(int port)
        {
            if (isRunning)
            {
                MyLogger.LogWarning(LOG_TAG_MAIN, "Start()", "cannot start duplicated Server!");
                return(false);
            }
            MyLogger.Log(LOG_TAG_MAIN, "Start()  port = {0}", port.ToString());

            DelAllSession();

            try
            {
                mLogicLastTicks   = DateTime.Now.Ticks;
                mRealTicksAtStart = mLogicLastTicks;

                //create Game Socket
                mGameSocket = new KCPSocket(0, 1);
                mGameSocket.AddReceiveListener(OnReceive);
                isRunning = true;

                //create game room
                mRoom = new FSPRoom();
                mRoom.Create();
                mRoomRPC = mRoom;

                //create  thread
                MyLogger.Log(LOG_TAG_MAIN, "Start()  create server thead");
                mThreadMain = new Thread(Thread_Main)
                {
                    IsBackground = true
                };
                mThreadMain.Start();
            }
            catch (Exception e)
            {
                MyLogger.LogError(LOG_TAG_MAIN, "Start() ", e.Message);
                Close();
                return(false);
            }

            //when user exit the game using stop button in UnityEditor, cannot release all resource in time
            //add listener here
#if UNITY_EDITOR
            UnityEditor.EditorApplication.playmodeStateChanged -= OnEditorPlayModeChanged;
            UnityEditor.EditorApplication.playmodeStateChanged += OnEditorPlayModeChanged;
#endif
            return(true);
        }
Esempio n. 15
0
            public void Init(string name, int localPort, int remotePort)
            {
                m_Name  = name;
                LOG_TAG = "KCPPlayer[" + m_Name + "]";

                IPAddress ipa = IPAddress.Parse(UnityEngine.Network.player.ipAddress);

                m_RemotePoint = new IPEndPoint(ipa, remotePort);

                m_Socket = new KCPSocket(localPort, 1, AddressFamily.InterNetwork);
                m_Socket.AddReceiveListener(KCPProxy.IPEP_Any, OnReceiveAny);
                m_Socket.AddReceiveListener(m_RemotePoint, OnReceive);

                NetworkDebuger.Log("Init() name:{0}, localPort:{1}, remotePort:{2}", name, localPort, remotePort);
            }
Esempio n. 16
0
        private void Disconnect()
        {
            MyLogger.Log(LOG_TAG_MAIN, "Disconnect()");



            isRunning = false;

            if (m_Socket != null)
            {
                m_Socket.Dispose();
                m_Socket = null;
            }


            m_HostEndPoint = null;
        }
Esempio n. 17
0
        public bool Connect(string host, int port)
        {
            if (m_Socket != null)
            {
                Debuger.LogError(LOG_TAG_MAIN, "Connect() 无法建立连接,需要先关闭上一次连接!");
                return(false);
            }

            Debuger.Log(LOG_TAG_MAIN, "Connect() 建立基础连接, host = {0}, port = {1}", (object)host, port);

            m_Host = host;
            m_Port = port;

            try
            {
                //获取Host对应的IPEndPoint
                Debuger.Log(LOG_TAG_MAIN, "Connect() 获取Host对应的IPEndPoint");
                m_HostEndPoint = IPUtils.GetHostEndPoint(m_Host, m_Port);
                if (m_HostEndPoint == null)
                {
                    Debuger.LogError(LOG_TAG_MAIN, "Connect() 无法将Host解析为IP!");
                    Close();
                    return(false);
                }
                Debuger.Log(LOG_TAG_MAIN, "Connect() HostEndPoint = {0}", m_HostEndPoint.ToString());

                m_IsRunning = true;

                //创建Socket
                Debuger.Log(LOG_TAG_MAIN, "Connect() 创建UdpSocket, AddressFamily = {0}", m_HostEndPoint.AddressFamily);
                m_Socket = new KCPSocket(0, 1);
                //m_Socket.Connect(m_HostEndPoint);
                m_Socket.AddReceiveListener(m_HostEndPoint, OnReceive);
            }
            catch (Exception e)
            {
                Debuger.LogError(LOG_TAG_MAIN, "Connect() " + e.Message + e.StackTrace);
                Close();
                return(false);
            }


            return(true);
        }
Esempio n. 18
0
        public bool Connect(string host, int port)
        {
            if (m_Socket != null)
            {
                MyLogger.LogError(LOG_TAG_MAIN, "Connect()", " cannot build connection, please shut down last connection!");
                return(false);
            }

            MyLogger.Log(LOG_TAG_MAIN, "Connect() start initial connection, host = {0}, port = {1}", port.ToString(), (object)host);

            m_Host = host;
            m_Port = port;

            try
            {
                //get Host's IPEndPoint
                MyLogger.Log(LOG_TAG_MAIN, "Connect() get Host's IPEndPoint");
                m_HostEndPoint = IPUtils.GetHostEndPoint(m_Host, m_Port);
                if (m_HostEndPoint == null)
                {
                    MyLogger.LogError(LOG_TAG_MAIN, "Connect()", "cannot convert host name to IP!");
                    Close();
                    return(false);
                }
                MyLogger.Log(LOG_TAG_MAIN, "Connect() HostEndPoint = {0}", m_HostEndPoint.ToString());

                isRunning = true;

                //create a Socket
                MyLogger.Log(LOG_TAG_MAIN, "Connect() create UdpSocket, AddressFamily = {0}", "" + m_HostEndPoint.AddressFamily);
                m_Socket = new KCPSocket(0, 1);
                //m_Socket.Connect(m_HostEndPoint);
                m_Socket.AddReceiveListener(m_HostEndPoint, OnReceive);
            }
            catch (Exception e)
            {
                MyLogger.LogError(LOG_TAG_MAIN, "Connect() ", e.Message + e.StackTrace);
                Close();
                return(false);
            }


            return(true);
        }
Esempio n. 19
0
        public bool Connect(string host, int port)
        {
            if (m_Socket != null)
            {
                Debuger.LogError(LOG_TAG_MAIN, "Connect() 无法建立连接,需要先关闭上一次连接!");
                return(false);
            }

            Debuger.Log(LOG_TAG_MAIN, "Connect() 尝试建立基础连接, host = {0}, port = {1}", host, port);

            try
            {
                //获取Host对应的IPEndPoint
                Debuger.Log(LOG_TAG_MAIN, "Connect() 获取Host对应的IPEndPoint");
                m_HostEndPoint = IPUtils.GetHostEndPoint(host, port);
                if (m_HostEndPoint == null)
                {
                    Debuger.LogError(LOG_TAG_MAIN, "Connect() 无法将Host解析为IP!");
                    Close();
                    return(false);
                }
                Debuger.Log(LOG_TAG_MAIN, "Connect() HostEndPoint = {0}", m_HostEndPoint.ToString());

                // 创建socket
                m_Socket = new KCPSocket(0, 1);
                m_Socket.Connect(m_HostEndPoint);
                m_Socket.AddReceiveListener(m_HostEndPoint, OnReceive);
            }
            catch (System.Exception e)
            {
                Debuger.LogError(LOG_TAG_MAIN, "Connect() " + e.Message + e.StackTrace);
                Close();
                return(false);
            }

            return(true);
        }
Esempio n. 20
0
        static void TestSocket()
        {
            UInt32 conv       = 0x12345678;
            var    counter    = 1;
            var    originText = "a quick brown fox jumps over the lazy dog";
            var    rawbytes   = Encoding.UTF8.GetBytes(String.Format("{0} {1}", originText, counter));

            KCPSocket sock = new KCPSocket();

            sock.SetHandler((byte[] data, int size) =>
            {
                Console.WriteLine(Encoding.UTF8.GetString(data, 0, size));

                Thread.Sleep(500);
                rawbytes = Encoding.UTF8.GetBytes(String.Format("{0} {1}", originText, ++counter));
                sock.Send(rawbytes, 0, rawbytes.Length);
            });

            sock.Connect(conv, "127.0.0.1", 9527);
            sock.StartRead();
            sock.Send(rawbytes, 0, rawbytes.Length);

            while (true)
            {
                Thread.Sleep(100);
                try
                {
                    sock.Update(Utils.iclock());
                }
                catch (Exception ex)
                {
                    sock.Close();
                    Console.WriteLine("Exception: {0}", ex);
                    break;
                }
            }
        }
Esempio n. 21
0
 public void Dispose()
 {
     m_Socket.Dispose();
     m_Socket = null;
 }
Esempio n. 22
0
 public FSPSession(uint sid, KCPSocket socket)
 {
     m_aSocket = socket;
     m_nSid    = sid;
 }
Esempio n. 23
0
 public FSPSession(uint sid, KCPSocket socket)
 {
     m_Sid    = sid;
     m_Socket = socket;
     LOG_TAG  = "FSPSession<" + m_Sid.ToString("d4") + ">";
 }