From 00fcc9dd99ef797bb0e489d0471b79d5cb66bedc Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sun, 26 Jul 2020 17:21:29 +0300
Subject: [PATCH] Display connection error dialogs

---
 applications/gui2/src/main.cpp              | 12 ++++++++++++
 components/net/cpp/include/ftl/net/peer.hpp |  3 +++
 components/net/cpp/src/peer.cpp             |  4 +++-
 3 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/applications/gui2/src/main.cpp b/applications/gui2/src/main.cpp
index bbdd04963..2367bd03f 100644
--- a/applications/gui2/src/main.cpp
+++ b/applications/gui2/src/main.cpp
@@ -92,6 +92,18 @@ FTLGui::FTLGui(int argc, char **argv) {
 	if (io_->feed()->listSources().size() == 0) {
 		adder->show();
 	}
+
+	net_->onDisconnect([this](ftl::net::Peer *p) {
+		if (p->status() != ftl::net::Peer::kConnected) {
+			screen_->showError("Connection Failed", std::string("Could not connect to network peer: ") + p->getURI());
+		} else {
+			screen_->showError("Disconnection", std::string("Network peer disconnected: ") + p->getURI());
+		}
+	});
+
+	net_->onError([this](ftl::net::Peer *, const ftl::net::Error &err) {
+
+	});
 }
 
 FTLGui::~FTLGui() {
diff --git a/components/net/cpp/include/ftl/net/peer.hpp b/components/net/cpp/include/ftl/net/peer.hpp
index 91f3f2f06..5ee90444c 100644
--- a/components/net/cpp/include/ftl/net/peer.hpp
+++ b/components/net/cpp/include/ftl/net/peer.hpp
@@ -101,6 +101,8 @@ class Peer {
 	 * Make a reconnect attempt. Called internally by Universe object.
 	 */
 	bool reconnect();
+
+	inline bool isOutgoing() const { return outgoing_; }
 	
 	/**
 	 * Test if the connection is valid. This returns true in all conditions
@@ -263,6 +265,7 @@ class Peer {
 	
 	std::string uri_;				// Original connection URI, or assumed URI
 	ftl::UUID peerid_;				// Received in handshake or allocated
+	bool outgoing_;
 	
 	ftl::net::Dispatcher *disp_;	// For RPC call dispatch
 	//std::vector<std::function<void(Peer &)>> open_handlers_;
diff --git a/components/net/cpp/src/peer.cpp b/components/net/cpp/src/peer.cpp
index ca2e13a03..effe08b1a 100644
--- a/components/net/cpp/src/peer.cpp
+++ b/components/net/cpp/src/peer.cpp
@@ -187,6 +187,7 @@ Peer::Peer(SOCKET s, Universe *u, Dispatcher *d) : sock_(s), can_reconnect_(fals
 	
 	is_waiting_ = true;
 	scheme_ = ftl::URI::SCHEME_TCP;
+	outgoing_ = false;
 
 	int flags =1; 
     if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const char *)&flags, sizeof(flags))) { LOG(ERROR) << "ERROR: setsocketopt(), TCP_NODELAY"; };
@@ -240,6 +241,7 @@ Peer::Peer(const char *pUri, Universe *u, Dispatcher *d) : can_reconnect_(true),
 	
 	status_ = kInvalid;
 	sock_ = INVALID_SOCKET;
+	outgoing_ = true;
 	
 	disp_ = new Dispatcher(d);
 
@@ -391,12 +393,12 @@ void Peer::_badClose(bool retry) {
 		closesocket(sock_);
 		#endif
 		sock_ = INVALID_SOCKET;
-		status_ = kDisconnected;
 		
 		//auto i = find(sockets.begin(),sockets.end(),this);
 		//sockets.erase(i);
 
 		universe_->_notifyDisconnect(this);
+		status_ = kDisconnected;
 
 		// Attempt auto reconnect?
 		if (retry && can_reconnect_) {
-- 
GitLab