Secure TCP client added, having one issue where the client is not connecting on initial connect call. Second call makes the connection. I think the nonsecure tcp client is working this way too, but need to verify

This commit is contained in:
Joshua Gutenplan
2017-03-10 00:59:51 -08:00
parent 721275ee2c
commit 408f58bf71
29 changed files with 415 additions and 5 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,308 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronSockets;
using PepperDash.Core;
namespace SecureTCP.SecureTCP.Client
{
public class GenericSecureTcpClient : Device, ISocketStatus, IAutoReconnect
{
/// <summary>
///
/// </summary>
public event EventHandler<GenericCommMethodReceiveBytesArgs> BytesReceived;
/// <summary>
///
/// </summary>
public event EventHandler<GenericCommMethodReceiveTextArgs> TextReceived;
/// <summary>
///
/// </summary>
//public event GenericSocketStatusChangeEventDelegate SocketStatusChange;
public event EventHandler<GenericSocketStatusChageEventArgs> ConnectionChange;
/// <summary>
/// Address of server
/// </summary>
public string Hostname { get; set; }
/// <summary>
/// Port on server
/// </summary>
public int Port { get; set; }
/// <summary>
/// Another damn S+ helper because S+ seems to treat large port nums as signed ints
/// which screws up things
/// </summary>
public ushort UPort
{
get { return Convert.ToUInt16(Port); }
set { Port = Convert.ToInt32(value); }
}
/// <summary>
/// Defaults to 2000
/// </summary>
public int BufferSize { get; set; }
public SecureTCPClient Client;
/// <summary>
///
/// </summary>
public bool IsConnected
{
get { return Client != null && Client.ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED; }
}
/// <summary>
/// S+ helper for IsConnected
/// </summary>
public ushort UIsConnected
{
get { return (ushort)(IsConnected ? 1 : 0); }
}
/// <summary>
///
/// </summary>
public SocketStatus ClientStatus
{
get
{
if (Client == null)
return SocketStatus.SOCKET_STATUS_NO_CONNECT;
return Client.ClientStatus;
}
}
/// <summary>
/// Contains the familiar Simpl analog status values. This drives the ConnectionChange event
/// and IsConnected with be true when this == 2.
/// </summary>
public ushort UStatus
{
get { return (ushort)ClientStatus; }
}
/// <summary>
///
/// </summary>
public string ClientStatusText { get { return ClientStatus.ToString(); } }
/// <summary>
///
/// </summary>
public string ConnectionFailure { get { return ClientStatus.ToString(); } }
/// <summary>
///
/// </summary>
public bool AutoReconnect { get; set; }
/// <summary>
/// S+ helper for AutoReconnect
/// </summary>
public ushort UAutoReconnect
{
get { return (ushort)(AutoReconnect ? 1 : 0); }
set { AutoReconnect = value == 1; }
}
/// <summary>
/// Milliseconds to wait before attempting to reconnect. Defaults to 5000
/// </summary>
public int AutoReconnectIntervalMs { get; set; }
/// <summary>
/// Set only when the disconnect method is called.
/// </summary>
bool DisconnectCalledByUser;
/// <summary>
///
/// </summary>
public bool Connected
{
get { return Client.ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED; }
}
CTimer RetryTimer;
public GenericSecureTcpClient(string key, string address, int port, int bufferSize)
: base(key)
{
Hostname = address;
Port = port;
BufferSize = bufferSize;
AutoReconnectIntervalMs = 5000;
}
public GenericSecureTcpClient()
: base("Uninitialized SecureTcpClient")
{
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler);
AutoReconnectIntervalMs = 5000;
BufferSize = 2000;
}
/// <summary>
/// Just to help S+ set the key
/// </summary>
public void Initialize(string key)
{
Key = key;
}
/// <summary>
/// Handles closing this up when the program shuts down
/// </summary>
void CrestronEnvironment_ProgramStatusEventHandler(eProgramStatusEventType programEventType)
{
if (programEventType == eProgramStatusEventType.Stopping)
{
if (Client != null)
{
Debug.Console(1, this, "Program stopping. Closing connection");
Client.DisconnectFromServer();
Client.Dispose();
}
}
}
public override bool Deactivate()
{
if(Client != null)
Client.SocketStatusChange -= this.Client_SocketStatusChange;
return true;
}
public void Connect()
{
if (IsConnected)
return;
if (string.IsNullOrEmpty(Hostname))
{
Debug.Console(1, Debug.ErrorLogLevel.Warning, "GenericSecureTcpClient '{0}': No address set", Key);
return;
}
if (Port < 1 || Port > 65535)
{
{
Debug.Console(1, Debug.ErrorLogLevel.Warning, "GenericSecureTcpClient '{0}': Invalid port", Key);
return;
}
}
Client = new SecureTCPClient(Hostname, Port, BufferSize);
Client.SocketStatusChange += Client_SocketStatusChange;
try
{
DisconnectCalledByUser = false;
SocketErrorCodes error = Client.ConnectToServer();
}
catch (Exception ex)
{
CrestronConsole.PrintLine("Secure Client could not connect. Error: {0}", ex.Message);
}
}
public void Disconnect()
{
DisconnectCalledByUser = true;
Client.DisconnectFromServer();
}
void ConnectToServerCallback(object o)
{
Client.ConnectToServer();
if (Client.ClientStatus != SocketStatus.SOCKET_STATUS_CONNECTED)
WaitAndTryReconnect();
}
void WaitAndTryReconnect()
{
Client.DisconnectFromServer();
Debug.Console(2, "Attempting reconnect, status={0}", Client.ClientStatus);
RetryTimer = new CTimer(ConnectToServerCallback, 1000);
}
void Receive(SecureTCPClient client, int numBytes)
{
if (numBytes > 0)
{
var bytes = client.IncomingDataBuffer.Take(numBytes).ToArray();
var bytesHandler = BytesReceived;
if (bytesHandler != null)
bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes));
var textHandler = TextReceived;
if (textHandler != null)
{
var str = Encoding.GetEncoding(28591).GetString(bytes, 0, bytes.Length);
textHandler(this, new GenericCommMethodReceiveTextArgs(str));
}
}
Client.ReceiveDataAsync(Receive);
}
/// <summary>
/// General send method
/// </summary>
public void SendText(string text)
{
var bytes = Encoding.GetEncoding(28591).GetBytes(text);
// Check debug level before processing byte array
//if (Debug.Level == 2)
// Debug.Console(2, this, "Sending {0} bytes: '{1}'", bytes.Length, ComTextHelper.GetEscapedText(bytes));
Client.SendData(bytes, bytes.Length);
}
/// <summary>
/// This is useful from console and...?
/// </summary>
public void SendEscapedText(string text)
{
var unescapedText = Regex.Replace(text, @"\\x([0-9a-fA-F][0-9a-fA-F])", s =>
{
var hex = s.Groups[1].Value;
return ((char)Convert.ToByte(hex, 16)).ToString();
});
SendText(unescapedText);
}
public void SendBytes(byte[] bytes)
{
//if (Debug.Level == 2)
// Debug.Console(2, this, "Sending {0} bytes: '{1}'", bytes.Length, ComTextHelper.GetEscapedText(bytes));
Client.SendData(bytes, bytes.Length);
}
void Client_SocketStatusChange(SecureTCPClient client, SocketStatus clientSocketStatus)
{
Debug.Console(2, this, "Socket status change {0} ({1})", clientSocketStatus, ClientStatusText);
if (client.ClientStatus != SocketStatus.SOCKET_STATUS_CONNECTED && !DisconnectCalledByUser)
WaitAndTryReconnect();
// Probably doesn't need to be a switch since all other cases were eliminated
switch (clientSocketStatus)
{
case SocketStatus.SOCKET_STATUS_CONNECTED:
Client.ReceiveDataAsync(Receive);
DisconnectCalledByUser = false;
break;
}
var handler = ConnectionChange;
if (handler != null)
ConnectionChange(this, new GenericSocketStatusChageEventArgs(this));
}
}
}

