Daniele Bartolini 8 лет назад
Родитель
Сommit
44a4994e53
2 измененных файлов с 28 добавлено и 43 удалено
  1. 24 35
      src/device/console_server.cpp
  2. 4 8
      src/device/console_server.h

+ 24 - 35
src/device/console_server.cpp

@@ -34,13 +34,13 @@ void ConsoleServer::listen(u16 port, bool wait)
 		}
 		while (ar.error != AcceptResult::SUCCESS);
 
-		add_client(client);
+		array::push_back(_clients, client);
 	}
 }
 
 void ConsoleServer::shutdown()
 {
-	for (u32 i = 0; i < vector::size(_clients); ++i)
+	for (u32 i = 0; i < array::size(_clients); ++i)
 		_clients[i].close();
 
 	_server.close();
@@ -63,7 +63,7 @@ void ConsoleServer::error(TCPSocket client, const char* msg)
 
 void ConsoleServer::send(const char* json)
 {
-	for (u32 i = 0; i < vector::size(_clients); ++i)
+	for (u32 i = 0; i < array::size(_clients); ++i)
 		send(_clients[i], json);
 }
 
@@ -72,13 +72,13 @@ void ConsoleServer::update()
 	TCPSocket client;
 	AcceptResult ar = _server.accept_nonblock(client);
 	if (ar.error == AcceptResult::SUCCESS)
-		add_client(client);
+		array::push_back(_clients, client);
 
 	TempAllocator256 alloc;
 	Array<u32> to_remove(alloc);
 
 	// Update all clients
-	for (u32 i = 0; i < vector::size(_clients); ++i)
+	for (u32 i = 0; i < array::size(_clients); ++i)
 	{
 		for (;;)
 		{
@@ -94,7 +94,7 @@ void ConsoleServer::update()
 				break;
 			}
 
-			// Read the message
+			// Read message
 			TempAllocator4096 ta;
 			Array<char> msg(ta);
 			array::resize(msg, msg_len + 1);
@@ -107,48 +107,37 @@ void ConsoleServer::update()
 				break;
 			}
 
-			process(_clients[i], array::begin(msg));
+			// Process message
+			JsonObject obj(ta);
+			sjson::parse(array::begin(msg), obj);
+
+			Command cmd;
+			cmd.function = NULL;
+			cmd.user_data = NULL;
+			cmd = hash_map::get(_commands
+				, sjson::parse_string_id(obj["type"])
+				, cmd
+				);
+
+			if (cmd.function)
+				cmd.function(*this, _clients[i], array::begin(msg), cmd.user_data);
+			else
+				error(_clients[i], "Unknown command");
 		}
 	}
 
 	// Remove clients
 	for (u32 i = 0; i < array::size(to_remove); ++i)
 	{
-		const u32 last = vector::size(_clients) - 1;
+		const u32 last = array::size(_clients) - 1;
 		const u32 c = to_remove[i];
 
 		_clients[c].close();
 		_clients[c] = _clients[last];
-		vector::pop_back(_clients);
+		array::pop_back(_clients);
 	}
 }
 
-void ConsoleServer::add_client(TCPSocket socket)
-{
-	vector::push_back(_clients, socket);
-}
-
-void ConsoleServer::process(TCPSocket client, const char* json)
-{
-	TempAllocator4096 ta;
-	JsonObject obj(ta);
-	sjson::parse(json, obj);
-
-	Command cmd;
-	cmd.function = NULL;
-	cmd.user_data = NULL;
-
-	cmd = hash_map::get(_commands
-		, sjson::parse_string_id(obj["type"])
-		, cmd
-		);
-
-	if (cmd.function)
-		cmd.function(*this, client, json, cmd.user_data);
-	else
-		error(client, "Unknown command");
-}
-
 void ConsoleServer::register_command(const char* type, CommandFunction function, void* user_data)
 {
 	CE_ENSURE(NULL != type);

+ 4 - 8
src/device/console_server.h

@@ -6,16 +6,16 @@
 #pragma once
 
 #include "container_types.h"
+#include "hash_map.h"
 #include "socket.h"
 #include "string_types.h"
-#include "hash_map.h"
 
 namespace crown
 {
 /// Provides service to communicate with engine via TCP/IP.
 ///
 /// @ingroup Device
-class ConsoleServer
+struct ConsoleServer
 {
 	typedef void (*CommandFunction)(ConsoleServer& cs, TCPSocket client, const char* json, void* user_data);
 
@@ -26,14 +26,10 @@ class ConsoleServer
 	};
 
 	TCPSocket _server;
-	Vector<TCPSocket> _clients;
+	Array<TCPSocket> _clients;
 	HashMap<StringId32, Command> _commands;
 
-	void add_client(TCPSocket socket);
-	void process(TCPSocket client, const char* json);
-
-public:
-
+	/// Constructor.
 	ConsoleServer(Allocator& a);
 
 	/// Listens on the given @a port. If @a wait is true, this function