internal byte[] Send(IDeviceMessage message) { byte[] buffer = message.GetSendBuffer(); bool timedOut = false; Connect(PrimaryEndpoint, PrimaryPort); try { for (int i = 0; i < 2; i++) { DateTime requestSent = DateTime.UtcNow; if (client != null && client.Connected && sslStream.IsAuthenticated) { sslStream.Write(buffer, 0, buffer.Length); sslStream.Flush(); } byte[] rvalue = GetGatewayResponse(); if (rvalue != null && !ForceGatewayTimeout) { return(rvalue); } // did not get a response, switch endpoints and try again timedOut = true; if (!currentEndpoint.Equals("secondary") && !string.IsNullOrEmpty(SecondaryEndpoint) && i < 1) { Disconnect(); Connect(SecondaryEndpoint, SecondaryPort); } } throw new GatewayTimeoutException(); } catch (GatewayTimeoutException exc) { exc.GatewayEvents = events; throw exc; } catch (Exception exc) { if (timedOut) { GatewayTimeoutException gatewayException = new GatewayTimeoutException(exc) { GatewayEvents = events }; throw gatewayException; } else { GatewayException gatewayException = new GatewayException("Failed to connect to primary or secondary processing endpoints.", exc); throw gatewayException; } } finally { Disconnect(); // remove the force timeout if (ForceGatewayTimeout) { ForceGatewayTimeout = false; } } }
public void TestMultilineException() { try { TextMultilineExceptionHelper(); } catch (Exception inner) { var outer = new GatewayException("Outer", new Exception(inner.StackTrace)); AssertStatusDescription(outer.ResponseStatusDescription); } }
// establish connection private void Connect(string endpoint, int port) { currentEndpoint = endpoint.Equals(PrimaryEndpoint) ? "primary" : "secondary"; // create the connection event ConnectionEvent connectionEvent = new ConnectionEvent(connectorName) { Endpoint = endpoint, Port = port.ToString(), Host = currentEndpoint, ConnectionAttempts = connectionFaults }; if (client == null) { try { connectionEvent.ConnectionStarted = DateTime.UtcNow; try { client = new TcpClient(); client.ConnectAsync(endpoint, port).Wait(); connectionEvent.SslNavigation = true; } catch (Exception) { connectionEvent.SslNavigation = false; } if (client != null && client.Connected) { // connection completed connectionEvent.ConnectionCompleted = DateTime.UtcNow; sslStream = new SslStream( client.GetStream(), false); sslStream.AuthenticateAsClientAsync(endpoint, null, System.Security.Authentication.SslProtocols.Tls12, false).Wait(30000); connectionFaults = 0; } else { // connection fail over connectionEvent.ConnectionFailOver = DateTime.UtcNow; //events.add(connectionEvent); if (connectionFaults++ != 3) { if (endpoint.Equals(PrimaryEndpoint) && SecondaryEndpoint != null) { Connect(SecondaryEndpoint, SecondaryPort); } else { Connect(PrimaryEndpoint, PrimaryPort); } } else { throw new IOException("Failed to connect to primary or secondary processing endpoints."); } } } catch (Exception exc) { GatewayException gatewayException = new GatewayException("Error occurred while communicating with gateway.", exc); //gatewayException.setGatewayEvents(events); throw gatewayException; } } }
public void TestNonAscii() { var outer = new GatewayException("ą", new Exception("ę")); AssertStatusDescription(outer.ResponseStatusDescription); }
public void TestLongException() { var outer = new GatewayException("Outer", new Exception(string.Join(" ", Enumerable.Repeat("Inner", 100)))); AssertStatusDescription(outer.ResponseStatusDescription); }
public void TestShortException() { var outer = new GatewayException("Outer", new Exception("Inner")); AssertStatusDescription(outer.ResponseStatusDescription); }