Esempio n. 1
0
        public void initClient(string host, int port, Action callback = null)
        {
            this.timeoutEvent.Reset();
            this.eventManager = new EventManager();
            this.NetWorkChanged(NetWorkState.CONNECTING);
            IPAddress     address     = (IPAddress)null;
            string        newServerIp = "";
            AddressFamily mIPType     = AddressFamily.InterNetwork;

            IPv6SupportMidleware.getIPType(host, port.ToString(), out newServerIp, out mIPType);
            if (!string.IsNullOrEmpty(newServerIp))
            {
                host = newServerIp;
            }
            this.socket = new Socket(mIPType, SocketType.Stream, ProtocolType.Tcp);
            if (!IPAddress.TryParse(host, out address))
            {
                Debug.LogWarning((object)"**********8IP地址格式错误***********");
            }
            if (address == null)
            {
                throw new Exception("can not parse host : " + host);
            }
            this.socket.BeginConnect((EndPoint) new IPEndPoint(address, port), (AsyncCallback)(result =>
            {
                try
                {
                    this.socket.EndConnect(result);
                    this.protocol = new Protocol(this, this.socket);
                    this.NetWorkChanged(NetWorkState.CONNECTED);
                    if (callback == null)
                    {
                        return;
                    }
                    callback();
                }
                catch (SocketException ex)
                {
                    Console.WriteLine((object)ex);
                    if (this.netWorkState != NetWorkState.TIMEOUT)
                    {
                        this.NetWorkChanged(NetWorkState.ERROR);
                    }
                    this.Dispose();
                }
                finally
                {
                    this.timeoutEvent.Set();
                }
            }), (object)this.socket);
            if (!this.timeoutEvent.WaitOne(this.timeoutMSec, false) || this.netWorkState == NetWorkState.CONNECTED || this.netWorkState == NetWorkState.ERROR)
            {
                return;
            }
            this.NetWorkChanged(NetWorkState.TIMEOUT);
            this.Dispose();
        }
Esempio n. 2
0
 public static void getIPType(string serverIp, string serverPorts, out string newServerIp, out AddressFamily mIPType)
 {
     mIPType     = AddressFamily.InterNetwork;
     newServerIp = serverIp;
     try
     {
         string ipv6 = IPv6SupportMidleware.GetIPv6(serverIp, serverPorts);
         if (string.IsNullOrEmpty(ipv6))
         {
             return;
         }
         string[] strArray = Regex.Split(ipv6, "&&");
         if (strArray == null || strArray.Length < 2 || !(strArray[1] == "ipv6"))
         {
             return;
         }
         newServerIp = strArray[0];
         mIPType     = AddressFamily.InterNetworkV6;
     }
     catch (Exception ex)
     {
         Debug.Log((object)("GetIPv6 error:" + (object)ex));
     }
 }