Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observable server state #29

Open
wants to merge 8 commits into
base: v2.2_develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 74 additions & 6 deletions lib60870/CS104/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,44 @@

namespace lib60870.CS104
{

/// <summary>
/// Server events concerning listening
/// </summary>
public class ServerStateEvent {

private bool nowListening;
private bool fatal;
private Exception occurredException;
/// <summary>
/// Either true (by Start()) or false (by Stop() or exception)
/// </summary>
public bool NowListening
{
get { return nowListening; }
}
/// <summary>
/// Is it a fatal exception (while listening) or not (no exception or while closing)
/// </summary>
public bool Fatal
{
get { return fatal; }
}
/// <summary>
/// Either null (by Start() and Stop() or caught exception)
/// </summary>
public Exception OccurredException
{
get { return occurredException; }
}
internal ServerStateEvent(bool nowListening, bool fatal, Exception occurredException)
{
this.nowListening = nowListening;
this.fatal = fatal;
this.occurredException = occurredException;
}
}
public delegate void ServerStateEventHandler(object parameter, ServerStateEvent stateEvent);

/// <summary>
/// Connection request handler is called when a client tries to connect to the server.
/// </summary>
Expand Down Expand Up @@ -516,7 +553,7 @@ public class Server : CS101.Slave
private string localHostname = "0.0.0.0";
private int localPort = 2404;

private bool running = false;
private volatile bool running = false;

private Socket listeningSocket;

Expand Down Expand Up @@ -681,6 +718,19 @@ public void AddRedundancyGroup(RedundancyGroup redundancyGroup)
redGroups.Add(redundancyGroup);
}

public ServerStateEventHandler serverStateEventHandler = null;
public object serverStateEventHandlerParameter = null;
/// <summary>
/// Sets a callback handler for server state changes.
/// </summary>
/// <param name="handler">Handler.</param>
/// <param name="parameter">Parameter.</param>
public void SetServerStateEventHandler(ServerStateEventHandler handler, object parameter)
{
this.serverStateEventHandler = handler;
this.serverStateEventHandlerParameter = parameter;
}

public ConnectionRequestHandler connectionRequestHandler = null;
public object connectionRequestHandlerParameter = null;

Expand Down Expand Up @@ -741,6 +791,7 @@ public int ReceiveTimeout
private void ServerAcceptThread()
{
running = true;
CallServerStateEventHandler(new ServerStateEvent(true, false, null));

DebugLog("Waiting for connections...");

Expand Down Expand Up @@ -837,9 +888,17 @@ private void ServerAcceptThread()
}

}
catch (Exception)
catch (Exception ex)
{
running = false;
DebugLog("Exception: " + ex.Message);
// Access to running not synchronized, is it? :-(
// But then, it's only about the log.
if (running || !(ex is SocketException))
{
// We are either not within shutdown or see some unexpected error.
running = false;
CallServerStateEventHandler(new ServerStateEvent(false, true, ex));
}
}

}
Expand Down Expand Up @@ -930,16 +989,18 @@ public void Start()
public void Stop()
{
running = false;
CallServerStateEventHandler(new ServerStateEvent(false, false, null));

try
{
try
{
listeningSocket.Shutdown(SocketShutdown.Both);
}
catch (SocketException ex)
catch (SocketException ex) // seems to be side effect of Shutdown(). No idea how to avoid it.
{
DebugLog("SocketException: " + ex.Message);
DebugLog("SocketException: " + ex.Message + " with code " + ex.ErrorCode);
// CallServerStateEventHandler(new ServerStateEvent(false, false, ex));
}

listeningSocket.Close();
Expand All @@ -954,6 +1015,7 @@ public void Stop()
catch (Exception e)
{
DebugLog("Exception: " + e.Message);
CallServerStateEventHandler(new ServerStateEvent(false, false, e));
}
}

Expand Down Expand Up @@ -995,6 +1057,12 @@ public void EnqueueASDU(ASDU asdu)
}
}

internal void CallServerStateEventHandler(ServerStateEvent e)
{
if (serverStateEventHandler != null)
serverStateEventHandler(serverStateEventHandlerParameter, e);
}

internal void CallConnectionEventHandler(ClientConnection connection, ClientConnectionEvent e)
{
if (connectionEventHandler != null)
Expand Down