Skip to content

Commit

Permalink
Issue #12
Browse files Browse the repository at this point in the history
  • Loading branch information
multiplug-hub committed May 24, 2022
1 parent 9f788f0 commit c768ac7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 22 deletions.
Expand Up @@ -20,12 +20,15 @@ public class SocketClientComponent : SocketClientProperties

private Socket m_Socket;

private bool m_ConnectionInitialising = false;
private bool m_ConnectionInError = false;

private ILoggingService m_LoggingService;

public string LogEventId { get { return m_LoggingService.EventId; } }

private System.Timers.Timer m_InitialiseDelayTimer;

public SocketClientComponent( string theGuid, ILoggingService theLoggingService)
{
Guid = theGuid;
Expand Down Expand Up @@ -249,6 +252,12 @@ internal void Shutdown()

private void InitialiseSetup()
{
if(m_ConnectionInitialising)
{
OnLogWriteEntry(EventLogEntryCodes.SocketClientAlreadyInitialising, null);
return;
}

if( string.IsNullOrEmpty(HostName) )
{
OnLogWriteEntry(EventLogEntryCodes.SocketClientNoHostName, null);
Expand All @@ -261,6 +270,8 @@ private void InitialiseSetup()
return;
}

m_ConnectionInitialising = true;

try
{
if( m_Socket != null )
Expand All @@ -287,18 +298,27 @@ private void InitialiseSetup()

m_Socket.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), m_Socket);
}
catch (SocketException theSocketException)
{
OnSocketException(theSocketException);
}
catch (Exception theException)
{
OnLogWriteEntry(EventLogEntryCodes.SocketClientException, new string[] { theException.Message });
}

m_ConnectionInitialising = false;
}

private void InitialiseAfterDelay()
{
var timer = new System.Timers.Timer(2000);
timer.AutoReset = false;
timer.Elapsed += (s, e) => { InitialiseSetup(); };
timer.Start();
if(m_InitialiseDelayTimer != null && ! m_InitialiseDelayTimer.Enabled)
{
m_InitialiseDelayTimer = new System.Timers.Timer(1500);
m_InitialiseDelayTimer.AutoReset = false;
m_InitialiseDelayTimer.Elapsed += (s, e) => { InitialiseSetup(); };
m_InitialiseDelayTimer.Start();
}
}

private void ConnectCallback(IAsyncResult ar)
Expand Down Expand Up @@ -334,9 +354,12 @@ private void ConnectCallback(IAsyncResult ar)
item = m_MessageBuffer.Peek();
}
}
catch (ObjectDisposedException)
catch (ObjectDisposedException theException)
{
// Connection Closed
ConnectionInError = true;
OnLogWriteEntry(EventLogEntryCodes.SocketClientObjectDisposedException, new string[] { "ConnectCallback " + theException.Message });
InitialiseAfterDelay();
}
catch (SocketException theException)
{
Expand Down Expand Up @@ -367,14 +390,23 @@ private void Receive(Socket client)
}
}

