debug_adapter_server.cpp 4.4 KB

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