Further updates on win32 build templates.

This commit is contained in:
genxium
2023-01-23 14:17:52 +08:00
parent 58b06f6a10
commit 0168e2182e
20 changed files with 242 additions and 43 deletions

View File

@@ -33,7 +33,7 @@
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
#include "cocos/scripting/js-bindings/event/EventDispatcher.h"
#include "cocos/scripting/js-bindings/manual/jsb_classtype.hpp"
#include "udp_session_bridge.hpp"
USING_NS_CC;
AppDelegate::AppDelegate(int width, int height) : Application("Cocos Game", width, height)
@@ -61,6 +61,7 @@ bool AppDelegate::applicationDidFinishLaunching()
});
jsb_register_all_modules();
se->addRegisterCallback(registerUdpSession);
se->start();

View File

@@ -1,14 +1,95 @@
#include "udp_session.hpp"
#include "base/ccMacros.h"
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
#include <stdio.h>
#include "uv/uv.h"
#include <thread>
static uv_udp_t* udpSocket = NULL;
uv_loop_t* loop = NULL; // Only this loop is used for this simple PoC
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));
uv_close((uv_handle_t*)req, NULL);
free(buf->base);
return;
}
char sender[17] = { 0 };
uv_ip4_name((const struct sockaddr_in*)addr, sender, 16);
CCLOG("Recv from %s", sender);
free(buf->base);
uv_udp_recv_stop(req);
}
static void _allocBuffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
(void)handle;
buf->base = (char *)malloc(suggested_size);
buf->len = suggested_size;
}
void startRecvLoop(void* arg) {
uv_loop_t* loop = (uv_loop_t*)arg;
uv_run(loop, UV_RUN_DEFAULT);
}
bool DelayNoMore::UdpSession::openUdpSession(int port) {
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));
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);
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);
bool DelayNoMore::UdpSession::upsertPeerUdpAddr(int joinIndex, CHARC* const ip, int port, CHARC* const authKey) {
printf("Called by js for joinIndex=%d, ip=%s, port=%d, authKey=%s.", joinIndex, ip, port, authKey);
return true;
}
void DelayNoMore::UdpSession::onMessage(CBYTE* const bytes) {
static void _onWalkCleanup(uv_handle_t* handle, void* data) {
(void)data;
uv_close(handle, NULL);
}
bool DelayNoMore::UdpSession::closeUdpSession() {
CCLOG("About to close udp session and dealloc all resources...");
uv_stop(loop);
uv_walk(loop, _onWalkCleanup, NULL);
uv_loop_close(loop);
free(udpSocket);
free(loop);
CCLOG("Closed udp session and dealloc all resources...");
return true;
}
bool DelayNoMore::UdpSession::upsertPeerUdpAddr(int joinIndex, CHARC* const ip, int port, uint32_t authKey) {
CCLOG("Called by js for joinIndex=%d, ip=%s, port=%d, authKey=%lu.", joinIndex, ip, port, authKey);
return true;
}
void DelayNoMore::UdpSession::onMessage(BYTEC* const bytes) {
se::ScriptEngine* se = se::ScriptEngine::getInstance();
se::Value func;

View File

@@ -3,16 +3,18 @@
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
typedef unsigned char const CBYTE;
typedef unsigned char const BYTEC;
typedef char const CHARC;
namespace DelayNoMore {
class UdpSession {
public:
static bool upsertPeerUdpAddr(int joinIndex, CHARC* const ip, int port, CHARC* const authKey);
static bool openUdpSession(int port);
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 void onMessage(CBYTE* const bytes);
static void onMessage(BYTEC* const bytes);
};
}
#endif

View File

@@ -2,6 +2,35 @@
#include "base/ccMacros.h"
#include "scripting/js-bindings/manual/jsb_conversions.hpp"
bool openUdpSession(se::State& s) {
const auto& args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (1 == argc) {
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);
return false;
}
SE_BIND_FUNC(openUdpSession)
bool closeUdpSession(se::State& s) {
const auto& args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (0 == argc) {
SE_PRECONDITION2(ok, false, "closeUdpSession: Error processing arguments");
return DelayNoMore::UdpSession::closeUdpSession();
}
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 0);
return false;
}
SE_BIND_FUNC(closeUdpSession)
bool upsertPeerUdpAddr(se::State& s) {
const auto& args = s.args();
size_t argc = args.size();
@@ -11,13 +40,12 @@ bool upsertPeerUdpAddr(se::State& s) {
int joinIndex = args[0].toInt32();
CHARC* ip = args[1].toString().c_str();
int port = args[2].toInt32();
CHARC* authKey = args[3].toString().c_str();
DelayNoMore::UdpSession::upsertPeerUdpAddr(joinIndex, ip, port, authKey);
return true;
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);
return true;
return false;
}
SE_BIND_FUNC(upsertPeerUdpAddr)
@@ -36,7 +64,7 @@ SE_BIND_FINALIZE_FUNC(udpSessionFinalize)
se::Object* __jsb_udp_session_proto = nullptr;
se::Class* __jsb_udp_session_class = nullptr;
bool register_udp_session(se::Object* obj)
bool registerUdpSession(se::Object* obj)
{
// Get the ns
se::Value nsVal;
@@ -49,7 +77,9 @@ bool register_udp_session(se::Object* obj)
se::Object* ns = nsVal.toObject();
auto cls = se::Class::create("UdpSession", ns, nullptr, nullptr);
cls->defineStaticFunction("openUdpSession", _SE(openUdpSession));
cls->defineStaticFunction("closeUdpSession", _SE(closeUdpSession));
cls->defineStaticFunction("upsertPeerUdpAddr", _SE(upsertPeerUdpAddr));
cls->defineFinalizeFunction(_SE(udpSessionFinalize));
cls->install();

View File

@@ -8,8 +8,10 @@
extern se::Object* __jsb_udp_session_proto;
extern se::Class* __jsb_udp_session_class;
bool register_udp_session(se::Object* obj);
bool registerUdpSession(se::Object* obj);
SE_DECLARE_FUNC(openUdpSession);
SE_DECLARE_FUNC(closeUdpSession);
SE_DECLARE_FUNC(upsertPeerUdpAddr);
#endif

View File

@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="resource">
<UniqueIdentifier>{ca9c9e15-d942-43a1-aa7a-5f0b74ca1afd}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;jpg;jpeg;jpe;png;manifest</Extensions>
</Filter>
<Filter Include="win32">
<UniqueIdentifier>{ccb2323b-1cfa-41ea-bcf4-ba5f07309396}</UniqueIdentifier>
</Filter>
<Filter Include="Classes">
<UniqueIdentifier>{e93a77e1-af1e-4400-87d3-504b62ebdbb0}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>win32</Filter>
</ClCompile>
<ClCompile Include="..\Classes\AppDelegate.cpp">
<Filter>Classes</Filter>
</ClCompile>
<ClCompile Include="..\Classes\jsb_module_register.cpp">
<Filter>Classes</Filter>
</ClCompile>
<ClCompile Include="..\Classes\udp_session.cpp">
<Filter>Classes</Filter>
</ClCompile>
<ClCompile Include="..\Classes\udp_session_bridge.cpp">
<Filter>Classes</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\Classes\AppDelegate.h">
<Filter>Classes</Filter>
</ClInclude>
<ClInclude Include="main.h">
<Filter>win32</Filter>
</ClInclude>
<ClInclude Include="resource.h" />
<ClInclude Include="..\Classes\udp_session.hpp">
<Filter>Classes</Filter>
</ClInclude>
<ClInclude Include="..\Classes\udp_session_bridge.hpp">
<Filter>Classes</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="game.rc">
<Filter>resource</Filter>
</ResourceCompile>
</ItemGroup>
<ItemGroup>
<Image Include="res\game.ico">
<Filter>resource</Filter>
</Image>
</ItemGroup>
</Project>