ecs-1141: fixed null ref in TCP/Secure TCP server StopListening()

This commit is contained in:
Heath Volmer
2019-08-16 09:30:36 -06:00
parent c1b5cfe193
commit 20f7265fdb
3 changed files with 48 additions and 56 deletions

View File

@@ -441,25 +441,21 @@ namespace PepperDash.Core
/// </summary> /// </summary>
public void StopListening() public void StopListening()
{ {
try try
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Stopping Listener"); Debug.Console(2, this, Debug.ErrorLogLevel.Notice, "Stopping Listener");
if (SecureServer != null) if (SecureServer != null)
{ {
SecureServer.Stop(); SecureServer.Stop();
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Server State: {0}", SecureServer.State); Debug.Console(2, this, Debug.ErrorLogLevel.Notice, "Server State: {0}", SecureServer.State);
//SecureServer = null; OnServerStateChange(SecureServer.State);
} }
ServerStopped = true;
ServerStopped = true; }
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Server Stopped"); catch (Exception ex)
{
OnServerStateChange(SecureServer.State); Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error stopping server. Error: {0}", ex);
} }
catch (Exception ex)
{
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Error stopping server. Error: {0}", ex);
}
} }
/// <summary> /// <summary>
@@ -471,11 +467,11 @@ namespace PepperDash.Core
try try
{ {
SecureServer.Disconnect(client); SecureServer.Disconnect(client);
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Disconnected client index: {0}", client); Debug.Console(2, this, Debug.ErrorLogLevel.Notice, "Disconnected client index: {0}", client);
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Error Disconnecting client index: {0}. Error: {1}", client, ex); Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error Disconnecting client index: {0}. Error: {1}", client, ex);
} }
} }
/// <summary> /// <summary>
@@ -495,17 +491,17 @@ namespace PepperDash.Core
try try
{ {
SecureServer.Disconnect(i); SecureServer.Disconnect(i);
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Disconnected client index: {0}", i); Debug.Console(2, this, Debug.ErrorLogLevel.Notice, "Disconnected client index: {0}", i);
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Error Disconnecting client index: {0}. Error: {1}", i, ex); Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error Disconnecting client index: {0}. Error: {1}", i, ex);
} }
} }
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Server Status: {0}", SecureServer.ServerSocketStatus); Debug.Console(2, this, Debug.ErrorLogLevel.Notice, "Server Status: {0}", SecureServer.ServerSocketStatus);
} }
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Disconnected All Clients"); Debug.Console(2, this, Debug.ErrorLogLevel.Notice, "Disconnected All Clients");
ConnectedClientsIndexes.Clear(); ConnectedClientsIndexes.Clear();
if (!ProgramIsStopping) if (!ProgramIsStopping)
@@ -536,7 +532,7 @@ namespace PepperDash.Core
{ {
SocketErrorCodes error = SecureServer.SendDataAsync(i, b, b.Length, (x, y, z) => { }); SocketErrorCodes error = SecureServer.SendDataAsync(i, b, b.Length, (x, y, z) => { });
if (error != SocketErrorCodes.SOCKET_OK && error != SocketErrorCodes.SOCKET_OPERATION_PENDING) if (error != SocketErrorCodes.SOCKET_OK && error != SocketErrorCodes.SOCKET_OPERATION_PENDING)
Debug.Console(0, error.ToString()); Debug.Console(2, error.ToString());
} }
} }
} }
@@ -567,7 +563,7 @@ namespace PepperDash.Core
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.Console(0, this, "Error sending text to client. Text: {1}. Error: {0}", ex.Message, text); Debug.Console(2, this, "Error sending text to client. Text: {1}. Error: {0}", ex.Message, text);
} }
} }
@@ -701,7 +697,7 @@ namespace PepperDash.Core
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Error in Socket Status Change Callback. Error: {0}", ex); Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error in Socket Status Change Callback. Error: {0}", ex);
} }
//Use a thread for this event so that the server state updates to listening while this event is processed. Listening must be added to the server state //Use a thread for this event so that the server state updates to listening while this event is processed. Listening must be added to the server state
//after every client connection so that the server can check and see if it is at max clients. Due to this the event fires and server listening enum bit flag //after every client connection so that the server can check and see if it is at max clients. Due to this the event fires and server listening enum bit flag
@@ -770,7 +766,7 @@ namespace PepperDash.Core
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Error in Socket Status Connect Callback. Error: {0}", ex); Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error in Socket Status Connect Callback. Error: {0}", ex);
} }
//Debug.Console(1, this, Debug.ErrorLogLevel, "((((((Server State bitfield={0}; maxclient={1}; ServerStopped={2}))))))", //Debug.Console(1, this, Debug.ErrorLogLevel, "((((((Server State bitfield={0}; maxclient={1}; ServerStopped={2}))))))",
// server.State, // server.State,
@@ -834,7 +830,7 @@ namespace PepperDash.Core
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Error Receiving data: {0}. Error: {1}", received, ex); Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error Receiving data: {0}. Error: {1}", received, ex);
} }
if (mySecureTCPServer.GetServerSocketStatusForSpecificClient(clientIndex) == SocketStatus.SOCKET_STATUS_CONNECTED) if (mySecureTCPServer.GetServerSocketStatusForSpecificClient(clientIndex) == SocketStatus.SOCKET_STATUS_CONNECTED)
mySecureTCPServer.ReceiveDataAsync(clientIndex, SecureReceivedDataAsyncCallback); mySecureTCPServer.ReceiveDataAsync(clientIndex, SecureReceivedDataAsyncCallback);
@@ -874,7 +870,7 @@ namespace PepperDash.Core
} }
catch (Exception e) catch (Exception e)
{ {
Debug.Console(0, "DequeueEvent error: {0}\r", e); Debug.Console(2, "DequeueEvent error: {0}\r", e);
} }
// Make sure to leave the CCritical section in case an exception above stops this thread, or we won't be able to restart it. // Make sure to leave the CCritical section in case an exception above stops this thread, or we won't be able to restart it.
if (DequeueLock != null) if (DequeueLock != null)
@@ -1040,13 +1036,13 @@ namespace PepperDash.Core
StopMonitorClient(); StopMonitorClient();
if (MonitorClientFailureCount < MonitorClientMaxFailureCount) if (MonitorClientFailureCount < MonitorClientMaxFailureCount)
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Warning, "Monitor client connection has hung {0} time{1}, maximum {2}", Debug.Console(2, this, Debug.ErrorLogLevel.Warning, "Monitor client connection has hung {0} time{1}, maximum {2}",
MonitorClientFailureCount, MonitorClientFailureCount > 1 ? "s" : "", MonitorClientMaxFailureCount); MonitorClientFailureCount, MonitorClientFailureCount > 1 ? "s" : "", MonitorClientMaxFailureCount);
StartMonitorClient(); StartMonitorClient();
} }
else else
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Error, Debug.Console(2, this, Debug.ErrorLogLevel.Error,
"\r***************************\rMonitor client connection has hung a maximum of {0} times. \r***************************", "\r***************************\rMonitor client connection has hung a maximum of {0} times. \r***************************",
MonitorClientMaxFailureCount); MonitorClientMaxFailureCount);

