Пример #1
0
        private IEnumerator ConnectSocket(EndPoint endPoint)
        {
            _socket      = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            Status       = SocketStatus.Connecting;
            _asyncResult = _socket.BeginConnect(endPoint, AsyncCallback, _socket);
            float curTime   = 0f;
            float totalTime = 10f;

            while (curTime < totalTime && !_asyncResult.IsCompleted)
            {
                curTime += Time.deltaTime;
                yield return(null);
            }
            bool finish = true;

            try
            {
                _socket.EndConnect(_asyncResult);
                _asyncResult = null;
            }
            catch (Exception e)
            {
                SocketTools.Log("SocketClientType:" + SocketClientType + "连接超时" + "\n" + e.ToString());
                finish = false;
            }
            finally
            {
                _asyncResult = null;
            }
            try
            {
                if (finish)
                {
                    Status    = SocketStatus.Connected;
                    _receiver = new SocketReceiver(this);
                    _sender   = new SocketSender(this);
                }
                else
                {
                    Status = SocketStatus.DisConnected;
                }
            }
            catch (Exception e)
            {
                SocketTools.LogError("SocketClientType:" + SocketClientType + "\n" + e.ToString());
                finish = false;
            }
            finally
            {
                if (_callback != null)
                {
                    Action <bool> temp = _callback;
                    _callback = null;
                    temp.Invoke(finish);
                }
            }
            yield return(null);
        }
Пример #2
0
 public void Close()
 {
     lock (lockObj)
     {
         _callback = null;
         if (_connectCoroutine != null)
         {
             this.SocketClientMgr.StopCoroutine(_connectCoroutine);
             _connectCoroutine = null;
         }
         if (_socket != null)
         {
             try
             {
                 if (_asyncResult != null)
                 {
                     _socket.EndConnect(_asyncResult);
                     _asyncResult = null;
                 }
                 if (Status == SocketStatus.Connected)
                 {
                     _socket.Shutdown(SocketShutdown.Both);
                 }
             }
             catch (Exception e)
             {
                 SocketTools.LogError(e.ToString());
             }
             finally
             {
                 try
                 {
                     _socket.Close();
                 }
                 catch (Exception ex)
                 {
                     SocketTools.LogError(ex.ToString());
                 }
                 finally
                 {
                     _socket = null;
                     Status  = SocketStatus.DisConnected;
                 }
             }
         }
         if (_receiver != null)
         {
             _receiver.Close();
             _receiver = null;
         }
         if (_sender != null)
         {
             _sender.Close();
             _sender = null;
         }
     }
 }