private void ReceiveCallback(IAsyncResult ar)
private void ReceiveCallback(IAsyncResult theAsyncResult)
{
SocketState state = (SocketState)ar.AsyncState;
SocketState state = (SocketState)theAsyncResult.AsyncState;

try
{
Socket client = state.workSocket;
int bytesRead = client.EndReceive(ar);

if( ! client.Connected )
{
ConnectionInError = true;
OnLogWriteEntry(EventLogEntryCodes.SocketClientClosedWhileReceive, new string[0]);
InitialiseAfterDelay();
return;
}

int bytesRead = client.EndReceive(theAsyncResult);

ConnectionInError = false;

Expand All @@ -390,7 +422,7 @@ private void ReceiveCallback(IAsyncResult ar)

if( LoggingLevel == 1)
{
OnLogWriteEntry(EventLogEntryCodes.SocketClientDataReceived, new string[] { string.Empty });
OnLogWriteEntry(EventLogEntryCodes.SocketClientDataReceived, new string[0]);
}
else if (LoggingLevel == 2)
{
Expand All @@ -407,10 +439,12 @@ private void ReceiveCallback(IAsyncResult ar)
client.BeginReceive(state.buffer, 0, SocketState.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
}
catch (ObjectDisposedException)
catch (ObjectDisposedException theException)
{
ConnectionInError = true;
// Connection Closed
OnLogWriteEntry(EventLogEntryCodes.SocketClientObjectDisposedException, new string[] { "ReceiveCallback " + theException.Message });
InitialiseAfterDelay();
}
catch (SocketException theSocketException)
{
Expand Down
Expand Up @@ -118,9 +118,9 @@ private void AcceptCallback(IAsyncResult ar)
client.BeginReceive(state.buffer, 0, SocketState.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
catch( ObjectDisposedException )
catch( ObjectDisposedException theException)
{
// Connection Closed
Log?.Invoke(EventLogEntryCodes.SocketEndpointObjectDisposedException, new string[] { "AcceptCallback " + theException.Message });
}
catch( SocketException theSocketException)
{
Expand All @@ -132,16 +132,24 @@ private void AcceptCallback(IAsyncResult ar)
}
}

public void ReceiveCallback(IAsyncResult ar)
public void ReceiveCallback(IAsyncResult theAsyncResult)
{
SocketState state = (SocketState)ar.AsyncState;
SocketState state = (SocketState)theAsyncResult.AsyncState;

try
{
Socket client = state.workSocket;

if (!client.Connected)
{
Log?.Invoke(EventLogEntryCodes.SocketEndpointClosedWhileReceive, new string[] { state.Address });
OnSocketException(state);
RemoveErroredSockets();
return;
}

// Read data from the client socket.
int bytesRead = client.EndReceive(ar);
int bytesRead = client.EndReceive(theAsyncResult);

if (bytesRead > 0)
{
Expand Down Expand Up @@ -178,9 +186,11 @@ public void ReceiveCallback(IAsyncResult ar)
new AsyncCallback(ReceiveCallback), state);
}
}
catch (ObjectDisposedException)
catch (ObjectDisposedException theException)
{
// Connection Closed
state.Errored = true;
Log?.Invoke(EventLogEntryCodes.SocketEndpointObjectDisposedException, new string[] { "ReceiveCallback " + theException.Message });
RemoveErroredSockets();
}
catch ( SocketException theSocketException)
{
Expand All @@ -189,7 +199,7 @@ public void ReceiveCallback(IAsyncResult ar)

if ( theSocketException.SocketErrorCode == SocketError.ConnectionReset)
{
Log?.Invoke(EventLogEntryCodes.SocketEndpointExceptionConnectionReset, new string[] { state.workSocket.RemoteEndPoint.ToString() });
Log?.Invoke(EventLogEntryCodes.SocketEndpointExceptionConnectionReset, new string[] { state.Address });
}
else
{
Expand Down
Expand Up @@ -30,13 +30,14 @@ internal class EventLogDefinitions
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketEndpointSending, Source = (uint) EventLogEntryCodes.SourceSocketEndpointListener, StringFormat = "Sending: {0}", Type = EventLogEntryType.Information },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketEndpointLocalIPAddressUpdated, Source = (uint) EventLogEntryCodes.SourceSocketEndpoint, StringFormat = "Local NIC IP updated to: {0}", Type = EventLogEntryType.Information },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketEndpointLocalIPAddressUpdateFailed, Source = (uint) EventLogEntryCodes.SourceSocketEndpoint, StringFormat = "No Local NIC IP Address of: {0}", Type = EventLogEntryType.Warning },

new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketEndpointSocketClosed, Source = (uint) EventLogEntryCodes.SourceSocketEndpoint, StringFormat = "Socket Closed", Type = EventLogEntryType.Information },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketEndpointSocketClosingDueToReconfigure, Source = (uint) EventLogEntryCodes.SourceSocketEndpoint, StringFormat = "Socket closing due to reconfiguration", Type = EventLogEntryType.Information },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketEndpointSocketClosingDueToSubscriptionControl, Source = (uint) EventLogEntryCodes.SourceSocketEndpoint, StringFormat = "Socket closing due to Subscription Control", Type = EventLogEntryType.Information },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketEndpointObjectDisposedException, Source = (uint) EventLogEntryCodes.SourceSocketEndpoint, StringFormat = "Object Disposed Exception: {0}", Type = EventLogEntryType.Error },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketEndpointClosedWhileReceive, Source = (uint) EventLogEntryCodes.SourceSocketEndpoint, StringFormat = "Connection Closed while Receiving", Type = EventLogEntryType.Warning },

