kuma is a multi-platform support network library developed in C++11. It implements interfaces for TCP/UDP/Multicast/HTTP/HTTP2/WebSocket/timer that drove by event loop. kuma supports epoll/poll/WSAPoll/kqueue/select on platform Linux/Windows/OSX/iOS/Android.
define macro KUMA_HAS_OPENSSL to enable openssl
open project bld/ios/kuma with xcode and build it
open project bld/osx/kuma with xcode and build it
open bld/msvc/kuma.sln with VS2015 and build it
$ cd src
$ make
$ cd src/jni
$ ndk-build
certificates location is same as your binary folder by default.
copy all CA certificates used to ca.cer
copy your server certificate to server.cer
copy your server private key to server.key
Please refer to test/client and test/server for more examples
#include "kmapi.h"
using namespace kuma;
int main(int argc, char *argv[])
{
kuma::init();
EventLoop main_loop(PollType::NONE);
if (!main_loop.init()) {
printf("failed to init EventLoop\n");
kuma::fini();
return -1;
}
WebSocket ws(&main_loop);
ws.setDataCallback([] (void* data, size_t len) {
printf("ws.onData, len=%lu\n", len);
});
ws.setWriteCallback([] (KMError err) {
printf("ws.onWrite, write available\n");
});
ws.setErrorCallback([] (KMError err) {
printf("ws.onError, err=%d\n", err);
});
ws.setProtocol("jws");
ws.setOrigin("www.jamol.cn");
ws.connect("wss://127.0.0.1:8443", [] (KMError err) {
printf("ws.onConnect, err=%d\n", err);
});
Timer timer(&main_loop);
timer.schedule(1000, [] {
printf("onTimer\n");
}, TimerMode::ONE_SHOT);
main_loop.loop();
kuma::fini();
return 0;
}
#include "kmapi.h"
using namespace kuma;
int main(int argc, char *argv[])
{
kuma::init();
EventLoop main_loop(PollType::NONE);
if (!main_loop.init()) {
printf("failed to init EventLoop\n");
kuma::fini();
return -1;
}
WebSocket ws(&main_loop);
ws.setDataCallback([] (void* data, size_t len) {
printf("ws.onData, len=%lu\n", len);
});
ws.setWriteCallback([] (KMError err) {
printf("ws.onWrite, write available\n");
});
ws.setErrorCallback([] (KMError err) {
printf("ws.onError, err=%d\n", err);
});
TcpListener server(&main_loop);
server.setAcceptCallback([&ws] (SOCKET_FD fd, const char* ip, uint16_t port) -> bool {
printf("server.onAccept, ip=%s\n", ip);
ws.setSslFlags(SSL_ENABLE);
ws.attachFd(fd, nullptr, 0);
return true;
});
server.setErrorCallback([] (KMError err) {
printf("server.onError, err=%d\n", err);
});
auto ret = server.startListen("0.0.0.0", 8443);
if (ret != KMError::NOERR) {
printf("failed to listen on 8443\n");
}
main_loop.loop();
kuma::fini();
return 0;
}