Daniele Bartolini 10 years ago
parent
commit
6cb17244cd
2 changed files with 31 additions and 39 deletions
  1. 28 33
      src/console_server.cpp
  2. 3 6
      src/console_server.h

+ 28 - 33
src/console_server.cpp

@@ -17,9 +17,10 @@ namespace crown
 {
 
 ConsoleServer::ConsoleServer(uint16_t port, bool wait)
+	: _clients(default_allocator())
 {
-	m_server.bind(port);
-	m_server.listen(5);
+	_server.bind(port);
+	_server.listen(5);
 
 	if (wait)
 	{
@@ -27,7 +28,7 @@ ConsoleServer::ConsoleServer(uint16_t port, bool wait)
 		TCPSocket client;
 		do
 		{
-			result = m_server.accept(client);
+			result = _server.accept(client);
 		}
 		while (result.error != AcceptResult::NO_ERROR);
 
@@ -37,12 +38,10 @@ ConsoleServer::ConsoleServer(uint16_t port, bool wait)
 
 void ConsoleServer::shutdown()
 {
-	for (uint32_t i = 0; i < id_array::size(m_clients); i++)
-	{
-		m_clients[i].close();
-	}
+	for (uint32_t i = 0; i < vector::size(_clients); ++i)
+		_clients[i].close();
 
-	m_server.close();
+	_server.close();
 }
 
 namespace console_server_internal
@@ -88,49 +87,45 @@ void ConsoleServer::send(TCPSocket client, const char* json)
 
 void ConsoleServer::send(const char* json)
 {
-	for (uint32_t i = 0; i < id_array::size(m_clients); i++)
-	{
-		send(m_clients[i].socket, json);
-	}
+	for (uint32_t i = 0; i < vector::size(_clients); ++i)
+		send(_clients[i].socket, json);
 }
 
 void ConsoleServer::update()
 {
-	// Check for new clients only if we have room for them
-	if (id_array::size(m_clients) < CE_MAX_CONSOLE_CLIENTS - 1)
-	{
-		TCPSocket client;
-		AcceptResult result = m_server.accept_nonblock(client);
-		if (result.error == AcceptResult::NO_ERROR)
-		{
-			add_client(client);
-		}
-	}
+	TCPSocket client;
+	AcceptResult result = _server.accept_nonblock(client);
+	if (result.error == AcceptResult::NO_ERROR)
+		add_client(client);
 
 	TempAllocator256 alloc;
-	Array<Id> to_remove(alloc);
+	Array<uint32_t> to_remove(alloc);
 
 	// Update all clients
-	for (uint32_t i = 0; i < id_array::size(m_clients); i++)
+	for (uint32_t i = 0; i < vector::size(_clients); ++i)
 	{
-		ReadResult rr = update_client(m_clients[i].socket);
-		if (rr.error != ReadResult::NO_ERROR) array::push_back(to_remove, m_clients[i].id);
+		ReadResult rr = update_client(_clients[i].socket);
+		if (rr.error != ReadResult::NO_ERROR)
+			array::push_back(to_remove, i);
 	}
 
 	// Remove clients
-	for (uint32_t i = 0; i < array::size(to_remove); i++)
+	for (uint32_t i = 0; i < array::size(to_remove); ++i)
 	{
-		id_array::get(m_clients, to_remove[i]).socket.close();
-		id_array::destroy(m_clients, to_remove[i]);
+		const uint32_t last = vector::size(_clients) - 1;
+		const uint32_t c = to_remove[i];
+
+		_clients[c].close();
+		_clients[c] = _clients[last];
+		vector::pop_back(_clients);
 	}
 }
 
 void ConsoleServer::add_client(TCPSocket socket)
 {
-	Client client;
-	client.socket = socket;
-	Id id = id_array::create(m_clients, client);
-	id_array::get(m_clients, id).id = id;
+	Client cl;
+	cl.socket = socket;
+	vector::push_back(_clients, cl);
 }
 
 ReadResult ConsoleServer::update_client(TCPSocket client)

+ 3 - 6
src/console_server.h

@@ -5,8 +5,8 @@
 
 #pragma once
 
+#include "container_types.h"
 #include "socket.h"
-#include "id_array.h"
 #include "log.h"
 
 namespace crown
@@ -43,11 +43,8 @@ private:
 
 private:
 
-	TCPSocket m_server;
-
 	struct Client
 	{
-		Id id;
 		TCPSocket socket;
 
 		void close()
@@ -56,8 +53,8 @@ private:
 		}
 	};
 
-	typedef IdArray<CE_MAX_CONSOLE_CLIENTS, Client> ClientArray;
-	ClientArray m_clients;
+	TCPSocket _server;
+	Vector<Client> _clients;
 };
 
 /// Functions for accessing global console.