同步android实现的功能
This commit is contained in:
parent
7672dba63b
commit
7c78c22dab
@ -37,6 +37,10 @@ NS_CC_BEGIN
|
|||||||
|
|
||||||
void performFunctionInCocosThread( const std::function<void()> &function);
|
void performFunctionInCocosThread( const std::function<void()> &function);
|
||||||
|
|
||||||
|
void tickRun();
|
||||||
|
|
||||||
|
void jsToUnity(std::string funId, std::string msg);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static JcWallet *_instance;
|
static JcWallet *_instance;
|
||||||
std::vector<std::function<void()>> _functionsToPerform;
|
std::vector<std::function<void()>> _functionsToPerform;
|
||||||
|
@ -28,10 +28,10 @@ NS_CC_BEGIN
|
|||||||
|
|
||||||
cocos2d::Application *g_app = nullptr;
|
cocos2d::Application *g_app = nullptr;
|
||||||
JcWallet *JcWallet::_instance = nullptr;
|
JcWallet *JcWallet::_instance = nullptr;
|
||||||
|
uv_loop_t *loop = nullptr;
|
||||||
bool _isStarted = false;
|
bool _isStarted = false;
|
||||||
uv_async_t gasync = {0};
|
uv_async_t gasync = {0};
|
||||||
|
|
||||||
|
|
||||||
struct AsyncTaskData {
|
struct AsyncTaskData {
|
||||||
std::mutex mtx;
|
std::mutex mtx;
|
||||||
std::list<std::function<void()> > tasks;
|
std::list<std::function<void()> > tasks;
|
||||||
@ -52,7 +52,9 @@ NS_CC_BEGIN
|
|||||||
|
|
||||||
void init_libuv_async_handle(uv_async_t *async) {
|
void init_libuv_async_handle(uv_async_t *async) {
|
||||||
memset(async, 0, sizeof(uv_async_t));
|
memset(async, 0, sizeof(uv_async_t));
|
||||||
uv_loop_t *loop = uv_default_loop();
|
// uv_loop_t *loop = uv_default_loop();
|
||||||
|
loop = (uv_loop_t*)malloc(sizeof(uv_loop_t));
|
||||||
|
uv_loop_init(loop);
|
||||||
uv_async_init(loop, async, cocos2d::flush_tasks_in_server_loop_cb);
|
uv_async_init(loop, async, cocos2d::flush_tasks_in_server_loop_cb);
|
||||||
async->data = new AsyncTaskData();
|
async->data = new AsyncTaskData();
|
||||||
uv_run(loop, UV_RUN_DEFAULT);
|
uv_run(loop, UV_RUN_DEFAULT);
|
||||||
@ -69,7 +71,12 @@ NS_CC_BEGIN
|
|||||||
//notify server thread to invoke `flush_tasks_in_server_loop_cb()`
|
//notify server thread to invoke `flush_tasks_in_server_loop_cb()`
|
||||||
return uv_async_send(asyn);
|
return uv_async_send(asyn);
|
||||||
}
|
}
|
||||||
|
#define RUN_IN_GAMETHREAD(task) \
|
||||||
|
do { \
|
||||||
|
JcWallet::getInstance()->performFunctionInCocosThread([=]() { \
|
||||||
|
task; \
|
||||||
|
});\
|
||||||
|
} while(0)
|
||||||
#define RUN_IN_SERVERTHREAD(task) do { \
|
#define RUN_IN_SERVERTHREAD(task) do { \
|
||||||
schedule_task_into_server_thread_task_queue(&gasync, [=](){ \
|
schedule_task_into_server_thread_task_queue(&gasync, [=](){ \
|
||||||
task; \
|
task; \
|
||||||
@ -98,6 +105,8 @@ NS_CC_BEGIN
|
|||||||
|
|
||||||
JcWallet::JcWallet() {
|
JcWallet::JcWallet() {
|
||||||
JcWallet::_instance = this;
|
JcWallet::_instance = this;
|
||||||
|
_functionsToPerform.reserve(30);
|
||||||
|
std::unique_lock<std::mutex> lock(_performMutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void JcWallet::initEnv() {
|
void JcWallet::initEnv() {
|
||||||
@ -111,6 +120,7 @@ NS_CC_BEGIN
|
|||||||
g_app->start();
|
g_app->start();
|
||||||
_isStarted = true;
|
_isStarted = true;
|
||||||
WalletEvent::Emit("wallet_init_event", "{}");
|
WalletEvent::Emit("wallet_init_event", "{}");
|
||||||
|
// RUN_IN_GAMETHREAD(JcWallet::getInstance()->jsToUnity("wallet_init_event", "{}"));
|
||||||
cocos2d::init_libuv_async_handle(&gasync);
|
cocos2d::init_libuv_async_handle(&gasync);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -127,12 +137,12 @@ NS_CC_BEGIN
|
|||||||
JcWallet::~JcWallet() {
|
JcWallet::~JcWallet() {
|
||||||
EventDispatcher::destroy();
|
EventDispatcher::destroy();
|
||||||
se::ScriptEngine::destroyInstance();
|
se::ScriptEngine::destroyInstance();
|
||||||
uv_loop_close(uv_default_loop());
|
uv_loop_close(loop);
|
||||||
JcWallet::_instance = nullptr;
|
JcWallet::_instance = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *JcWallet::runJsMethod(std::shared_ptr<JSMethodParam> data) {
|
char *JcWallet::runJsMethod(std::shared_ptr<JSMethodParam> data) {
|
||||||
cocos2d::log("thread: %ld call method %s", uv_thread_self(), data->methodName.c_str());
|
cocos2d::log("thread: %ld call method %s params: %d", uv_thread_self(), data->methodName.c_str(), data->args.size());
|
||||||
se::Value value;
|
se::Value value;
|
||||||
bool ok = cocos2d::runGlobalMethod(data->methodName.c_str(), data->args, &value);
|
bool ok = cocos2d::runGlobalMethod(data->methodName.c_str(), data->args, &value);
|
||||||
static std::string result;
|
static std::string result;
|
||||||
@ -143,14 +153,39 @@ NS_CC_BEGIN
|
|||||||
result = "";
|
result = "";
|
||||||
}
|
}
|
||||||
cocos2d::log("method: %s ::result: %s", data->methodName.c_str(), result.c_str());
|
cocos2d::log("method: %s ::result: %s", data->methodName.c_str(), result.c_str());
|
||||||
WalletEvent::Emit(data->funId.c_str(), result.c_str());
|
RUN_IN_GAMETHREAD(JcWallet::getInstance()->jsToUnity(data->funId, result));
|
||||||
|
// WalletEvent::Emit(data->funId.c_str(), result.c_str());
|
||||||
}
|
}
|
||||||
return const_cast<char *>(result.c_str());
|
return const_cast<char *>(result.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void JcWallet::performFunctionInCocosThread( const std::function<void()> &function) {
|
||||||
|
_performMutex.lock();
|
||||||
|
_functionsToPerform.push_back(function);
|
||||||
|
_performMutex.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void JcWallet::tickRun() {
|
||||||
|
_performMutex.lock();
|
||||||
|
if( !_functionsToPerform.empty() ) {
|
||||||
|
// fixed #4123: Save the callback functions, they must be invoked after '_performMutex.unlock()', otherwise if new functions are added in callback, it will cause thread deadlock.
|
||||||
|
auto temp = _functionsToPerform;
|
||||||
|
for( const auto &function : temp ) {
|
||||||
|
function();
|
||||||
|
}
|
||||||
|
_functionsToPerform.clear();
|
||||||
|
}
|
||||||
|
_performMutex.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void JcWallet::jsToUnity(std::string funId, std::string msg) {
|
||||||
|
WalletEvent::Emit(funId.c_str(), msg.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
void initEnv() {
|
void initEnv() {
|
||||||
if (!_isStarted) {
|
if (!_isStarted) {
|
||||||
|
new JcWallet();
|
||||||
std::shared_ptr<JcWallet> wallet(JcWallet::getInstance());
|
std::shared_ptr<JcWallet> wallet(JcWallet::getInstance());
|
||||||
std::thread([=]() {
|
std::thread([=]() {
|
||||||
JcWallet::initJSThread(wallet);
|
JcWallet::initJSThread(wallet);
|
||||||
@ -162,6 +197,7 @@ NS_CC_BEGIN
|
|||||||
schedule_task_into_server_thread_task_queue(&gasync, [=](){
|
schedule_task_into_server_thread_task_queue(&gasync, [=](){
|
||||||
JcWallet::getInstance()->tick(dt);
|
JcWallet::getInstance()->tick(dt);
|
||||||
});
|
});
|
||||||
|
JcWallet::getInstance()->tickRun();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,9 +216,9 @@ NS_CC_BEGIN
|
|||||||
int result = schedule_task_into_server_thread_task_queue(&gasync, [=](){
|
int result = schedule_task_into_server_thread_task_queue(&gasync, [=](){
|
||||||
JcWallet::getInstance()->runJsMethod(params);
|
JcWallet::getInstance()->runJsMethod(params);
|
||||||
});
|
});
|
||||||
// RUN_IN_SERVERTHREAD(JcWallet::getInstance()->runJsMethod(params));
|
|
||||||
return result == 0 ? 1 : 0;
|
return result == 0 ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int nativeCallBack(const char *funId, const char *methodName, const char *cparams) {
|
int nativeCallBack(const char *funId, const char *methodName, const char *cparams) {
|
||||||
@ -201,6 +237,7 @@ int nativeCallBack(const char *funId, const char *methodName, const char *cparam
|
|||||||
}
|
}
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
||||||
static bool getOrCreatePlainObject_r(const char* name, se::Object* parent, se::Object** outObj)
|
static bool getOrCreatePlainObject_r(const char* name, se::Object* parent, se::Object** outObj)
|
||||||
{
|
{
|
||||||
assert(parent != nullptr);
|
assert(parent != nullptr);
|
||||||
@ -232,7 +269,8 @@ bool jsb_wallet_callback(se::State& s) {
|
|||||||
std::string msg;
|
std::string msg;
|
||||||
ok = seval_to_std_string(args[1], &msg);
|
ok = seval_to_std_string(args[1], &msg);
|
||||||
SE_PRECONDITION2(ok, false, "Error processing arguments");
|
SE_PRECONDITION2(ok, false, "Error processing arguments");
|
||||||
WalletEvent::Emit(funId.c_str(), msg.c_str());
|
// WalletEvent::Emit(funId.c_str(), msg.c_str());
|
||||||
|
RUN_IN_GAMETHREAD(JcWallet::getInstance()->jsToUnity(funId, msg));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user