void Awake() { _allTransform = gameObject.GetComponentsInChildren <Transform>(); EventSystem.current.SetSelectedGameObject(gameObject); _timer = TimeMgr.Instance.CreateTimer(0.1f, -1, CheckOutSide); _timer.Start(); }
/** * 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("与服务器断开了"); } } }
//注意这个不是在主线程中被调用的 void OnConnect(IAsyncResult result) { lock (m_lock) { if (m_timeoutTimers != null) { m_timeoutTimers.Release(); m_timeoutTimers = null; } } Socket socket = (Socket)result.AsyncState; // Windows handles async sockets differently than other platforms, it seems. // If a socket is closed, OnConnectResult() is never called on Windows. // On the mac it does get called, however, and if the socket is used here // then a null exception gets thrown because the socket is not usable by this point. if (socket == null) { Util.SafeLogError("sock == null"); return; } if (m_socket == null || (socket != m_socket)) { Util.SafeLogError("mSocket == null || (sock != mSocket)"); return; } try { m_socket.EndConnect(result); } catch (System.Net.Sockets.SocketException ex) { Util.SafeLogError("EndConnect fail {0} {1}", ex.SocketErrorCode, ex.Message); Util.SafeDo(OnTimeOut); return; } //主线中再进入状态切换 Util.SafeDo(OnBegin); }
void ShowNext() { if (m_index >= m_showObjs.Count) { m_timer = null; return; } if (m_showObjs[m_index].m_obj == null) { return; } m_showObjs[m_index].m_obj.SetActive(true); m_index++; m_timer = null; if (m_index < m_showObjs.Count) { m_timer = TimeMgr.instance.AddTimer(m_showObjs[m_index - 1].m_delay, ShowNext); } }
/** * 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(); } } }
// 进入连接态,创建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(); } }