Skip to content

Commit 1786528

Browse files
Add fast send methods for strings and buffers
1 parent 3f16489 commit 1786528

1 file changed

Lines changed: 27 additions & 8 deletions

File tree

‎src/WebSocketWrapper.h‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,32 @@ struct WebSocketWrapper {
325325
* A null internal-field pointer (closed socket) sets options.fallback = true so V8
326326
* re-invokes the slow path which throws the proper exception. */
327327

328-
template <bool SSL>
329-
static uint32_t uWS_WebSocket_send_fast(v8::Local<v8::Object> receiver, const v8::FastOneByteString& message, bool isBinary, bool compress, v8::FastApiCallbackOptions& options) {
330-
auto *ws = (uWS::WebSocket<SSL, true, PerSocketData> *) receiver->GetAlignedPointerFromInternalField(0);
331-
if (!ws) { return 0; }
332-
return ws->send(std::string_view((char *) message.data, message.length),
333-
isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, compress);
334-
}
328+
// Version A: Handles Strings
329+
template <bool SSL>
330+
static uint32_t uWS_WebSocket_send_fast_string(v8::Local<v8::Object> receiver,
331+
const v8::FastOneByteString& message,
332+
bool isBinary, bool compress) {
333+
auto *ws = (uWS::WebSocket<SSL, true, PerSocketData> *) receiver->GetAlignedPointerFromInternalField(0);
334+
if (!ws) return 0;
335+
return ws->send(std::string_view(message.data, message.length),
336+
isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, compress);
337+
}
338+
339+
// Version B: Handles ArrayBuffer/TypedArray
340+
template <bool SSL>
341+
static uint32_t uWS_WebSocket_send_fast_buffer(v8::Local<v8::Object> receiver,
342+
v8::Local<v8::Value> message,
343+
bool isBinary, bool compress) {
344+
auto *ws = (uWS::WebSocket<SSL, true, PerSocketData> *) receiver->GetAlignedPointerFromInternalField(0);
345+
if (!ws || !message->IsArrayBufferView()) return 0;
346+
347+
auto array = message.As<v8::ArrayBufferView>();
348+
auto contents = array->Buffer()->GetBackingStore();
349+
char* data = static_cast<char*>(contents->Data()) + array->ByteOffset();
350+
351+
return ws->send(std::string_view(data, array->ByteLength()),
352+
uWS::OpCode::BINARY, compress);
353+
}
335354

336355
template <bool SSL>
337356
static Local<Object> init(Isolate *isolate) {
@@ -350,7 +369,7 @@ struct WebSocketWrapper {
350369

351370
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getUserData", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getUserData<SSL>));
352371
static v8::CFunction fast_send = v8::CFunction::Make(uWS_WebSocket_send_fast<SSL>);
353-
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "send", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_send<SSL>, Local<Value>(), Local<Signature>(), 0, ConstructorBehavior::kThrow, SideEffectType::kHasSideEffect, &fast_send));
372+
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "send", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_send<SSL>, Local<Value>(), Local<Signature>(), 0, ConstructorBehavior::kThrow, SideEffectType::kHasSideEffect, &uWS_WebSocket_send_fast_buffer));
354373
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "end", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_end<SSL>));
355374
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "close", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_close<SSL>));
356375
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getBufferedAmount", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getBufferedAmount<SSL>));

0 commit comments

Comments
 (0)