ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
BrowserHostBinding Class Reference

Public Member Functions

 BrowserHostBinding ()
 ~BrowserHostBinding ()
 BrowserHostBinding (const BrowserHostBinding &)=delete
BrowserHostBinding & operator= (const BrowserHostBinding &)=delete
bool valid () const
int error () const
void close ()
uint32_t addClient ()
void removeClient (uint32_t id)
int setLocalClient (uint32_t id)
int render ()
int renderAudio ()
int receive (uint32_t id, uint32_t type, ProtocolBytes packet)
emscripten::val drainPackets ()

Detailed Description

Definition at line 34 of file session_host.cpp.

Constructor & Destructor Documentation

◆ BrowserHostBinding() [1/2]

BrowserHostBinding::BrowserHostBinding ( )
inline

Definition at line 36 of file session_host.cpp.

36 {
37 create();
38 }

Referenced by BrowserHostBinding(), and operator=().

◆ ~BrowserHostBinding()

BrowserHostBinding::~BrowserHostBinding ( )
inline

Definition at line 39 of file session_host.cpp.

39 {
40 close();
41 }

References close().

◆ BrowserHostBinding() [2/2]

BrowserHostBinding::BrowserHostBinding ( const BrowserHostBinding & )
delete

References BrowserHostBinding().

Member Function Documentation

◆ addClient()

uint32_t BrowserHostBinding::addClient ( )

Definition at line 114 of file session_host.cpp.

114 {
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}
#define SET_ERRNO(code,...)
unsigned int uint32_t
@ ERROR_INVALID_STATE
@ ASCIICHAT_OK
Definition error_codes.h:50
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
asciichat_error_t session_host_remove_client(session_host_t *host, uint32_t client_id)
Remove a client by ID.
Definition host.c:1342
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

References ACIP_TRANSPORT_BORROWED, ASCIICHAT_OK, ERROR_INVALID_STATE, session_host_add_client(), session_host_remove_client(), session_host_set_client_transport(), and SET_ERRNO.

Referenced by EMSCRIPTEN_BINDINGS().

◆ close()

void BrowserHostBinding::close ( )
inline

Definition at line 51 of file session_host.cpp.

51 {
53 host_ = nullptr;
54 packets_.clear();
55 for (auto &client : clients_)
56 client.reset();
57 }
asciichat_error_t session_host_destroy(session_host_t *host)
Destroy session host and free resources.
Definition host.c:289

References session_host_destroy().

Referenced by EMSCRIPTEN_BINDINGS(), and ~BrowserHostBinding().

◆ drainPackets()

emscripten::val BrowserHostBinding::drainPackets ( )

Definition at line 161 of file session_host.cpp.

161 {
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}
emscripten::val binding_copy_output(const void *data, size_t length)
Definition byte_arrays.h:18

References binding_copy_output().

Referenced by EMSCRIPTEN_BINDINGS().

◆ error()

int BrowserHostBinding::error ( ) const
inline

Definition at line 48 of file session_host.cpp.

48 {
49 return host_ ? 0 : static_cast<int>(ERROR_INVALID_STATE);
50 }

References ERROR_INVALID_STATE.

Referenced by EMSCRIPTEN_BINDINGS().

◆ operator=()

BrowserHostBinding & BrowserHostBinding::operator= ( const BrowserHostBinding & )
delete

References BrowserHostBinding().

◆ receive()

int BrowserHostBinding::receive ( uint32_t id,
uint32_t type,
ProtocolBytes packet )

Definition at line 152 of file session_host.cpp.

152 {
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}
@ ERROR_INVALID_PARAM
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:315
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

References ERROR_INVALID_PARAM, session_host_receive_packet(), and SET_ERRNO.

Referenced by EMSCRIPTEN_BINDINGS().

◆ removeClient()

void BrowserHostBinding::removeClient ( uint32_t id)

Definition at line 137 of file session_host.cpp.

137 {
138 if (!host_)
139 return;
141 for (auto &client : clients_)
142 if (client.id == id)
143 client.reset();
144}

References session_host_remove_client().

Referenced by EMSCRIPTEN_BINDINGS().

◆ render()

int BrowserHostBinding::render ( )
inline

Definition at line 61 of file session_host.cpp.

61 {
62 return host_ ? static_cast<int>(session_host_render_video(host_)) : invalidState();
63 }
asciichat_error_t session_host_render_video(session_host_t *host)
Definition host.c:787

References session_host_render_video().

Referenced by EMSCRIPTEN_BINDINGS().

◆ renderAudio()

int BrowserHostBinding::renderAudio ( )
inline

Definition at line 64 of file session_host.cpp.

64 {
65 return host_ ? static_cast<int>(session_host_render_audio(host_)) : invalidState();
66 }
asciichat_error_t session_host_render_audio(session_host_t *host)
Definition host.c:794

References session_host_render_audio().

Referenced by EMSCRIPTEN_BINDINGS().

◆ setLocalClient()

int BrowserHostBinding::setLocalClient ( uint32_t id)

Definition at line 145 of file session_host.cpp.

145 {
146 if (!host_)
147 return invalidState();
148 session_host_render_target_t target{};
149 target.client_id = id;
150 return static_cast<int>(session_host_set_render_target(host_, id ? &target : nullptr));
151}
asciichat_error_t session_host_set_render_target(session_host_t *host, const session_host_render_target_t *target)
Definition host.c:1504

References session_host_render_target_t::client_id, and session_host_set_render_target().

Referenced by EMSCRIPTEN_BINDINGS().

◆ valid()

bool BrowserHostBinding::valid ( ) const
inline

Definition at line 45 of file session_host.cpp.

45 {
46 return host_ != nullptr;
47 }

Referenced by EMSCRIPTEN_BINDINGS().


The documentation for this class was generated from the following file: