Begin WebSocketWrapper nice struct

This commit is contained in:
Alex Hultman 2019-01-14 11:54:37 +01:00
parent a6b897cdbc
commit 31e7463c9b
3 changed files with 37 additions and 37 deletions

View File

@ -66,7 +66,7 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
HandleScope hs(isolate); HandleScope hs(isolate);
/* Create a new websocket object */ /* Create a new websocket object */
Local<Object> wsObject = getWsInstance<APP>(); Local<Object> wsObject = WebSocketWrapper::getWsInstance<APP>();
wsObject->SetAlignedPointerInInternalField(0, ws); wsObject->SetAlignedPointerInInternalField(0, ws);
/* Attach a new V8 object with pointer to us, to us */ /* Attach a new V8 object with pointer to us, to us */

View File

@ -1,47 +1,47 @@
// ws.getBufferedAmount()
// ws.close(lalala)
// ws.?
/* Helping QtCreator */
#include "App.h" #include "App.h"
#include <v8.h> #include <v8.h>
#include "Utilities.h" #include "Utilities.h"
using namespace v8; using namespace v8;
// also wrap all of this in some common struct struct WebSocketWrapper {
Persistent<Object> wsTemplate[2]; static Persistent<Object> wsTemplate[2];
/* WebSocket send */ // ws.getBufferedAmount()
// ws.close(lalala)
// ws.?
template <bool SSL> template <bool SSL>
void uWS_WebSocket_send(const FunctionCallbackInfo<Value> &args) { static void uWS_WebSocket_send(const FunctionCallbackInfo<Value> &args) {
NativeString nativeString(args.GetIsolate(), args[0]); NativeString nativeString(args.GetIsolate(), args[0]);
bool isBinary = args[1]->Int32Value(); bool isBinary = args[1]->Int32Value();
bool ok = ((uWS::WebSocket<SSL, true> *) args.Holder()->GetAlignedPointerFromInternalField(0))->send( bool ok = ((uWS::WebSocket<SSL, true> *) args.Holder()->GetAlignedPointerFromInternalField(0))->send(
std::string_view(nativeString.getData(), nativeString.getLength()), isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT std::string_view(nativeString.getData(), nativeString.getLength()), isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT
); );
args.GetReturnValue().Set(Boolean::New(isolate, ok)); args.GetReturnValue().Set(Boolean::New(isolate, ok));
}
template <bool SSL>
void initWsTemplate() {
Local<FunctionTemplate> wsTemplateLocal = FunctionTemplate::New(isolate);
if (SSL) {
wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLWebSocket"));
} else {
wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.WebSocket"));
} }
wsTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "send"), FunctionTemplate::New(isolate, uWS_WebSocket_send<SSL>));
Local<Object> wsObjectLocal = wsTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); template <bool SSL>
wsTemplate[SSL].Reset(isolate, wsObjectLocal); static void initWsTemplate() {
} Local<FunctionTemplate> wsTemplateLocal = FunctionTemplate::New(isolate);
if (SSL) {
wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLWebSocket"));
} else {
wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.WebSocket"));
}
wsTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "send"), FunctionTemplate::New(isolate, uWS_WebSocket_send<SSL>));
template <class APP> Local<Object> wsObjectLocal = wsTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
Local<Object> getWsInstance() { wsTemplate[SSL].Reset(isolate, wsObjectLocal);
return Local<Object>::New(isolate, wsTemplate[std::is_same<APP, uWS::SSLApp>::value])->Clone(); }
}
template <class APP>
static Local<Object> getWsInstance() {
return Local<Object>::New(isolate, wsTemplate[std::is_same<APP, uWS::SSLApp>::value])->Clone();
}
};
Persistent<Object> WebSocketWrapper::wsTemplate[2];

View File

@ -76,8 +76,8 @@ void Main(Local<Object> exports) {
// these inits should probably happen elsewhere (in their respective struct's constructor)? // these inits should probably happen elsewhere (in their respective struct's constructor)?
/* The template for websockets */ /* The template for websockets */
initWsTemplate<0>(); WebSocketWrapper::initWsTemplate<0>();
initWsTemplate<1>(); WebSocketWrapper::initWsTemplate<1>();
/* Initialize SSL and non-SSL templates */ /* Initialize SSL and non-SSL templates */
initResTemplate<0>(); initResTemplate<0>();