A mini cross platform posix socket API wrapper(support IPv6-only network, Integrated with Application Layer Binary Protocol Auto Generator)

This is a mini cross platform posix socket API wrapper, support win32 &
linux & ios & android & wp8 & wp8.1-universal &
win10-universal
It also support IPv6-only network.

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("192.168.1.23"/*could be ipv6 address or hostname*/, 2016)) {
    tcpcli.send(buffer, sizeof("hello server"));
}
4 Likes

Please don’t just post a blind link. Introduce it and put it in Game/Demo Showcase

Edit, actually put it in Extensions

OK, I done it.

Add Application Layer Binary Protocol C++ enc/dec source code & luabinding Auto Generator

Add API xxsocket::xpconnect/xxsocket::xpconnect_n for ipv6 only support.

Add async tcp client & timer support.

2.3.5
optimize tcp recv read performance when busy.

1 Like

@halx99

Is this lib is not depending on any Third party libs like boost . for A sync and UDP
you absolutely got here something that is missing from the c++ world and cocos2d-x .
Well done

another one https://github.com/cloudwu/lsocket

Yes, It’s not depending any 3rd party libs.
And fix compare operator error bug.

V3.0 released:

  1. Support multi-channels at one thread service.
  2. Async reconnect support
  3. Optimize log for cross platform
  4. Optimize log for cocos2d, avoid crash
  5. Optimize unpack logic
  6. Avoid buffer attack
  7. Integrate c-ares support, macro: _USE_ARES_LIB, default value is 0

update v3.0-rel
1.Support multi-channels at 1 thread service.
2.Async reconnect support
3.Optimize log for cross platform
4.Optimize log for cocos2d, avoid crash
5.Optimize unpack logic
6.Avoid buffer attack
7.Integrate c-ares support, macro: _USE_ARES_LIB, default value is 0
8.Add macros to control whether use object pool, shared_ptr and enable send callback
9.Fix shutdown can’t close connection.
10.object_pool, thread safe support

Error: 10057
Connect to 2 service in file simple_test.cpp

  purelib::inet::channel_endpoint endpoints[] = {
     { "203.162.71.67", 80 },
     { "www.ip138.com", 80 }
   };
   .....
   .....
   myasio->open(0, CHANNEL_TCP_CLIENT);
   myasio->open(1, CHANNEL_TCP_CLIENT);

Many time get error 1057, full log:

[mini-asio][1532866023984774] [index: 0] connecting server 203.162.71.67:80…
[mini-asio][1532866023986874] [index: 0] the connection [0.0.0.0:52783] —> 203.162.71.67:80 is established.
[mini-asio][1532866023987859] [index: 1] connecting server www.ip138.com:80
[mini-asio][1532866023988676] [index: 0] do_write error, the connection should be closed, retval=-1, ec:10057, detail:A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied.

[mini-asio][1532866023998673] [index: 1] the connection [192.168.33.107:52784] —> 42.114.104.27:80 is established.
[mini-asio][1532866083426316] [index: 1] do_read error, the server close the connection, retval=0, ec:0, detail:The operation completed successfully.

Please help me. Thanks

The issue has been solved at v3.3:

  1. Improve server support
  2. Add dns cache timeout support.
  3. Fix Error: 10057, when start with 2 channels.
  4. Add getaddrinfo_thread async domain resolve support.
  5. Rename some API.

update v3.3.1

  1. Use event queue to ensure io event sequence produced by the async io service.
  2. Remove unused callback ‘threadsafe_call’
  3. Rename some APIs
1 Like

quick test

 g++ src/*.cpp test/xxsocket_test/simple_test.cpp --std=c++11 -I./src -o simple_test && ./simple_test
1 Like

The latest master branch can be easy to binding for lua with sol2, for detail, see here: https://github.com/halx99/mini-asio/issues/8

update v3.3.2

  1. Improve set_option implement, use va_list for more options
  2. Add tcp keepalive support, option: MASIO_OPT_TCP_KEEPALIVE
  3. Add custom resolve function support, option: MASIO_OPT_RESOLV_FUNCTION
  4. Update readme.md & simple_test.cpp for new API.

Update to v3.9.0
1. Add Lua bindings with sol2 support, see https://github.com/halx99/mini-asio/blob/master/test/test/example.lua
2. Remove game engine specific code
3. Upgrade c++ require standard to c++17
4. Deep integration binary stream encode/decode, and export to Lua
5. Update protocol autogen tool for new lua bindings
6. Remove set_callbacks, only need set event callback at start_service.

Update to v3.9.1
1. Make required std::string_view backport to C++11 standard
2. Fix compile errors & warnings for GCC

quick test: g++ src/xxsocket.cpp src/masio.cpp test/test/cpptest.cpp --std=c++11 -lpthread -I./src -o cpptest && ./cpptest

1 Like

You wrote c++17. As far as I know Android can use it only since API level 21 (Lollipop), starting in NDK r18. The default cocos template will use older versions. So everbody with support for older devices can’t use your library. :thinking: