platform/packages/wings/lib/src/util.cc

146 lines
3.5 KiB
C++
Raw Normal View History

2019-09-29 05:26:27 +00:00
#include <cstring>
2019-04-29 05:10:27 +00:00
#include "angel_wings.h"
2019-04-29 05:13:45 +00:00
#include "wings_socket.h"
2019-09-29 05:26:27 +00:00
#include <dart_native_api.h>
#include <iostream>
2019-04-29 05:13:45 +00:00
using namespace wings;
2019-04-29 05:10:27 +00:00
2019-09-29 05:26:27 +00:00
void Dart_WingsSocket_listen(Dart_NativeArguments arguments) {
uint64_t ptr;
Dart_Handle pointerHandle = Dart_GetNativeArgument(arguments, 0);
HandleError(Dart_IntegerToUint64(pointerHandle, &ptr));
2019-04-29 05:55:02 +00:00
2019-09-29 05:26:27 +00:00
auto *socket = (WingsSocket *)ptr;
socket->start(arguments);
2019-04-29 05:55:02 +00:00
}
2019-09-29 05:26:27 +00:00
struct wingsSockName {
sa_family_t family;
sockaddr_in v4;
sockaddr_in6 v6;
struct sockaddr *ptr() const {
if (family == AF_INET6) {
return (sockaddr *)&v6;
} else {
return (sockaddr *)&v4;
2019-05-01 01:33:46 +00:00
}
2019-09-29 05:26:27 +00:00
}
2019-05-01 01:33:46 +00:00
2019-09-29 05:26:27 +00:00
void *addrPtr() const {
if (family == AF_INET6) {
return (void *)&v6.sin6_addr;
} else {
return (void *)&v4.sin_addr;
2019-05-01 01:33:46 +00:00
}
2019-09-29 05:26:27 +00:00
}
2019-05-01 01:33:46 +00:00
2019-09-29 05:26:27 +00:00
socklen_t length() const {
if (family == AF_INET6) {
return sizeof(v6);
} else {
return sizeof(v4);
2019-05-01 01:33:46 +00:00
}
2019-09-29 05:26:27 +00:00
}
2019-05-01 01:33:46 +00:00
};
2019-09-29 05:26:27 +00:00
void wingsThrowOSError() {
wingsThrowError(strerror(errno), "dart:io", "OSError", errno);
2019-05-01 01:33:46 +00:00
}
2019-09-29 05:26:27 +00:00
bool wingsReadSocket(Dart_NativeArguments arguments, wingsSockName *out) {
uint64_t ptr;
Dart_Handle pointerHandle = Dart_GetNativeArgument(arguments, 0);
HandleError(Dart_IntegerToUint64(pointerHandle, &ptr));
2019-04-29 05:13:45 +00:00
2019-09-29 05:26:27 +00:00
auto *socket = (WingsSocket *)ptr;
int fd = socket->getFD();
2019-05-01 01:33:46 +00:00
2019-09-29 05:26:27 +00:00
socklen_t len;
out->family = socket->getFamily();
len = out->length();
2019-05-01 01:33:46 +00:00
2019-09-29 05:26:27 +00:00
int result;
2019-05-01 01:33:46 +00:00
2019-09-29 05:26:27 +00:00
// result = connect(fd, out->ptr(), len);
2019-05-01 01:33:46 +00:00
2019-09-29 05:26:27 +00:00
// if (result < 0)
// {
// wingsThrowOSError();
// return false;
// }
2019-05-01 01:33:46 +00:00
2019-09-29 05:26:27 +00:00
result = getsockname(fd, out->ptr(), &len);
2019-05-01 01:33:46 +00:00
2019-09-29 05:26:27 +00:00
if (result == -1) {
wingsThrowOSError();
return false;
}
2019-05-01 01:33:46 +00:00
2019-09-29 05:26:27 +00:00
return true;
2019-05-01 01:33:46 +00:00
}
2019-09-29 05:26:27 +00:00
void Dart_WingsSocket_getAddress(Dart_NativeArguments arguments) {
wingsSockName sock;
if (wingsReadSocket(arguments, &sock)) {
char addrBuf[INET6_ADDRSTRLEN + 1] = {0};
2019-05-01 01:33:46 +00:00
2019-09-29 05:26:27 +00:00
auto *result =
inet_ntop(sock.family, sock.addrPtr(), addrBuf, sock.length());
2019-05-01 01:33:46 +00:00
2019-09-29 05:26:27 +00:00
if (result == NULL) {
wingsThrowOSError();
2019-05-01 01:33:46 +00:00
}
2019-09-29 05:26:27 +00:00
Dart_Handle outHandle = Dart_NewStringFromCString(addrBuf);
Dart_SetReturnValue(arguments, outHandle);
}
2019-05-01 01:33:46 +00:00
}
2019-09-29 05:26:27 +00:00
void Dart_WingsSocket_getPort(Dart_NativeArguments arguments) {
wingsSockName sock;
if (wingsReadSocket(arguments, &sock)) {
Dart_Handle outHandle;
if (sock.family == AF_INET6) {
outHandle = Dart_NewIntegerFromUint64(ntohs(sock.v6.sin6_port));
} else {
outHandle = Dart_NewIntegerFromUint64(ntohs(sock.v4.sin_port));
2019-05-01 01:33:46 +00:00
}
2019-09-29 05:26:27 +00:00
Dart_SetReturnValue(arguments, outHandle);
}
2019-04-29 05:10:27 +00:00
}
2019-09-29 05:26:27 +00:00
void Dart_WingsSocket_write(Dart_NativeArguments arguments) {
int64_t fd;
void *data;
Dart_TypedData_Type type;
intptr_t len;
Dart_Handle fdHandle = Dart_GetNativeArgument(arguments, 0);
Dart_Handle dataHandle = Dart_GetNativeArgument(arguments, 1);
HandleError(Dart_IntegerToInt64(fdHandle, &fd));
HandleError(Dart_TypedDataAcquireData(dataHandle, &type, &data, &len));
write(fd, data, len);
HandleError(Dart_TypedDataReleaseData(dataHandle));
2019-04-29 05:10:27 +00:00
}
2019-09-29 05:26:27 +00:00
void Dart_WingsSocket_closeDescriptor(Dart_NativeArguments arguments) {
int64_t fd;
Dart_Handle fdHandle = Dart_GetNativeArgument(arguments, 0);
HandleError(Dart_IntegerToInt64(fdHandle, &fd));
close(fd);
2019-04-29 05:10:27 +00:00
}
2019-09-29 05:26:27 +00:00
void Dart_WingsSocket_close(Dart_NativeArguments arguments) {
Dart_Port port;
uint64_t ptr;
Dart_Handle pointerHandle = Dart_GetNativeArgument(arguments, 0);
Dart_Handle sendPortHandle = Dart_GetNativeArgument(arguments, 1);
HandleError(Dart_IntegerToUint64(pointerHandle, &ptr));
HandleError(Dart_SendPortGetId(sendPortHandle, &port));
auto *socket = (WingsSocket *)ptr;
socket->decrRef(port);
2019-04-29 05:10:27 +00:00
}