mirror of
https://github.com/PepperDash/PepperDashCore.git
synced 2026-02-16 13:14:49 +00:00
merge development into release-2
This commit is contained in:
@@ -231,7 +231,10 @@ namespace PepperDash.Core
|
|||||||
this.LogDebug("Attempting connect");
|
this.LogDebug("Attempting connect");
|
||||||
|
|
||||||
// Cancel reconnect if running.
|
// Cancel reconnect if running.
|
||||||
|
if (ReconnectTimer != null)
|
||||||
|
{
|
||||||
ReconnectTimer.Stop();
|
ReconnectTimer.Stop();
|
||||||
|
}
|
||||||
|
|
||||||
// Cleanup the old client if it already exists
|
// Cleanup the old client if it already exists
|
||||||
if (Client != null)
|
if (Client != null)
|
||||||
@@ -248,8 +251,6 @@ namespace PepperDash.Core
|
|||||||
this.LogDebug("Creating new SshClient");
|
this.LogDebug("Creating new SshClient");
|
||||||
ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth);
|
ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth);
|
||||||
Client = new SshClient(connectionInfo);
|
Client = new SshClient(connectionInfo);
|
||||||
|
|
||||||
Client.ErrorOccurred -= Client_ErrorOccurred;
|
|
||||||
Client.ErrorOccurred += Client_ErrorOccurred;
|
Client.ErrorOccurred += Client_ErrorOccurred;
|
||||||
|
|
||||||
//Attempt to connect
|
//Attempt to connect
|
||||||
@@ -258,6 +259,11 @@ namespace PepperDash.Core
|
|||||||
{
|
{
|
||||||
Client.Connect();
|
Client.Connect();
|
||||||
TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534);
|
TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534);
|
||||||
|
if (TheStream.DataAvailable)
|
||||||
|
{
|
||||||
|
// empty the buffer if there is data
|
||||||
|
string str = TheStream.Read();
|
||||||
|
}
|
||||||
TheStream.DataReceived += Stream_DataReceived;
|
TheStream.DataReceived += Stream_DataReceived;
|
||||||
this.LogInformation("Connected");
|
this.LogInformation("Connected");
|
||||||
ClientStatus = SocketStatus.SOCKET_STATUS_CONNECTED;
|
ClientStatus = SocketStatus.SOCKET_STATUS_CONNECTED;
|
||||||
@@ -336,7 +342,7 @@ namespace PepperDash.Core
|
|||||||
if (ReconnectTimer != null)
|
if (ReconnectTimer != null)
|
||||||
{
|
{
|
||||||
ReconnectTimer.Stop();
|
ReconnectTimer.Stop();
|
||||||
ReconnectTimer = null;
|
// ReconnectTimer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
KillClient(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY);
|
KillClient(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY);
|
||||||
@@ -349,12 +355,49 @@ namespace PepperDash.Core
|
|||||||
{
|
{
|
||||||
KillStream();
|
KillStream();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
if (Client != null)
|
if (Client != null)
|
||||||
{
|
{
|
||||||
|
Client.ErrorOccurred -= Client_ErrorOccurred;
|
||||||
Client.Disconnect();
|
Client.Disconnect();
|
||||||
|
Client.Dispose();
|
||||||
Client = null;
|
Client = null;
|
||||||
ClientStatus = status;
|
ClientStatus = status;
|
||||||
this.LogDebug("Disconnected");
|
Debug.Console(1, this, "Disconnected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception in Kill Client:{0}", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Anything to do with reestablishing connection on failures
|
||||||
|
/// </summary>
|
||||||
|
void HandleConnectionFailure()
|
||||||
|
{
|
||||||
|
KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED);
|
||||||
|
|
||||||
|
Debug.Console(1, this, "Client nulled due to connection failure. AutoReconnect: {0}, ConnectEnabled: {1}", AutoReconnect, ConnectEnabled);
|
||||||
|
if (AutoReconnect && ConnectEnabled)
|
||||||
|
{
|
||||||
|
Debug.Console(1, this, "Checking autoreconnect: {0}, {1}ms", AutoReconnect, AutoReconnectIntervalMs);
|
||||||
|
if (ReconnectTimer == null)
|
||||||
|
{
|
||||||
|
ReconnectTimer = new CTimer(o =>
|
||||||
|
{
|
||||||
|
Connect();
|
||||||
|
}, AutoReconnectIntervalMs);
|
||||||
|
Debug.Console(1, this, "Attempting connection in {0} seconds",
|
||||||
|
(float) (AutoReconnectIntervalMs/1000));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.Console(1, this, "{0} second reconnect cycle running",
|
||||||
|
(float) (AutoReconnectIntervalMs/1000));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -362,6 +405,8 @@ namespace PepperDash.Core
|
|||||||
/// Kills the stream
|
/// Kills the stream
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void KillStream()
|
void KillStream()
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (TheStream != null)
|
if (TheStream != null)
|
||||||
{
|
{
|
||||||
@@ -372,6 +417,11 @@ namespace PepperDash.Core
|
|||||||
this.LogDebug("Disconnected stream");
|
this.LogDebug("Disconnected stream");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this.LogException(ex, "Exception in Kill Stream:{0}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Handles the keyboard interactive authentication, should it be required.
|
/// Handles the keyboard interactive authentication, should it be required.
|
||||||
@@ -388,12 +438,17 @@ namespace PepperDash.Core
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
void Stream_DataReceived(object sender, ShellDataEventArgs e)
|
void Stream_DataReceived(object sender, ShellDataEventArgs e)
|
||||||
{
|
{
|
||||||
var bytes = e.Data;
|
if (((ShellStream)sender).Length <= 0L)
|
||||||
if (bytes.Length > 0)
|
|
||||||
{
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var response = ((ShellStream)sender).Read();
|
||||||
|
|
||||||
var bytesHandler = BytesReceived;
|
var bytesHandler = BytesReceived;
|
||||||
|
|
||||||
if (bytesHandler != null)
|
if (bytesHandler != null)
|
||||||
{
|
{
|
||||||
|
var bytes = Encoding.UTF8.GetBytes(response);
|
||||||
if (StreamDebugging.RxStreamDebuggingIsEnabled)
|
if (StreamDebugging.RxStreamDebuggingIsEnabled)
|
||||||
{
|
{
|
||||||
this.LogInformation("Received {1} bytes: '{0}'", ComTextHelper.GetEscapedText(bytes), bytes.Length);
|
this.LogInformation("Received {1} bytes: '{0}'", ComTextHelper.GetEscapedText(bytes), bytes.Length);
|
||||||
@@ -404,13 +459,12 @@ namespace PepperDash.Core
|
|||||||
var textHandler = TextReceived;
|
var textHandler = TextReceived;
|
||||||
if (textHandler != null)
|
if (textHandler != null)
|
||||||
{
|
{
|
||||||
var str = Encoding.GetEncoding(28591).GetString(bytes, 0, bytes.Length);
|
|
||||||
if (StreamDebugging.RxStreamDebuggingIsEnabled)
|
if (StreamDebugging.RxStreamDebuggingIsEnabled)
|
||||||
this.LogInformation("Received: '{0}'", ComTextHelper.GetDebugText(str));
|
this.LogInformation("Received: '{0}'", ComTextHelper.GetDebugText(response));
|
||||||
|
|
||||||
textHandler(this, new GenericCommMethodReceiveTextArgs(str));
|
textHandler(this, new GenericCommMethodReceiveTextArgs(response));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -465,16 +519,27 @@ namespace PepperDash.Core
|
|||||||
if (Client != null && TheStream != null && IsConnected)
|
if (Client != null && TheStream != null && IsConnected)
|
||||||
{
|
{
|
||||||
if (StreamDebugging.TxStreamDebuggingIsEnabled)
|
if (StreamDebugging.TxStreamDebuggingIsEnabled)
|
||||||
this.LogInformation("Sending {0} characters of text: '{1}'", text.Length, ComTextHelper.GetDebugText(text));
|
Debug.Console(0,
|
||||||
|
this,
|
||||||
|
"Sending {0} characters of text: '{1}'",
|
||||||
|
text.Length,
|
||||||
|
ComTextHelper.GetDebugText(text));
|
||||||
|
|
||||||
TheStream.Write(text);
|
TheStream.Write(text);
|
||||||
TheStream.Flush();
|
TheStream.Flush();
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.LogDebug("Client is null or disconnected. Cannot Send Text");
|
Debug.Console(1, this, "Client is null or disconnected. Cannot Send Text");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch (ObjectDisposedException ex)
|
||||||
|
{
|
||||||
|
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception: {0}", ex.Message);
|
||||||
|
Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Stack Trace: {0}", ex.StackTrace);
|
||||||
|
|
||||||
|
KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED);
|
||||||
|
ReconnectTimer.Reset();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -503,9 +568,27 @@ namespace PepperDash.Core
|
|||||||
this.LogDebug("Client is null or disconnected. Cannot Send Bytes");
|
this.LogDebug("Client is null or disconnected. Cannot Send Bytes");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (ObjectDisposedException ex)
|
||||||
|
{
|
||||||
|
this.LogException(ex, "ObjectDisposedException sending {message}", ComTextHelper.GetEscapedText(bytes));
|
||||||
|
|
||||||
|
KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED);
|
||||||
|
ReconnectTimer.Reset();
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
this.LogException(ex, "Exception sending bytes: {message}", ComTextHelper.GetEscapedText(bytes));
|
this.LogException(ex, "Exception sending {message}", ComTextHelper.GetEscapedText(bytes));
|
||||||
|
}
|
||||||
|
|
||||||
|
KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED);
|
||||||
|
ReconnectTimer.Reset();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception: {0}", ex.Message);
|
||||||
|
Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Stack Trace: {0}", ex.StackTrace);
|
||||||
|
|
||||||
|
Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -220,6 +220,7 @@ namespace PepperDash.Core
|
|||||||
public GenericTcpIpClient()
|
public GenericTcpIpClient()
|
||||||
: base(SplusKey)
|
: base(SplusKey)
|
||||||
{
|
{
|
||||||
|
StreamDebugging = new CommunicationStreamDebugging(SplusKey);
|
||||||
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler);
|
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler);
|
||||||
AutoReconnectIntervalMs = 5000;
|
AutoReconnectIntervalMs = 5000;
|
||||||
BufferSize = 2000;
|
BufferSize = 2000;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
|
using Crestron.SimplSharp.WebScripting;
|
||||||
|
|
||||||
namespace PepperDash.Core.Web.RequestHandlers
|
namespace PepperDash.Core.Web.RequestHandlers
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user