Skip to content
This repository has been archived by the owner on Sep 28, 2019. It is now read-only.

Commit

Permalink
fix PubSub extension issues
Browse files Browse the repository at this point in the history
  • Loading branch information
minhdtb committed May 28, 2017
1 parent 3b0e365 commit 73f52c8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
1 change: 0 additions & 1 deletion IEC60870/IEC60870.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
<Compile Include="SAP\ServerSAP.cs" />
<Compile Include="Utils\CountDownLatch.cs" />
<Compile Include="Utils\PeriodicTaskFactory.cs" />
<Compile Include="Utils\PubSubExtensions.cs" />
<Compile Include="Utils\PubSubHub.cs" />
<Compile Include="Utils\ThreadBase.cs" />
</ItemGroup>
Expand Down
37 changes: 25 additions & 12 deletions IEC60870/SAP/ServerSAP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,33 @@ internal class ConnectionHandler : ThreadBase
private readonly ConnectionSettings _settings;
private Connection _connection;
private readonly ConnectionEventListener.NewASdu _newAsduEvent;
private readonly PubSubHub _pubSubHub;

public ConnectionHandler(Socket socket, ConnectionSettings settings,
ConnectionEventListener.NewASdu newASduEvent)
ConnectionEventListener.NewASdu newASduEvent, PubSubHub pubSubHub)
{
_socket = socket;
_settings = settings;
_newAsduEvent = newASduEvent;
_pubSubHub = pubSubHub;

this.Subscribe<ASdu>("send", asdu =>
_pubSubHub.Subscribe<ASdu>(this, "send", asdu =>
{
try
{
_connection.Send(asdu);
}
catch (Exception e)
{
this.Publish("error", e);
_pubSubHub.Publish(this, "error", e);
}
});
}

public override void Run()
{
_connection = new Connection(_socket, _settings);
_connection.ConnectionClosed += e => { this.Publish<Exception>("error", e); };
_connection.ConnectionClosed += e => { _pubSubHub.Publish<Exception>(this, "error", e); };

_connection.NewASdu += _newAsduEvent;

Expand All @@ -48,39 +50,49 @@ public override void Run()

internal class ServerThread : ThreadBase
{
private int _maxConnections;
private readonly int _maxConnections;
private readonly ConnectionSettings _settings;
private readonly Socket _serverSocket;
private readonly ConnectionEventListener.NewASdu _newAsduEvent;
private readonly PubSubHub _pubSubHub;
private int _connectionCount;

public ServerThread(Socket serverSocket, ConnectionSettings settings, int maxConnections,
ConnectionEventListener.NewASdu newASduEvent)
ConnectionEventListener.NewASdu newASduEvent, PubSubHub pubSubHub)
{
_maxConnections = maxConnections;
_serverSocket = serverSocket;
_settings = settings;
_newAsduEvent = newASduEvent;
_pubSubHub = pubSubHub;
}

public override void Run()
{
try
{
while (true)
{
{
try
{
var clientSocket = _serverSocket.Accept();
var handler = new ConnectionHandler(clientSocket, _settings, _newAsduEvent);
if (_connectionCount == _maxConnections)
{
clientSocket.Close();
continue;
}

var handler = new ConnectionHandler(clientSocket, _settings, _newAsduEvent, _pubSubHub);
handler.Start();
_connectionCount++;
}
catch (IOException e)
{
this.Publish<Exception>("error", e);
_pubSubHub.Publish<Exception>(this, "error", e);
}
catch (Exception e)
{
this.Publish("error", e);
_pubSubHub.Publish(this, "error", e);
break;
}
}
Expand All @@ -95,6 +107,7 @@ public override void Run()
public class ServerSAP
{
private readonly ConnectionSettings _settings = new ConnectionSettings();
private readonly PubSubHub _pubSubHub = new PubSubHub();
private readonly IPAddress _host;
private readonly int _port;
private const int _maxConnections = 10;
Expand Down Expand Up @@ -131,13 +144,13 @@ public void StartListen(int backlog)
var socket = new Socket(_host.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(remoteEp);
socket.Listen(backlog);
var serverThread = new ServerThread(socket, _settings, _maxConnections, NewASdu);
var serverThread = new ServerThread(socket, _settings, _maxConnections, NewASdu, _pubSubHub);
serverThread.Start();
}

public void SendASdu(ASdu asdu)
{
this.Publish("send", asdu);
_pubSubHub.Publish(this, "send", asdu);
}

public void SetMessageFragmentTimeout(int timeout)
Expand Down
19 changes: 0 additions & 19 deletions IEC60870/Utils/PubSubExtensions.cs

This file was deleted.

0 comments on commit 73f52c8

Please sign in to comment.