Esempio n. 1
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. 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 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. 4
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. 5
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. 6
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. 7
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. 8
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. 9
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. 10
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. 11
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);
        }