Преглед изворни кода

Create ConsoleServer in Device

Daniele Bartolini пре 10 година
родитељ
комит
f4f1bf7298

+ 1 - 30
src/device/console_server.cpp

@@ -21,7 +21,7 @@ ConsoleServer::ConsoleServer(Allocator& a)
 {
 }
 
-void ConsoleServer::init(u16 port, bool wait)
+void ConsoleServer::listen(u16 port, bool wait)
 {
 	_server.bind(port);
 	_server.listen(5);
@@ -196,33 +196,4 @@ void ConsoleServer::process(TCPSocket client, const char* json)
 	}
 }
 
-namespace console_server_globals
-{
-	char _buffer[sizeof(ConsoleServer)];
-	ConsoleServer* _console = NULL;
-
-	void init(u16 port, bool wait)
-	{
-		_console = new (_buffer) ConsoleServer(default_allocator());
-		_console->init(port, wait);
-	}
-
-	void shutdown()
-	{
-		_console->~ConsoleServer();
-		_console = NULL;
-	}
-
-	void update()
-	{
-#if CROWN_DEBUG
-		_console->update();
-#endif // CROWN_DEBUG
-	}
-
-	ConsoleServer* console()
-	{
-		return _console;
-	}
-} // namespace console_server
 } // namespace crown

+ 1 - 16
src/device/console_server.h

@@ -32,7 +32,7 @@ public:
 
 	/// Listens on the given @a port. If @a wait is true, this function
 	/// blocks until a client is connected.
-	void init(u16 port, bool wait);
+	void listen(u16 port, bool wait);
 
 	/// Shutdowns the server.
 	void shutdown();
@@ -44,19 +44,4 @@ public:
 	void send(const char* json);
 };
 
-/// Functions for accessing global console.
-namespace console_server_globals
-{
-	// Creates the global console server.
-	void init(u16 port, bool wait);
-
-	/// Destroys the global console server.
-	void shutdown();
-
-	/// Updates the global console server.
-	void update();
-
-	/// Returns the global console server object.
-	ConsoleServer* console();
-} // namespace console_server_globals
 } // namespace crown

+ 82 - 65
src/device/device.cpp

@@ -31,6 +31,7 @@
 #include "resource_package.h"
 #include "shader_manager.h"
 #include "sjson.h"
+#include "string_stream.h"
 #include "string_utils.h"
 #include "temp_allocator.h"
 #include "types.h"
@@ -126,6 +127,7 @@ private:
 Device::Device(const DeviceOptions& opts)
 	: _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
 	, _device_options(opts)
+	, _console_server(NULL)
 	, _bundle_filesystem(NULL)
 	, _last_log(NULL)
 	, _resource_loader(NULL)
@@ -153,9 +155,8 @@ Device::Device(const DeviceOptions& opts)
 	, _mouse_curr_y(0)
 	, _mouse_last_x(0)
 	, _mouse_last_y(0)
-	, _is_init(false)
-	, _is_running(false)
-	, _is_paused(false)
+	, _quit(false)
+	, _paused(false)
 	, _frame_count(0)
 	, _last_time(0)
 	, _current_time(0)
@@ -168,6 +169,9 @@ void Device::init()
 {
 	profiler_globals::init();
 
+	_console_server = CE_NEW(_allocator, ConsoleServer)(default_allocator());
+	_console_server->listen(_device_options._console_port, _device_options._wait_console);
+
 #if CROWN_PLATFORM_ANDROID
 	_bundle_filesystem = CE_NEW(_allocator, ApkFilesystem)(default_allocator(), const_cast<AAssetManager*>((AAssetManager*)_device_options._asset_manager));
 #else
@@ -219,19 +223,45 @@ void Device::init()
 	_lua_environment->execute((LuaResource*)_resource_manager->get(RESOURCE_TYPE_SCRIPT, _boot_script_name));
 	_lua_environment->call_global("init", 0);
 
-	_is_init = true;
-	_is_running = true;
 	_last_time = os::clocktime();
 
 	CE_LOGD("Engine initialized");
-}
 
