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

Commit

Permalink
add features: add new SendASdu function to client SAP, add NewASdu ev…
Browse files Browse the repository at this point in the history
…ent property to server SAP
  • Loading branch information
minhdtb committed Nov 15, 2016
1 parent 74ae6d2 commit ea51b69
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 97 deletions.
2 changes: 1 addition & 1 deletion IEC60870/Connection/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using IEC60870.IE;
using IEC60870.IE.Base;
using IEC60870.Object;
using IEC60870.Util;
using IEC60870.Utils;

namespace IEC60870.Connections
{
Expand Down
10 changes: 5 additions & 5 deletions IEC60870/IEC60870.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SAP\ClientSAP.cs" />
<Compile Include="SAP\ServerSAP.cs" />
<Compile Include="Util\CountDownLatch.cs" />
<Compile Include="Util\PeriodicTaskFactory.cs" />
<Compile Include="Util\PubSubExtensions.cs" />
<Compile Include="Util\PubSubHub.cs" />
<Compile Include="Util\ThreadBase.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>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
27 changes: 17 additions & 10 deletions IEC60870/SAP/ClientSAP.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using IEC60870.Connections;
using IEC60870.Object;
using System;
using System.IO;
using System.Net;
Expand All @@ -8,7 +9,7 @@ namespace IEC60870.SAP
{
public class ClientSAP
{
private ConnectionSettings settings = new ConnectionSettings();
private ConnectionSettings _settings = new ConnectionSettings();
private Connection _connection;
private IPAddress _host;
private int _port;
Expand Down Expand Up @@ -48,7 +49,7 @@ public void Connect()
var socket = new Socket(_host.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(remoteEp);

_connection = new Connection(socket, settings);
_connection = new Connection(socket, _settings);
_connection.NewASdu = NewASdu ?? null;
_connection.ConnectionClosed = ConnectionClosed ?? null;
_connection.StartDataTransfer();
Expand All @@ -59,14 +60,20 @@ public void Connect()
}
}

public void SendASdu(ASdu asdu)
{
if (_connection != null)
_connection.Send(asdu);
}

public void SetMessageFragmentTimeout(int timeout)
{
if (timeout < 0)
{
throw new ArgumentException("Invalid message fragment timeout: " + timeout);
}

settings.MessageFragmentTimeout = timeout;
_settings.MessageFragmentTimeout = timeout;
}

public void SetCotFieldLength(int length)
Expand All @@ -76,7 +83,7 @@ public void SetCotFieldLength(int length)
throw new ArgumentException("Invalid COT length: " + length);
}

settings.CotFieldLength = length;
_settings.CotFieldLength = length;
}

public void SetCommonAddressFieldLength(int length)
Expand All @@ -86,7 +93,7 @@ public void SetCommonAddressFieldLength(int length)
throw new ArgumentException("Invalid CA length: " + length);
}

settings.CommonAddressFieldLength = length;
_settings.CommonAddressFieldLength = length;
}

public void SetIoaFieldLength(int length)
Expand All @@ -96,7 +103,7 @@ public void SetIoaFieldLength(int length)
throw new ArgumentException("Invalid IOA length: " + length);
}

settings.IoaFieldLength = length;
_settings.IoaFieldLength = length;
}

public void SetMaxTimeNoAckReceived(int time)
Expand All @@ -107,7 +114,7 @@ public void SetMaxTimeNoAckReceived(int time)
+ ", time must be between 1000ms and 255000ms");
}

settings.MaxTimeNoAckReceived = time;
_settings.MaxTimeNoAckReceived = time;
}

public void SetMaxTimeNoAckSent(int time)
Expand All @@ -118,7 +125,7 @@ public void SetMaxTimeNoAckSent(int time)
+ ", time must be between 1000ms and 255000ms");
}

settings.MaxTimeNoAckSent = time;
_settings.MaxTimeNoAckSent = time;
}

public void SetMaxIdleTime(int time)
Expand All @@ -129,7 +136,7 @@ public void SetMaxIdleTime(int time)
+ ", time must be between 1000ms and 172800000ms");
}

settings.MaxIdleTime = time;
_settings.MaxIdleTime = time;
}

public void SetMaxUnconfirmedIPdusReceived(int maxNum)
Expand All @@ -139,7 +146,7 @@ public void SetMaxUnconfirmedIPdusReceived(int maxNum)
throw new ArgumentException("invalid maxNum: " + maxNum + ", must be a value between 1 and 32767");
}