View File

@@ -67,6 +67,7 @@
<Compile Include="Comm\eControlMethods.cs" />
<Compile Include="Comm\FINISH CommStatic.cs" />
<Compile Include="Comm\CommunicationGather.cs" />
<Compile Include="Comm\GenericSecureTcpClient.cs" />
<Compile Include="Comm\GenericSocketStatusChangeEventArgs.cs" />
<Compile Include="Comm\GenericSshClient.cs" />
<Compile Include="CoreInterfaces.cs" />

View File

@@ -0,0 +1,67 @@
2/28/2017 3:37:03 PM, Info: Initializing SIMPLSharp Services...
2/28/2017 3:37:03 PM, Info: ProjectInfo successfully initialized.
2/28/2017 3:41:39 PM, Info: Saving project information...
2/28/2017 3:41:39 PM, Info: Saving project information...
2/28/2017 3:46:39 PM, Info: Saving project information...
2/28/2017 3:46:39 PM, Info: Saving project information...
2/28/2017 3:51:39 PM, Info: Saving project information...
2/28/2017 3:51:39 PM, Info: Saving project information...
2/28/2017 3:56:39 PM, Info: Saving project information...
2/28/2017 3:56:39 PM, Info: Saving project information...
2/28/2017 4:01:39 PM, Info: Saving project information...
2/28/2017 4:01:39 PM, Info: Saving project information...
2/28/2017 4:06:39 PM, Info: Saving project information...
2/28/2017 4:06:39 PM, Info: Saving project information...
2/28/2017 4:11:39 PM, Info: Saving project information...
2/28/2017 4:11:39 PM, Info: Saving project information...
2/28/2017 4:16:39 PM, Info: Saving project information...
2/28/2017 4:16:39 PM, Info: Saving project information...
2/28/2017 4:21:39 PM, Info: Saving project information...
2/28/2017 4:21:39 PM, Info: Saving project information...
2/28/2017 4:26:39 PM, Info: Saving project information...
2/28/2017 4:26:39 PM, Info: Saving project information...
2/28/2017 4:31:39 PM, Info: Saving project information...
2/28/2017 4:31:39 PM, Info: Saving project information...
2/28/2017 4:36:39 PM, Info: Saving project information...
2/28/2017 4:36:39 PM, Info: Saving project information...
2/28/2017 4:41:39 PM, Info: Saving project information...
2/28/2017 4:41:39 PM, Info: Saving project information...
2/28/2017 4:46:39 PM, Info: Saving project information...
2/28/2017 4:46:39 PM, Info: Saving project information...
2/28/2017 4:51:39 PM, Info: Saving project information...
2/28/2017 4:51:39 PM, Info: Saving project information...
2/28/2017 4:56:39 PM, Info: Saving project information...
2/28/2017 4:56:39 PM, Info: Saving project information...
2/28/2017 5:01:39 PM, Info: Saving project information...
2/28/2017 5:01:39 PM, Info: Saving project information...
2/28/2017 5:06:39 PM, Info: Saving project information...
2/28/2017 5:06:39 PM, Info: Saving project information...
2/28/2017 5:11:39 PM, Info: Saving project information...
2/28/2017 5:11:39 PM, Info: Saving project information...
2/28/2017 5:16:39 PM, Info: Saving project information...
2/28/2017 5:16:39 PM, Info: Saving project information...
2/28/2017 5:21:39 PM, Info: Saving project information...
2/28/2017 5:21:39 PM, Info: Saving project information...
2/28/2017 5:26:39 PM, Info: Saving project information...
2/28/2017 5:26:39 PM, Info: Saving project information...
2/28/2017 5:31:39 PM, Info: Saving project information...
2/28/2017 5:31:39 PM, Info: Saving project information...
2/28/2017 5:36:39 PM, Info: Saving project information...
2/28/2017 5:36:39 PM, Info: Saving project information...
2/28/2017 5:41:39 PM, Info: Saving project information...
2/28/2017 5:41:39 PM, Info: Saving project information...
2/28/2017 5:46:39 PM, Info: Saving project information...
2/28/2017 5:46:39 PM, Info: Saving project information...
2/28/2017 5:51:39 PM, Info: Saving project information...
2/28/2017 5:51:39 PM, Info: Saving project information...
2/28/2017 5:56:39 PM, Info: Saving project information...
2/28/2017 5:56:39 PM, Info: Saving project information...
2/28/2017 6:01:39 PM, Info: Saving project information...
2/28/2017 6:01:39 PM, Info: Saving project information...
2/28/2017 6:06:39 PM, Info: Saving project information...
2/28/2017 6:06:39 PM, Info: Saving project information...
2/28/2017 6:11:39 PM, Info: Saving project information...
2/28/2017 6:11:39 PM, Info: Saving project information...
2/28/2017 6:16:39 PM, Info: Saving project information...
2/28/2017 6:16:39 PM, Info: Saving project information...
2/28/2017 6:20:39 PM, Info: Terminating SIMPLSharp Services

