Hi,
I am trying to create multiplayer P2P game for IOS, Android and WP8.
The connection between the peers will be based on TCP and I have decided to use boost::asio and it will be synchronous.
I have tried to run a simple code block for the peer which will act like server but it crushes:
I tried to put it inside an empty project and I couldnât compile it. It said that there are missing files like the namespace purelib.
Is this namespace is part of the cocos2d-x framework ?
And also is it possible for you to provide some small example how to use this wrapper classes ?
Thanks once again.
You should put both âpolitedef.h xxsocket.h xxsocket.cppâ to your empty project, by the way, whatâs platform do you test?
The purelib namespace is defined at politedef.h, itâs not part of cocos2d-x framework.
In my project, itâs work well on Android and iOS.
usage:
#include "xxsocket.h"
using namespace purelib::net;
// server
xxsocket tcpserv;
if(0 == tcpserv.pserv("192.168.1.23"/*could be ipv6 address*/, 2016)) {
xxsocket cli = tcpserv.accept();
char buffer[256];
cli.recv(buffer, sizeof("hello server"));
}
// client
xxsocket tcpcli;
if(0 == tcpserv.xpconnect("www.baidu.com"/*could be ipv6 address or hostname*/, 443)) {
tcpcli.send(buffer, sizeof("hello server"));
}
Halx99, once again thanks for the time you spend to help.
I am working on windows 10 machine(I tried both on simple console and windows 8.1 shared projects), I also included all the needed files, but it couldnât compile. I may did something wrong and I will check it once again.
I assume that you and halx99 from github are the same guy, so thanks for sharing this lib. Its exactly what I was looking for - lightweight network lib. And if everything will work for me it will be , simply priceless
Hi, I tried in my windows 8.1 share project, It actual chould not compile, because I try to make it support IPv6 recently. so currently It work fine on Android & iOS. I will let it work on windows 8.1/10 UWP later.
Only one small question: when you call the tcpserv.accept(); while waiting for connection from the other peer how you call it without blocking the cocos UI ?
You can start a thread to process network event, or you can use select+nonblocking socket to do this, or other asynchronous socket io mode, such as poll, epoll, and etc. Certainly for client network, I think select is ok.
I am sorry for being annoying, but I have not written any low level network programming (I only finninshed to read some books about boost::asio).
As long as I understood, using the poll function is the right way to do the job but I also have read that poll is not supported in operation systems based on windows and I need to use select or I/O operations.
For other platforms I need to use the epoll,
so my question is, is there something that you did in the library to handle such cases or you are using it as blocking library and implement the non blocking stuff outside the library ?
In your opinion what will be better choice, to use select or poll ?
By this I mean, because in windows phone there is no poll and I still didnât figure out how the xxsocket library works.
I am reading everything from Beejâs Guide to Network Programming, and I see similar approaches, but I still canât figure how to implement it and what exactly to use (so it will work - on all three platform and be efficient).
For client network, I think the select is the best choice.
Actually, in our project, âselectâ is not required, I just set the socket nonblocking mode, and do socket read & write at a sub thread looply. I sleep 1 millisecond when there is no data to read or write according to return value of recv_i & send_i, itâs for avoid high CPU occupation.
For P2P case, If you want process C/S and P2P at a single thread, âselectâ still required.
I have one question regarding P2P,
When you created your P2P game did you needed to modify something in the cocos2d framework ?
because I succeeded to create the server but it doesnât succeed to receive anything but the client part do send.
I checked the firewall but everything seems to be OK.