From 0362c5c9ca7c2d8222bd08dae15af7af8c787d47 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Thu, 21 Feb 2019 11:34:55 +0200
Subject: [PATCH] Added onconnection to listener and fixed bugs

---
 net/include/ftl/net/handlers.hpp |  2 ++
 net/include/ftl/net/listener.hpp |  8 ++++++++
 net/src/listener.cpp             |  8 ++++++--
 net/src/net.cpp                  | 26 ++++++++------------------
 net/src/socket.cpp               |  4 +++-
 net/test/net_raw.cpp             | 19 +++++++++++++++++++
 6 files changed, 46 insertions(+), 21 deletions(-)

diff --git a/net/include/ftl/net/handlers.hpp b/net/include/ftl/net/handlers.hpp
index f3904da8b..9a515db88 100644
--- a/net/include/ftl/net/handlers.hpp
+++ b/net/include/ftl/net/handlers.hpp
@@ -6,6 +6,8 @@
 namespace ftl {
 namespace net {
 
+class Socket;
+
 typedef std::function<void(int, std::string&)> sockdatahandler_t;
 typedef std::function<void(int)> sockerrorhandler_t;
 typedef std::function<void()> sockconnecthandler_t;
diff --git a/net/include/ftl/net/listener.hpp b/net/include/ftl/net/listener.hpp
index ca3b2c5d1..97171b5f1 100644
--- a/net/include/ftl/net/listener.hpp
+++ b/net/include/ftl/net/listener.hpp
@@ -10,6 +10,10 @@
 #include <winsock.h>
 #endif
 
+#include <ftl/net/handlers.hpp>
+
+#include <vector>
+
 namespace ftl {
 namespace net {
 
@@ -23,9 +27,13 @@ class Listener {
 	void close();
 	int _socket() { return descriptor_; }
 	
+	void connection(Socket &s);
+	void onConnection(connecthandler_t h) { handler_connect_.push_back(h); };
+	
 	private:
 	int descriptor_;
 	sockaddr_in slocalAddr;
+	std::vector<connecthandler_t> handler_connect_;
 };
 
 };
diff --git a/net/src/listener.cpp b/net/src/listener.cpp
index f3a92d8a7..4b9e8d1ef 100644
--- a/net/src/listener.cpp
+++ b/net/src/listener.cpp
@@ -97,8 +97,12 @@ Listener::~Listener() {
 	close();
 }
 
+void Listener::connection(Socket &s) {
+	for (auto h : handler_connect_) h(s);
+}
+
 void Listener::close() {
-	//if (isConnected()) {
+	if (isListening()) {
 		#ifndef WIN32
 		::close(descriptor_);
 		#else
@@ -107,6 +111,6 @@ void Listener::close() {
 		descriptor_ = INVALID_SOCKET;
 
 		// Attempt auto reconnect?
-	//}
+	}
 }
 
diff --git a/net/src/net.cpp b/net/src/net.cpp
index 23eaeae68..ad56f07b8 100644
--- a/net/src/net.cpp
+++ b/net/src/net.cpp
@@ -14,7 +14,7 @@ static std::vector<shared_ptr<ftl::net::Listener>> listeners;
 static fd_set sfdread;
 static fd_set sfderror;
 
-static int freeSocket() {
+/*static int freeSocket() {
 	int freeclient = -1;
 
 	//Find a free client slot and allocated it
@@ -37,7 +37,7 @@ static int freeSocket() {
 	}
 
 	return freeclient;
-}
+}*/
 
 static int setDescriptors() {
 	//Reset all file descriptors
@@ -79,30 +79,17 @@ shared_ptr<Listener> ftl::net::listen(const char *uri) {
 
 shared_ptr<Socket> ftl::net::connect(const char *uri) {
 	shared_ptr<Socket> s(new Socket(uri));
-	int fs = freeSocket();
-	if (fs >= 0) {
-		sockets[fs] = s;
-		return s;
-	} else {
-		return NULL;
-	}
+	sockets.push_back(s);
+	return s;
 }
 
 void ftl::net::stop() {
 	for (auto s : sockets) {
-		if (s != NULL) s->close();
+		s->close();
 	}
 	
 	sockets.clear();
 	
-	/*#ifndef WIN32
-	if (ssock != INVALID_SOCKET) close(ssock);
-	#else
-	if (ssock != INVALID_SOCKET) closesocket(ssock);
-	#endif
-
-	ssock = INVALID_SOCKET;*/
-	
 	for (auto l : listeners) {
 		l->close();
 	}
@@ -157,6 +144,9 @@ bool _run(bool blocking, bool nodelay) {
 							
 							sockets.push_back(sock);
 							
+							// Call connection handlers
+							l->connection(*sock);
+							
 							// TODO Save the ip address
 							// deal with both IPv4 and IPv6:
 							/*if (addr.ss_family == AF_INET) {
diff --git a/net/src/socket.cpp b/net/src/socket.cpp
index c2d81c172..b81c7151c 100644
--- a/net/src/socket.cpp
+++ b/net/src/socket.cpp
@@ -108,6 +108,7 @@ static int wsConnect(URI &uri) {
 Socket::Socket(int s) : m_sock(s), m_pos(0) {
 	// TODO Get the remote address.
 	m_valid = true;
+	m_buffer = new char[BUFFER_SIZE];
 }
 
 Socket::Socket(const char *pUri) : m_uri(pUri), m_pos(0) {
@@ -209,6 +210,7 @@ Socket::~Socket() {
 	close();
 	
 	// Delete socket buffer
-	delete [] m_buffer;
+	if (m_buffer) delete [] m_buffer;
+	m_buffer = NULL;
 }
 
diff --git a/net/test/net_raw.cpp b/net/test/net_raw.cpp
index e5b443cff..59a849064 100644
--- a/net/test/net_raw.cpp
+++ b/net/test/net_raw.cpp
@@ -253,6 +253,25 @@ TEST_CASE("net::listen()", "[net]") {
 
 		ftl::net::stop();
 	}
+	
+	SECTION("on connection event") {
+		auto l = ftl::net::listen("tcp://*:7078");
+		REQUIRE( l->isListening() );
+		
+		bool connected = false;
+		
+		l->onConnection([&](Socket &s) {
+			REQUIRE( s.isConnected() );
+			connected = true;
+		});
+		
+		auto sock = ftl::net::connect("tcp://127.0.0.1:7078");
+		ftl::net::wait();
+		REQUIRE( connected );
+		std::cout << "PRE STOP" << std::endl;
+		ftl::net::stop();
+		std::cout << "POST STOP" << std::endl;
+	}
 }
 
 TEST_CASE("Socket.onMessage()", "[net]") {
-- 
GitLab