Changes to unify GenericTcpClient and GenericSshClient under ISocketStatus

This commit is contained in:
Heath Volmer
2016-09-27 17:30:41 -06:00
parent 69802c8bc5
commit 7137563e96
12 changed files with 54 additions and 87 deletions

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronSockets;
namespace PepperDash.Core
{
public delegate void GenericSocketStatusChangeEventDelegate(ISocketStatus client);
}

View File

@@ -2,6 +2,7 @@
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronSockets;
using Crestron.SimplSharp.Ssh;
using Crestron.SimplSharp.Ssh.Common;
@@ -25,11 +26,13 @@ namespace PepperDash.Core
/// <summary>
/// Event when the connection status changes.
/// </summary>
public event EventHandler<SshConnectionChangeEventArgs> ConnectionChange;
public event Crestron.SimplSharp.CrestronSockets.TCPClientSocketStatusChangeEventHandler SocketStatusChange;
//[Obsolete("Use SocketStatusChange instead")]
//public event EventHandler<SshConnectionChangeEventArgs> ConnectionChange;
/// <summary>
///
/// </summary>
public event GenericSocketStatusChangeEventDelegate SocketStatusChange;
/// <summary>
/// Address of server
@@ -57,16 +60,24 @@ namespace PepperDash.Core
public bool IsConnected
{
// returns false if no client or not connected
get { return UStatus == 2; }
get { return ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED; }
}
/// <summary>
///
/// </summary>
public Crestron.SimplSharp.CrestronSockets.SocketStatus ClientStatus
public SocketStatus ClientStatus
{
get { throw new NotImplementedException(); }
get { return _ClientStatus; }
private set
{
if (_ClientStatus == value)
return;
_ClientStatus = value;
OnConnectionChange();
}
}
SocketStatus _ClientStatus;
/// <summary>
/// Contains the familiar Simpl analog status values. This drives the ConnectionChange event
@@ -74,17 +85,8 @@ namespace PepperDash.Core
/// </summary>
public ushort UStatus
{
get { return _UStatus; }
private set
{
if (_UStatus == value)
return;
_UStatus = value;
OnConnectionChange();
}
get { return (ushort)_ClientStatus; }
}
ushort _UStatus;
/// <summary>
/// Determines whether client will attempt reconnection on failure. Default is true
@@ -226,7 +228,7 @@ namespace PepperDash.Core
PreviousUsername = Username;
//You can do it!
UStatus = 1;
ClientStatus = SocketStatus.SOCKET_STATUS_WAITING;
try
{
Client.Connect();
@@ -237,7 +239,7 @@ namespace PepperDash.Core
TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534);
TheStream.DataReceived += Stream_DataReceived;
Debug.Console(1, this, "Connected");
UStatus = 2;
ClientStatus = SocketStatus.SOCKET_STATUS_CONNECTED;
PreviousHostname = Hostname;
PreviousPassword = Password;
PreviousPort = Port;
@@ -268,7 +270,7 @@ namespace PepperDash.Core
// Sucess will not make it this far
Client.Disconnect();
UStatus = 3;
ClientStatus = SocketStatus.SOCKET_STATUS_CONNECT_FAILED;
HandleConnectionFailure();
}
@@ -286,7 +288,7 @@ namespace PepperDash.Core
if(TheStream != null)
TheStream.DataReceived -= Stream_DataReceived;
Client.Disconnect();
UStatus = 5;
ClientStatus = SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY;
Debug.Console(1, this, "Disconnected");
}
@@ -386,7 +388,7 @@ namespace PepperDash.Core
//Client.Dispose();
//Client = null;
}
UStatus = 4;
ClientStatus = SocketStatus.SOCKET_STATUS_BROKEN_REMOTELY;
HandleConnectionFailure();
}
@@ -395,8 +397,12 @@ namespace PepperDash.Core
/// </summary>
void OnConnectionChange()
{
if(ConnectionChange != null)
ConnectionChange(this, new SshConnectionChangeEventArgs(IsConnected, this));
//if(ConnectionChange != null)
// ConnectionChange(this, new SshConnectionChangeEventArgs(IsConnected, this));
var handler = SocketStatusChange;
if (handler != null)
SocketStatusChange(this);
}
#region IBasicCommunication Members
@@ -415,7 +421,7 @@ namespace PepperDash.Core
catch
{
Debug.Console(1, this, "Stream write failed. Disconnected, closing");
UStatus = 4;
ClientStatus = SocketStatus.SOCKET_STATUS_BROKEN_REMOTELY;
HandleConnectionFailure();
}
}
@@ -430,7 +436,7 @@ namespace PepperDash.Core
catch
{
Debug.Console(1, this, "Stream write failed. Disconnected, closing");
UStatus = 4;
ClientStatus = SocketStatus.SOCKET_STATUS_BROKEN_REMOTELY;
HandleConnectionFailure();
}
}

