ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
session_host.cpp
Go to the documentation of this file.
1#include <emscripten/bind.h>
2#include "protocol_bytes.h"
3#include "byte_arrays.h"
4extern "C" {
5#include <ascii-chat/common.h>
9#include "session/host.h"
10}
11#include <array>
12#include <cstdint>
13#include <new>
14#include <vector>
15
17struct HostPacket {
19 std::vector<uint8_t> bytes;
20};
23 uint32_t id = 0;
25
26 void reset() {
27 owner = nullptr;
28 id = 0;
29 transport.~acip_transport_t();
31 }
32};
33
35public:
37 create();
38 }
40 close();
41 }
44
45 bool valid() const {
46 return host_ != nullptr;
47 }
48 int error() const {
49 return host_ ? 0 : static_cast<int>(ERROR_INVALID_STATE);
50 }
51 void close() {
53 host_ = nullptr;
54 packets_.clear();
55 for (auto &client : clients_)
56 client.reset();
57 }
59 void removeClient(uint32_t id);
61 int render() {
62 return host_ ? static_cast<int>(session_host_render_video(host_)) : invalidState();
63 }
65 return host_ ? static_cast<int>(session_host_render_audio(host_)) : invalidState();
66 }
67 int receive(uint32_t id, uint32_t type, ProtocolBytes packet);
68 emscripten::val drainPackets();
69
70private:
71 session_host_t *host_ = nullptr;
72 std::array<HostTransportContext, MAX_SESSION_PARTICIPANTS> clients_{};
73 static int invalidState() {
74 return static_cast<int>(ERROR_INVALID_STATE);
75 }
76 void create() {
77 session_host_config_t config{};
79 host_ = session_host_create(&config);
80 if (host_)
82 }
83 void send(uint32_t id, const void *data, size_t length) {
84 packets_.push_back({id, std::vector<uint8_t>(static_cast<const uint8_t *>(data), static_cast<const uint8_t *>(data) + length)});
85 }
86 static asciichat_error_t sendPacket(acip_transport_t *transport, const void *data, size_t length);
87 static bool connected(acip_transport_t *transport);
88 static socket_t noSocket(acip_transport_t *transport);
89 static acip_transport_type_t type(acip_transport_t *transport);
90 static const acip_transport_methods_t methods;
91 std::vector<HostPacket> packets_;
92};
93
94asciichat_error_t BrowserHostBinding::sendPacket(acip_transport_t *transport, const void *data, size_t length) {
95 auto *context = transport ? static_cast<HostTransportContext *>(transport->impl_data) : nullptr;
96 if (!context || !context->owner || !data || !length)
97 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid browser host packet");
98 context->owner->send(context->id, data, length);
99 return ASCIICHAT_OK;
100}
101bool BrowserHostBinding::connected(acip_transport_t *transport) {
102 auto *context = transport ? static_cast<HostTransportContext *>(transport->impl_data) : nullptr;
103 return context && context->owner && context->owner->valid();
104}
105socket_t BrowserHostBinding::noSocket(acip_transport_t *) {
106 return INVALID_SOCKET_VALUE;
107}
108acip_transport_type_t BrowserHostBinding::type(acip_transport_t *) {
110}
111const acip_transport_methods_t BrowserHostBinding::methods = {
112 .send = &BrowserHostBinding::sendPacket, .get_type = &BrowserHostBinding::type, .get_socket = &BrowserHostBinding::noSocket, .is_connected = &BrowserHostBinding::connected};
113
115 if (!host_)
116 return 0;
117 for (auto &client : clients_) {
118 if (client.id)
119 continue;
120 const uint32_t id = session_host_add_client(host_, nullptr, ACIP_TRANSPORT_BORROWED, "browser", 0);
121 if (!id)
122 return 0;
123 client.owner = this;
124 client.id = id;
125 client.transport.methods = &methods;
126 client.transport.impl_data = &client;
127 if (session_host_set_client_transport(host_, id, &client.transport, ACIP_TRANSPORT_BORROWED) != ASCIICHAT_OK) {
129 client.reset();
130 return 0;
131 }
132 return id;
133 }
134 SET_ERRNO(ERROR_INVALID_STATE, "Browser session host has no available client slots");
135 return 0;
136}
138 if (!host_)
139 return;
141 for (auto &client : clients_)
142 if (client.id == id)
143 client.reset();
144}
146 if (!host_)
147 return invalidState();
149 target.client_id = id;
150 return static_cast<int>(session_host_set_render_target(host_, id ? &target : nullptr));
151}
152int BrowserHostBinding::receive(uint32_t id, uint32_t typeValue, ProtocolBytes packet) {
153 if (!host_ || !id)
154 return static_cast<int>(SET_ERRNO(ERROR_INVALID_PARAM, "Invalid browser host client"));
155 const size_t length = packet["length"].as<size_t>();
156 std::vector<uint8_t> input(length ? length : 1);
157 if (length)
158 emscripten::val(emscripten::typed_memory_view(length, input.data())).call<void>("set", packet);
159 return static_cast<int>(session_host_receive_packet(host_, id, static_cast<packet_type_t>(typeValue), input.data(), length));
160}
162 emscripten::val result = emscripten::val::array();
163 for (const auto &packet : packets_) {
164 emscripten::val output = emscripten::val::object();
165 output.set("id", packet.client);
166 output.set("bytes", binding_copy_output(packet.bytes.data(), packet.bytes.size()));
167 result.call<void>("push", output);
168 }
169 packets_.clear();
170 return result;
171}
172
173EMSCRIPTEN_BINDINGS(ascii_chat_session_host) {
174 emscripten::class_<BrowserHostBinding>("BrowserHostBinding")
175 .constructor<>()
176 .function("valid", &BrowserHostBinding::valid)
177 .function("error", &BrowserHostBinding::error)
178 .function("close", &BrowserHostBinding::close)
179 .function("addClient", &BrowserHostBinding::addClient)
180 .function("removeClient", &BrowserHostBinding::removeClient)
181 .function("setLocalClient", &BrowserHostBinding::setLocalClient)
182 .function("render", &BrowserHostBinding::render)
183 .function("renderAudio", &BrowserHostBinding::renderAudio)
184 .function("drainPackets", &BrowserHostBinding::drainPackets)
185 .function("receive", &BrowserHostBinding::receive);
186}
ascii-chat Discovery Service (ACDS) Protocol Message Formats
ACIP client-side protocol library and ACDS client connection API.
#define SET_ERRNO(code,...)
emscripten::val binding_copy_output(const void *data, size_t length)
Definition byte_arrays.h:18
int receive(uint32_t id, uint32_t type, ProtocolBytes packet)
BrowserHostBinding(const BrowserHostBinding &)=delete
void removeClient(uint32_t id)
int setLocalClient(uint32_t id)
BrowserHostBinding & operator=(const BrowserHostBinding &)=delete
emscripten::val drainPackets()
unsigned int uint32_t
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define MAX_SESSION_PARTICIPANTS
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:315
asciichat_error_t session_host_destroy(session_host_t *host)
Destroy session host and free resources.
Definition host.c:289
asciichat_error_t session_host_set_client_transport(session_host_t *host, uint32_t client_id, acip_transport_t *transport, acip_transport_ownership_t ownership)
Select a transport for a specific client in the host.
Definition host.c:1653
struct session_host session_host_t
Opaque session host handle.
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
asciichat_error_t session_host_remove_client(session_host_t *host, uint32_t client_id)
Remove a client by ID.
Definition host.c:1342
session_host_t * session_host_create(const session_host_config_t *config)
Create a new session host.
Definition host.c:234
asciichat_error_t session_host_render_audio(session_host_t *host)
Definition host.c:794
asciichat_error_t session_host_render_video(session_host_t *host)
Definition host.c:787
uint32_t session_host_add_client(session_host_t *host, acip_transport_t *transport, acip_transport_ownership_t ownership, const char *ip, int port)
Definition host.c:1094
asciichat_error_t session_host_set_render_target(session_host_t *host, const session_host_render_target_t *target)
Definition host.c:1504
asciichat_error_t session_host_receive_packet(session_host_t *host, uint32_t client_id, packet_type_t ptype, const void *data, size_t len)
Definition host.c:343
🏠 Server-side session hosting abstraction
int socket_t
⚙️ Common definitions, error codes, macros, and types shared throughout the application
Transport abstraction layer for ACIP protocol.
acip_transport_type_t
Transport method table (virtual function table).
@ ACIP_TRANSPORT_WEBRTC
WebRTC DataChannel (P2P).
EMSCRIPTEN_BINDINGS(ascii_chat_session_host)
std::vector< uint8_t > bytes
uint32_t client
BrowserHostBinding * owner
acip_transport_t transport
Transport method table (virtual function table).
void * impl_data
Transport-specific state.
int max_clients
Maximum number of clients.
Definition host.h:174
session host render target is intentionally a small, concrete record. Fields such as client_id,...
Definition host.h:452