2020-09-13 21:32:55 -04:00
|
|
|
#pragma once
|
|
|
|
|
2020-08-22 22:30:12 -04:00
|
|
|
/*!
|
|
|
|
* @file Listener.h
|
|
|
|
* The Listener can connect to a Deci2Server for debugging.
|
|
|
|
*/
|
|
|
|
|
2022-06-22 23:37:46 -04:00
|
|
|
// clang-format off
|
2020-08-22 22:30:12 -04:00
|
|
|
#ifndef JAK1_LISTENER_H
|
|
|
|
#define JAK1_LISTENER_H
|
|
|
|
|
2022-06-22 23:37:46 -04:00
|
|
|
#include <mutex>
|
2020-08-22 22:30:12 -04:00
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
2020-10-31 14:07:43 -04:00
|
|
|
#include <unordered_map>
|
2022-06-22 23:37:46 -04:00
|
|
|
#include <vector>
|
|
|
|
|
2020-08-22 22:30:12 -04:00
|
|
|
#include "common/common_types.h"
|
2020-10-31 14:07:43 -04:00
|
|
|
#include "common/cross_os_debug/xdbg.h"
|
2023-04-22 14:13:57 -04:00
|
|
|
#include "common/versions/versions.h"
|
2022-06-22 23:37:46 -04:00
|
|
|
#include "common/listener_common.h"
|
|
|
|
|
2020-10-31 14:07:43 -04:00
|
|
|
#include "goalc/debugger/Debugger.h"
|
2020-11-13 22:33:57 -05:00
|
|
|
#include "MemoryMap.h"
|
2022-06-22 23:37:46 -04:00
|
|
|
// clang-format on
|
2020-08-22 22:30:12 -04:00
|
|
|
|
|
|
|
namespace listener {
|
2020-10-31 14:07:43 -04:00
|
|
|
|
2020-08-22 22:30:12 -04:00
|
|
|
class Listener {
|
|
|
|
public:
|
|
|
|
static constexpr int BUFFER_SIZE = 32 * 1024 * 1024;
|
|
|
|
Listener();
|
|
|
|
~Listener();
|
2023-04-02 00:57:21 -04:00
|
|
|
bool connect_to_target(int n_tries = 1, const std::string& ip = "127.0.0.1", int port = -1);
|
2020-08-22 22:30:12 -04:00
|
|
|
void record_messages(ListenerMessageKind kind);
|
2020-12-08 21:41:36 -05:00
|
|
|
int get_received_message_count();
|
2020-09-06 16:58:25 -04:00
|
|
|
std::vector<std::string> stop_recording_messages();
|
2020-08-22 22:30:12 -04:00
|
|
|
bool is_connected() const;
|
2020-09-06 16:58:25 -04:00
|
|
|
void send_reset(bool shutdown);
|
2020-09-07 13:28:16 -04:00
|
|
|
void send_poke();
|
2020-08-22 22:30:12 -04:00
|
|
|
void disconnect();
|
2021-06-30 22:13:15 -04:00
|
|
|
void send_code(std::vector<uint8_t>& code, const std::optional<std::string>& load_name = {});
|
2020-10-31 14:07:43 -04:00
|
|
|
void add_debugger(Debugger* debugger);
|
|
|
|
bool most_recent_send_was_acked() const { return got_ack; }
|
2023-04-02 00:57:21 -04:00
|
|
|
void set_default_port(GameVersion v) { m_default_port = DECI2_PORT - 1 + (int)v; }
|
2020-11-13 22:33:57 -05:00
|
|
|
MemoryMap build_memory_map();
|
2020-08-22 22:30:12 -04:00
|
|
|
|
|
|
|
private:
|
2020-10-31 14:07:43 -04:00
|
|
|
void add_load(const std::string& name, const LoadEntry& le);
|
|
|
|
void do_unload(const std::string& name);
|
|
|
|
|
2020-09-06 12:45:31 -04:00
|
|
|
void send_buffer(int sz);
|
|
|
|
bool wait_for_ack();
|
2020-10-31 14:07:43 -04:00
|
|
|
void handle_output_message(const char* msg);
|
2020-09-06 12:45:31 -04:00
|
|
|
|
2023-04-02 00:57:21 -04:00
|
|
|
int m_default_port = DECI2_PORT;
|
2020-08-26 01:21:33 -04:00
|
|
|
char* m_buffer = nullptr; //! buffer for incoming messages
|
|
|
|
bool m_connected = false; //! do we think we are connected?
|
|
|
|
bool receive_thread_running = false; //! is the receive thread unjoined?
|
2020-09-08 19:15:14 -04:00
|
|
|
int listen_socket = -1; //! socket
|
2020-08-22 22:30:12 -04:00
|
|
|
bool got_ack = false;
|
|
|
|
bool waiting_for_ack = false;
|
|
|
|
|
2020-10-31 14:07:43 -04:00
|
|
|
Debugger* m_debugger = nullptr;
|
|
|
|
|
2020-08-22 22:30:12 -04:00
|
|
|
std::thread rcv_thread;
|
|
|
|
std::mutex rcv_mtx;
|
|
|
|
void receive_func();
|
|
|
|
ListenerMessageKind filter = ListenerMessageKind::MSG_INVALID;
|
|
|
|
std::vector<std::string> message_record;
|
2020-10-31 14:07:43 -04:00
|
|
|
std::unordered_map<std::string, LoadEntry> m_load_entries;
|
2021-06-30 22:13:15 -04:00
|
|
|
|
|
|
|
std::optional<std::string> m_pending_listener_load_object_name;
|
2020-08-22 22:30:12 -04:00
|
|
|
char ack_recv_buff[512];
|
2020-12-28 18:37:05 -05:00
|
|
|
uint64_t last_sent_id = 0;
|
|
|
|
uint64_t last_recvd_id = 0;
|
2020-08-22 22:30:12 -04:00
|
|
|
};
|
2020-08-26 01:21:33 -04:00
|
|
|
} // namespace listener
|
2020-08-22 22:30:12 -04:00
|
|
|
|
|
|
|
#endif // JAK1_LISTENER_H
|