Пример #1
0
        /// <summary>
        /// 发起重新连接
        /// </summary>
        public void Reconnect()
        {
            if (mIsConnected)
            {
                Disconnect("reconnect");
            }

            mTcpClient = new TcpClient
            {
                NoDelay           = true,
                ReceiveBufferSize = mRecvBuffSize,
                SendBufferSize    = mSendBuffSize
            };

            try
            {
                mTcpClient.BeginConnect(IPAddress.Parse(mIP), mPort, new AsyncCallback(OnConnectCallback), mTcpClient);

                Invoke("ConnectTimeOutCheck", 3);
            }
            catch (Exception ex)
            {
                if (IsInvoking("ConnectTimeOutCheck"))
                {
                    CancelInvoke("ConnectTimeOutCheck");
                }

                if (mOnConnect != null)
                {
                    mOnConnect.Invoke(false, ex.ToString());
                }
            }
        }
Пример #2
0
        private void OnConnect()
        {
            // Only when the TCP connection is successful, we connect the UDP
            _udpNetClient.Connect(_lastHost, _lastPort, _tcpNetClient.GetConnectedPort());

            IsConnected = true;

            _udpUpdateManager.StartUdpUpdates();

            // Invoke callback if it exists
            OnConnectEvent?.Invoke();
        }
Пример #3
0
        void OnConnected(ConnectRequest request)
        {
            if (request.Error != null)
            {
                //
                Console.WriteLine($"Connect error!{request.Error}");

                request.Dispose();
                OnConnectEvent?.Invoke(false);
            }
            else
            {
                _connected            = true;
                _channel.OnReadEvent += OnReadCB;
                //_channel.OnWriteEvent +=
                OnConnectEvent?.Invoke(true);
            }
        }
Пример #4
0
        /// <summary>
        /// 新建连接
        /// </summary>
        /// <param name="ip">连接的服务器IP</param>
        /// <param name="port">连接的端口号</param>
        /// <param name="connEvent">连接完成后回调</param>
        /// <param name="disconnEvent">断开连接发生时回调</param>
        /// <param name="recvEvent">接收到消息包时回调</param>
        public void Connect(string ip, int port, OnConnectEvent connEvent, OnDisconnectEvent disconnEvent, OnRecvEvent recvEvent)
        {
            if (mIsConnected)
            {
                Disconnect("reconnect");
            }

            mOnConnect     = connEvent;
            mOnDisConnect  = disconnEvent;
            mOnRecvPackage = recvEvent;
            mIP            = ip;
            mPort          = port;

            mTcpClient = new TcpClient
            {
                NoDelay           = true,
                ReceiveBufferSize = mRecvBuffSize,
                SendBufferSize    = mSendBuffSize
            };

            mIsConnected = false;

            try
            {
                mTcpClient.BeginConnect(IPAddress.Parse(ip), port, new AsyncCallback(OnConnectCallback), mTcpClient);

                Invoke("ConnectTimeOutCheck", 3);
            }
            catch (Exception ex)
            {
                if (IsInvoking("ConnectTimeOutCheck"))
                {
                    CancelInvoke("ConnectTimeOutCheck");
                }

                if (mOnConnect != null)
                {
                    mOnConnect.Invoke(false, ex.ToString());
                }
            }
        }
        public void HandleEvents()
        {
            lock (netEventQueue)
            {
                while (netEventQueue.Count != 0)
                {
                    NetEvent netEvent = netEventQueue.Dequeue();

                    switch (netEvent.type)
                    {
                    case ENet.EventType.None:
                        break;

                    case ENet.EventType.Connect:
                    {
                        isConnected = true;
                        OnConnect();
                        OnConnectEvent?.Invoke();
                    }
                    break;

                    case ENet.EventType.Receive:
                    {
                        HandleAnyPacket(netEvent.packet);
                    }
                    break;

                    case ENet.EventType.Disconnect:
                    {
                        isConnected = false;
                        OnDisconnect();
                        OnDisconnectEvent?.Invoke();
                    }
                    break;
                    }
                }
            }
        }
Пример #6
0
 /// <summary>
 /// Internal pass through for connected events
 /// </summary>
 private static void OnInternalConnected()
 {
     OnConnected.Invoke();
 }
Пример #7
0
    protected override void OnConnect()
    {
        Debug.Log("Connected");

        OnConnectEvent.Invoke();
    }