Using boost::asio for P2P game

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:

asio::ip::tcp::endpoint ep(asio::ip::address_v4::any(), 3333);
asio::io_service ios;
asio::ip::tcp::acceptor acceptor(ios, ep);
asio::ip::tcp::socket sock(ios, ep.protocol());
acceptor.accept(sock);

I have tried the same code in simple console application and it runs perfect but when I run it inside the cocos2d-x framework it crushes.

Is there something that I am doing wrong ?

P.S do not pay attention on the ::any() part, it is just for the purposes of testing.

Thanks in advance :slight_smile:

2 Likes

I use this https://github.com/halx99/xxsocket to implement p2p.

Looks interesting I will check it immediately.
Thanks a lot :slight_smile:

You are welcome :smile:

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 :slight_smile:

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.

OK, I fix the compile error for Windows 8.0/10 UWP,please try again.
Thanks for your feedback make this lightweight network lib properly :smile:

Hi, projectgoo
I upload a visual studio 2013 test project, you can open it and compile directly. :smile:

Awsome !!!

I download it and it seems to work :slight_smile:

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.

You may needs asynchronous socket for this case always.
Just call tcpserv.set_nonblocking(true)

Thanks :smile:
I will try it

Hi halx99,

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 ?

Hi, projectgoo
I’m writing for this, you can watch my github “xxsocket” project, and communicate with me for implement that.

Thanks halx99 :slight_smile:

Hi halx99,

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).

Thanks for your help :slight_smile:

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.

Hi halx99,

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.

Thanks you.