Ejemplo n.º 1
0
        /**
         * Releases any resources allocated by this service.  Please note that
         * this method might block as long as there are any sessions managed by
         * this service.
         */
        public void dispose()
        {
            if (m_state != enConnectorState.Connecting && m_state != enConnectorState.Connected)
            {
                //Debuger.Log("状态异常.当前状态:{0}", m_state);
                return;
            }
            lock (m_lock)
            {
                m_state = enConnectorState.NotConnect;
                if (m_timeoutTimers != null)
                {
                    m_timeoutTimers.Release();
                    m_timeoutTimers = null;
                }

                if (m_recvTimers != null)
                {
                    m_recvTimers.Release();
                    m_recvTimers = null;
                }

                if (m_errorTimers.Count != 0)
                {
                    foreach (TimeMgr.Timer t in m_errorTimers)
                    {
                        t.Release();
                    }
                    m_errorTimers.Clear();
                }

                //关闭io线程
                EndIOThread();

                if (m_socket != null)
                {
                    try
                    {
                        if (m_socket.Connected)
                        {
                            m_socket.Shutdown(SocketShutdown.Both);
                        }

                        m_socket.Close();
                    }
                    catch (System.Exception e)
                    {
                        Debuger.LogError(e.Message);
                    }
                    m_socket = null;
                    Debuger.Log("与服务器断开了");
                }
            }
        }
Ejemplo n.º 2
0
        /**
         * Connects to the specified remote address.
         *
         * @return the {@link ConnectFuture} instance which is completed when the
         *         connection attempt initiated by this call succeeds or fails.
         */
        public void connect(string address, int port)
        {
            if (m_state != enConnectorState.NotConnect)
            {
                Debuger.Log("状态异常.当前状态:{0}", m_state);
                return;
            }
            try
            {
                m_state = enConnectorState.Connecting;

                ResetEncode();
                ResetDecode();

                m_timeoutTimers = TimeMgr.instance.AddTimer(5f, OnTimeOut);

                m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IAsyncResult result = m_socket.BeginConnect(address, port, OnConnect, m_socket);
                Debuger.Log("连接服务器:{0}:{1}", address, port);
            }
            catch (SocketException e)
            {
                dispose();
                Debuger.LogError(e.Message);
                if (m_handle != null)
                {
                    m_handle.OnConnectFail();
                }
            }
            catch (Exception e)
            {
                dispose();
                Debuger.LogError(e.Message);
                if (m_handle != null)
                {
                    m_handle.OnConnectFail();
                }
            }
        }
Ejemplo n.º 3
0
        // 进入连接态,创建io线程收发数据
        void OnBegin()
        {
            if (m_state != enConnectorState.Connecting)
            {
                Debuger.LogError("Begin()状态异常.当前状态:{0}", m_state);
                return;
            }
            m_state = enConnectorState.Connected;
            m_socket.SendBufferSize = 2048;

            //开启主线程中收包用的定时器
            m_recvTimers = TimeMgr.instance.AddTimer(0, OnUpdate, -1, -1);

            //开启io线程
            BeginIOThread();

            //通知上层逻辑
            if (m_handle != null)
            {
                m_handle.OnConnectOK();
            }
        }