new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketClientNoHostName, Source = (uint) EventLogEntryCodes.SourceSocketClient, StringFormat = "Connection aborted: No HostName", Type = EventLogEntryType.Warning },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketClientIncorrectPort, Source = (uint) EventLogEntryCodes.SourceSocketClient, StringFormat = "Connection aborted: Incorrect port", Type = EventLogEntryType.Warning },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketClientNoHostName, Source = (uint) EventLogEntryCodes.SourceSocketClient, StringFormat = "Connection initialisation aborted: No HostName", Type = EventLogEntryType.Warning },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketClientIncorrectPort, Source = (uint) EventLogEntryCodes.SourceSocketClient, StringFormat = "Connection initialisation aborted: Incorrect port", Type = EventLogEntryType.Warning },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketClientConnectingTo, Source = (uint) EventLogEntryCodes.SourceSocketClient, StringFormat = "Connecting to {0}", Type = EventLogEntryType.Information },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketClientException, Source = (uint) EventLogEntryCodes.SourceSocketClient, StringFormat = "Exception: {0}", Type = EventLogEntryType.Error },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketClientConnectedTo, Source = (uint) EventLogEntryCodes.SourceSocketClient, StringFormat = "Connected to {0}", Type = EventLogEntryType.Information },
Expand All @@ -49,6 +50,9 @@ internal class EventLogDefinitions
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketClientSocketClosedException, Source = (uint) EventLogEntryCodes.SourceSocketClient, StringFormat = "Socket Closed Exception: {0}", Type = EventLogEntryType.Error },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketClientSocketClosingDueToReconfigure, Source = (uint) EventLogEntryCodes.SourceSocketClient, StringFormat = "Socket closing due to reconfiguration", Type = EventLogEntryType.Information },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketClientSocketClosingDueToSubscriptionControl, Source = (uint) EventLogEntryCodes.SourceSocketClient, StringFormat = "Socket closing due to Subscription Control", Type = EventLogEntryType.Information },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketClientAlreadyInitialising, Source = (uint) EventLogEntryCodes.SourceSocketClient, StringFormat = "Connection initialisation aborted: Already initialising", Type = EventLogEntryType.Information },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketClientObjectDisposedException, Source = (uint) EventLogEntryCodes.SourceSocketClient, StringFormat = "Object Disposed Exception: {0}", Type = EventLogEntryType.Error },
new EventLogDefinition { Code = (uint) EventLogEntryCodes.SocketClientClosedWhileReceive, Source = (uint) EventLogEntryCodes.SourceSocketClient, StringFormat = "Connection Closed while Receiving", Type = EventLogEntryType.Warning }
};
}
}
Expand Up @@ -31,6 +31,8 @@ internal enum EventLogEntryCodes
SocketEndpointSocketClosed = 38,
SocketEndpointSocketClosingDueToReconfigure = 39,
SocketEndpointSocketClosingDueToSubscriptionControl = 40,
SocketEndpointObjectDisposedException = 41,
SocketEndpointClosedWhileReceive = 42,

SocketClientNoHostName = 55,
SocketClientIncorrectPort = 56,
Expand All @@ -45,6 +47,9 @@ internal enum EventLogEntryCodes
SocketClientSocketClosed = 65,
SocketClientSocketClosedException = 66,
SocketClientSocketClosingDueToReconfigure = 67,
SocketClientSocketClosingDueToSubscriptionControl = 68
SocketClientSocketClosingDueToSubscriptionControl = 68,
SocketClientAlreadyInitialising = 69,
SocketClientObjectDisposedException = 70,
SocketClientClosedWhileReceive = 71,
}
}

0 comments on commit c768ac7

Please sign in to comment.