Skip to content

Commit

Permalink
Added more debug tracing re Issue #29
Browse files Browse the repository at this point in the history
  • Loading branch information
tig committed Dec 10, 2022
1 parent 7a8d04e commit 10ea030
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Installer/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.10.2
2.2.11.3
17 changes: 12 additions & 5 deletions src/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,17 @@ private void StopSerialServer() {
}

private void StartClient(bool delay = false) {
if (Client == null) {
Logger.Instance.Log4.Info("Client: Starting...");
Client = new SocketClient(Settings);
Client.Notifications += clientSocketNotificationHandler;
Client.Start(delay);
if (Settings.ActAsClient) {
if (Client == null) {
Logger.Instance.Log4.Info($"Client: Starting (delay = {delay})");
Client = new SocketClient(Settings);
Client.Notifications += clientSocketNotificationHandler;
Client.Start(delay);
}
}
else {
Logger.Instance.Log4.Debug("Client: StartClient attempt but ActAsClient is not enabled...");

}
}

Expand All @@ -384,6 +390,7 @@ private void StopClient() {
}

private void ToggleClient() {
Logger.Instance.Log4.Debug("Client: ToggleClient...");
if (Client == null) {
StartClient();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Services/ServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using log4net;

namespace MCEControl {
Expand Down Expand Up @@ -85,6 +86,7 @@ public virtual void Send(string text, Reply replyContext = null) {

// Send a status notification
protected void SetStatus(ServiceStatus status, String msg = "") {

// TELEMETRY:
// what: Service status
// why: to understand the typical/non-typical conenction flows
Expand Down
27 changes: 20 additions & 7 deletions src/Services/SocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
//-------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using MCEControl.Properties;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace MCEControl {
/// <summary>
Expand Down Expand Up @@ -123,28 +125,41 @@ public override void Send(string text, Reply replyContext = null) {

private void Connect() {
IPEndPoint endPoint;
Log4.Debug($"SocketClient: Connect - {_host}:{_port}");
Debug.Assert(_tcpClient != null);
try {
// GetHostEntry returns a list. We need to pick the IPv4 entry.
// TODO: Support ipv6
var ipv4Addresses = Array.FindAll(Dns.GetHostEntry(_host).AddressList, a => a.AddressFamily == AddressFamily.InterNetwork);
Log4.Debug($"SocketClient: {ipv4Addresses.Length} IP v4 addresses found");

if (ipv4Addresses.Length == 0) {
throw new IOException($"{_host}:{_port} didn't resolve to a valid address");
}

Log4.Debug($"SocketClient: new IPEndPoint({ipv4Addresses[0]}, {_port})");
endPoint = new IPEndPoint(ipv4Addresses[0], _port);

// TELEMETRY: Do not pass _host to SetStatus to avoid collecting PII
SetStatus(ServiceStatus.Started, $"{ipv4Addresses[0]}:{_port}");

if (_tcpClient == null) {
Log4.Debug($"SocketClient: Can't Connect - _tcpClient is null");
return;
}

Log4.Debug($"SocketClient: BeginConnect({endPoint.Address}, {_port})");
_ = _tcpClient.BeginConnect(endPoint.Address, _port, ar => {
Log4.Debug($"SocketClient: In BeginConnect call back: {_host}:{_port}");
if (_tcpClient == null) {
Log4.Debug($"SocketClient: Can't Connect - _tcpClient is null");
return;
}

try {
Log4.Debug($"Client BeginConnect: { _host}:{ _port}");
Log4.Debug($"SocketClient: BeginConnect succeeded: {_host}:{_port}");
_tcpClient.EndConnect(ar);
Log4.Debug($"Client Back from EndConnect: { _host}:{ _port}");
Log4.Debug($"SocketClient: Back from EndConnect: {_host}:{_port}");
SetStatus(ServiceStatus.Connected, $"{_host}:{_port}");
var sb = new StringBuilder();
while (_bw != null &&
Expand Down Expand Up @@ -190,8 +205,8 @@ private void Connect() {
catch (Exception e) {
// Got this when endPoint = new IPEndPoint(Dns.GetHostEntry(_host).AddressList[0], _port)
// resolved to an ipv6 address
Log4.Debug($"SocketClient Generic Exception: {e.GetType().Name}: {e.Message}");
Error($"SocketClient Generic Exception: {e.GetType().Name} {e.Message}");
Log4.Debug($"SocketClient: Generic Exception: {e.GetType().Name}: {e.Message}");
Error($"SocketClient: Generic Exception: {e.GetType().Name} {e.Message}");
}
finally {
//log4.Debug("finally - Stopping");
Expand All @@ -209,16 +224,14 @@ private void Connect() {
return;
}
catch (Exception e) {
Log4.Debug($"SocketClient: (BeginConnect){e.GetType().Name}: {e.Message}");
Log4.Debug($"SocketClient: (BeginConnect) {e.GetType().Name}: {e.Message}");
Error($"SocketClient: (BeginConnect) Generic Exception: {e.GetType().Name}: {e.Message}");
if (_tcpClient != null) {
_tcpClient.Close();
}

return;
}

Log4.Debug("BeginConnect returned");
}

private void CatchSocketException(SocketException e) {
Expand Down

0 comments on commit 10ea030

Please sign in to comment.