|
19 | 19 |
|
20 | 20 | #pragma once |
21 | 21 |
|
22 | | -#include <string> |
23 | 22 | #include <queue> |
| 23 | +#include <string> |
24 | 24 |
|
25 | 25 | #include "core/debug.h" |
26 | 26 | #include "core/psxemulator.h" |
27 | 27 | #include "core/psxmem.h" |
28 | 28 | #include "support/eventbus.h" |
29 | 29 | #include "support/hashtable.h" |
30 | | -#include "support/slice.h" |
31 | 30 | #include "support/list.h" |
32 | | - |
| 31 | +#include "support/slice.h" |
33 | 32 |
|
34 | 33 | namespace PCSX { |
35 | | - class SIO1Client; |
36 | | - class SIO1Server; |
37 | | - |
38 | | - class SIO1Client : public Intrusive::List<SIO1Client>::Node { |
39 | | - public: |
40 | | - SIO1Client(uv_tcp_t* server); |
41 | | - typedef Intrusive::List<SIO1Client> ListType; |
42 | | - |
43 | | - bool accept(uv_tcp_t* server) { |
44 | | - assert(m_status == CLOSED); |
45 | | - if (uv_accept(reinterpret_cast<uv_stream_t*>(server), reinterpret_cast<uv_stream_t*>(&m_tcp)) == 0) { |
46 | | - uv_read_start(reinterpret_cast<uv_stream_t*>(&m_tcp), allocTrampoline, readTrampoline); |
47 | | - m_status = OPEN; |
48 | | - } |
49 | | - return m_status == OPEN; |
50 | | - } |
51 | | - void close() { |
52 | | - if (m_status != OPEN) return; |
53 | | - m_status = CLOSING; |
54 | | - uv_close(reinterpret_cast<uv_handle_t*>(&m_tcp), closeCB); |
55 | | - } |
| 34 | +class SIO1Client; |
| 35 | +class SIO1Server; |
56 | 36 |
|
57 | | - private: |
| 37 | +class SIO1Client : public Intrusive::List<SIO1Client>::Node { |
| 38 | + public: |
| 39 | + SIO1Client(uv_tcp_t* server); |
| 40 | + typedef Intrusive::List<SIO1Client> ListType; |
58 | 41 |
|
59 | | - uv_tcp_t m_tcp; |
60 | | - enum { CLOSED, OPEN, CLOSING } m_status = CLOSED; |
61 | | - bool m_allocated = false; |
| 42 | + bool accept(uv_tcp_t* server); |
| 43 | + void close(); |
62 | 44 |
|
63 | | - EventBus::Listener m_listener; |
64 | | - uv_loop_t* m_loop; |
65 | | - friend SIO1Server; |
66 | | - static constexpr size_t BUFFER_SIZE = 4096; |
| 45 | + private: |
| 46 | + uv_tcp_t m_tcp; |
| 47 | + enum class SIO1ClientStatus { CLOSED, OPEN, CLOSING }; |
| 48 | + SIO1ClientStatus m_status = SIO1ClientStatus::CLOSED; |
67 | 49 |
|
68 | | - static void allocTrampoline(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf) { |
69 | | - SIO1Client* client = static_cast<SIO1Client*>(handle->data); |
70 | | - client->alloc(suggestedSize, buf); |
71 | | - } |
| 50 | + bool m_allocated = false; |
72 | 51 |
|
73 | | - void alloc(size_t suggestedSize, uv_buf_t* buf) { |
74 | | - assert(!m_allocated); |
75 | | - m_allocated = true; |
76 | | - buf->base = m_buffer; |
77 | | - buf->len = sizeof(m_buffer); |
78 | | - } |
| 52 | + EventBus::Listener m_listener; |
| 53 | + uv_loop_t* m_loop = 0; |
| 54 | + friend SIO1Server; |
| 55 | + static constexpr size_t BUFFER_SIZE = 4096; |
79 | 56 |
|
80 | | - static void readTrampoline(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) { |
81 | | - SIO1Client* client = static_cast<SIO1Client*>(stream->data); |
82 | | - client->read(nread, buf); |
83 | | - } |
84 | | - void read(ssize_t nread, const uv_buf_t* buf) { |
85 | | - m_allocated = false; |
86 | | - if (nread <= 0) { |
87 | | - close(); |
88 | | - return; |
89 | | - } |
90 | | - |
91 | | - Slice slice; |
92 | | - slice.borrow(m_buffer, nread); |
93 | | - processData(slice); |
94 | | - } |
| 57 | + void alloc(size_t suggestedSize, uv_buf_t* buf); |
95 | 58 |
|
96 | | - void write(unsigned char c) |
97 | | - { |
98 | | - auto* req = new WriteRequest(); |
99 | | - req->m_slice.copy(static_cast<void*>(&c), 1); |
100 | | - req->enqueue(this); |
101 | | - } |
102 | | - static void closeCB(uv_handle_t* handle) { |
103 | | - SIO1Client* client = static_cast<SIO1Client*>(handle->data); |
104 | | - delete client; |
105 | | - } |
| 59 | + static void allocTrampoline(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf) { |
| 60 | + SIO1Client* client = static_cast<SIO1Client*>(handle->data); |
| 61 | + client->alloc(suggestedSize, buf); |
| 62 | + } |
106 | 63 |
|
107 | | - void processData(const Slice& slice); |
108 | | - |
109 | | - struct WriteRequest : public Intrusive::HashTable<uintptr_t, WriteRequest>::Node { |
110 | | - WriteRequest() {} |
111 | | - WriteRequest(Slice&& slice) : m_slice(std::move(slice)) {} |
112 | | - void enqueue(SIO1Client* client) { |
113 | | - m_buf.base = static_cast<char*>(const_cast<void*>(m_slice.data())); |
114 | | - m_buf.len = m_slice.size(); |
115 | | - client->m_requests.insert(reinterpret_cast<uintptr_t>(&m_req), this); |
116 | | - uv_write(&m_req, reinterpret_cast<uv_stream_t*>(&client->m_tcp), &m_buf, 1, writeCB); |
117 | | - } |
118 | | - static void writeCB(uv_write_t* request, int status) { |
119 | | - SIO1Client* client = static_cast<SIO1Client*>(request->handle->data); |
120 | | - auto self = client->m_requests.find(reinterpret_cast<uintptr_t>(request)); |
121 | | - delete &*self; |
122 | | - if (status != 0) client->close(); |
123 | | - } |
124 | | - uv_buf_t m_buf; |
125 | | - uv_write_t m_req; |
126 | | - Slice m_slice; |
127 | | - }; |
128 | | - Intrusive::HashTable<uintptr_t, WriteRequest> m_requests; |
129 | | - |
130 | | - char m_buffer[BUFFER_SIZE]; |
131 | | - }; |
| 64 | + static void closeCB(uv_handle_t* handle) { |
| 65 | + SIO1Client* client = static_cast<SIO1Client*>(handle->data); |
| 66 | + delete client; |
| 67 | + } |
132 | 68 |
|
133 | | - class SIO1Server { |
134 | | - public: |
135 | | - SIO1Server(); |
136 | | - //~SIO1Server() { } |
137 | | - enum SIO1ServerStatus { |
138 | | - SERVER_STOPPED, |
139 | | - SERVER_STOPPING, |
140 | | - SERVER_STARTED, |
141 | | - }; |
142 | | - SIO1ServerStatus getServerStatus() { return m_serverStatus; } |
| 69 | + void processData(const Slice& slice); |
143 | 70 |
|
144 | | - void startServer(uv_loop_t* loop, int port = 6699); |
145 | | - void stopServer(); |
| 71 | + void read(ssize_t nread, const uv_buf_t* buf); |
146 | 72 |
|
| 73 | + static void readTrampoline(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) { |
| 74 | + SIO1Client* client = static_cast<SIO1Client*>(stream->data); |
| 75 | + client->read(nread, buf); |
| 76 | + } |
147 | 77 |
|
148 | | - void write(unsigned char c) { |
149 | | - for (auto& client : m_clients) client.write(c); |
150 | | - } |
| 78 | + void write(unsigned char c); |
151 | 79 |
|
152 | | - private: |
153 | | - static void onNewConnectionTrampoline(uv_stream_t* server, int status); |
154 | | - void onNewConnection(int status); |
155 | | - static void closeCB(uv_handle_t* handle); |
156 | | - SIO1ServerStatus m_serverStatus = SERVER_STOPPED; |
157 | | - uv_tcp_t m_server; |
158 | | - uv_loop_t* m_loop; |
159 | | - SIO1Client::ListType m_clients; |
160 | | - EventBus::Listener m_listener; |
161 | | - |
162 | | - std::string m_gotError; |
| 80 | + struct WriteRequest : public Intrusive::HashTable<uintptr_t, WriteRequest>::Node { |
| 81 | + WriteRequest() {} |
| 82 | + WriteRequest(Slice&& slice) : m_slice(std::move(slice)) {} |
| 83 | + void enqueue(SIO1Client* client) { |
| 84 | + m_buf.base = static_cast<char*>(const_cast<void*>(m_slice.data())); |
| 85 | + m_buf.len = m_slice.size(); |
| 86 | + client->m_requests.insert(reinterpret_cast<uintptr_t>(&m_req), this); |
| 87 | + uv_write(&m_req, reinterpret_cast<uv_stream_t*>(&client->m_tcp), &m_buf, 1, writeCB); |
| 88 | + } |
| 89 | + static void writeCB(uv_write_t* request, int status) { |
| 90 | + SIO1Client* client = static_cast<SIO1Client*>(request->handle->data); |
| 91 | + auto self = client->m_requests.find(reinterpret_cast<uintptr_t>(request)); |
| 92 | + delete &*self; |
| 93 | + if (status != 0) client->close(); |
| 94 | + } |
| 95 | + uv_buf_t m_buf; |
| 96 | + uv_write_t m_req; |
| 97 | + Slice m_slice; |
| 98 | + }; |
| 99 | + Intrusive::HashTable<uintptr_t, WriteRequest> m_requests; |
| 100 | + |
| 101 | + char m_buffer[BUFFER_SIZE]; |
| 102 | +}; |
| 103 | + |
| 104 | +class SIO1Server { |
| 105 | + public: |
| 106 | + SIO1Server(); |
| 107 | + //~SIO1Server() { } |
| 108 | + enum class SIO1ServerStatus { |
| 109 | + SERVER_STOPPED, |
| 110 | + SERVER_STOPPING, |
| 111 | + SERVER_STARTED, |
163 | 112 | }; |
| 113 | + SIO1ServerStatus getServerStatus() { return m_serverStatus; } |
| 114 | + |
| 115 | + void startServer(uv_loop_t* loop, int port = 6699); |
| 116 | + void stopServer(); |
| 117 | + |
| 118 | + void write(unsigned char c) { |
| 119 | + for (auto& client : m_clients) client.write(c); |
| 120 | + } |
| 121 | + |
| 122 | + private: |
| 123 | + static void onNewConnectionTrampoline(uv_stream_t* server, int status); |
| 124 | + void onNewConnection(int status); |
| 125 | + static void closeCB(uv_handle_t* handle); |
| 126 | + SIO1ServerStatus m_serverStatus = SIO1ServerStatus::SERVER_STOPPED; |
| 127 | + uv_tcp_t m_server; |
| 128 | + uv_loop_t* m_loop; |
| 129 | + SIO1Client::ListType m_clients; |
| 130 | + EventBus::Listener m_listener; |
| 131 | + |
| 132 | + std::string m_gotError; |
| 133 | +}; |
164 | 134 |
|
165 | 135 | } // namespace PCSX |
0 commit comments