Static Webview - RTC - UDP - TCP - WSS
This commit is contained in:
@ -235,6 +235,141 @@ void v8_runt_socket_is_connected(const FunctionCallbackInfo<Value>& args) {
|
||||
args.GetReturnValue().Set(Boolean::New(isolate, connected));
|
||||
}
|
||||
|
||||
void v8_runt_udp_create(const FunctionCallbackInfo<Value>& args) {
|
||||
Isolate* isolate = args.GetIsolate();
|
||||
|
||||
int socket_id = runt_udp_create();
|
||||
#ifdef DEBUG_NETWORK
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "[V8] runt_udp_create returned: %d",
|
||||
socket_id);
|
||||
#endif
|
||||
|
||||
args.GetReturnValue().Set(Integer::New(isolate, socket_id));
|
||||
}
|
||||
|
||||
void v8_runt_udp_bind(const FunctionCallbackInfo<Value>& args) {
|
||||
Isolate* isolate = args.GetIsolate();
|
||||
|
||||
if (args.Length() < 3 || !args[0]->IsInt32() || !args[1]->IsString()
|
||||
|| !args[2]->IsInt32()) {
|
||||
isolate->ThrowException(Exception::TypeError(
|
||||
String::NewFromUtf8(isolate, "Wrong arguments")
|
||||
.ToLocalChecked()));
|
||||
return;
|
||||
}
|
||||
|
||||
int socket_id = args[0]->Int32Value(isolate->GetCurrentContext())
|
||||
.FromJust();
|
||||
String::Utf8Value address(isolate, args[1]);
|
||||
int port = args[2]->Int32Value(isolate->GetCurrentContext())
|
||||
.FromJust();
|
||||
|
||||
bool result = runt_udp_bind(socket_id, *address, port);
|
||||
args.GetReturnValue().Set(Boolean::New(isolate, result));
|
||||
}
|
||||
|
||||
void v8_runt_udp_sendto(const FunctionCallbackInfo<Value>& args) {
|
||||
Isolate* isolate = args.GetIsolate();
|
||||
|
||||
if (args.Length() < 4 || !args[0]->IsInt32() || !args[1]->IsString()
|
||||
|| !args[2]->IsInt32()) {
|
||||
isolate->ThrowException(Exception::TypeError(
|
||||
String::NewFromUtf8(isolate, "Wrong arguments")
|
||||
.ToLocalChecked()));
|
||||
return;
|
||||
}
|
||||
|
||||
int socket_id = args[0]->Int32Value(isolate->GetCurrentContext())
|
||||
.FromJust();
|
||||
String::Utf8Value host(isolate, args[1]);
|
||||
int port = args[2]->Int32Value(isolate->GetCurrentContext())
|
||||
.FromJust();
|
||||
int sent = -1;
|
||||
|
||||
if (args[3]->IsArrayBuffer()) {
|
||||
Local<ArrayBuffer> buffer = Local<ArrayBuffer>::Cast(args[3]);
|
||||
std::shared_ptr<BackingStore> backing =
|
||||
buffer->GetBackingStore();
|
||||
sent = runt_udp_sendto(socket_id, *host, port,
|
||||
static_cast<const char*>(backing->Data()),
|
||||
static_cast<int>(backing->ByteLength()));
|
||||
} else if (args[3]->IsArrayBufferView()) {
|
||||
Local<ArrayBufferView> view =
|
||||
Local<ArrayBufferView>::Cast(args[3]);
|
||||
Local<ArrayBuffer> buffer = view->Buffer();
|
||||
std::shared_ptr<BackingStore> backing =
|
||||
buffer->GetBackingStore();
|
||||
const char* data =
|
||||
static_cast<const char*>(backing->Data())
|
||||
+ view->ByteOffset();
|
||||
sent = runt_udp_sendto(socket_id, *host, port, data,
|
||||
static_cast<int>(view->ByteLength()));
|
||||
} else if (args[3]->IsString()) {
|
||||
String::Utf8Value str_data(isolate, args[3]);
|
||||
if (*str_data == nullptr) {
|
||||
args.GetReturnValue().Set(Integer::New(isolate, -1));
|
||||
return;
|
||||
}
|
||||
sent = runt_udp_sendto(socket_id, *host, port, *str_data,
|
||||
str_data.length());
|
||||
} else {
|
||||
isolate->ThrowException(Exception::TypeError(
|
||||
String::NewFromUtf8(isolate,
|
||||
"Data must be string, ArrayBuffer, or TypedArray")
|
||||
.ToLocalChecked()));
|
||||
return;
|
||||
}
|
||||
|
||||
args.GetReturnValue().Set(Integer::New(isolate, sent));
|
||||
}
|
||||
|
||||
void v8_runt_udp_recvfrom(const FunctionCallbackInfo<Value>& args) {
|
||||
Isolate* isolate = args.GetIsolate();
|
||||
Local<Context> context = isolate->GetCurrentContext();
|
||||
|
||||
if (args.Length() < 2 || !args[0]->IsInt32() || !args[1]->IsInt32()) {
|
||||
isolate->ThrowException(Exception::TypeError(
|
||||
String::NewFromUtf8(isolate, "Wrong arguments")
|
||||
.ToLocalChecked()));
|
||||
return;
|
||||
}
|
||||
|
||||
int socket_id = args[0]->Int32Value(context).FromJust();
|
||||
int max_length = args[1]->Int32Value(context).FromJust();
|
||||
|
||||
char* data = nullptr;
|
||||
char host[64] = {0};
|
||||
int port = 0;
|
||||
int received = runt_udp_recvfrom(socket_id, max_length, &data,
|
||||
host, sizeof(host), &port);
|
||||
|
||||
if (received > 0 && data) {
|
||||
std::unique_ptr<BackingStore> backing =
|
||||
ArrayBuffer::NewBackingStore(data, received,
|
||||
[](void* data, size_t length, void* deleter_data) {
|
||||
delete[] static_cast<char*>(data);
|
||||
}, nullptr);
|
||||
Local<ArrayBuffer> buffer =
|
||||
ArrayBuffer::New(isolate, std::move(backing));
|
||||
|
||||
Local<Object> obj = Object::New(isolate);
|
||||
obj->Set(context,
|
||||
String::NewFromUtf8(isolate, "data").ToLocalChecked(),
|
||||
buffer);
|
||||
obj->Set(context,
|
||||
String::NewFromUtf8(isolate, "host").ToLocalChecked(),
|
||||
String::NewFromUtf8(isolate, host).ToLocalChecked());
|
||||
obj->Set(context,
|
||||
String::NewFromUtf8(isolate, "port").ToLocalChecked(),
|
||||
Integer::New(isolate, port));
|
||||
args.GetReturnValue().Set(obj);
|
||||
} else if (received < 0) {
|
||||
args.GetReturnValue().Set(Integer::New(isolate, -1));
|
||||
} else {
|
||||
args.GetReturnValue().Set(Null(isolate));
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef WITH_SSL
|
||||
void v8_runt_socket_enable_ssl(const FunctionCallbackInfo<Value>& args) {
|
||||
Isolate* isolate = args.GetIsolate();
|
||||
@ -302,6 +437,18 @@ void bind_socket_bridge(Isolate* isolate, const v8::Global<v8::Context>& context
|
||||
global->Set(current_context, String::NewFromUtf8(isolate, "runt_socket_is_connected").ToLocalChecked(),
|
||||
Function::New(current_context, v8_runt_socket_is_connected).ToLocalChecked());
|
||||
|
||||
global->Set(current_context, String::NewFromUtf8(isolate, "runt_udp_create").ToLocalChecked(),
|
||||
Function::New(current_context, v8_runt_udp_create).ToLocalChecked());
|
||||
|
||||
global->Set(current_context, String::NewFromUtf8(isolate, "runt_udp_bind").ToLocalChecked(),
|
||||
Function::New(current_context, v8_runt_udp_bind).ToLocalChecked());
|
||||
|
||||
global->Set(current_context, String::NewFromUtf8(isolate, "runt_udp_sendto").ToLocalChecked(),
|
||||
Function::New(current_context, v8_runt_udp_sendto).ToLocalChecked());
|
||||
|
||||
global->Set(current_context, String::NewFromUtf8(isolate, "runt_udp_recvfrom").ToLocalChecked(),
|
||||
Function::New(current_context, v8_runt_udp_recvfrom).ToLocalChecked());
|
||||
|
||||
#ifdef WITH_SSL
|
||||
global->Set(current_context, String::NewFromUtf8(isolate, "runt_socket_enable_ssl").ToLocalChecked(),
|
||||
Function::New(current_context, v8_runt_socket_enable_ssl).ToLocalChecked());
|
||||
|
||||
Reference in New Issue
Block a user