-void Device::shutdown()
-{
-	CE_ASSERT(_is_init, "Engine is not initialized");
+	while (!process_events() && !_quit)
+	{
+		_current_time = os::clocktime();
+		const s64 time = _current_time - _last_time;
+		_last_time = _current_time;
+		const f64 freq = (f64) os::clockfrequency();
+		_last_delta_time = f32(time * (1.0 / freq));
+		_time_since_start += _last_delta_time;
+
+		profiler_globals::clear();
+		_console_server->update();
+
+		RECORD_FLOAT("device.dt", _last_delta_time);
+		RECORD_FLOAT("device.fps", 1.0f/_last_delta_time);
+
+		if (!_paused)
+		{
+			_resource_manager->complete_requests();
+			_lua_environment->call_global("update", 1, ARGUMENT_FLOAT, last_delta_time());
+			_lua_environment->call_global("render", 1, ARGUMENT_FLOAT, last_delta_time());
+		}
+
+		_input_manager->update();
 
-	_is_running = false;
-	_is_init = false;
+		const bgfx::Stats* stats = bgfx::getStats();
+		RECORD_FLOAT("bgfx.gpu_time", f64(stats->gpuTimeEnd - stats->gpuTimeBegin)*1000.0/stats->gpuTimerFreq);
+		RECORD_FLOAT("bgfx.cpu_time", f64(stats->cpuTimeEnd - stats->cpuTimeBegin)*1000.0/stats->cpuTimerFreq);
+
+		bgfx::frame();
+		profiler_globals::flush();
+
+		_lua_environment->reset_temporaries();
+
+		_frame_count++;
+	}
 
 	_lua_environment->call_global("shutdown", 0);
 
@@ -262,6 +292,9 @@ void Device::shutdown()
 
 	CE_DELETE(_allocator, _bundle_filesystem);
 
+	_console_server->shutdown();
+	CE_DELETE(_allocator, _console_server);
+
 	profiler_globals::shutdown();
 
 	_allocator.clear();
@@ -269,18 +302,18 @@ void Device::shutdown()
 
 void Device::quit()
 {
-	_is_running = false;
+	_quit = true;
 }
 
 void Device::pause()
 {
-	_is_paused = true;
+	_paused = true;
 	CE_LOGI("Engine paused.");
 }
 
 void Device::unpause()
 {
-	_is_paused = false;
+	_paused = false;
 	CE_LOGI("Engine unpaused.");
 }
 
@@ -290,11 +323,6 @@ void Device::resolution(u16& width, u16& height)
 	height = _height;
 }
 
-bool Device::is_running() const
-{
-	return _is_running;
-}
-
 u64 Device::frame_count() const
 {
 	return _frame_count;
@@ -310,45 +338,6 @@ f64 Device::time_since_start() const
 	return _time_since_start;
 }
 
-void Device::update()
-{
-	while (!process_events() && _is_running)
-	{
-		_current_time = os::clocktime();
-		const s64 time = _current_time - _last_time;
-		_last_time = _current_time;
-		const f64 freq = (f64) os::clockfrequency();
-		_last_delta_time = f32(time * (1.0 / freq));
-		_time_since_start += _last_delta_time;
-
-		profiler_globals::clear();
-		console_server_globals::update();
-
-		RECORD_FLOAT("device.dt", _last_delta_time);
-		RECORD_FLOAT("device.fps", 1.0f/_last_delta_time);
-
-		if (!_is_paused)
-		{
-			_resource_manager->complete_requests();
-			_lua_environment->call_global("update", 1, ARGUMENT_FLOAT, last_delta_time());
-			_lua_environment->call_global("render", 1, ARGUMENT_FLOAT, last_delta_time());
-		}
-
-		_input_manager->update();
-
-		const bgfx::Stats* stats = bgfx::getStats();
-		RECORD_FLOAT("bgfx.gpu_time", f64(stats->gpuTimeEnd - stats->gpuTimeBegin)*1000.0/stats->gpuTimerFreq);
-		RECORD_FLOAT("bgfx.cpu_time", f64(stats->cpuTimeEnd - stats->cpuTimeBegin)*1000.0/stats->cpuTimerFreq);
-
-		bgfx::frame();
-		profiler_globals::flush();
-
-		_lua_environment->reset_temporaries();
-
-		_frame_count++;
-	}
-}
-
 void Device::render(World& world, CameraInstance camera)
 {
 	bgfx::setViewClear(0
@@ -430,7 +419,21 @@ void Device::reload(StringId64 type, StringId64 name)
 	}
 }
 
