Exemplo n.º 1
0
        //连接
        public void SocketConnect()
        {
            if (isConnected || m_ConnectState == ConnectStateType.Connecting)
            {
                ZLogger.Info("Socket已经连接上了,别再重复进行连接");
                return;
            }
            string        tempStrNewIp      = "";
            AddressFamily tempAddressFamily = AddressFamily.InterNetwork;

            //这里准备以后引入一个解析IP地址的库,从而可以ipv4,还是ipv6 no edit
            ZLogger.Info("Socket AddressFaminly:{0}  原始IP:{1},  结果IP:{2},  端口:{3}", tempAddressFamily, m_IP, tempStrNewIp, m_Port);

            m_ClientSocket                   = new Socket(tempAddressFamily, SocketType.Stream, ProtocolType.Tcp);
            m_ClientSocket.SendTimeout       = 1000;                   //指定同步 Send 调用将超时的时间长度
            m_ClientSocket.ReceiveTimeout    = 5000;                   //指定同步 Receive 调用将超时的时间长度
            m_ClientSocket.NoDelay           = true;                   //指定流 Socket 是否正在使用 Nagle 算法
            m_ClientSocket.SendBufferSize    = 1024 * 8;               //指定 Socket 发送缓冲区的大小
            m_ClientSocket.ReceiveBufferSize = NetConst.TEMP_MSG_SIZE; //获取或设置此 ServicePoint 使用的套接字的接收缓冲区的大小
            m_ClientSocket.Blocking          = true;                   //指示 Socket 是否处于阻止模式

            m_ConnectState = ConnectStateType.Connecting;

            try
            {
                m_ClientSocket.BeginConnect(tempStrNewIp, m_Port, new AsyncCallback(OnConnectCB), m_ClientSocket);
            }
            catch (Exception e)
            {
                //设置失败次数++
                ZLogger.Info("连接不通:{0}  ==>  {1}", e.Message, e.ToString());
                m_ConnectState = ConnectStateType.ConnectFail;
                OnDisconnect(NetResultType.ConnectTimeOut, "连接游戏服务器失败!(-1)");
            }
        }
Exemplo n.º 2
0
 //连接上服务器回调
 private void OnConnectCB(IAsyncResult asr)
 {
     try
     {
         Socket tempClientSocket = (Socket)asr.AsyncState;
         if (tempClientSocket.Connected)
         {
             tempClientSocket.EndConnect(asr);
             //重置失败次数=0
             tempClientSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
             m_ConnectState       = ConnectStateType.Connected;
             m_DispatchConnectBoo = true;
             //进行广播
             ZLogger.Info("socket 连接成功 net Connected ip={0},prot={1}", m_IP, m_Port);
         }
         else
         {
             //设置连接失败次数++
             m_ConnectState = ConnectStateType.ConnectFail;
             OnDisconnect(NetResultType.ConnectTimeOut, "连接超时!(404)");
         }
     }
     catch (Exception e)
     {
         //设置连接失败次数++
         m_ConnectState = ConnectStateType.ConnectFail;
         OnDisconnect(NetResultType.Exception, "连接超时:" + e.Message);
     }
 }
Exemplo n.º 3
0
 //发送连接请求
 public void SendConnect(string canHost, int canPort)
 {
     if (UtilTool.NetAvailable)
     {
         SetIpAndPort(canHost, canPort);
         SocketConnect();
     }
     else
     {
         m_ConnectState = ConnectStateType.ConnectFail;
         OnDisconnect(NetResultType.ConnectTimeOut, "连接游戏服务器失败!(404)");
     }
 }
Exemplo n.º 4
0
 public void Update()
 {
     if (!UtilTool.NetAvailable && m_ClientSocket != null)
     {
         CloseSocket();
         m_ConnectState = ConnectStateType.OffLine;
     }
     if (AppConst.USE_LUA_BOO)
     {
         UpdateForLua();
     }
     else
     {
         UpdateForC();
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// 主动断开Socket
 /// </summary>
 public void DoDisconnect()
 {
     m_ConnectState = ConnectStateType.Disconnect;
     OnDisconnect(NetResultType.Disconnect);
 }