View File

@@ -0,0 +1,3 @@
2/28/2017 11:59:56 PM, Info: Initializing SIMPLSharp Services...
2/28/2017 11:59:56 PM, Info: ProjectInfo successfully initialized.
3/2/2017 1:03:52 AM, Info: Terminating SIMPLSharp Services

View File

@@ -0,0 +1,3 @@
3/3/2017 12:07:38 AM, Info: Initializing SIMPLSharp Services...
3/3/2017 12:07:38 AM, Info: ProjectInfo successfully initialized.
3/3/2017 1:09:41 AM, Info: Terminating SIMPLSharp Services

View File

@@ -0,0 +1,3 @@
3/3/2017 8:17:06 PM, Info: Initializing SIMPLSharp Services...
3/3/2017 8:17:06 PM, Info: ProjectInfo successfully initialized.
3/3/2017 8:27:24 PM, Info: Terminating SIMPLSharp Services

View File

@@ -0,0 +1,3 @@
3/3/2017 9:03:55 PM, Info: Initializing SIMPLSharp Services...
3/3/2017 9:03:55 PM, Info: ProjectInfo successfully initialized.
3/3/2017 9:06:43 PM, Info: Terminating SIMPLSharp Services

View File

@@ -0,0 +1,3 @@
3/3/2017 9:07:02 PM, Info: Initializing SIMPLSharp Services...
3/3/2017 9:07:02 PM, Info: ProjectInfo successfully initialized.
3/4/2017 11:03:40 AM, Info: Terminating SIMPLSharp Services