-void Device::log(const char* msg)
+static StringStream& sanitize(StringStream& ss, const char* msg)
+{
+	using namespace string_stream;
+	const char* ch = msg;
+	for (; *ch; ch++)
+	{
+		if (*ch == '"')
+			ss << "\\";
+		ss << *ch;
+	}
+
+	return ss;
+}
+
+void Device::log(const char* msg, LogSeverity::Enum severity)
 {
 	if (_last_log)
 	{
@@ -438,6 +441,26 @@ void Device::log(const char* msg)
 		_last_log->write("\n", 1);
 		_last_log->flush();
 	}
+
+	if (_console_server)
+	{
+		static const char* stt[] = { "info", "warning", "error", "debug" };
+
+		using namespace string_stream;
+		TempAllocator4096 ta;
+		StringStream json(ta);
+
+		json << "{\"type\":\"message\",";
+		json << "\"severity\":\"" << stt[severity] << "\",";
+		json << "\"message\":\""; sanitize(json, msg) << "\"}";
+
+		_console_server->send(c_str(json));
+	}
+}
+
+ConsoleServer* Device::console_server()
+{
+	return _console_server;
 }
 
 ResourceManager* Device::resource_manager()
@@ -651,14 +674,8 @@ void init(const DeviceOptions& opts)
 	_device->init();
 }
 
-void update()
-{
-	_device->update();
-}
-
 void shutdown()
 {
-	_device->shutdown();
 	_device->~Device();
 	_device = NULL;
 }

+ 10 - 11
src/device/device.h

@@ -7,12 +7,14 @@
 
 #include "allocator.h"
 #include "config.h"
+#include "console_server.h"
 #include "container_types.h"
 #include "device_options.h"
 #include "display.h"
 #include "filesystem_types.h"
 #include "input_types.h"
 #include "linear_allocator.h"
+#include "log.h"
 #include "lua_types.h"
 #include "resource_types.h"
 #include "string_id.h"
@@ -35,6 +37,7 @@ class Device
 	LinearAllocator _allocator;
 
 	const DeviceOptions& _device_options;
+	ConsoleServer* _console_server;
 	Filesystem* _bundle_filesystem;
 	File* _last_log;
 	ResourceLoader* _resource_loader;
@@ -67,9 +70,8 @@ class Device
 	s16 _mouse_last_x;
 	s16 _mouse_last_y;
 
-	bool _is_init;
-	bool _is_running;
-	bool _is_paused;
+	bool _quit;
+	bool _paused;
 
 	u64 _frame_count;
 	s64 _last_time;
@@ -87,9 +89,6 @@ public:
 	/// Initializes the engine.
 	void init();
 
-	/// Shutdowns the engine freeing all the allocated resources.
-	void shutdown();
-
 	/// Returns the number of command line parameters.
 	int argc() const { return _device_options._argc; }
 
@@ -105,9 +104,6 @@ public:
 	/// Returns a string identifying the engine version.
 	const char* version() const { return CROWN_VERSION_STRING; }
 
-	/// Returns wheter the engine is running.
-	bool is_running() const;
-
 	/// Return the number of frames rendered.
 	u64 frame_count() const;
 
@@ -153,7 +149,11 @@ public:
 	/// Reloads the resource @a type @a name.
 	void reload(StringId64 type, StringId64 name);
 
-	void log(const char* msg);
+	/// Logs @a msg to log file and console.
+	void log(const char* msg, LogSeverity::Enum severity);
+
+	/// Returns the console server.
+	ConsoleServer* console_server();
 
 	/// Returns the resource manager.
 	ResourceManager* resource_manager();
@@ -187,7 +187,6 @@ private:
 };
 
 void init(const DeviceOptions& opts);
-void update();
 void shutdown();
 Device* device();
 

+ 1 - 38
src/device/log.cpp

@@ -3,13 +3,11 @@
  * License: https://github.com/taylor001/crown/blob/master/LICENSE
  */
 
-#include "console_server.h"
 #include "device.h"
 #include "log.h"
 #include "mutex.h"
 #include "os.h"
 #include "platform.h"
-#include "string_stream.h"
 #include "string_utils.h"
 #include "temp_allocator.h"
 
