Skip to content

Commit

Permalink
Mono workarounds for DataReceivedEvent
Browse files Browse the repository at this point in the history
Abstraction of network server and client into separate class library
Migration to .NET 4.6
General cleanups
  • Loading branch information
drkno committed Jan 1, 2016
1 parent a41167c commit 5a25117
Show file tree
Hide file tree
Showing 22 changed files with 440 additions and 329 deletions.
62 changes: 62 additions & 0 deletions PCR1000.Network/PCR1000.Network.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C4792356-055E-4181-9ECE-EC75FB04FDA0}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PCR1000.Network</RootNamespace>
<AssemblyName>PCR1000.Network</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="PcrNetworkComm.cs" />
<Compile Include="PcrNetworkServer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PCR1000\PCR1000.csproj">
<Project>{29487920-2926-48b7-bd8c-b2761696663b}</Project>
<Name>PCR1000</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
269 changes: 12 additions & 257 deletions PCR1000/PcrNetworkComm.cs → PCR1000.Network/PcrNetworkComm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,263 +16,11 @@
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Text;
using System.Threading;

namespace PCR1000
namespace PCR1000.Network
{
/*public class PcrNetworkClientComm : IComm
{
private string server;
private int port;
private bool ssl;
private string password;
private TcpClient _tcpClient;
private Stream _tcpStream;
private Thread _listenThread;
private bool isAuthenticated;
private void ListenThread()
{
while (isAuthenticated)
{
_tcpStream.Length;
_tcpStream.Read()
_tcpStream.Flush();
}
}
public PcrNetworkClientComm(string server, int port = 4456, bool ssl = false, string password = "")
{
this.server = server;
this.port = port;
this.ssl = ssl;
this.password = password;
if (string.IsNullOrWhiteSpace(password))
{
isAuthenticated = true;
}
}
public bool PcrOpen()
{
try
{
_tcpClient = new TcpClient();
_tcpClient.Connect(server, port);
_tcpStream = ssl
? (Stream) new SslStream(_tcpClient.GetStream())
: _tcpClient.GetStream();
_listenThread = new Thread(ListenThread);
_listenThread.Start();
if (!isAuthenticated)
{
var str = SendWait("<auth>" + password + "</auth>");
if (str != "<auth>pass</auth>")
{
_listenThread.Abort();
throw new AuthenticationException("Not Authorised");
}
isAuthenticated = true;
}
}
catch (Exception)
{
return false;
}
return true;
}
public bool PcrClose()
{
_listenThread.Abort();
return true;
}
public void Dispose()
{
Debug.WriteLine("PcrComm Dispose");
PcrClose();
}
public event AutoUpdateDataRecv DataReceived;
public bool AutoUpdate { get; set; }
public object GetRawPort()
{
return _tcpClient;
}
#if DEBUG
/// <summary>
/// Keeps track of wheather debug logging is enabled.
/// </summary>
private bool _debugLogger;
/// <summary>
/// Enables or disables debug logging in the comminication library.
/// </summary>
/// <param name="debug">Enable or disable.</param>
public void SetDebugLogger(bool debug)
{
Debug.WriteLine("PcrComm Debug Logging: " + debug);
_debugLogger = debug;
}
#endif
public bool Send(string cmd)
{
try
{
_tcpStream.WriteAsync(Encoding.ASCII.GetBytes(cmd), 0, cmd.Length);
}
catch (Exception)
{
return false;
}
return true;
}
/// <summary>
/// Number of 50ms timeouts to wait before aborting in SendWait.
/// </summary>
private const int RecvTimeout = 20;
/// <summary>
/// Sends a message to the PCR1000 and waits for a reply.
/// </summary>
/// <param name="cmd">The command to send.</param>
/// <param name="overrideAutoupdate">When in autoupdate mode behaves like Send()
/// this overrides that behaviour.</param>
/// <returns>The reply or "" if nothing is received.</returns>
public string SendWait(string cmd, bool overrideAutoupdate = false)
{
Debug.WriteLine("PcrComm SendWait");
Send(cmd);
if (AutoUpdate && !overrideAutoupdate) return "";
var dt = DateTime.Now;
for (var i = 0; i < RecvTimeout; i++)
{
if (dt < _msgSlot1.Time)
{
return dt < _msgSlot2.Time ? _msgSlot2.Message : _msgSlot1.Message;
}
Thread.Sleep(50);
}
return "";
}
/// <summary>
/// Received message structure.
/// </summary>
private struct RecvMsg
{
/// <summary>
/// The message received.
/// </summary>
public string Message;
/// <summary>
/// The time the message was received.
/// </summary>
public DateTime Time;
}
/// <summary>
/// Last two received messages.
/// </summary>
private RecvMsg _msgSlot1, _msgSlot2;
/// <summary>
/// Gets the latest message from the PCR1000.
/// </summary>
/// <returns>The latest message.</returns>
public string GetLastReceived()
{
Debug.WriteLine("PcrComm Last Recv");
try
{
return _msgSlot1.Message;
}
catch (Exception)
{
return "";
}
}
/// <summary>
/// Gets the previously received message.
/// </summary>
/// <returns>The previous message.</returns>
public string GetPrevReceived()
{
Debug.WriteLine("PcrComm PrevRecv");
try
{
return _msgSlot2.Message;
}
catch (Exception)
{
return "";
}
}
}*/



















































/// <summary>
/// Client to connect to a remote radio.
/// </summary>
Expand Down Expand Up @@ -380,22 +128,29 @@ public PcrNetworkClient(string server, int port = 4456, bool ssl = false, string
_ssl = ssl;
}

/// <summary>
/// Disposes of the PcrNetworkClient
/// </summary>
public void Dispose()
{
if (!_tcpClient.Connected) return;
_tcpListen.Abort();
_tcpListen.Join();
_tcpClient.Close();
}

/// <summary>
///
/// Data was received from the remote radio.
/// </summary>
public event AutoUpdateDataRecv DataReceived;
/// <summary>
///
/// The remote radio should auto-update.
/// </summary>
public bool AutoUpdate { get; set; }
/// <summary>
///
/// Gets the underlying system port of the radio.
/// </summary>
/// <returns></returns>
/// <returns>The network port connected to the remote radio.</returns>
public object GetRawPort()
{
return _tcpClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Text;
using System.Threading;

namespace PCR1000
namespace PCR1000.Network
{
/// <summary>
/// Server class to manage remote connections.
Expand Down
Loading

0 comments on commit 5a25117

Please sign in to comment.