mirror of
https://github.com/genxium/DelayNoMore
synced 2025-01-13 14:31:36 +00:00
Fixed Android segfault on mysterious udp reception callback.
This commit is contained in:
parent
26660d75d2
commit
bbf07fe518
@ -43,9 +43,7 @@ window.onUdpMessage = (args) => {
|
|||||||
}
|
}
|
||||||
cc.log(`#1 Js called back by CPP: onUdpMessage: args=${args}, typeof(args)=${typeof (args)}, argslen=${args.length}, ui8Arr=${ui8Arr}`);
|
cc.log(`#1 Js called back by CPP: onUdpMessage: args=${args}, typeof(args)=${typeof (args)}, argslen=${args.length}, ui8Arr=${ui8Arr}`);
|
||||||
|
|
||||||
if (6 == len) {
|
cc.log(`#2 Js called back by CPP for upsync: trying to decode by WsReq...`);
|
||||||
cc.log(`#2 Js called back by CPP for peer hole punching`);
|
|
||||||
} else {
|
|
||||||
const req = window.pb.protos.WsReq.decode(ui8Arr);
|
const req = window.pb.protos.WsReq.decode(ui8Arr);
|
||||||
if (req) {
|
if (req) {
|
||||||
cc.log(`#2 Js called back by CPP for upsync: onUdpMessage: ${JSON.stringify(req)}`);
|
cc.log(`#2 Js called back by CPP for upsync: onUdpMessage: ${JSON.stringify(req)}`);
|
||||||
@ -69,7 +67,6 @@ window.onUdpMessage = (args) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
cc.Class({
|
cc.Class({
|
||||||
|
@ -14,30 +14,37 @@ struct PeerAddr peerAddrList[maxPeerCnt];
|
|||||||
char SRV_IP[256];
|
char SRV_IP[256];
|
||||||
int SRV_PORT = 0;
|
int SRV_PORT = 0;
|
||||||
|
|
||||||
void _onRead(uv_udp_t* req, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags) {
|
void _onRead(uv_udp_t* req, ssize_t nread, uv_buf_t const* buf, struct sockaddr const* addr, unsigned flags) {
|
||||||
if (nread < 0) {
|
if (nread < 0) {
|
||||||
CCLOGERROR("Read error %s", uv_err_name(nread));
|
CCLOGERROR("Read error %s", uv_err_name(nread));
|
||||||
uv_close((uv_handle_t*)req, NULL);
|
uv_close((uv_handle_t*)req, NULL);
|
||||||
free(buf->base);
|
free(buf->base);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (NULL != addr) {
|
||||||
struct sockaddr_in* sockAddr = (struct sockaddr_in*)addr;
|
// The null check for "addr" is necessary, on Android there'd be such mysterious call to "_onRead"!
|
||||||
char ip[17] = { 0 };
|
switch (addr->sa_family) {
|
||||||
uv_ip4_name(sockAddr, ip, sizeof ip);
|
case AF_INET: {
|
||||||
|
struct sockaddr_in const* sockAddr = (struct sockaddr_in const*)addr;
|
||||||
|
char ip[INET_ADDRSTRLEN];
|
||||||
|
memset(ip, 0, sizeof ip);
|
||||||
|
//uv_ip4_name(sockAddr, ip, 16);
|
||||||
|
uv_inet_ntop(sockAddr->sin_family, &(sockAddr->sin_addr), ip, INET_ADDRSTRLEN);
|
||||||
int port = ntohs(sockAddr->sin_port);
|
int port = ntohs(sockAddr->sin_port);
|
||||||
|
CCLOG("UDP received %d bytes from %s:%d", nread, ip, port);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (6 != nread) {
|
||||||
|
// Non-holepunching
|
||||||
int const gameThreadMsgSize = 256;
|
int const gameThreadMsgSize = 256;
|
||||||
char* const gameThreadMsg = (char* const)malloc(gameThreadMsgSize);
|
char* const gameThreadMsg = (char* const)malloc(gameThreadMsgSize);
|
||||||
memset(gameThreadMsg, 0, gameThreadMsgSize);
|
memset(gameThreadMsg, 0, gameThreadMsgSize);
|
||||||
memcpy(gameThreadMsg, buf->base, nread);
|
memcpy(gameThreadMsg, buf->base, nread);
|
||||||
|
|
||||||
free(buf->base);
|
|
||||||
//uv_udp_recv_stop(req);
|
|
||||||
|
|
||||||
if (6 != nread) {
|
|
||||||
// Non-holepunching
|
|
||||||
CCLOG("UDP received %d bytes from %s:%d, converted to %d bytes for the JS callback", nread, ip, port, strlen(gameThreadMsg));
|
|
||||||
cocos2d::Application::getInstance()->getScheduler()->performFunctionInCocosThread([=]() {
|
cocos2d::Application::getInstance()->getScheduler()->performFunctionInCocosThread([=]() {
|
||||||
// [WARNING] Use of the "ScriptEngine" is only allowed in "GameThread a.k.a. CocosThread"!
|
// [WARNING] Use of the "ScriptEngine" is only allowed in "GameThread a.k.a. CocosThread"!
|
||||||
se::Value onUdpMessageCb;
|
se::Value onUdpMessageCb;
|
||||||
@ -55,8 +62,11 @@ void _onRead(uv_udp_t* req, ssize_t nread, const uv_buf_t* buf, const struct soc
|
|||||||
free(gameThreadMsg);
|
free(gameThreadMsg);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
CCLOG("UDP received hole punching from %s:%d", ip, port);
|
CCLOG("UDP received hole punching");
|
||||||
}
|
}
|
||||||
|
free(buf->base);
|
||||||
|
//uv_udp_recv_stop(req);
|
||||||
|
uv_close((uv_handle_t*)req, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _allocBuffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
|
static void _allocBuffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user