View File

@@ -422,22 +422,18 @@ namespace PepperDash.Core
{ {
try try
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Stopping Listener"); Debug.Console(2, this, Debug.ErrorLogLevel.Notice, "Stopping Listener");
if (myTcpServer != null) if (myTcpServer != null)
{ {
myTcpServer.Stop(); myTcpServer.Stop();
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Server State: {0}", myTcpServer.State); Debug.Console(2, this, Debug.ErrorLogLevel.Notice, "Server State: {0}", myTcpServer.State);
//SecureServer = null; OnServerStateChange(myTcpServer.State);
} }
ServerStopped = true;
ServerStopped = true;
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Server Stopped");
OnServerStateChange(myTcpServer.State);
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Error stopping server. Error: {0}", ex); Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error stopping server. Error: {0}", ex);
} }
} }
@@ -450,11 +446,11 @@ namespace PepperDash.Core
try try
{ {
myTcpServer.Disconnect(client); myTcpServer.Disconnect(client);
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Disconnected client index: {0}", client); Debug.Console(2, this, Debug.ErrorLogLevel.Notice, "Disconnected client index: {0}", client);
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Error Disconnecting client index: {0}. Error: {1}", client, ex); Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error Disconnecting client index: {0}. Error: {1}", client, ex);
} }
} }
/// <summary> /// <summary>
@@ -474,17 +470,17 @@ namespace PepperDash.Core
try try
{ {
myTcpServer.Disconnect(i); myTcpServer.Disconnect(i);
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Disconnected client index: {0}", i); Debug.Console(2, this, Debug.ErrorLogLevel.Notice, "Disconnected client index: {0}", i);
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Error Disconnecting client index: {0}. Error: {1}", i, ex); Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error Disconnecting client index: {0}. Error: {1}", i, ex);
} }
} }
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Server Status: {0}", myTcpServer.ServerSocketStatus); Debug.Console(2, this, Debug.ErrorLogLevel.Notice, "Server Status: {0}", myTcpServer.ServerSocketStatus);
} }
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Disconnected All Clients"); Debug.Console(2, this, Debug.ErrorLogLevel.Notice, "Disconnected All Clients");
ConnectedClientsIndexes.Clear(); ConnectedClientsIndexes.Clear();
if (!ProgramIsStopping) if (!ProgramIsStopping)
@@ -515,7 +511,7 @@ namespace PepperDash.Core
{ {
SocketErrorCodes error = myTcpServer.SendDataAsync(i, b, b.Length, (x, y, z) => { }); SocketErrorCodes error = myTcpServer.SendDataAsync(i, b, b.Length, (x, y, z) => { });
if (error != SocketErrorCodes.SOCKET_OK && error != SocketErrorCodes.SOCKET_OPERATION_PENDING) if (error != SocketErrorCodes.SOCKET_OK && error != SocketErrorCodes.SOCKET_OPERATION_PENDING)
Debug.Console(0, error.ToString()); Debug.Console(2, error.ToString());
} }
} }
} }
@@ -546,7 +542,7 @@ namespace PepperDash.Core
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.Console(0, this, "Error sending text to client. Text: {1}. Error: {0}", ex.Message, text); Debug.Console(2, this, "Error sending text to client. Text: {1}. Error: {0}", ex.Message, text);
} }
} }
@@ -680,7 +676,7 @@ namespace PepperDash.Core
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Error in Socket Status Change Callback. Error: {0}", ex); Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error in Socket Status Change Callback. Error: {0}", ex);
} }
onConnectionChange(clientIndex, server.GetServerSocketStatusForSpecificClient(clientIndex)); onConnectionChange(clientIndex, server.GetServerSocketStatusForSpecificClient(clientIndex));
} }
@@ -746,7 +742,7 @@ namespace PepperDash.Core
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Error in Socket Status Connect Callback. Error: {0}", ex); Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error in Socket Status Connect Callback. Error: {0}", ex);
} }
//Debug.Console(1, this, Debug.ErrorLogLevel, "((((((Server State bitfield={0}; maxclient={1}; ServerStopped={2}))))))", //Debug.Console(1, this, Debug.ErrorLogLevel, "((((((Server State bitfield={0}; maxclient={1}; ServerStopped={2}))))))",
// server.State, // server.State,
@@ -803,7 +799,7 @@ namespace PepperDash.Core
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Error Receiving data: {0}. Error: {1}", received, ex); Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error Receiving data: {0}. Error: {1}", received, ex);
} }
if (myTCPServer.GetServerSocketStatusForSpecificClient(clientIndex) == SocketStatus.SOCKET_STATUS_CONNECTED) if (myTCPServer.GetServerSocketStatusForSpecificClient(clientIndex) == SocketStatus.SOCKET_STATUS_CONNECTED)
myTCPServer.ReceiveDataAsync(clientIndex, TcpServerReceivedDataAsyncCallback); myTCPServer.ReceiveDataAsync(clientIndex, TcpServerReceivedDataAsyncCallback);
@@ -973,13 +969,13 @@ namespace PepperDash.Core
StopMonitorClient(); StopMonitorClient();
if (MonitorClientFailureCount < MonitorClientMaxFailureCount) if (MonitorClientFailureCount < MonitorClientMaxFailureCount)
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Warning, "Monitor client connection has hung {0} time{1}, maximum {2}", Debug.Console(2, this, Debug.ErrorLogLevel.Warning, "Monitor client connection has hung {0} time{1}, maximum {2}",
MonitorClientFailureCount, MonitorClientFailureCount > 1 ? "s" : "", MonitorClientMaxFailureCount); MonitorClientFailureCount, MonitorClientFailureCount > 1 ? "s" : "", MonitorClientMaxFailureCount);
StartMonitorClient(); StartMonitorClient();
} }
else else
{ {
Debug.Console(0, this, Debug.ErrorLogLevel.Error, Debug.Console(2, this, Debug.ErrorLogLevel.Error,
"\r***************************\rMonitor client connection has hung a maximum of {0} times.\r***************************", "\r***************************\rMonitor client connection has hung a maximum of {0} times.\r***************************",
MonitorClientMaxFailureCount); MonitorClientMaxFailureCount);

View File

@@ -4,4 +4,4 @@
[assembly: AssemblyCompany("")] [assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Pepperdash_Core")] [assembly: AssemblyProduct("Pepperdash_Core")]
[assembly: AssemblyCopyright("Copyright © PepperDash 2019")] [assembly: AssemblyCopyright("Copyright © PepperDash 2019")]
[assembly: AssemblyVersion("1.0.22.*")] [assembly: AssemblyVersion("1.0.23.*")]