Drafted udp holepunching upsync pathway.

This commit is contained in:
genxium
2023-01-24 12:00:49 +08:00
parent ef345e0e48
commit e21e1b840f
11 changed files with 476 additions and 28 deletions

View File

@@ -4,9 +4,12 @@
#include "uv/uv.h"
#include <thread>
static uv_udp_t* udpSocket = NULL;
uv_udp_t* udpSocket = NULL;
uv_loop_t* loop = NULL; // Only this loop is used for this simple PoC
int const sendBufferLen = 1024;
uv_mutex_t sendLock, recvLock;
void _onRead(uv_udp_t* req, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags) {
if (nread < 0) {
CCLOGERROR("Read error %s", uv_err_name(nread));
@@ -32,9 +35,13 @@ static void _allocBuffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* b
void startRecvLoop(void* arg) {
uv_loop_t* loop = (uv_loop_t*)arg;
uv_run(loop, UV_RUN_DEFAULT);
CCLOG("UDP session is ended!");
}
bool DelayNoMore::UdpSession::openUdpSession(int port) {
uv_mutex_init(&sendLock);
uv_mutex_init(&recvLock);
CCLOG("About to open UDP session at port=%d...", port);
loop = uv_loop_new(); // Only the default loop is used for this simple PoC
udpSocket = (uv_udp_t*)malloc(sizeof(uv_udp_t));
@@ -48,19 +55,6 @@ bool DelayNoMore::UdpSession::openUdpSession(int port) {
uv_thread_t recvTid;
uv_thread_create(&recvTid, startRecvLoop, loop);
//std::thread([=]() {
// udpSocket = (uv_udp_t*)malloc(sizeof(uv_udp_t));
// SOCKADDR_IN recv_addr;
// uv_ip4_addr("0.0.0.0", port, &recv_addr);
//
// uv_udp_init(loop, udpSocket);
//
// uv_udp_bind(udpSocket, (struct sockaddr const*)&recv_addr, UV_UDP_REUSEADDR);
// uv_udp_recv_start(udpSocket, _allocBuffer, _onRead);
//
// startRecvLoop(loop);
//}).detach();
CCLOG("Finished opening UDP session at port=%d", port);
@@ -79,6 +73,9 @@ bool DelayNoMore::UdpSession::closeUdpSession() {
uv_loop_close(loop);
free(udpSocket);
free(loop);
uv_mutex_destroy(&sendLock);
uv_mutex_destroy(&recvLock);
CCLOG("Closed udp session and dealloc all resources...");
return true;
@@ -104,3 +101,22 @@ void DelayNoMore::UdpSession::onMessage(BYTEC* const bytes) {
}
}
}
void _onSend(uv_udp_send_t* req, int status) {
free(req);
if (status) {
fprintf(stderr, "uv_udp_send_cb error: %s\n", uv_strerror(status));
}
}
bool DelayNoMore::UdpSession::punchToServer(BYTEC* const bytes) {
uv_mutex_lock(&sendLock);
uv_udp_send_t* req = (uv_udp_send_t*)malloc(sizeof(uv_udp_send_t));
uv_buf_t sendBuffer = uv_buf_init(bytes, sizeof bytes); // [WARNING] The RAM space used for "bytes", either on stack or in heap, is preallocated and managed by the caller.
SOCKADDR_IN destAddr;
uv_ip4_addr("127.0.0.1", 3000, &destAddr);
uv_udp_send(req, udpSocket, &sendBuffer, 1, (struct sockaddr const*)&destAddr, _onSend);
uv_mutex_unlock(&sendLock);
return true;
}

View File

@@ -3,7 +3,7 @@
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
typedef unsigned char const BYTEC;
typedef char BYTEC;
typedef char const CHARC;
namespace DelayNoMore {
@@ -13,7 +13,7 @@ namespace DelayNoMore {
static bool closeUdpSession();
static bool upsertPeerUdpAddr(int joinIndex, CHARC* const ip, int port, uint32_t authKey);
//static bool clearPeerUDPAddrList();
//static void punchToServer(CBYTE* const bytes);
static bool punchToServer(BYTEC* const bytes);
static void onMessage(BYTEC* const bytes);
};
}

View File

@@ -6,17 +6,35 @@ bool openUdpSession(se::State& s) {
const auto& args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (1 == argc) {
if (1 == argc && args[0].isNumber()) {
SE_PRECONDITION2(ok, false, "openUdpSession: Error processing arguments");
int port = args[0].toInt32();
return DelayNoMore::UdpSession::openUdpSession(port);
}
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1);
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d; or wrong arg type!", (int)argc, 1);
return false;
}
SE_BIND_FUNC(openUdpSession)
bool punchToServer(se::State& s) {
const auto& args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (1 == argc && args[0].isObject() && args[0].toObject()->isTypedArray()) {
SE_PRECONDITION2(ok, false, "punchToServer: Error processing arguments");
BYTEC bytes[1024];
memset(bytes, 0, sizeof bytes);
se::Object* obj = args[0].toObject();
size_t sz = 0;
obj->getTypedArrayData((uint8_t**)&bytes, &sz);
return DelayNoMore::UdpSession::punchToServer(bytes);
}
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d; or wrong arg type!", (int)argc, 1);
return false;
}
SE_BIND_FUNC(punchToServer)
bool closeUdpSession(se::State& s) {
const auto& args = s.args();
size_t argc = args.size();
@@ -35,7 +53,7 @@ bool upsertPeerUdpAddr(se::State& s) {
const auto& args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (4 == argc) {
if (4 == argc && args[0].isNumber() && args[1].isString() && args[2].isNumber() && args[3].isNumber()) {
SE_PRECONDITION2(ok, false, "upsertPeerUdpAddr: Error processing arguments");
int joinIndex = args[0].toInt32();
CHARC* ip = args[1].toString().c_str();
@@ -43,7 +61,7 @@ bool upsertPeerUdpAddr(se::State& s) {
uint32_t authKey = args[3].toUint32();
return DelayNoMore::UdpSession::upsertPeerUdpAddr(joinIndex, ip, port, authKey);
}
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 4);
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d; or wrong arg type!", (int)argc, 4);
return false;
}
@@ -79,6 +97,7 @@ bool registerUdpSession(se::Object* obj)
auto cls = se::Class::create("UdpSession", ns, nullptr, nullptr);
cls->defineStaticFunction("openUdpSession", _SE(openUdpSession));
cls->defineStaticFunction("punchToServer", _SE(punchToServer));
cls->defineStaticFunction("closeUdpSession", _SE(closeUdpSession));
cls->defineStaticFunction("upsertPeerUdpAddr", _SE(upsertPeerUdpAddr));
cls->defineFinalizeFunction(_SE(udpSessionFinalize));

View File

@@ -11,6 +11,7 @@ extern se::Class* __jsb_udp_session_class;
bool registerUdpSession(se::Object* obj);
SE_DECLARE_FUNC(openUdpSession);
SE_DECLARE_FUNC(punchToServer);
SE_DECLARE_FUNC(closeUdpSession);
SE_DECLARE_FUNC(upsertPeerUdpAddr);