Exemple #1
0
 /// <summary>
 /// 连接服务器回调
 /// </summary>
 private void OnUdpConnected(uint conv, NErrorCode code)
 {
     GameLog.Log("OnUdpConnected,conv:{0}, code:{1}", conv, code);
     if (code == NErrorCode.SUCCESS)
     {
         State = ConnectState.Connected;
         if (m_thread == null)
         {
             // 新建一个线程
             GameLog.Log("[OnUdpConnected]新建一个线程");
             m_thread = new Thread(ThreadProcess);
             m_thread.Start();
         }
         else
         {
             // 恢复线程
             GameLog.Log("[OnUdpConnected]恢复线程");
             m_threadResetEvent.Set();
         }
         m_timerTick = FrameTimer.Create(Tick, 1, -1);
         InitKCP(conv);
         if (OnConnected != null)
         {
             OnConnected();
         }
     }
     else
     {
         ConnectFailed(code);
     }
 }
    // 重连接口
    public void Reconnect(Action <uint, NErrorCode> callback)
    {
        m_onConnected = callback;
        if (m_timerConn != null)
        {
            GameLog.LogError("重连的时候,上次连接还没完成,逻辑有问题哦");
            return;
        }

        m_timerConn = FrameTimer.Create(OnTimerCheckConn, 1, -1);
        SendReconnectRequest();
    }
Exemple #3
0
 // udp重连回调
 private void OnUdpReconnected(uint conv, NErrorCode code)
 {
     if (code == NErrorCode.SUCCESS)
     {
         State = ConnectState.Connected;
         // 恢复线程
         m_threadResetEvent.Set();
         m_timerTick = FrameTimer.Create(Tick, 1, -1);
     }
     else
     {
         if (code == NErrorCode.ERROR && m_udpClient.DisconnectType == KDisconnectType.FromChange)
         {
             Reconnect();
         }
         else
         {
             ConnectFailed(code);
         }
     }
 }
    public void Connect(string ipOrName, int port, Action <uint, NErrorCode> callback)
    {
        m_onConnected = callback;
        m_host        = ipOrName;
        m_port        = port;

        // UDP端口绑定
        IPAddress[] addr = Dns.GetHostAddresses(ipOrName);
        if (addr == null || addr.Length == 0)
        {
            m_onConnected(0, NErrorCode.ERROR);
            return;
        }

        State = ConnectState.Connecting;
        m_udp = new UdpClient(addr[0].AddressFamily);
        m_udp.Connect(addr[0], port);

        m_timerConn = FrameTimer.Create(OnTimerCheckConn, 1, -1);
        // 发送连接请求
        SendConnectRequest();
        return;
    }