From 43a94be50cb35030e18501b3334c410276b51ff9 Mon Sep 17 00:00:00 2001
From: Jonathan Balls <jonathanballs@protonmail.com>
Date: Fri, 24 Jan 2020 22:46:30 +0000
Subject: [PATCH] Display network connection error messages and fix networking
 on MacOS

---
 source/networking/connection.d | 17 +++++++++++++----
 source/networking/package.d    | 15 +++++++--------
 source/ui/networkwidget.d      |  9 +++++++++
 3 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/source/networking/connection.d b/source/networking/connection.d
index 40a450b..7af1da0 100644
--- a/source/networking/connection.d
+++ b/source/networking/connection.d
@@ -76,10 +76,19 @@ class Connection {
             }
 
             if (amountRead == Socket.ERROR) {
-                if (conn.getErrorText() == "Success") {
-                    amountRead = 0;
-                } else {
-                    throw new Exception("Socket Error: ", conn.getErrorText());
+                version(linux) {
+                    if (conn.getErrorText() == "Success") {
+                        amountRead = 0;
+                    } else {
+                        throw new Exception("Socket Error: ", conn.getErrorText());
+                    }
+                }
+                version(OSX) {
+                    if (conn.getErrorText() == "Undefined error: 0") {
+                        amountRead = 0;
+                    } else {
+                        throw new Exception("Socket Error: ", conn.getErrorText());
+                    }
                 }
             }
             recBuffer ~= cast(string) buffer[0..amountRead];
diff --git a/source/networking/package.d b/source/networking/package.d
index b46fb31..4da15c5 100644
--- a/source/networking/package.d
+++ b/source/networking/package.d
@@ -25,21 +25,19 @@ private enum serverPort = 420_69;
 // (p2p, client/server etc).
 
 // TODO list
-// - Exit when window closes - Notifying opponent.
-// - Basically entire game logic
 // - Validate that strings are valid UTF-8
 // - Reconnection.
-// - Could this be cleaner with fibers? The layout is not smooth
+// - Could this be cleaner with fibers? The layout is not very clean and wont scale very well.
 
 // Handles gamestate for a dice roll
 // Document and clean this up please!
 class DiceRoll {
-    bool goFirst;
+    private bool goFirst;
     bool done;
-    Connection conn;
-    ulong mySeed;
-    string oppSeed;
-    string oppSeedHash;
+    private Connection conn;
+    private ulong mySeed;
+    private string oppSeed;
+    private string oppSeedHash;
 
     this(Connection conn, bool goFirst) {
         this.conn = conn;
@@ -244,6 +242,7 @@ class NetworkingThread {
                 }
             }
         } catch (Exception e) {
+            writeln("NETWORK THREAD EXCEPTION");
             writeln(e);
             send(ownerTid, NetworkThreadUnhandledException(e.msg, e.info.to!string));
         }
diff --git a/source/ui/networkwidget.d b/source/ui/networkwidget.d
index 2c2ed07..38aed9f 100644
--- a/source/ui/networkwidget.d
+++ b/source/ui/networkwidget.d
@@ -37,6 +37,7 @@ class NetworkWidget : Dialog {
 
     Box inetBox;
     HumanSelector inetHuman;
+    Label inetErrorMessage;
     Button inetStartSearchButton;
     Box inetStartSearchBox;
     Label inetStartSearchLabel;
@@ -97,6 +98,11 @@ class NetworkWidget : Dialog {
         });
         inetStartSearchBox.setHalign(GtkAlign.CENTER);
 
+        // In case of any errors we'll put them here
+        inetErrorMessage = new Label("");
+        inetErrorMessage.setMarkup("<span foreground='red'>%s<\\span>");
+        inetBox.packEnd(inetErrorMessage, false, false, 0);
+
         /**
          * LAN
          */
@@ -134,6 +140,9 @@ class NetworkWidget : Dialog {
                     assert(ng.clientPlayer == Player.P2);
                     this.onCreateNewGame.emit(new GameState(opponent, player));
                 }
+            },
+            (NetworkThreadUnhandledException e) {
+                inetErrorMessage.setText(e.message);
             }
         );
         return true;