Fixed undesirable auto-reconnect behavior when manually disconnecting

This commit is contained in:
Heath Volmer
2016-09-12 14:42:53 -06:00
parent c1e60134cc
commit 71d1b43fc9
8 changed files with 85 additions and 13 deletions

View File

@@ -147,7 +147,7 @@ namespace PepperDash.Core
IsConnected = true; IsConnected = true;
Debug.Console(1, this, "Connected"); Debug.Console(1, this, "Connected");
TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534); TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534);
TheStream.DataReceived += Stream_DataReceived; TheStream.DataReceived += Stream_DataReceived;
} }
return; return;
} }
@@ -162,14 +162,18 @@ namespace PepperDash.Core
Key, Hostname, Port, ie.GetType()); Key, Hostname, Port, ie.GetType());
else if (ie is SshAuthenticationException) else if (ie is SshAuthenticationException)
{ {
msg = string.Format("'{0}' Authentication failure for username '{1}', ({2})", msg = string.Format("'{0}' Authentication failure for username '{1}', ({2})",
Username, Key, ie.GetType()); Username, Key, ie.GetType());
Debug.Console(0, this, "Authentication failure for username '{0}', ({1})", Debug.Console(0, this, "Authentication failure for username '{0}', ({1})",
Username, ie.GetType()); Username, ie.GetType());
} }
else else
Debug.Console(0, this, "Error on connect:\r({0})", e); Debug.Console(0, this, "Error on connect:\r({0})", e);
} }
catch (Exception e)
{
Debug.Console(0, this, "Unhandled exception on connect:\r({0})", e);
}
} }
else else
{ {

View File

@@ -13,17 +13,64 @@ namespace PepperDash.Core
{ {
public class GenericTcpIpClient : Device, IBasicCommunication, IAutoReconnect public class GenericTcpIpClient : Device, IBasicCommunication, IAutoReconnect
{ {
/// <summary>
///
/// </summary>
public event EventHandler<GenericCommMethodReceiveBytesArgs> BytesReceived; public event EventHandler<GenericCommMethodReceiveBytesArgs> BytesReceived;
/// <summary>
///
/// </summary>
public event EventHandler<GenericCommMethodReceiveTextArgs> TextReceived; public event EventHandler<GenericCommMethodReceiveTextArgs> TextReceived;
/// <summary>
///
/// </summary>
public TCPClient Client { get; private set; } public TCPClient Client { get; private set; }
/// <summary>
///
/// </summary>
public bool IsConnected { get { return Client.ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED; } } public bool IsConnected { get { return Client.ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED; } }
public string Status { get { return Client.ClientStatus.ToString(); } }
/// <summary>
///
/// </summary>
public SocketStatus ClientStatus { get { return Client.ClientStatus; } }
/// <summary>
///
/// </summary>
public string ClientStatusText { get { return Client.ClientStatus.ToString(); } }
/// <summary>
///
/// </summary>
public ushort UClientStatus { get { return (ushort)Client.ClientStatus; } }
/// <summary>
///
/// </summary>
public string ConnectionFailure { get { return Client.ClientStatus.ToString(); } } public string ConnectionFailure { get { return Client.ClientStatus.ToString(); } }
/// <summary>
///
/// </summary>
public bool AutoReconnect { get; set; } public bool AutoReconnect { get; set; }
/// <summary>
///
/// </summary>
public int AutoReconnectIntervalMs { get; set; } public int AutoReconnectIntervalMs { get; set; }
/// <summary>
/// Set only when the disconnect method is called.
/// </summary>
bool DisconnectCalledByUser;
/// <summary>
///
/// </summary>
public bool Connected public bool Connected
{ {
get { return Client.ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED; } get { return Client.ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED; }
@@ -52,10 +99,12 @@ namespace PepperDash.Core
public void Connect() public void Connect()
{ {
Client.ConnectToServerAsync(null); Client.ConnectToServerAsync(null);
DisconnectCalledByUser = false;
} }
public void Disconnnect() public void Disconnnect()
{ {
DisconnectCalledByUser = true;
Client.DisconnectFromServer(); Client.DisconnectFromServer();
} }
@@ -126,12 +175,11 @@ namespace PepperDash.Core
void Client_SocketStatusChange(TCPClient client, SocketStatus clientSocketStatus) void Client_SocketStatusChange(TCPClient client, SocketStatus clientSocketStatus)
{ {
if (client.ClientStatus != SocketStatus.SOCKET_STATUS_CONNECTED &&
client.ClientStatus != SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY) Debug.Console(2, this, "Socket status change {0} ({1})", clientSocketStatus, UClientStatus);
if (client.ClientStatus != SocketStatus.SOCKET_STATUS_CONNECTED && !DisconnectCalledByUser)
WaitAndTryReconnect(); WaitAndTryReconnect();
Debug.Console(2, this, "Socket status change {0}", clientSocketStatus);
switch (clientSocketStatus) switch (clientSocketStatus)
{ {
case SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY: case SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY:
@@ -140,6 +188,7 @@ namespace PepperDash.Core
break; break;
case SocketStatus.SOCKET_STATUS_CONNECTED: case SocketStatus.SOCKET_STATUS_CONNECTED:
Client.ReceiveDataAsync(Receive); Client.ReceiveDataAsync(Receive);
DisconnectCalledByUser = false;
break; break;
case SocketStatus.SOCKET_STATUS_CONNECT_FAILED: case SocketStatus.SOCKET_STATUS_CONNECT_FAILED:
break; break;

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
namespace PepperDash.Core
{
public static class NetworkComm
{
static NetworkComm()
{
}
}
}

View File

@@ -72,6 +72,7 @@
<Compile Include="Device.cs" /> <Compile Include="Device.cs" />
<Compile Include="EthernetHelper.cs" /> <Compile Include="EthernetHelper.cs" />
<Compile Include="Comm\GenericTcpIpClient.cs" /> <Compile Include="Comm\GenericTcpIpClient.cs" />
<Compile Include="Network\DiscoveryThings.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<None Include="Properties\ControlSystem.cfg" /> <None Include="Properties\ControlSystem.cfg" />
</ItemGroup> </ItemGroup>
@@ -84,7 +85,7 @@
<Programmer /> <Programmer />
<ArchiveFilename>C:\Users\hvolm\Desktop\working\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\bin\PepperDash_Core.clz</ArchiveFilename> <ArchiveFilename>C:\Users\hvolm\Desktop\working\pepperdash-simplsharp-core\Pepperdash Core\Pepperdash Core\bin\PepperDash_Core.clz</ArchiveFilename>
<MinFirmwareVersion>1.007.0017</MinFirmwareVersion> <MinFirmwareVersion>1.007.0017</MinFirmwareVersion>
<CompiledOn>9/2/2016 4:24:23 PM</CompiledOn> <CompiledOn>9/12/2016 2:06:04 PM</CompiledOn>
<AdditionalInfo /> <AdditionalInfo />
<EmbedSourceArchive>False</EmbedSourceArchive> <EmbedSourceArchive>False</EmbedSourceArchive>
<CopyTo /> <CopyTo />

View File

@@ -10,7 +10,7 @@
<ArchiveName /> <ArchiveName />
</RequiredInfo> </RequiredInfo>
<OptionalInfo> <OptionalInfo>
<CompiledOn>9/2/2016 4:24:23 PM</CompiledOn> <CompiledOn>9/12/2016 2:06:04 PM</CompiledOn>
<CompilerRev>1.0.0.27730</CompilerRev> <CompilerRev>1.0.0.23581</CompilerRev>
</OptionalInfo> </OptionalInfo>
</ProgramInfo> </ProgramInfo>

View File

@@ -1,4 +1,4 @@
MainAssembly=PepperDash_Core.dll:91ae222c64760e498b41b5b05b1e591d MainAssembly=PepperDash_Core.dll:841414db99de3a39f802c9ba80e23ad1
MainAssemblyMinFirmwareVersion=1.007.0017 MainAssemblyMinFirmwareVersion=1.007.0017
ü ü
DependencySource=Newtonsoft.Json.Compact.dll:ea996aa2ec65aa1878e7c9d09e37a896 DependencySource=Newtonsoft.Json.Compact.dll:ea996aa2ec65aa1878e7c9d09e37a896