Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #ifndef INCLUDED_DAPINTERFACE 19 : #define INCLUDED_DAPINTERFACE 20 : 21 : #include "lib/code_annotation.h" 22 : 23 : #include <condition_variable> 24 : #include <js/RootingAPI.h> 25 : #include <js/TypeDecls.h> 26 : #include <js/Value.h> 27 : #include <memory> 28 : #include <mutex> 29 : #include <stdexcept> 30 : #include <string> 31 : 32 : namespace Script { class Context; } 33 : namespace Script { class Interface; } 34 : 35 : namespace DAP 36 : { 37 : // DapInterfaceException 38 : class DapInterfaceException : public std::runtime_error 39 : { 40 : public: 41 3 : explicit DapInterfaceException(const std::string& message) 42 2 : : std::runtime_error(message) {} 43 : }; 44 : 45 : class DapInterfaceNoJSDebuggerException : public DapInterfaceException 46 : { 47 : public: 48 1 : explicit DapInterfaceNoJSDebuggerException(const std::string& message) 49 1 : : DapInterfaceException(message) {} 50 : }; 51 : 52 : class Interface 53 : { 54 : public: 55 : Interface(const std::string server_address, int port, Script::Context& scriptContext); 56 : ~Interface(); 57 : NONCOPYABLE(Interface); 58 : 59 : void TryHandleMessage(); 60 : 61 : void WaitForMessage(); 62 : void EndWaitingForMessage(); 63 : 64 : private: 65 : class SocketHandler; 66 : 67 : bool isJSHandlerDefined(); 68 : std::string SendDapMessage(const std::string& message); 69 : std::string OnMessage(const std::string& message); 70 : void SendEventToClient(); 71 : 72 : std::unique_ptr<SocketHandler> m_SocketImpl; 73 : std::unique_ptr<Script::Interface> m_ScriptInterface; 74 : 75 : std::string m_DapRequest; 76 : std::string m_DapResponse; 77 : 78 : std::mutex m_MsgLock; 79 : std::mutex m_WaitingLock; 80 : std::condition_variable m_MsgApplied; 81 : 82 : bool m_IsWaiting{false}; 83 : JS::RootedValue m_ModuleValue; 84 : }; 85 : } 86 : 87 : #endif // !INCLUDED_DAPINTERFACE