public bool Connect(string host, int port, int connectTimeout) { var addreses = DNS.Lookup(host); if (addreses.Length == 0) { EB.Debug.LogError("failed to lookup {0}", host); _error = NetworkFailure.DNSLookupFailed; return(false); } for (int i = 0; i < addreses.Length; ++i) { var ip = addreses[i]; if (TryConnect(host, ip, port, connectTimeout)) { if (i > 0) { DNS.StoreLast(host, ip); } return(true); } } DNS.Clear(host); return(false); }
bool TryConnect(string host, System.Net.IPAddress ip, int port, int connectTimeout) { //EB.Debug.Log("Try connect {0}:{1}", ip, port); DestroyClient(); _handle = _NSTcpClientCreate(_secure); var timeout = System.DateTime.Now + System.TimeSpan.FromMilliseconds(connectTimeout); _NSTcpClientConnect(_handle, ip.ToString(), port); while (!Connected && System.DateTime.Now < timeout) { System.Threading.Thread.Sleep(100); } if (!Connected) { EB.Debug.LogError("Failed to connect to {0}:{1}", ip, port); _error = NetworkFailure.CannotConnectToHost; return(false); } //OnConnected(); //EB.Debug.Log("Connected to {0}:{1}", ip, port); return(Connected); }
bool TryConnect(string hostname, System.Net.IPAddress ip, int port, int connectTimeout) { //EB.Debug.Log("Try connect {0}:{1}", ip, port); if (_client.Client.AddressFamily != ip.AddressFamily) { _client.Close(); _client = new System.Net.Sockets.TcpClient(ip.AddressFamily); _client.NoDelay = true; } var async = _client.BeginConnect(ip, port, null, null); if (!async.AsyncWaitHandle.WaitOne(System.TimeSpan.FromMilliseconds(connectTimeout))) { _error = NetworkFailure.TimedOut; return(false); } if (!async.IsCompleted) { _error = NetworkFailure.TimedOut; return(false); } _client.EndConnect(async); if (_client.Connected == false) { EB.Debug.LogError("Failed to connect to {0}:{1}", ip, port); _error = NetworkFailure.CannotConnectToHost; return(false); } OnConnected(); _net = _client.GetStream(); _stream = _net; if (_secure) { //EB.Debug.Log("Doing ssl connect {0}:{1}", ip, port); _ssl = new System.Net.Security.SslStream(_stream, true, RemoteCertificateValidationCallback, null); try { _ssl.AuthenticateAsClient(hostname); } catch (System.Exception e) { EB.Debug.LogError("Failed to authenticate: " + e); return(false); } _stream = _ssl; } //EB.Debug.Log("Connected to {0}:{1}", ip, port); LastTime = System.DateTime.Now; return(true); }
public bool CalledExternalParty(IFailure failure) { return(failure switch { NetworkFailure _ => true, ServiceNotAvailableFailure _ => true, InvalidRequestFailure _ => true, WarrantyServiceInternalErrorFailure _ => true, SaveExternalPartyResponseFailure _ => true, SaveWarrantyProofFailure _ => true, ResponseValidationFailure _ => true, ResponseConversionFailure _ => true, // save warranty proof SaveWarrantyCaseVerificationFailure _ => throw new InvalidOperationException(), _ => false });
bool TryConnect(string hostname, System.Net.IPAddress ip, int port, int connectTimeout) { //EB.Debug.Log("Try connect {0}:{1}", ip, port); if (_client.Client.AddressFamily != ip.AddressFamily) { _client.Close(); _client = new System.Net.Sockets.TcpClient(ip.AddressFamily); _client.NoDelay = true; } var async = _client.BeginConnect(ip, port, null, null); if (!async.AsyncWaitHandle.WaitOne(System.TimeSpan.FromMilliseconds(connectTimeout))) { _error = NetworkFailure.TimedOut; return(false); } if (!async.IsCompleted) { _error = NetworkFailure.TimedOut; return(false); } _client.EndConnect(async); if (_client.Connected == false) { EB.Debug.LogError("Failed to connect to {0}:{1}", ip, port); _error = NetworkFailure.CannotConnectToHost; return(false); } _net = _client.GetStream(); _stream = _net; OnConnected(); if (_secure) { //EB.Debug.Log("Doing ssl connect {0}:{1}", ip, port); try { var random = new System.Random(); var bytes = new byte[20]; random.NextBytes(bytes); #if BCWP71 var secureRandom = new SecureRandom(bytes); #else var secureRandom = SecureRandom.GetInstance("SHA1PRNG", false); #endif secureRandom.SetSeed(bytes); _auth = new MyTlsAuthentication(); _tlsClient = new MyTlsClient(_auth); #if BCWP71 _handler = new TlsProtocolHandler(_net, secureRandom); #else _handler = new TlsClientProtocol(_net, secureRandom); #endif _handler.Connect(_tlsClient); _stream = _handler.Stream; if (_stream == null) { EB.Debug.LogError("stream is null"); _error = NetworkFailure.SecureConnectionFailed; return(false); } } catch (System.Exception ex) { EB.Debug.LogError("ssl connect failed {0}\n{1}", ex.Message, ex.StackTrace); _error = NetworkFailure.SecureConnectionFailed; return(false); } } //EB.Debug.Log("Connected to {0}:{1}", ip, port); LastTime = System.DateTime.Now; return(true); }