Fix up getUserData
This commit is contained in:
+8
-5
@@ -6,6 +6,7 @@
|
|||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
struct PerSocketData {
|
struct PerSocketData {
|
||||||
|
char pad[256];
|
||||||
int hello;
|
int hello;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -14,7 +15,7 @@ int main(int argc, char **argv) {
|
|||||||
const char *passphrase;
|
const char *passphrase;
|
||||||
const char *dh_params_file_name;*/
|
const char *dh_params_file_name;*/
|
||||||
|
|
||||||
uWS::App(/*SSLApp({
|
uWS::/*SSL*/App(/*{
|
||||||
"/home/alexhultman/key.pem",
|
"/home/alexhultman/key.pem",
|
||||||
"/home/alexhultman/cert.pem",
|
"/home/alexhultman/cert.pem",
|
||||||
"1234"
|
"1234"
|
||||||
@@ -28,11 +29,13 @@ int main(int argc, char **argv) {
|
|||||||
.open = [](auto *ws, auto *req) {
|
.open = [](auto *ws, auto *req) {
|
||||||
std::cout << "WebSocket connected" << std::endl;
|
std::cout << "WebSocket connected" << std::endl;
|
||||||
/* Access per socket data */
|
/* Access per socket data */
|
||||||
/*PerSocketData *perSocketData = *///ws->getUserData();
|
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
||||||
/*perSocketData->hello = 13;*/
|
perSocketData->hello = 13;
|
||||||
},
|
},
|
||||||
.message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
|
.message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
|
||||||
ws->send(message, opCode, true);
|
ws->send(message, opCode, true);
|
||||||
|
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
||||||
|
std::cout << "OK per socket data: " << (perSocketData->hello == 13) << std::endl;
|
||||||
},
|
},
|
||||||
.drain = [](auto *ws) {
|
.drain = [](auto *ws) {
|
||||||
std::cout << "Drainage: " << ws->getBufferedAmount() << std::endl;
|
std::cout << "Drainage: " << ws->getBufferedAmount() << std::endl;
|
||||||
@@ -46,8 +49,8 @@ int main(int argc, char **argv) {
|
|||||||
.close = [](auto *ws, int code, std::string_view message) {
|
.close = [](auto *ws, int code, std::string_view message) {
|
||||||
std::cout << "WebSocket disconnected: " << code << "[" << message << "]" << std::endl;
|
std::cout << "WebSocket disconnected: " << code << "[" << message << "]" << std::endl;
|
||||||
/* Access per socket data */
|
/* Access per socket data */
|
||||||
//PerSocketData *perSocketData = ws->getUserData<PerSocketData>();
|
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
||||||
//std::cout << "OK per socket data: " << (perSocketData->hello == 13) << std::endl;
|
std::cout << "OK per socket data: " << (perSocketData->hello == 13) << std::endl;
|
||||||
}
|
}
|
||||||
}).listen(9001, [](auto *token) {
|
}).listen(9001, [](auto *token) {
|
||||||
if (token) {
|
if (token) {
|
||||||
|
|||||||
@@ -149,7 +149,7 @@ public:
|
|||||||
|
|
||||||
/* Adopting a socket invalidates it, do not rely on it directly to carry any data */
|
/* Adopting a socket invalidates it, do not rely on it directly to carry any data */
|
||||||
WebSocket<SSL, true> *webSocket = (WebSocket<SSL, true> *) StaticDispatch<SSL>::static_dispatch(us_ssl_socket_context_adopt_socket, us_socket_context_adopt_socket)(
|
WebSocket<SSL, true> *webSocket = (WebSocket<SSL, true> *) StaticDispatch<SSL>::static_dispatch(us_ssl_socket_context_adopt_socket, us_socket_context_adopt_socket)(
|
||||||
(typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE *) webSocketContext, (typename StaticDispatch<SSL>::SOCKET_TYPE *) res, sizeof(WebSocketData));
|
(typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE *) webSocketContext, (typename StaticDispatch<SSL>::SOCKET_TYPE *) res, sizeof(WebSocketData) + sizeof(UserData));
|
||||||
|
|
||||||
/* Update corked socket in case we got a new one (assuming we always are corked in handlers). */
|
/* Update corked socket in case we got a new one (assuming we always are corked in handlers). */
|
||||||
webSocket->cork();
|
webSocket->cork();
|
||||||
|
|||||||
+2
-3
@@ -40,11 +40,10 @@ private:
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
/* Returns pointer to the per socket user data */
|
/* Returns pointer to the per socket user data */
|
||||||
//template <typename K>
|
|
||||||
void *getUserData() {
|
void *getUserData() {
|
||||||
WebSocketData *webSocketData = (WebSocketData *) static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) this);
|
WebSocketData *webSocketData = (WebSocketData *) static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) this);
|
||||||
|
/* We just have it overallocated by sizeof type */
|
||||||
return nullptr;
|
return (webSocketData + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* See AsyncSocket */
|
/* See AsyncSocket */
|
||||||
|
|||||||
Reference in New Issue
Block a user