Skip to content

Commit

Permalink
Link uwebsocket
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-abram committed Mar 11, 2018
1 parent c83f9ec commit 9175fde
Show file tree
Hide file tree
Showing 22 changed files with 2,678 additions and 0 deletions.
Binary file added Program/uWS.dll
Binary file not shown.
12 changes: 12 additions & 0 deletions StepmaniaCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,18 @@ if(WIN32)
link_libraries(${SM_EXTERN_DIR}/LuaJIT/lua51.lib)
include_directories(${SM_EXTERN_DIR}/LuaJIT/include)

find_library(LIB_UWS NAMES "uWS"
PATHS "${SM_EXTERN_DIR}/uWebSocket" NO_DEFAULT_PATH
)
find_library(LIB_EAY NAMES "libeay32"
PATHS "${SM_EXTERN_DIR}/uWebSocket" NO_DEFAULT_PATH
)
find_library(LIB_SSL NAMES "ssleay32"
PATHS "${SM_EXTERN_DIR}/uWebSocket" NO_DEFAULT_PATH
)
find_library(LIB_UV NAMES "libuv"
PATHS "${SM_EXTERN_DIR}/uWebSocket" NO_DEFAULT_PATH
)
if (MINGW AND WITH_FFMPEG)
include("${SM_CMAKE_DIR}/SetupFfmpeg.cmake")
set(HAS_FFMPEG TRUE)
Expand Down
192 changes: 192 additions & 0 deletions extern/uWebSocket/include/Asio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
#ifndef ASIO_H
#define ASIO_H

#include <boost/asio.hpp>

typedef boost::asio::ip::tcp::socket::native_handle_type uv_os_sock_t;
static const int UV_READABLE = 1;
static const int UV_WRITABLE = 2;

namespace uS {

struct Loop : boost::asio::io_service {

static Loop *createLoop(bool defaultLoop = true) {
return new Loop;
}

void destroy() {
delete this;
}

void run() {
boost::asio::io_service::run();
}

void poll() {
boost::asio::io_service::poll();
}
};

struct Timer {
boost::asio::deadline_timer asio_timer;
void *data;

Timer(Loop *loop) : asio_timer(*loop) {

}

void start(void (*cb)(Timer *), int first, int repeat) {
asio_timer.expires_from_now(boost::posix_time::milliseconds(first));
asio_timer.async_wait([this, cb, repeat](const boost::system::error_code &ec) {
if (ec != boost::asio::error::operation_aborted) {
if (repeat) {
start(cb, repeat, repeat);
}
cb(this);
}
});
}

void setData(void *data) {
this->data = data;
}

void *getData() {
return data;
}

// bug: cancel does not cancel expired timers!
// it has to guarantee that the timer is not called after
// stop is called! ffs boost!
void stop() {
asio_timer.cancel();
}

void close() {
asio_timer.get_io_service().post([this]() {
delete this;
});
}
};

struct Async {
Loop *loop;
void (*cb)(Async *);
void *data;

boost::asio::io_service::work asio_work;

Async(Loop *loop) : loop(loop), asio_work(*loop) {
}

void start(void (*cb)(Async *)) {
this->cb = cb;
}

void send() {
loop->post([this]() {
cb(this);
});
}

void close() {
loop->post([this]() {
delete this;
});
}

void setData(void *data) {
this->data = data;
}

void *getData() {
return data;
}
};

struct Poll {
boost::asio::posix::stream_descriptor *socket;
void (*cb)(Poll *p, int status, int events);

Poll(Loop *loop, uv_os_sock_t fd) {
socket = new boost::asio::posix::stream_descriptor(*loop, fd);
socket->non_blocking(true);
}

bool isClosed() {
return !socket;
}

boost::asio::ip::tcp::socket::native_handle_type getFd() {
return socket ? socket->native_handle() : -1;
}

void setCb(void (*cb)(Poll *p, int status, int events)) {
this->cb = cb;
}

void (*getCb())(Poll *, int, int) {
return cb;
}

void reInit(Loop *loop, uv_os_sock_t fd) {
delete socket;
socket = new boost::asio::posix::stream_descriptor(*loop, fd);
socket->non_blocking(true);
}

void start(Loop *, Poll *self, int events) {
if (events & UV_READABLE) {
socket->async_read_some(boost::asio::null_buffers(), [self](boost::system::error_code ec, std::size_t) {
if (ec != boost::asio::error::operation_aborted) {
self->start(nullptr, self, UV_READABLE);
self->cb(self, ec ? -1 : 0, UV_READABLE);
}
});
}

if (events & UV_WRITABLE) {
socket->async_write_some(boost::asio::null_buffers(), [self](boost::system::error_code ec, std::size_t) {
if (ec != boost::asio::error::operation_aborted) {
self->start(nullptr, self, UV_WRITABLE);
self->cb(self, ec ? -1 : 0, UV_WRITABLE);
}
});
}
}

void change(Loop *, Poll *self, int events) {
socket->cancel();
start(nullptr, self, events);
}

bool fastTransfer(Loop *loop, Loop *newLoop, int events) {
return false;
}

// todo: asio is thread safe, use it!
bool threadSafeChange(Loop *loop, Poll *self, int events) {
return false;
}

void stop(Loop *) {
socket->cancel();
}

// this is not correct, but it works for now
// think about transfer - should allow one to not delete
// but in this case it doesn't matter at all
void close(Loop *loop, void (*cb)(Poll *)) {
socket->release();
socket->get_io_service().post([cb, this]() {
cb(this);
});
delete socket;
socket = nullptr;
}
};

}

#endif // ASIO_H
17 changes: 17 additions & 0 deletions extern/uWebSocket/include/Backend.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef BACKEND_H
#define BACKEND_H

// Default to Epoll if nothing specified and on Linux
// Default to Libuv if nothing specified and not on Linux
#ifdef USE_ASIO
#include "Asio.h"
#elif !defined(__linux__) || defined(USE_LIBUV)
#include "Libuv.h"
#else
#ifndef USE_EPOLL
#define USE_EPOLL
#endif
#include "Epoll.h"
#endif

#endif // BACKEND_H
Loading

0 comments on commit 9175fde

Please sign in to comment.