Skip to content

Commit 9175fde

Browse files
committed
Link uwebsocket
1 parent c83f9ec commit 9175fde

22 files changed

+2678
-0
lines changed

Program/uWS.dll

202 KB
Binary file not shown.

StepmaniaCore.cmake

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,18 @@ if(WIN32)
254254
link_libraries(${SM_EXTERN_DIR}/LuaJIT/lua51.lib)
255255
include_directories(${SM_EXTERN_DIR}/LuaJIT/include)
256256

257+
find_library(LIB_UWS NAMES "uWS"
258+
PATHS "${SM_EXTERN_DIR}/uWebSocket" NO_DEFAULT_PATH
259+
)
260+
find_library(LIB_EAY NAMES "libeay32"
261+
PATHS "${SM_EXTERN_DIR}/uWebSocket" NO_DEFAULT_PATH
262+
)
263+
find_library(LIB_SSL NAMES "ssleay32"
264+
PATHS "${SM_EXTERN_DIR}/uWebSocket" NO_DEFAULT_PATH
265+
)
266+
find_library(LIB_UV NAMES "libuv"
267+
PATHS "${SM_EXTERN_DIR}/uWebSocket" NO_DEFAULT_PATH
268+
)
257269
if (MINGW AND WITH_FFMPEG)
258270
include("${SM_CMAKE_DIR}/SetupFfmpeg.cmake")
259271
set(HAS_FFMPEG TRUE)

extern/uWebSocket/include/Asio.h

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
#ifndef ASIO_H
2+
#define ASIO_H
3+
4+
#include <boost/asio.hpp>
5+
6+
typedef boost::asio::ip::tcp::socket::native_handle_type uv_os_sock_t;
7+
static const int UV_READABLE = 1;
8+
static const int UV_WRITABLE = 2;
9+
10+
namespace uS {
11+
12+
struct Loop : boost::asio::io_service {
13+
14+
static Loop *createLoop(bool defaultLoop = true) {
15+
return new Loop;
16+
}
17+
18+
void destroy() {
19+
delete this;
20+
}
21+
22+
void run() {
23+
boost::asio::io_service::run();
24+
}
25+
26+
void poll() {
27+
boost::asio::io_service::poll();
28+
}
29+
};
30+
31+
struct Timer {
32+
boost::asio::deadline_timer asio_timer;
33+
void *data;
34+
35+
Timer(Loop *loop) : asio_timer(*loop) {
36+
37+
}
38+
39+
void start(void (*cb)(Timer *), int first, int repeat) {
40+
asio_timer.expires_from_now(boost::posix_time::milliseconds(first));
41+
asio_timer.async_wait([this, cb, repeat](const boost::system::error_code &ec) {
42+
if (ec != boost::asio::error::operation_aborted) {
43+
if (repeat) {
44+
start(cb, repeat, repeat);
45+
}
46+
cb(this);
47+
}
48+
});
49+
}
50+
51+
void setData(void *data) {
52+
this->data = data;
53+
}
54+
55+
void *getData() {
56+
return data;
57+
}
58+
59+
// bug: cancel does not cancel expired timers!
60+
// it has to guarantee that the timer is not called after
61+
// stop is called! ffs boost!
62+
void stop() {
63+
asio_timer.cancel();
64+
}
65+
66+
void close() {
67+
asio_timer.get_io_service().post([this]() {
68+
delete this;
69+
});
70+
}
71+
};
72+
73+
struct Async {
74+
Loop *loop;
75+
void (*cb)(Async *);
76+
void *data;
77+
78+
boost::asio::io_service::work asio_work;
79+
80+
Async(Loop *loop) : loop(loop), asio_work(*loop) {
81+
}
82+
83+
void start(void (*cb)(Async *)) {
84+
this->cb = cb;
85+
}
86+
87+
void send() {
88+
loop->post([this]() {
89+
cb(this);
90+
});
91+
}
92+
93+
void close() {
94+
loop->post([this]() {
95+
delete this;
96+
});
97+
}
98+
99+
void setData(void *data) {
100+
this->data = data;
101+
}
102+
103+
void *getData() {
104+
return data;
105+
}
106+
};
107+
108+
struct Poll {
109+
boost::asio::posix::stream_descriptor *socket;
110+
void (*cb)(Poll *p, int status, int events);
111+
112+
Poll(Loop *loop, uv_os_sock_t fd) {
113+
socket = new boost::asio::posix::stream_descriptor(*loop, fd);
114+
socket->non_blocking(true);
115+
}
116+
117+
bool isClosed() {
118+
return !socket;
119+
}
120+
121+
boost::asio::ip::tcp::socket::native_handle_type getFd() {
122+
return socket ? socket->native_handle() : -1;
123+
}
124+
125+
void setCb(void (*cb)(Poll *p, int status, int events)) {
126+
this->cb = cb;
127+
}
128+
129+
void (*getCb())(Poll *, int, int) {
130+
return cb;
131+
}
132+
133+
void reInit(Loop *loop, uv_os_sock_t fd) {
134+
delete socket;
135+
socket = new boost::asio::posix::stream_descriptor(*loop, fd);
136+
socket->non_blocking(true);
137+
}
138+
139+
void start(Loop *, Poll *self, int events) {
140+
if (events & UV_READABLE) {
141+
socket->async_read_some(boost::asio::null_buffers(), [self](boost::system::error_code ec, std::size_t) {
142+
if (ec != boost::asio::error::operation_aborted) {
143+
self->start(nullptr, self, UV_READABLE);
144+
self->cb(self, ec ? -1 : 0, UV_READABLE);
145+
}
146+
});
147+
}
148+
149+
if (events & UV_WRITABLE) {
150+
socket->async_write_some(boost::asio::null_buffers(), [self](boost::system::error_code ec, std::size_t) {
151+
if (ec != boost::asio::error::operation_aborted) {
152+
self->start(nullptr, self, UV_WRITABLE);
153+
self->cb(self, ec ? -1 : 0, UV_WRITABLE);
154+
}
155+
});
156+
}
157+
}
158+
159+
void change(Loop *, Poll *self, int events) {
160+
socket->cancel();
161+
start(nullptr, self, events);
162+
}
163+
164+
bool fastTransfer(Loop *loop, Loop *newLoop, int events) {
165+
return false;
166+
}
167+
168+
// todo: asio is thread safe, use it!
169+
bool threadSafeChange(Loop *loop, Poll *self, int events) {
170+
return false;
171+
}
172+
173+
void stop(Loop *) {
174+
socket->cancel();
175+
}
176+
177+
// this is not correct, but it works for now
178+
// think about transfer - should allow one to not delete
179+
// but in this case it doesn't matter at all
180+
void close(Loop *loop, void (*cb)(Poll *)) {
181+
socket->release();
182+
socket->get_io_service().post([cb, this]() {
183+
cb(this);
184+
});
185+
delete socket;
186+
socket = nullptr;
187+
}
188+
};
189+
190+
}
191+
192+
#endif // ASIO_H

extern/uWebSocket/include/Backend.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef BACKEND_H
2+
#define BACKEND_H
3+
4+
// Default to Epoll if nothing specified and on Linux
5+
// Default to Libuv if nothing specified and not on Linux
6+
#ifdef USE_ASIO
7+
#include "Asio.h"
8+
#elif !defined(__linux__) || defined(USE_LIBUV)
9+
#include "Libuv.h"
10+
#else
11+
#ifndef USE_EPOLL
12+
#define USE_EPOLL
13+
#endif
14+
#include "Epoll.h"
15+
#endif
16+
17+
#endif // BACKEND_H

0 commit comments

Comments
 (0)