-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c83f9ec
commit 9175fde
Showing
22 changed files
with
2,678 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.