1 | /**************************************************************************/ |
2 | /* debug_adapter_server.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #include "debug_adapter_server.h" |
32 | |
33 | #include "core/os/os.h" |
34 | #include "editor/editor_log.h" |
35 | #include "editor/editor_node.h" |
36 | #include "editor/editor_settings.h" |
37 | |
38 | DebugAdapterServer::DebugAdapterServer() { |
39 | _EDITOR_DEF("network/debug_adapter/remote_port" , remote_port); |
40 | _EDITOR_DEF("network/debug_adapter/request_timeout" , protocol._request_timeout); |
41 | _EDITOR_DEF("network/debug_adapter/sync_breakpoints" , protocol._sync_breakpoints); |
42 | } |
43 | |
44 | void DebugAdapterServer::_notification(int p_what) { |
45 | switch (p_what) { |
46 | case NOTIFICATION_ENTER_TREE: { |
47 | start(); |
48 | } break; |
49 | |
50 | case NOTIFICATION_EXIT_TREE: { |
51 | stop(); |
52 | } break; |
53 | |
54 | case NOTIFICATION_INTERNAL_PROCESS: { |
55 | // The main loop can be run again during request processing, which modifies internal state of the protocol. |
56 | // Thus, "polling" is needed to prevent it from parsing other requests while the current one isn't finished. |
57 | if (started && !polling) { |
58 | polling = true; |
59 | protocol.poll(); |
60 | polling = false; |
61 | } |
62 | } break; |
63 | |
64 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
65 | protocol._request_timeout = EDITOR_GET("network/debug_adapter/request_timeout" ); |
66 | protocol._sync_breakpoints = EDITOR_GET("network/debug_adapter/sync_breakpoints" ); |
67 | int port = (int)_EDITOR_GET("network/debug_adapter/remote_port" ); |
68 | if (port != remote_port) { |
69 | stop(); |
70 | start(); |
71 | } |
72 | } break; |
73 | } |
74 | } |
75 | |
76 | void DebugAdapterServer::start() { |
77 | remote_port = (int)_EDITOR_GET("network/debug_adapter/remote_port" ); |
78 | if (protocol.start(remote_port, IPAddress("127.0.0.1" )) == OK) { |
79 | EditorNode::get_log()->add_message("--- Debug adapter server started ---" , EditorLog::MSG_TYPE_EDITOR); |
80 | set_process_internal(true); |
81 | started = true; |
82 | } |
83 | } |
84 | |
85 | void DebugAdapterServer::stop() { |
86 | protocol.stop(); |
87 | started = false; |
88 | EditorNode::get_log()->add_message("--- Debug adapter server stopped ---" , EditorLog::MSG_TYPE_EDITOR); |
89 | } |
90 | |