Skip to content

Commit

Permalink
Protocol tidyups
Browse files Browse the repository at this point in the history
  • Loading branch information
drkno committed Jan 4, 2016
1 parent fe551c5 commit cb8043b
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 216 deletions.
5 changes: 3 additions & 2 deletions PCR1000.Network/PCR1000.Network.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="PcrNetworkComm.cs" />
<Compile Include="PcrNetworkProtocolHandler.cs" />
<Compile Include="PcrNetworkServer.cs" />
<Compile Include="Server\ClientErrorCodes.cs" />
<Compile Include="Server\ConnectedClient.cs" />
<Compile Include="Server\PcrNetworkServer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
60 changes: 32 additions & 28 deletions PCR1000.Network/PcrNetworkComm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Net.Sockets;
using System.Text;
using System.Threading;
using PCR1000.Network.Server;

namespace PCR1000.Network
{
Expand Down Expand Up @@ -86,9 +87,6 @@ private void ListenThread()
datarecv = datarecv.Substring(0, lData);
datarecv = datarecv.Trim(TrimChars);
if (datarecv.Length <= 0) continue;
#if DEBUG
Debug.WriteLineIf(!_debugLogger, "PcrNetwork Data Recv");
#endif
if (AutoUpdate)
{
DataReceived?.Invoke(this, DateTime.Now, datarecv);
Expand All @@ -97,9 +95,7 @@ private void ListenThread()
_msgSlot2 = _msgSlot1;
_msgSlot1 = new RecvMsg {Message = datarecv, Time = DateTime.Now};

#if DEBUG
Debug.WriteLineIf(_debugLogger, _server + ":" + _port + " : RECV -> " + datarecv);
#endif
Debug.WriteLine(_server + ":" + _port + " : RECV -> " + datarecv);
}
}
catch (ThreadAbortException)
Expand Down Expand Up @@ -138,6 +134,7 @@ public PcrNetworkClient(string server, int port = 4456, bool ssl = false, string
public void Dispose()
{
if (!_tcpClient.Connected) return;
PcrClose();
_tcpListen.Abort();
_tcpListen.Join();
_tcpClient.Close();
Expand All @@ -160,22 +157,6 @@ 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)
{
_debugLogger = debug;
}
#endif

/// <summary>
/// Sends a command to the radio.
/// </summary>
Expand Down Expand Up @@ -275,12 +256,10 @@ public bool PcrOpen()
_tcpStream = _ssl ? (Stream) new SslStream(_tcpClient.GetStream()) : _tcpClient.GetStream();
_tcpBuffer = new byte[_tcpClient.ReceiveBufferSize];
_listenActive = true;
_tcpListen = new Thread(ListenThread);
_tcpListen = new Thread(ListenThread) {IsBackground = true};
_tcpListen.Start();
if (!string.IsNullOrWhiteSpace(_password))
{
Send("<pwd>" + _password + "</pwd>");
}
PerformClientHello();
PerformClientAuth();
return true;
}
catch (Exception ex)
Expand All @@ -302,7 +281,7 @@ public bool PcrClose()
{
return true;
}
Send("<disconnect>");
SendWait("$DISCONNECT");
_listenActive = false;
_tcpListen.Abort();
_tcpBuffer = null;
Expand All @@ -316,5 +295,30 @@ public bool PcrClose()
return false;
}
}

private void PerformClientHello()
{
const float clientProtocolVersion = 2.0f;

var response = SendWait($"$HELLO {clientProtocolVersion}");
if (!response.StartsWith("$" + ClientErrorCode.SUC_HELLO_PASSED))
{
throw new InvalidOperationException("Cannot connect to server correctly. " + response + ".");
}
}

private void PerformClientAuth()
{
if (string.IsNullOrEmpty(_password))
{
return;
}

var response = SendWait("$AUTH \"" + _password + "\"");
if (!response.StartsWith("$" + ClientErrorCode.SUC_AUTH_PASSED))
{
throw new InvalidOperationException("Cannot authenticate with server correctly. " + response + ".");
}
}
}
}
30 changes: 30 additions & 0 deletions PCR1000.Network/Server/ClientErrorCodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// ReSharper disable InconsistentNaming
namespace PCR1000.Network.Server
{
internal enum ClientErrorCode
{
// Client Hello
ERR_HELLO_NOTFOUND,
ERR_HELLO_INVALID,
WAR_HELLO_UNKNOWN,
ERR_PROTOVER_TOOOLD,
SUC_HELLO_PASSED,

// Auth
ERR_AUTH_NOTFOUND,
ERR_AUTH_INVALID,
ERR_AUTH_INCORRECT,
SUC_AUTH_PASSED,

// Other
SUC_ECHO_RESPONSE,
SUC_HASCONTROL_RESPONSE,
SUC_TAKECONTROL_RESPONSE,
WAR_COMMAND_UNKNOWN,
INF_CLIENT_DISCONNECT,

// Query
ERR_QUERY_FAILED,
ERR_HASCONTROL_RESPONSE
}
}
Loading

0 comments on commit cb8043b

Please sign in to comment.