mirror of
https://github.com/PepperDash/PepperDashCore.git
synced 2026-01-11 19:44:44 +00:00
Working on Dynamic Server. Also updated the Generic Socket event args class to be just event args class and contain other event args as well. Should not affect anything as I did not change the name of the event args class defined, just changed the file name to show that it has multiple EventArgs definitions.
This commit is contained in:
@@ -1,20 +1,19 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 10.00
|
||||
# Visual Studio 2008
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PepperDash_Core", "Pepperdash Core\PepperDash_Core.csproj", "{87E29B4C-569B-4368-A4ED-984AC1440C96}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{87E29B4C-569B-4368-A4ED-984AC1440C96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{87E29B4C-569B-4368-A4ED-984AC1440C96}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{87E29B4C-569B-4368-A4ED-984AC1440C96}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{87E29B4C-569B-4368-A4ED-984AC1440C96}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
Microsoft Visual Studio Solution File, Format Version 10.00
|
||||
# Visual Studio 2008
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PepperDash_Core", "Pepperdash Core\PepperDash_Core.csproj", "{87E29B4C-569B-4368-A4ED-984AC1440C96}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{87E29B4C-569B-4368-A4ED-984AC1440C96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{87E29B4C-569B-4368-A4ED-984AC1440C96}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{87E29B4C-569B-4368-A4ED-984AC1440C96}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{87E29B4C-569B-4368-A4ED-984AC1440C96}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
19
Pepperdash Core/Pepperdash Core.slnold
Normal file
19
Pepperdash Core/Pepperdash Core.slnold
Normal file
@@ -0,0 +1,19 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 10.00
|
||||
# Visual Studio 2008
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PepperDash_Core", "Pepperdash Core\PepperDash_Core.csproj", "{87E29B4C-569B-4368-A4ED-984AC1440C96}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{87E29B4C-569B-4368-A4ED-984AC1440C96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{87E29B4C-569B-4368-A4ED-984AC1440C96}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{87E29B4C-569B-4368-A4ED-984AC1440C96}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{87E29B4C-569B-4368-A4ED-984AC1440C96}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
Binary file not shown.
@@ -3,12 +3,51 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
using Crestron.SimplSharp.CrestronSockets;
|
||||
using PepperDash.Core;
|
||||
|
||||
namespace PepperDash_Core
|
||||
{
|
||||
public class DynamicTCPServer
|
||||
#region UNUSED OBJECT JUST IN CASE TILL DONE WITH CODING
|
||||
//public class DynamicServer
|
||||
//{
|
||||
// public object Server
|
||||
// {
|
||||
// get
|
||||
// {
|
||||
// if(Secure)
|
||||
// return secureServer;
|
||||
// else
|
||||
// return unsecureServer;
|
||||
// }
|
||||
// private set;
|
||||
// }
|
||||
// public bool Secure { get; set; }
|
||||
// private TCPServer unsecureServer;
|
||||
// private SecureTCPServer secureServer;
|
||||
// public DynamicServer(bool secure)
|
||||
// {
|
||||
// Secure = secure;
|
||||
// }
|
||||
//}
|
||||
#endregion
|
||||
|
||||
public class DynamicTCPServer : Device
|
||||
{
|
||||
public event EventHandler<GenericCommMethodReceiveTextArgs> TextReceived;
|
||||
|
||||
public event EventHandler<DynamicTCPServerSocketStatusChangeEventArgs> ClientConnectionChange;
|
||||
|
||||
public event EventHandler<DynamicTCPServerStateChangedEventArgs> ServerStateChange;
|
||||
|
||||
/// <summary>
|
||||
/// Secure or unsecure TCP server. Defaults to Unsecure or standard TCP server without SSL
|
||||
/// </summary>
|
||||
public bool Secure { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// S+ Helper for Secure bool
|
||||
/// </summary>
|
||||
public ushort uSecure
|
||||
{
|
||||
set
|
||||
@@ -20,6 +59,234 @@ namespace PepperDash_Core
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bool showing if socket is connected
|
||||
/// </summary>
|
||||
public bool IsConnected
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Secure && SecureServer != null)
|
||||
return SecureServer.State == ServerState.SERVER_CONNECTED;
|
||||
else if (!Secure && UnsecureServer != null)
|
||||
return UnsecureServer.State == ServerState.SERVER_CONNECTED;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// S+ helper for IsConnected
|
||||
/// </summary>
|
||||
public ushort uIsConnected
|
||||
{
|
||||
get { return (ushort)(IsConnected ? 1 : 0); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bool showing if socket is connected
|
||||
/// </summary>
|
||||
public bool IsListening
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Secure && SecureServer != null)
|
||||
return SecureServer.State == ServerState.SERVER_LISTENING;
|
||||
else if (!Secure && UnsecureServer != null)
|
||||
return UnsecureServer.State == ServerState.SERVER_LISTENING;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public ushort MaxConnections { get; set; } // should be set by parameter in SIMPL+ in the MAIN method, Should not ever need to be configurable
|
||||
|
||||
/// <summary>
|
||||
/// S+ helper for IsConnected
|
||||
/// </summary>
|
||||
public ushort uIsListening
|
||||
{
|
||||
get { return (ushort)(IsListening ? 1 : 0); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Number of clients currently connected.
|
||||
/// </summary>
|
||||
public ushort NumberOfClientsConnected { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Port on server
|
||||
/// </summary>
|
||||
public int Port { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// S+ helper
|
||||
/// </summary>
|
||||
public ushort UPort
|
||||
{
|
||||
get { return Convert.ToUInt16(Port); }
|
||||
set { Port = Convert.ToInt32(value); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bool to show whether the server requires a preshared key. Must be set the same in the client, and if true shared keys must be identical on server/client
|
||||
/// </summary>
|
||||
public bool RequiresPresharedKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// S+ helper for requires shared key bool
|
||||
/// </summary>
|
||||
public ushort uRequiresPresharedKey
|
||||
{
|
||||
set
|
||||
{
|
||||
if (value == 1)
|
||||
RequiresPresharedKey = true;
|
||||
else
|
||||
RequiresPresharedKey = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SharedKey is sent for varification to the server. Shared key can be any text (255 char limit in SIMPL+ Module), but must match the Shared Key on the Server module.
|
||||
/// If SharedKey changes while server is listening or clients are connected, disconnect and stop listening will be called
|
||||
/// </summary>
|
||||
public string SharedKey
|
||||
{
|
||||
get
|
||||
{
|
||||
return _SharedKey;
|
||||
}
|
||||
set
|
||||
{
|
||||
|
||||
_SharedKey = value;
|
||||
}
|
||||
}
|
||||
private string _SharedKey;
|
||||
|
||||
/// <summary>
|
||||
/// flags to show the server is waiting for client at index to send the shared key
|
||||
/// </summary>
|
||||
public List<uint> WaitingForSharedKey = new List<uint>();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Defaults to 2000
|
||||
/// </summary>
|
||||
public int BufferSize { get; set; }
|
||||
|
||||
public string OnlyAcceptConnectionFromAddress { get; set; }
|
||||
|
||||
public SecureTCPServer SecureServer;
|
||||
public TCPServer UnsecureServer;
|
||||
|
||||
|
||||
//base class constructor
|
||||
public DynamicTCPServer()
|
||||
: base("Uninitialized Dynamic TCP Server")
|
||||
{
|
||||
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler);
|
||||
BufferSize = 2000;
|
||||
Secure = false;
|
||||
}
|
||||
|
||||
|
||||
void CrestronEnvironment_ProgramStatusEventHandler(eProgramStatusEventType programEventType)
|
||||
{
|
||||
if (programEventType == eProgramStatusEventType.Stopping)
|
||||
{
|
||||
Debug.Console(1, this, "Program stopping. Closing server");
|
||||
DisconnectAllClients();
|
||||
StopListening();
|
||||
}
|
||||
}
|
||||
|
||||
public void Listen()
|
||||
{
|
||||
if (Port < 1 || Port > 65535)
|
||||
{
|
||||
Debug.Console(1, Debug.ErrorLogLevel.Warning, "GenericSecureTcpClient '{0}': Invalid port", Key);
|
||||
ErrorLog.Warn(string.Format("GenericSecureTcpClient '{0}': Invalid port", Key));
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrEmpty(SharedKey) && RequiresPresharedKey)
|
||||
{
|
||||
Debug.Console(1, Debug.ErrorLogLevel.Warning, "GenericSecureTcpClient '{0}': No Shared Key set", Key);
|
||||
ErrorLog.Warn(string.Format("GenericSecureTcpClient '{0}': No Shared Key set", Key));
|
||||
return;
|
||||
}
|
||||
if (Secure)
|
||||
{
|
||||
if (SecureServer.State == ServerState.SERVER_LISTENING)
|
||||
return;
|
||||
SecureServer = new SecureTCPServer(Port, MaxConnections);
|
||||
SecureServer.SocketStatusChange += new SecureTCPServerSocketStatusChangeEventHandler(SecureServer_SocketStatusChange);
|
||||
SecureServer.WaitForConnectionAsync(IPAddress.Any, SecureConnectCallback);
|
||||
Debug.Console(0, "Secure Server Status: {0}, Socket Status: {1}\r\n", SecureServer.State.ToString(), SecureServer.ServerSocketStatus);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (UnsecureServer.State == ServerState.SERVER_LISTENING)
|
||||
return;
|
||||
UnsecureServer = new TCPServer(Port, MaxConnections);
|
||||
UnsecureServer.SocketStatusChange += new TCPServerSocketStatusChangeEventHandler(UnsecureServer_SocketStatusChange);
|
||||
UnsecureServer.WaitForConnectionAsync(IPAddress.Any, UnsecureConnectCallback);
|
||||
Debug.Console(0, "Unsecure Server Status: {0}, Socket Status: {1}\r\n", UnsecureServer.State.ToString(), UnsecureServer.ServerSocketStatus);
|
||||
}
|
||||
}
|
||||
|
||||
public void StopListening()
|
||||
{
|
||||
if (SecureServer != null && SecureServer.State == ServerState.SERVER_LISTENING)
|
||||
SecureServer.Stop();
|
||||
if (UnsecureServer != null && UnsecureServer.State == ServerState.SERVER_LISTENING)
|
||||
UnsecureServer.Stop();
|
||||
var handler = ServerStateChange;
|
||||
if (ServerStateChange != null)
|
||||
ServerStateChange(this, new DynamicTCPServerStateChangedEventArgs(this, Secure));
|
||||
}
|
||||
|
||||
public void DisconnectAllClients()
|
||||
{
|
||||
if (SecureServer != null && SecureServer.NumberOfClientsConnected > 0)
|
||||
SecureServer.DisconnectAll();
|
||||
if (UnsecureServer != null && UnsecureServer.NumberOfClientsConnected > 0)
|
||||
UnsecureServer.DisconnectAll();
|
||||
}
|
||||
|
||||
void onConnectionChange()
|
||||
{
|
||||
var handler = ClientConnectionChange;
|
||||
if (handler != null)
|
||||
{
|
||||
if (Secure)
|
||||
ClientConnectionChange(this, new DynamicTCPServerSocketStatusChangeEventArgs(SecureServer, Secure));
|
||||
else
|
||||
ClientConnectionChange(this, new DynamicTCPServerSocketStatusChangeEventArgs(UnsecureServer, Secure));
|
||||
}
|
||||
}
|
||||
|
||||
void SecureServer_SocketStatusChange(SecureTCPServer mySecureTCPServer, uint clientIndex, SocketStatus serverSocketStatus)
|
||||
{
|
||||
onConnectionChange();//Go to simpl and send the server and whether it is secure or not. Could check secure from Simpl+ but better to use the passed
|
||||
//variable in case we need to use this in Pro. Simpl+ won't use arguments, will just check the uIsConnected Property. Currently Simpl+ is just going to report
|
||||
//connected not reporting by client
|
||||
}
|
||||
|
||||
void UnsecureServer_SocketStatusChange(TCPServer mySecureTCPServer, uint clientIndex, SocketStatus serverSocketStatus)
|
||||
{
|
||||
onConnectionChange();
|
||||
}
|
||||
|
||||
void SecureConnectCallback(SecureTCPServer mySecureTCPServer, uint clientIndex)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void UnsecureConnectCallback(TCPServer myTCPServer, uint clientIndex)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
63
Pepperdash Core/Pepperdash Core/Comm/EventArgs.cs
Normal file
63
Pepperdash Core/Pepperdash Core/Comm/EventArgs.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
using Crestron.SimplSharp.CrestronSockets;
|
||||
|
||||
|
||||
namespace PepperDash.Core
|
||||
{
|
||||
#region GenericSocketStatusChangeEventArgs
|
||||
public delegate void GenericSocketStatusChangeEventDelegate(ISocketStatus client);
|
||||
|
||||
public class GenericSocketStatusChageEventArgs : EventArgs
|
||||
{
|
||||
public ISocketStatus Client { get; private set; }
|
||||
|
||||
public GenericSocketStatusChageEventArgs() { }
|
||||
|
||||
public GenericSocketStatusChageEventArgs(ISocketStatus client)
|
||||
{
|
||||
Client = client;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region DynamicTCPServerStateChangedEventArgs
|
||||
public delegate void DynamicTCPServerStateChangedEventDelegate(object server);
|
||||
|
||||
public class DynamicTCPServerStateChangedEventArgs : EventArgs
|
||||
{
|
||||
public bool Secure { get; private set; }
|
||||
public object Server { get; private set; }
|
||||
|
||||
public DynamicTCPServerStateChangedEventArgs() { }
|
||||
|
||||
public DynamicTCPServerStateChangedEventArgs(object server, bool secure)
|
||||
{
|
||||
Secure = secure;
|
||||
Server = server;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region DynamicTCPServerSocketStatusChangeEventArgs
|
||||
public delegate void DynamicTCPServerSocketStatusChangeEventDelegate(object server);
|
||||
|
||||
public class DynamicTCPServerSocketStatusChangeEventArgs : EventArgs
|
||||
{
|
||||
public bool Secure { get; private set; }
|
||||
public object Server { get; private set; }
|
||||
|
||||
public DynamicTCPServerSocketStatusChangeEventArgs() { }
|
||||
|
||||
public DynamicTCPServerSocketStatusChangeEventArgs(object server, bool secure)
|
||||
{
|
||||
Secure = secure;
|
||||
Server = server;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
@@ -11,22 +11,17 @@ namespace PepperDash.Core
|
||||
{
|
||||
public class GenericSecureTcpClient : Device, ISocketStatus, IAutoReconnect
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
#region Events
|
||||
|
||||
public event EventHandler<GenericCommMethodReceiveBytesArgs> BytesReceived;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public event EventHandler<GenericCommMethodReceiveTextArgs> TextReceived;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
//public event GenericSocketStatusChangeEventDelegate SocketStatusChange;
|
||||
public event EventHandler<GenericSocketStatusChageEventArgs> ConnectionChange;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties & Variables
|
||||
/// <summary>
|
||||
/// Address of server
|
||||
/// </summary>
|
||||
@@ -38,8 +33,7 @@ namespace PepperDash.Core
|
||||
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
|
||||
/// S+ helper
|
||||
/// </summary>
|
||||
public ushort UPort
|
||||
{
|
||||
@@ -47,8 +41,14 @@ namespace PepperDash.Core
|
||||
set { Port = Convert.ToInt32(value); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bool to show whether the server requires a preshared key. This is used in the DynamicTCPServer class
|
||||
/// </summary>
|
||||
public bool RequiresPresharedKey { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// S+ helper for requires shared key bool
|
||||
/// </summary>
|
||||
public ushort uRequiresPresharedKey
|
||||
{
|
||||
set
|
||||
@@ -63,7 +63,6 @@ namespace PepperDash.Core
|
||||
/// <summary>
|
||||
/// SharedKey is sent for varification to the server. Shared key can be any text (255 char limit in SIMPL+ Module), but must match the Shared Key on the Server module
|
||||
/// </summary>
|
||||
private string _SharedKey;
|
||||
public string SharedKey
|
||||
{
|
||||
get
|
||||
@@ -77,7 +76,11 @@ namespace PepperDash.Core
|
||||
_SharedKey = value;
|
||||
}
|
||||
}
|
||||
private string _SharedKey;
|
||||
|
||||
/// <summary>
|
||||
/// flag to show the client is waiting for the server to send the shared key
|
||||
/// </summary>
|
||||
private bool WaitingForSharedKeyResponse { get; set; }
|
||||
|
||||
/// <summary>
|
||||
@@ -85,10 +88,8 @@ namespace PepperDash.Core
|
||||
/// </summary>
|
||||
public int BufferSize { get; set; }
|
||||
|
||||
public SecureTCPClient Client;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// Bool showing if socket is connected
|
||||
/// </summary>
|
||||
public bool IsConnected
|
||||
{
|
||||
@@ -104,7 +105,7 @@ namespace PepperDash.Core
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// Client socket status Read only
|
||||
/// </summary>
|
||||
public SocketStatus ClientStatus
|
||||
{
|
||||
@@ -118,7 +119,7 @@ namespace PepperDash.Core
|
||||
|
||||
/// <summary>
|
||||
/// Contains the familiar Simpl analog status values. This drives the ConnectionChange event
|
||||
/// and IsConnected with be true when this == 2.
|
||||
/// and IsConnected would be true when this == 2.
|
||||
/// </summary>
|
||||
public ushort UStatus
|
||||
{
|
||||
@@ -126,17 +127,12 @@ namespace PepperDash.Core
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// Status text shows the message associated with socket status
|
||||
/// </summary>
|
||||
public string ClientStatusText { get { return ClientStatus.ToString(); } }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string ConnectionFailure { get { return ClientStatus.ToString(); } }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// bool to track if auto reconnect should be set on the socket
|
||||
/// </summary>
|
||||
public bool AutoReconnect { get; set; }
|
||||
|
||||
@@ -154,20 +150,27 @@ namespace PepperDash.Core
|
||||
public int AutoReconnectIntervalMs { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Set only when the disconnect method is called.
|
||||
/// Flag Set only when the disconnect method is called.
|
||||
/// </summary>
|
||||
bool DisconnectCalledByUser;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// Connected bool
|
||||
/// </summary>
|
||||
public bool Connected
|
||||
{
|
||||
get { return Client.ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED; }
|
||||
}
|
||||
|
||||
CTimer RetryTimer;
|
||||
CTimer RetryTimer; //private Timer for auto reconnect
|
||||
|
||||
public SecureTCPClient Client; //Secure Client Class
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
//Base class constructor
|
||||
public GenericSecureTcpClient(string key, string address, int port, int bufferSize)
|
||||
: base(key)
|
||||
{
|
||||
@@ -177,14 +180,17 @@ namespace PepperDash.Core
|
||||
AutoReconnectIntervalMs = 5000;
|
||||
}
|
||||
|
||||
//base class constructor
|
||||
public GenericSecureTcpClient()
|
||||
: base("Uninitialized SecureTcpClient")
|
||||
{
|
||||
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler);
|
||||
AutoReconnectIntervalMs = 5000;
|
||||
BufferSize = 2000;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
/// <summary>
|
||||
/// Just to help S+ set the key
|
||||
/// </summary>
|
||||
@@ -209,6 +215,10 @@ namespace PepperDash.Core
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deactivate the client. Unregisters the socket status change event and returns true.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override bool Deactivate()
|
||||
{
|
||||
if(Client != null)
|
||||
@@ -216,6 +226,9 @@ namespace PepperDash.Core
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Connect Method. Will return if already connected. Will write errors if missing address, port, or unique key/name.
|
||||
/// </summary>
|
||||
public void Connect()
|
||||
{
|
||||
if (IsConnected)
|
||||
@@ -224,20 +237,21 @@ namespace PepperDash.Core
|
||||
if (string.IsNullOrEmpty(Hostname))
|
||||
{
|
||||
Debug.Console(1, Debug.ErrorLogLevel.Warning, "GenericSecureTcpClient '{0}': No address set", Key);
|
||||
ErrorLog.Warn(string.Format("GenericSecureTcpClient '{0}': No address set", Key));
|
||||
return;
|
||||
}
|
||||
if (Port < 1 || Port > 65535)
|
||||
{
|
||||
Debug.Console(1, Debug.ErrorLogLevel.Warning, "GenericSecureTcpClient '{0}': Invalid port", Key);
|
||||
ErrorLog.Warn(string.Format("GenericSecureTcpClient '{0}': Invalid port", Key));
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrEmpty(SharedKey) && RequiresPresharedKey)
|
||||
{
|
||||
Debug.Console(1, Debug.ErrorLogLevel.Warning, "GenericSecureTcpClient '{0}': No Shared Key set", Key);
|
||||
ErrorLog.Warn(string.Format("GenericSecureTcpClient '{0}': No Shared Key set", Key));
|
||||
return;
|
||||
}
|
||||
if (Client != null)
|
||||
Client.Dispose();
|
||||
Client = new SecureTCPClient(Hostname, Port, BufferSize);
|
||||
Client.SocketStatusChange += Client_SocketStatusChange;
|
||||
try
|
||||
@@ -253,12 +267,19 @@ namespace PepperDash.Core
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disconnect client. Does not dispose.
|
||||
/// </summary>
|
||||
public void Disconnect()
|
||||
{
|
||||
DisconnectCalledByUser = true;
|
||||
Client.DisconnectFromServer();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// callback after connection made
|
||||
/// </summary>
|
||||
/// <param name="o"></param>
|
||||
void ConnectToServerCallback(object o)
|
||||
{
|
||||
Client.ConnectToServer();
|
||||
@@ -266,6 +287,9 @@ namespace PepperDash.Core
|
||||
WaitAndTryReconnect();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called from Socket Status change if auto reconnect and socket disconnected (Not disconnected by user)
|
||||
/// </summary>
|
||||
void WaitAndTryReconnect()
|
||||
{
|
||||
Client.DisconnectFromServer();
|
||||
@@ -273,6 +297,11 @@ namespace PepperDash.Core
|
||||
RetryTimer = new CTimer(ConnectToServerCallback, AutoReconnectIntervalMs);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Receive callback
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="numBytes"></param>
|
||||
void Receive(SecureTCPClient client, int numBytes)
|
||||
{
|
||||
if (numBytes > 0)
|
||||
@@ -325,6 +354,11 @@ namespace PepperDash.Core
|
||||
Client.SendData(bytes, bytes.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SocketStatusChange Callback
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="clientSocketStatus"></param>
|
||||
void Client_SocketStatusChange(SecureTCPClient client, SocketStatus clientSocketStatus)
|
||||
{
|
||||
Debug.Console(2, this, "Socket status change {0} ({1})", clientSocketStatus, ClientStatusText);
|
||||
@@ -344,6 +378,7 @@ namespace PepperDash.Core
|
||||
var handler = ConnectionChange;
|
||||
if (handler != null)
|
||||
ConnectionChange(this, new GenericSocketStatusChageEventArgs(this));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
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);
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class GenericSocketStatusChageEventArgs : EventArgs
|
||||
{
|
||||
public ISocketStatus Client { get; private set; }
|
||||
|
||||
public GenericSocketStatusChageEventArgs() { }
|
||||
|
||||
public GenericSocketStatusChageEventArgs(ISocketStatus client)
|
||||
{
|
||||
Client = client;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -69,7 +69,7 @@
|
||||
<Compile Include="Comm\FINISH CommStatic.cs" />
|
||||
<Compile Include="Comm\CommunicationGather.cs" />
|
||||
<Compile Include="Comm\GenericSecureTcpClient.cs" />
|
||||
<Compile Include="Comm\GenericSocketStatusChangeEventArgs.cs" />
|
||||
<Compile Include="Comm\EventArgs.cs" />
|
||||
<Compile Include="Comm\GenericSshClient.cs" />
|
||||
<Compile Include="CoreInterfaces.cs" />
|
||||
<Compile Include="Debug\Debug.cs" />
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -10,8 +10,8 @@
|
||||
<ArchiveName />
|
||||
</RequiredInfo>
|
||||
<OptionalInfo>
|
||||
<CompiledOn>3/13/2017 5:30:30 PM</CompiledOn>
|
||||
<CompilerRev>1.0.6281.19814</CompilerRev>
|
||||
<CompiledOn>3/14/2017 6:24:05 PM</CompiledOn>
|
||||
<CompilerRev>1.0.6282.31321</CompilerRev>
|
||||
</OptionalInfo>
|
||||
<Plugin>
|
||||
<Version>Crestron.SIMPLSharp, Version=2.0.52.0, Culture=neutral, PublicKeyToken=812d080f93e2de10</Version>
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,4 +1,4 @@
|
||||
MainAssembly=PepperDash_Core.dll:bbf10e2b8fa14d4d571eeafc3bffb1a3
|
||||
MainAssembly=PepperDash_Core.dll:513456572aabc0b1fbd5122dbfb691e5
|
||||
MainAssemblyMinFirmwareVersion=1.007.0017
|
||||
MainAssemblyResource=SimplSharpData.dat:315526abf906cded47fb0c7510266a7e
|
||||
ü
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user