-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscr_python_remote.h
More file actions
79 lines (65 loc) · 2.14 KB
/
Copy pathscr_python_remote.h
File metadata and controls
79 lines (65 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma once
#include "scr_python_console.h"
#include <atomic>
#include <cstdint>
#include <functional>
#include <mutex>
#include <string>
#include <thread>
#include <unordered_set>
#include <vector>
/// Parsed `--listen [host:]port`. Default host is 127.0.0.1 when only a port is given.
struct Python_listen_endpoint
{
std::string host = "127.0.0.1";
uint16_t port = 0;
bool valid() const { return port != 0; }
};
/// Parse `8765` or `127.0.0.1:8765`. Returns false and sets error on failure.
bool parse_listen_arg(const std::string& arg, Python_listen_endpoint& out, std::string& error);
/// Thread-safe queue: remote listener enqueues; main thread runs process_pending().
class Python_execution_queue
{
public:
using Completer = std::function<void(Python_exec_result)>;
void enqueue(std::string code, Completer done);
void process_pending(Python_console& console);
/// Fail all pending jobs and reject new ones (unblocks remote waiters on shutdown).
void shutdown();
private:
struct Job
{
std::string code;
Completer done;
};
std::mutex m_mutex;
std::vector<Job> m_jobs;
bool m_shutdown = false;
};
/// Background TCP server: length-prefixed UTF-8 JSON frames for remote Python console exec.
class Python_remote_server
{
public:
explicit Python_remote_server(Python_execution_queue& queue);
~Python_remote_server();
Python_remote_server(const Python_remote_server&) = delete;
Python_remote_server& operator=(const Python_remote_server&) = delete;
bool start(const Python_listen_endpoint& ep, std::string& error);
void stop();
private:
void accept_loop_();
void handle_client_(uintptr_t client_sock);
void track_client_(uintptr_t client_sock);
void untrack_client_(uintptr_t client_sock);
void close_all_clients_();
Python_execution_queue& m_queue;
Python_listen_endpoint m_ep;
std::thread m_thread;
std::atomic<bool> m_running{false};
uintptr_t m_listen_sock = 0;
std::mutex m_clients_mutex;
std::unordered_set<uintptr_t> m_clients;
#ifdef _WIN32
bool m_wsa_started = false;
#endif
};