View File

@@ -26,7 +26,7 @@ namespace PepperDash.Core
/// <summary>
///
/// </summary>
public event TCPClientSocketStatusChangeEventHandler SocketStatusChange;
public event GenericSocketStatusChangeEventDelegate SocketStatusChange;
/// <summary>
///
@@ -107,7 +107,7 @@ namespace PepperDash.Core
DisconnectCalledByUser = false;
}
public void Disconnnect()
public void Disconnect()
{
DisconnectCalledByUser = true;
Client.DisconnectFromServer();
@@ -196,7 +196,7 @@ namespace PepperDash.Core
// Relay the event
var handler = SocketStatusChange;
if (handler != null)
SocketStatusChange(client, clientSocketStatus);
SocketStatusChange(this);
}
}

View File

@@ -1,35 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
namespace PepperDash.Core
{
/// <summary>
/// A wrapper class that creates a TCP client and gather for use within S+
/// </summary>
public class SplusGenericTcpIpClientWithGather
{
public GenericTcpIpClient Client { get; private set; }
public CommunicationGather Gather { get; private set; }
public SplusGenericTcpIpClientWithGather()
{
}
/// <summary>
/// In place of the useless contstructor, for S+ compatability
/// </summary>
/// <param name="key"></param>
/// <param name="host"></param>
/// <param name="port"></param>
/// <param name="bufferSize"></param>
/// <param name="delimiter"></param>
public void Initialize(string key, string host, int port, int bufferSize, char delimiter)
{
Client = new GenericTcpIpClient(key, host, port, bufferSize);
Gather = new CommunicationGather(Client, delimiter);
}
}
}

View File

@@ -1,16 +0,0 @@
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using Crestron.SimplSharp;
//using Newtonsoft.Json;
//namespace PepperDash.Core
//{
// public class SshConfig : TcpIpConfig
// {
// public string Username { get; set; }
// public string Password { get; set; }
// }
//}

View File

@@ -22,6 +22,7 @@ namespace PepperDash.Core
void SendText(string text);
void SendBytes(byte[] bytes);
void Connect();
void Disconnect();
}
/// <summary>
@@ -30,7 +31,7 @@ namespace PepperDash.Core
/// </summary>
public interface ISocketStatus : IBasicCommunication
{
event TCPClientSocketStatusChangeEventHandler SocketStatusChange;
event GenericSocketStatusChangeEventDelegate SocketStatusChange;
SocketStatus ClientStatus { get; }
}

View File

@@ -64,9 +64,8 @@
<ItemGroup>
<Compile Include="CommunicationExtras.cs" />
<Compile Include="Comm\CommunicationGather.cs" />
<Compile Include="Comm\GenericSocketStatusChangeEventArgs.cs" />
<Compile Include="Comm\GenericSshClient.cs" />
<Compile Include="Comm\GenericTcpIpClientWithGather.cs" />
<Compile Include="Comm\SshConfig.cs" />
<Compile Include="CoreInterfaces.cs" />
<Compile Include="Debug.cs" />
<Compile Include="Device.cs" />
@@ -85,7 +84,7 @@
<Programmer />
<ArchiveFilename>C:\Users\hvolm\Desktop\working\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\bin\PepperDash_Core.clz</ArchiveFilename>
<MinFirmwareVersion>1.007.0017</MinFirmwareVersion>
<CompiledOn>9/26/2016 9:02:28 AM</CompiledOn>
<CompiledOn>9/27/2016 5:17:31 PM</CompiledOn>
<AdditionalInfo />
<EmbedSourceArchive>False</EmbedSourceArchive>
<CopyTo />

View File

@@ -10,7 +10,7 @@
<ArchiveName />
</RequiredInfo>
<OptionalInfo>
<CompiledOn>9/26/2016 9:02:28 AM</CompiledOn>
<CompilerRev>1.0.0.14473</CompilerRev>
<CompiledOn>9/27/2016 5:17:31 PM</CompiledOn>
<CompilerRev>1.0.0.29325</CompilerRev>
</OptionalInfo>
</ProgramInfo>

View File

@@ -1,4 +1,4 @@
MainAssembly=PepperDash_Core.dll:ddd7e6504766069b1c909eac0c499966
MainAssembly=PepperDash_Core.dll:945e6c6366e369445585fb8cacdefe8e
MainAssemblyMinFirmwareVersion=1.007.0017
ü
DependencySource=Newtonsoft.Json.Compact.dll:ea996aa2ec65aa1878e7c9d09e37a896