View File

@@ -0,0 +1,3 @@
3/6/2017 4:15:17 PM, Info: Initializing SIMPLSharp Services...
3/6/2017 4:15:18 PM, Info: ProjectInfo successfully initialized.
3/7/2017 11:02:41 AM, Info: Terminating SIMPLSharp Services

View File

@@ -0,0 +1,3 @@
3/7/2017 11:30:02 AM, Info: Initializing SIMPLSharp Services...
3/7/2017 11:30:02 AM, Info: ProjectInfo successfully initialized.
3/7/2017 2:27:46 PM, Info: Terminating SIMPLSharp Services

View File

@@ -0,0 +1,3 @@
3/8/2017 10:36:13 AM, Info: Initializing SIMPLSharp Services...
3/8/2017 10:36:13 AM, Info: ProjectInfo successfully initialized.
3/8/2017 5:47:06 PM, Info: Terminating SIMPLSharp Services

View File

@@ -10,11 +10,11 @@
<ArchiveName />
</RequiredInfo>
<OptionalInfo>
<CompiledOn>2/27/2017 12:04:27 PM</CompiledOn>
<CompilerRev>1.0.6267.21733</CompilerRev>
<CompiledOn>3/9/2017 11:48:28 PM</CompiledOn>
<CompilerRev>1.0.6277.42853</CompilerRev>
</OptionalInfo>
<Plugin>
<Version>Crestron.SIMPLSharp, Version=2.0.48.0, Culture=neutral, PublicKeyToken=812d080f93e2de10</Version>
<Include4.dat />
<Version>Crestron.SIMPLSharp, Version=2.0.52.0, Culture=neutral, PublicKeyToken=812d080f93e2de10</Version>
<Include4.dat>2.05.020</Include4.dat>
</Plugin>
</ProgramInfo>

View File

@@ -1,4 +1,4 @@
MainAssembly=PepperDash_Core.dll:ea8b7cdd5d032cd2744d5213412c61f1
MainAssembly=PepperDash_Core.dll:27cdfa30fe4a62d52b62944b0c35d6e3
MainAssemblyMinFirmwareVersion=1.007.0017
MainAssemblyResource=SimplSharpData.dat:315526abf906cded47fb0c7510266a7e
ü

View File

@@ -18,3 +18,13 @@ C:\Users\hvolmer\Desktop\working\pepperdash-simplsharp-core\Pepperdash Core\Pepp
C:\Users\hvolmer\Desktop\working\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\obj\Debug\ResolveAssemblyReference.cache
C:\Users\hvolmer\Desktop\working\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\obj\Debug\PepperDash_Core.dll
C:\Users\hvolmer\Desktop\working\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\obj\Debug\PepperDash_Core.pdb
C:\P\BitBucket\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\bin\PepperDash_Core.dll
C:\P\BitBucket\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\bin\PepperDash_Core.pdb
C:\P\BitBucket\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\bin\SimplSharpCustomAttributesInterface.dll
C:\P\BitBucket\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\bin\SimplSharpHelperInterface.dll
C:\P\BitBucket\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\bin\SimplSharpNewtonsoft.dll
C:\P\BitBucket\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\bin\SimplSharpSQLHelperInterface.dll
C:\P\BitBucket\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\bin\SimplSharpReflectionInterface.dll
C:\P\BitBucket\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\obj\Debug\ResolveAssemblyReference.cache
C:\P\BitBucket\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\obj\Debug\PepperDash_Core.dll
C:\P\BitBucket\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\obj\Debug\PepperDash_Core.pdb