From 030e5119e7f906635d41a0e796df0f35a1aac464 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Tue, 26 Feb 2019 13:38:34 +0200
Subject: [PATCH] Minor corrections to ftlnet to work with p2p

---
 net/CMakeLists.txt           | 1 +
 net/include/ftl/net.hpp      | 2 +-
 net/src/net.cpp              | 4 ++--
 net/test/net_integration.cpp | 9 ++++-----
 4 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/net/CMakeLists.txt b/net/CMakeLists.txt
index 5af66370e..85d31c34d 100644
--- a/net/CMakeLists.txt
+++ b/net/CMakeLists.txt
@@ -29,6 +29,7 @@ set(FTLSOURCE
 	src/listener.cpp
 	src/socket.cpp
 	src/dispatcher.cpp
+	src/protocol.cpp
 )
 
 check_include_file("uriparser/Uri.h" HAVE_URI_H)
diff --git a/net/include/ftl/net.hpp b/net/include/ftl/net.hpp
index 0647fb3f6..c0915ed75 100644
--- a/net/include/ftl/net.hpp
+++ b/net/include/ftl/net.hpp
@@ -40,7 +40,7 @@ bool run(bool async=false);
  */
 bool wait();
 
-void wait(std::function<bool(void)>);
+void wait(std::function<bool(void)>, float t=3.0f);
 
 /**
  * Check and process any waiting messages, but do not block if there are none.
diff --git a/net/src/net.cpp b/net/src/net.cpp
index d6c120a1b..0d2943f91 100644
--- a/net/src/net.cpp
+++ b/net/src/net.cpp
@@ -191,9 +191,9 @@ bool ftl::net::wait() {
 	return _run(false,false);
 }
 
-void ftl::net::wait(std::function<bool(void)> f) {
+void ftl::net::wait(std::function<bool(void)> f, float to) {
 	auto start = steady_clock::now();
-	while (!f() && duration<float>(steady_clock::now() - start).count() < 3.0)
+	while (!f() && duration<float>(steady_clock::now() - start).count() < to)
 		_run(false,false);
 }
 
diff --git a/net/test/net_integration.cpp b/net/test/net_integration.cpp
index 0989f6772..20b2c6760 100644
--- a/net/test/net_integration.cpp
+++ b/net/test/net_integration.cpp
@@ -178,7 +178,7 @@ TEST_CASE("net::listen()", "[net]") {
 		bool connected = false;
 		
 		l->onConnection([&](shared_ptr<Socket> s) {
-			ftl::net::wait(); // Wait for handshake
+			ftl::net::wait([&s]() { return s->isConnected(); });
 			REQUIRE( s->isConnected() );
 			connected = true;
 		});
@@ -212,11 +212,10 @@ TEST_CASE("Net Integration", "[integrate]") {
 	
 	shared_ptr<Socket> s2 = ftl::net::connect("tcp://localhost:9000");
 	
-	ftl::net::wait(); // TODO, make setProtocol block until handshake complete
-	ftl::net::wait();
-	REQUIRE( s1 != nullptr );
 	REQUIRE( s2 != nullptr );
-	
+	ftl::net::wait([&s2]() { return s2->isConnected(); });
+	REQUIRE( s1 != nullptr );	
+
 	REQUIRE( s1->isConnected() );
 	REQUIRE( s2->isConnected() );
 	
-- 
GitLab