debug_adapter_server.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #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. int DebugAdapterServer::port_override = -1;
  36. DebugAdapterServer::DebugAdapterServer() {
  37. // TODO: Move to editor_settings.cpp
  38. _EDITOR_DEF("network/debug_adapter/remote_port", remote_port);
  39. _EDITOR_DEF("network/debug_adapter/request_timeout", protocol._request_timeout);
  40. _EDITOR_DEF("network/debug_adapter/sync_breakpoints", protocol._sync_breakpoints);
  41. }
  42. void DebugAdapterServer::_notification(int p_what) {
  43. switch (p_what) {
  44. case NOTIFICATION_ENTER_TREE: {
  45. start();
  46. } break;
  47. case NOTIFICATION_EXIT_TREE: {
  48. stop();
  49. } break;
  50. case NOTIFICATION_INTERNAL_PROCESS: {
  51. // The main loop can be run again during request processing, which modifies internal state of the protocol.
  52. // Thus, "polling" is needed to prevent it from parsing other requests while the current one isn't finished.
  53. if (started && !polling) {
  54. polling = true;
  55. protocol.poll();
  56. polling = false;
  57. }
  58. } break;
  59. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  60. if (!EditorSettings::get_singleton()->check_changed_settings_in_group("network/debug_adapter")) {
  61. break;
  62. }
  63. protocol._request_timeout = EDITOR_GET("network/debug_adapter/request_timeout");
  64. protocol._sync_breakpoints = EDITOR_GET("network/debug_adapter/sync_breakpoints");
  65. int port = (DebugAdapterServer::port_override > -1) ? DebugAdapterServer::port_override : (int)_EDITOR_GET("network/debug_adapter/remote_port");
  66. if (port != remote_port) {
  67. stop();
  68. start();
  69. }
  70. } break;
  71. }
  72. }
  73. void DebugAdapterServer::start() {
  74. remote_port = (DebugAdapterServer::port_override > -1) ? DebugAdapterServer::port_override : (int)_EDITOR_GET("network/debug_adapter/remote_port");
  75. if (protocol.start(remote_port, IPAddress("127.0.0.1")) == OK) {
  76. EditorNode::get_log()->add_message("--- Debug adapter server started on port " + itos(remote_port) + " ---", EditorLog::MSG_TYPE_EDITOR);
  77. set_process_internal(true);
  78. started = true;
  79. }
  80. }
  81. void DebugAdapterServer::stop() {
  82. protocol.stop();
  83. started = false;
  84. EditorNode::get_log()->add_message("--- Debug adapter server stopped ---", EditorLog::MSG_TYPE_EDITOR);
  85. }