mirror of
https://github.com/genxium/DelayNoMore
synced 2025-10-09 16:46:38 +00:00
Added basic input upsync via udp.
This commit is contained in:
@@ -40,7 +40,7 @@ void _onRead(uv_udp_t* req, ssize_t nread, const uv_buf_t* buf, const struct soc
|
||||
memset(gameThreadMsg, 0, gameThreadMsgSize);
|
||||
memcpy(gameThreadMsg, buf->base, nread);
|
||||
|
||||
CCLOG("Recv %d bytes from %s:%d, converted to %d bytes for the JS callback", nread, ip, port, strlen(gameThreadMsg));
|
||||
CCLOG("UDP read %d bytes from %s:%d, converted to %d bytes for the JS callback", nread, ip, port, strlen(gameThreadMsg));
|
||||
free(buf->base);
|
||||
//uv_udp_recv_stop(req);
|
||||
|
||||
@@ -153,13 +153,13 @@ void _onSend(uv_udp_send_t* req, int status) {
|
||||
|
||||
bool DelayNoMore::UdpSession::upsertPeerUdpAddr(int joinIndex, CHARC* const ip, int port, uint32_t authKey, int roomCapacity, int selfJoinIndex) {
|
||||
CCLOG("upsertPeerUdpAddr called by js for joinIndex=%d, ip=%s, port=%d, authKey=%lu; roomCapacity=%d, selfJoinIndex=%d.", joinIndex, ip, port, authKey, roomCapacity, selfJoinIndex);
|
||||
uv_ip4_addr(ip, port, &(peerAddrList[joinIndex - 1].sockAddrIn));
|
||||
peerAddrList[joinIndex - 1].authKey = authKey;
|
||||
|
||||
// Punching between existing peer-pairs for Address/Port-restricted Cone NAT (not need for Full Cone NAT)
|
||||
uv_mutex_lock(&sendLock);
|
||||
for (int i = 0; i < roomCapacity; i++) {
|
||||
if (i == selfJoinIndex - 1) continue;
|
||||
uv_ip4_addr(ip, port, &(peerAddrList[i].sockAddrIn));
|
||||
peerAddrList[i].authKey = authKey;
|
||||
for (int j = 0; j < 10; j++) {
|
||||
uv_udp_send_t* req = (uv_udp_send_t*)malloc(sizeof(uv_udp_send_t));
|
||||
uv_buf_t sendBuffer = uv_buf_init("foobar", 6); // hardcoded for now
|
||||
@@ -171,7 +171,7 @@ bool DelayNoMore::UdpSession::upsertPeerUdpAddr(int joinIndex, CHARC* const ip,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DelayNoMore::UdpSession::punchToServer(CHARC* const srvIp, int const srvPort, BYTEC* const bytes) {
|
||||
bool DelayNoMore::UdpSession::punchToServer(CHARC* const srvIp, int const srvPort, BYTEC* const bytes, size_t bytesLen) {
|
||||
/*
|
||||
[WARNING] The RAM space used for "bytes", either on stack or in heap, is preallocatedand managed by the caller.
|
||||
|
||||
@@ -181,7 +181,7 @@ bool DelayNoMore::UdpSession::punchToServer(CHARC* const srvIp, int const srvPor
|
||||
SRV_PORT = srvPort;
|
||||
|
||||
uv_udp_send_t* req = (uv_udp_send_t*)malloc(sizeof(uv_udp_send_t));
|
||||
uv_buf_t sendBuffer = uv_buf_init(bytes, strlen(bytes));
|
||||
uv_buf_t sendBuffer = uv_buf_init(bytes, bytesLen);
|
||||
struct sockaddr_in destAddr;
|
||||
|
||||
uv_ip4_addr(SRV_IP, SRV_PORT, &destAddr);
|
||||
@@ -191,3 +191,18 @@ bool DelayNoMore::UdpSession::punchToServer(CHARC* const srvIp, int const srvPor
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DelayNoMore::UdpSession::broadcastInputFrameUpsync(BYTEC* const bytes, size_t bytesLen, int roomCapacity, int selfJoinIndex) {
|
||||
uv_mutex_lock(&sendLock);
|
||||
for (int i = 0; i < roomCapacity; i++) {
|
||||
if (i == selfJoinIndex - 1) continue;
|
||||
for (int j = 0; j < 10; j++) {
|
||||
uv_udp_send_t* req = (uv_udp_send_t*)malloc(sizeof(uv_udp_send_t));
|
||||
uv_buf_t sendBuffer = uv_buf_init(bytes, bytesLen);
|
||||
uv_udp_send(req, udpSocket, &sendBuffer, 1, (struct sockaddr const*)&peerAddrList[i], _onSend);
|
||||
}
|
||||
}
|
||||
uv_mutex_unlock(&sendLock);
|
||||
|
||||
return true;
|
||||
}
|
@@ -13,7 +13,8 @@ namespace DelayNoMore {
|
||||
static bool closeUdpSession();
|
||||
static bool upsertPeerUdpAddr(int joinIndex, CHARC* const ip, int port, uint32_t authKey, int roomCapacity, int selfJoinIndex);
|
||||
//static bool clearPeerUDPAddrList();
|
||||
static bool punchToServer(CHARC* const srvIp, int const srvPort, BYTEC* const bytes);
|
||||
static bool punchToServer(CHARC* const srvIp, int const srvPort, BYTEC* const bytes, size_t bytesLen);
|
||||
static bool broadcastInputFrameUpsync(BYTEC* const bytes, size_t bytesLen, int roomCapacity, int selfJoinIndex);
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
@@ -30,17 +30,44 @@ bool punchToServer(se::State& s) {
|
||||
memset(bytes, 0, sizeof bytes);
|
||||
se::Object* obj = args[2].toObject();
|
||||
size_t sz = 0;
|
||||
uint8_t* ptr;
|
||||
uint8_t* ptr = NULL;
|
||||
obj->getTypedArrayData(&ptr, &sz);
|
||||
memcpy(bytes, ptr, sz);
|
||||
CCLOG("Should punch %s:%d by %d bytes v.s. strlen(bytes)=%u.", srvIp, srvPort, sz, strlen(bytes));
|
||||
return DelayNoMore::UdpSession::punchToServer(srvIp, srvPort, bytes);
|
||||
for (size_t i = 0; i < sz; i++) {
|
||||
bytes[i] = (char)(*(ptr + i));
|
||||
}
|
||||
CCLOG("Should punch %s:%d by %d bytes.", srvIp, srvPort, sz);
|
||||
return DelayNoMore::UdpSession::punchToServer(srvIp, srvPort, bytes, sz);
|
||||
}
|
||||
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d; or wrong arg type!", (int)argc, 3);
|
||||
return false;
|
||||
}
|
||||
SE_BIND_FUNC(punchToServer)
|
||||
|
||||
bool broadcastInputFrameUpsync(se::State& s) {
|
||||
const auto& args = s.args();
|
||||
size_t argc = args.size();
|
||||
CC_UNUSED bool ok = true;
|
||||
if (3 == argc && args[0].toObject()->isTypedArray() && args[1].isNumber() && args[2].isNumber()) {
|
||||
SE_PRECONDITION2(ok, false, "broadcastInputFrameUpsync: Error processing arguments");
|
||||
BYTEC bytes[1024];
|
||||
memset(bytes, 0, sizeof bytes);
|
||||
se::Object* obj = args[0].toObject();
|
||||
size_t sz = 0;
|
||||
uint8_t* ptr = NULL;
|
||||
obj->getTypedArrayData(&ptr, &sz);
|
||||
for (size_t i = 0; i < sz; i++) {
|
||||
bytes[i] = (char)(*(ptr + i));
|
||||
}
|
||||
int roomCapacity = args[1].toInt32();
|
||||
int selfJoinIndex = args[2].toInt32();
|
||||
CCLOG("Should broadcastInputFrameUpsync %u bytes; roomCapacity=%d, selfJoinIndex=%d.", sz, roomCapacity, selfJoinIndex);
|
||||
return DelayNoMore::UdpSession::broadcastInputFrameUpsync(bytes, sz, roomCapacity, selfJoinIndex);
|
||||
}
|
||||
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d; or wrong arg type!", (int)argc, 3);
|
||||
return false;
|
||||
}
|
||||
SE_BIND_FUNC(broadcastInputFrameUpsync)
|
||||
|
||||
bool closeUdpSession(se::State& s) {
|
||||
const auto& args = s.args();
|
||||
size_t argc = args.size();
|
||||
@@ -106,6 +133,7 @@ bool registerUdpSession(se::Object* obj)
|
||||
|
||||
cls->defineStaticFunction("openUdpSession", _SE(openUdpSession));
|
||||
cls->defineStaticFunction("punchToServer", _SE(punchToServer));
|
||||
cls->defineStaticFunction("broadcastInputFrameUpsync", _SE(broadcastInputFrameUpsync));
|
||||
cls->defineStaticFunction("closeUdpSession", _SE(closeUdpSession));
|
||||
cls->defineStaticFunction("upsertPeerUdpAddr", _SE(upsertPeerUdpAddr));
|
||||
cls->defineFinalizeFunction(_SE(udpSessionFinalize));
|
||||
|
@@ -12,6 +12,7 @@ bool registerUdpSession(se::Object* obj);
|
||||
|
||||
SE_DECLARE_FUNC(openUdpSession);
|
||||
SE_DECLARE_FUNC(punchToServer);
|
||||
SE_DECLARE_FUNC(broadcastInputFrameUpsync);
|
||||
SE_DECLARE_FUNC(closeUdpSession);
|
||||
SE_DECLARE_FUNC(upsertPeerUdpAddr);
|
||||
|
||||
|
Reference in New Issue
Block a user