debug_adapter_server.cpp 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*************************************************************************/
  2. /* debug_adapter_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "debug_adapter_server.h"
  31. #include "core/os/os.h"
  32. #include "editor/editor_log.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. DebugAdapterServer::DebugAdapterServer() {
  36. _EDITOR_DEF("network/debug_adapter/remote_port", remote_port);
  37. _EDITOR_DEF("network/debug_adapter/request_timeout", protocol._request_timeout);
  38. _EDITOR_DEF("network/debug_adapter/sync_breakpoints", protocol._sync_breakpoints);
  39. }
  40. void DebugAdapterServer::_notification(int p_what) {
  41. switch (p_what) {
  42. case NOTIFICATION_ENTER_TREE: {
  43. start();
  44. } break;
  45. case NOTIFICATION_EXIT_TREE: {
  46. stop();
  47. } break;
  48. case NOTIFICATION_INTERNAL_PROCESS: {
  49. // The main loop can be run again during request processing, which modifies internal state of the protocol.
  50. // Thus, "polling" is needed to prevent it from parsing other requests while the current one isn't finished.
  51. if (started && !polling) {
  52. polling = true;
  53. protocol.poll();
  54. polling = false;
  55. }
  56. } break;
  57. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  58. protocol._request_timeout = EditorSettings::get_singleton()->get("network/debug_adapter/request_timeout");
  59. protocol._sync_breakpoints = EditorSettings::get_singleton()->get("network/debug_adapter/sync_breakpoints");
  60. int port = (int)_EDITOR_GET("network/debug_adapter/remote_port");
  61. if (port != remote_port) {
  62. stop();
  63. start();
  64. }
  65. } break;
  66. }
  67. }
  68. void DebugAdapterServer::start() {
  69. remote_port = (int)_EDITOR_GET("network/debug_adapter/remote_port");
  70. if (protocol.start(remote_port, IPAddress("127.0.0.1")) == OK) {
  71. EditorNode::get_log()->add_message("--- Debug adapter server started ---", EditorLog::MSG_TYPE_EDITOR);
  72. set_process_internal(true);
  73. started = true;
  74. }
  75. }
  76. void DebugAdapterServer::stop() {
  77. protocol.stop();
  78. started = false;
  79. EditorNode::get_log()->add_message("--- Debug adapter server stopped ---", EditorLog::MSG_TYPE_EDITOR);
  80. }