settings.MaxUnconfirmedIPdusReceived = maxNum;
_settings.MaxUnconfirmedIPdusReceived = maxNum;
}
}
}
29 changes: 19 additions & 10 deletions IEC60870/SAP/ServerSAP.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using IEC60870.Connections;
using IEC60870.Object;
using IEC60870.Util;
using IEC60870.Utils;
using System;
using System.IO;
using System.Net;
Expand All @@ -12,17 +12,20 @@ class ConnectionHandler : ThreadBase
{
private Socket _socket;
private ConnectionSettings _settings;
private Connection serverConnection;
public ConnectionHandler(Socket socket, ConnectionSettings settings): base()
private Connection _connection;
private ConnectionEventListener.NewASdu _newAsduEvent;

public ConnectionHandler(Socket socket, ConnectionSettings settings, ConnectionEventListener.NewASdu newASduEvent) : base()
{
_socket = socket;
_settings = settings;
_newAsduEvent = newASduEvent;

this.Subscribe<ASdu>("send", asdu =>
{
try
{
serverConnection.Send(asdu);
_connection.Send(asdu);
}
catch (Exception e)
{
Expand All @@ -33,13 +36,15 @@ public ConnectionHandler(Socket socket, ConnectionSettings settings): base()

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

serverConnection.WaitForStartDT(5000);
_connection.NewASdu += _newAsduEvent;

_connection.WaitForStartDT(5000);
}
}

Expand All @@ -48,12 +53,14 @@ class ServerThread : ThreadBase
private int _maxConnections;
private ConnectionSettings _settings;
private Socket _serverSocket;
private ConnectionEventListener.NewASdu _newAsduEvent;

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

public override void Run()
Expand All @@ -67,7 +74,7 @@ public override void Run()
try
{
clientSocket = _serverSocket.Accept();
var handler = new ConnectionHandler(clientSocket, _settings);
var handler = new ConnectionHandler(clientSocket, _settings, _newAsduEvent);
handler.Start();
}
catch (IOException e)
Expand Down Expand Up @@ -95,6 +102,8 @@ public class ServerSAP
private int _port;
private int _maxConnections = 10;

public ConnectionEventListener.NewASdu NewASdu { get; set; }

public ServerSAP(IPAddress host)
{
_host = host;
Expand Down Expand Up @@ -125,7 +134,7 @@ 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);
var serverThread = new ServerThread(socket, _settings, _maxConnections, NewASdu);
serverThread.Start();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
using System.Threading;

namespace IEC60870.Util
{
public class CountDownLatch
{
private readonly EventWaitHandle _event;
private int _remain;

public CountDownLatch(int count)
{
_remain = count;
_event = new ManualResetEvent(false);
}

public void CountDown()
{
if (Interlocked.Decrement(ref _remain) == 0)
_event.Set();
}

public void Wait(int timeout)
{
_event.WaitOne(timeout);
}

public void Wait()
{
_event.WaitOne();
}
}
using System.Threading;

namespace IEC60870.Utils
{
public class CountDownLatch
{
private readonly EventWaitHandle _event;
private int _remain;

public CountDownLatch(int count)
{
_remain = count;
_event = new ManualResetEvent(false);
}

public void CountDown()
{
if (Interlocked.Decrement(ref _remain) == 0)
_event.Set();
}

public void Wait(int timeout)
{
_event.WaitOne(timeout);
}

public void Wait()
{
_event.WaitOne();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace IEC60870.Util
namespace IEC60870.Utils
{
public class RunTask
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace IEC60870.Util
namespace IEC60870.Utils
{
static public class PubSubExtensions
{
Expand Down
2 changes: 1 addition & 1 deletion IEC60870/Util/PubSubHub.cs → IEC60870/Utils/PubSubHub.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace IEC60870.Util
namespace IEC60870.Utils
{
public class PubSubHub
{
Expand Down
70 changes: 35 additions & 35 deletions IEC60870/Util/ThreadBase.cs → IEC60870/Utils/ThreadBase.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
using System.Threading;

namespace IEC60870.Util
{
public abstract class ThreadBase
{
private readonly Thread thread;

protected ThreadBase()
{
thread = new Thread(Run);
}

public bool IsAlive
{
get { return thread.IsAlive; }
}

public void Start()
{
thread.Start();
}

public void Join()
{
thread.Join();
}

public void Abort()
{
thread.Abort();
}

public abstract void Run();
}
using System.Threading;

namespace IEC60870.Utils
{
public abstract class ThreadBase
{
private readonly Thread thread;

protected ThreadBase()
{
thread = new Thread(Run);
}

public bool IsAlive
{
get { return thread.IsAlive; }
}

public void Start()
{
thread.Start();
}

public void Join()
{
thread.Join();
}

public void Abort()
{
thread.Abort();
}

public abstract void Run();
}
}
Loading

0 comments on commit ea51b69

Please sign in to comment.