Hi @hainh / @camkadev / @almax27,
I made some good progress over Boost.Asio integration with cocos2d-x project. First I was trying with Asio standalone but then found a git script ([faithfracture’s Boost build script][1]), and using that compiled framework for boost_1_60_0. Then to get comfortable with library, took its async tcp example and played with it as a standalone command line application along with thread.
After that linked iOS framework to cocos2d-x project and then copied modified version of tcp example to cocos2d-x project. And with some small code, my cocos2d-x game now connects to socket server and also can read / write string messages.
But then encountered a issue with threading
GameManager:InitConnection()
{
boost::asio::io_service io_service;
tcp::resolver r(io_service);
//TCPSocketClient is async tcp client
TCPSocketClient tcp_client(io_service);
//my socket server is running on port 8007 on same machine
tcp_client.start(r.resolve(tcp::resolver::query("127.0.0.1", "8007")));
/**
When boost::thread is used to run boost event loop in separate thread,
so that cocos2d-x's default game loop (scene rendering etc work fine)
it crashes.
*/
boost::thread bt(boost::bind(&boost::asio::io_service::run, &io_service));
//However if instead of thread I do a sync run, then it works fine.
//io_service.run();
}
GameManager is a singleton class, which is similar to https://gist.github.com/solkar/8651478
In AppDelegate.cpp
, after director->runWithScene(scene);
, I am calling GameManager’s InitConnection.
GameManager * mgr = GameManager::getInstance();
mgr->InitConnection();
And it seems to work as long as I keep boost’s io_service run in main loop. But then it prevent cocos2d-x scene execution further. and if I keep io_service loop inside a boost::thread (as in above snippet), right after the launch, simulator crashes.
I think its something related to weak reference, but can’t figure out. Any help?
Thanks
[1]: https://gist.github.com/faithfracture/c629ae4c7168216a9856 “faithfracture’s Boost Build Script”