@@ -19,39 +17,6 @@ namespace log_internal
 {
 	static Mutex s_mutex;
 
-	static StringStream& sanitize(StringStream& ss, const char* msg)
-	{
-		using namespace string_stream;
-		const char* ch = msg;
-		for (; *ch; ch++)
-		{
-			if (*ch == '"')
-				ss << "\\";
-			ss << *ch;
-		}
-
-		return ss;
-	}
-
-	static void console_log(const char* msg, LogSeverity::Enum sev)
-	{
-		if (!console_server_globals::console())
-			return;
-
-		static const char* stt[] = { "info", "warning", "error", "debug" };
-
-		// Build json message
-		using namespace string_stream;
-		TempAllocator4096 ta;
-		StringStream json(ta);
-
-		json << "{\"type\":\"message\",";
-		json << "\"severity\":\"" << stt[sev] << "\",";
-		json << "\"message\":\""; sanitize(json, msg) << "\"}";
-
-		console_server_globals::console()->send(c_str(json));
-	}
-
 	void logx(LogSeverity::Enum sev, const char* msg, va_list args)
 	{
 		ScopedMutex sm(s_mutex);
@@ -83,10 +48,8 @@ namespace log_internal
 
 		if (device())
 		{
-			device()->log(buf);
+			device()->log(buf, sev);
 		}
-
-		console_log(buf, sev);
 	}
 
 	void logx(LogSeverity::Enum sev, const char* msg, ...)

+ 0 - 4
src/device/main_android.cpp

@@ -7,7 +7,6 @@
 
 #if CROWN_PLATFORM_ANDROID
 
-#include "console_server.h"
 #include "device.h"
 #include "os_event_queue.h"
 #include "thread.h"
@@ -35,7 +34,6 @@ s32 func(void* data)
 {
 	MainThreadArgs* args = (MainThreadArgs*)data;
 	crown::init(*args->opts);
-	crown::update();
 	crown::shutdown();
 	s_exit = true;
 	return EXIT_SUCCESS;
@@ -313,9 +311,7 @@ void android_main(struct android_app* app)
 	DeviceOptions opts(0, NULL);
 	opts._asset_manager = app->activity->assetManager;
 
-	console_server_globals::init(opts._console_port, false);
 	crown::s_advc.run(app, opts);
-	console_server_globals::shutdown();
 	memory_globals::shutdown();
 }
 

+ 0 - 5
src/device/main_linux.cpp

@@ -8,7 +8,6 @@
 #if CROWN_PLATFORM_LINUX
 
 #include "bundle_compiler.h"
-#include "console_server.h"
 #include "device.h"
 #include "display.h"
 #include "os_event_queue.h"
@@ -294,7 +293,6 @@ s32 func(void* data)
 {
 	MainThreadArgs* args = (MainThreadArgs*)data;
 	crown::init(*args->opts);
-	crown::update();
 	crown::shutdown();
 	s_exit = true;
 	return EXIT_SUCCESS;
@@ -728,8 +726,6 @@ int main(int argc, char** argv)
 		return exitcode;
 	}
 
-	console_server_globals::init(opts._console_port, opts._wait_console);
-
 	bool do_continue = true;
 
 	if (opts._do_compile)
@@ -744,7 +740,6 @@ int main(int argc, char** argv)
 	if (opts._do_compile)
 		bundle_compiler_globals::shutdown();
 
-	console_server_globals::shutdown();
 	memory_globals::shutdown();
 	return exitcode;
 }

+ 0 - 5
src/device/main_windows.cpp

@@ -8,7 +8,6 @@
 #if CROWN_PLATFORM_WINDOWS
 
 #include "bundle_compiler.h"
-#include "console_server.h"
 #include "device.h"
 #include "os_event_queue.h"
 #include "thread.h"
@@ -294,7 +293,6 @@ s32 func(void* data)
 {
 	MainThreadArgs* args = (MainThreadArgs*)data;
 	crown::init(*args->opts);
-	crown::update();
 	crown::shutdown();
 	s_exit = true;
 	return EXIT_SUCCESS;
@@ -629,8 +627,6 @@ int main(int argc, char** argv)
 		return exitcode;
 	}
 
-	console_server_globals::init(opts._console_port, opts._wait_console);
-
 	bool do_continue = true;
 
 	if (opts._do_compile)
@@ -645,7 +641,6 @@ int main(int argc, char** argv)
 	if (opts._do_compile)
 		bundle_compiler_globals::shutdown();
 
-	console_server_globals::shutdown();
 	memory_globals::shutdown();
 	return exitcode;
 }

+ 1 - 1
src/lua/lua_api.cpp

@@ -2513,7 +2513,7 @@ static int device_console_send(lua_State* L)
 	stack.pop(1);
 	json << "}";
 
-	console_server_globals::console()->send(c_str(json));
+	device()->console_server()->send(c_str(json));
 	return 0;
 }