Skip to content

Commit

Permalink
Improve backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Oct 21, 2024
1 parent 600e7fe commit c3051f9
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 18 deletions.
17 changes: 10 additions & 7 deletions src/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace uWS {
static_assert(sizeof(struct us_socket_context_options_t) == sizeof(SocketContextOptions), "Mismatching uSockets/uWebSockets ABI");

template <bool SSL, typename BuilderPatternReturnType>
struct TemplatedApp {
struct TemplatedAppBase {
private:
/* The app always owns at least one http context, but creates websocket contexts on demand */
HttpContext<SSL> *httpContext;
Expand Down Expand Up @@ -176,7 +176,7 @@ struct TemplatedApp {
return 0;
}

~TemplatedApp() {
~TemplatedAppBase() {
/* Let's just put everything here */
if (httpContext) {
httpContext->free();
Expand All @@ -199,9 +199,9 @@ struct TemplatedApp {
}

/* Disallow copying, only move */
TemplatedApp(const TemplatedApp &other) = delete;
TemplatedAppBase(const TemplatedAppBase &other) = delete;

TemplatedApp(TemplatedApp &&other) {
TemplatedAppBase(TemplatedAppBase &&other) {
/* Move HttpContext */
httpContext = other.httpContext;
other.httpContext = nullptr;
Expand All @@ -216,7 +216,7 @@ struct TemplatedApp {
other.topicTree = nullptr;
}

TemplatedApp(SocketContextOptions options = {}) {
TemplatedAppBase(SocketContextOptions options = {}) {
httpContext = HttpContext<SSL>::create(Loop::get(), options);

/* Register default handler for 404 (can be overridden by user) */
Expand Down Expand Up @@ -649,8 +649,11 @@ struct TemplatedApp {
#include "CachingApp.h"

namespace uWS {
typedef uWS::CachingApp<false> App;
typedef uWS::CachingApp<true> SSLApp;
template <bool SSL>
using TemplatedApp = uWS::TemplatedAppBase<SSL, uWS::CachingApp<SSL>>;

typedef uWS::TemplatedApp<false> App;
typedef uWS::TemplatedApp<true> SSLApp;
}

#endif // UWS_APP_H
2 changes: 1 addition & 1 deletion src/AsyncSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct AsyncSocket {
/* This guy is promiscuous */
template <bool> friend struct HttpContext;
template <bool, bool, typename> friend struct WebSocketContext;
template <bool, typename> friend struct TemplatedApp;
template <bool, typename> friend struct TemplatedAppBase;
template <bool, typename> friend struct WebSocketContextData;
template <typename, typename> friend struct TopicTree;
template <bool> friend struct HttpResponse;
Expand Down
12 changes: 7 additions & 5 deletions src/CachingApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class CachingHttpResponse {
res->end(buffer);

created = time(0);

std::ignore = closeConnection;
}

public:
Expand All @@ -55,14 +57,14 @@ typedef std::unordered_map<std::string_view, CachingHttpResponse *,

// we can also derive from H3app later on
template <bool SSL>
struct CachingApp : public uWS::TemplatedApp<SSL, CachingApp<SSL>> {
struct CachingApp : public uWS::TemplatedAppBase<SSL, CachingApp<SSL>> {
public:
CachingApp(SocketContextOptions options = {}) : uWS::TemplatedApp<SSL, CachingApp<SSL>>(options) {}
CachingApp(SocketContextOptions options = {}) : uWS::TemplatedAppBase<SSL, CachingApp<SSL>>(options) {}

using uWS::TemplatedApp<SSL, CachingApp<SSL>>::get;
using uWS::TemplatedAppBase<SSL, CachingApp<SSL>>::get;

CachingApp(const CachingApp &other) = delete;
CachingApp(CachingApp<SSL> &&other) : uWS::TemplatedApp<SSL, CachingApp<SSL>>(std::move(other)) {
CachingApp(CachingApp<SSL> &&other) : uWS::TemplatedAppBase<SSL, CachingApp<SSL>>(std::move(other)) {
// also move the cache
}

Expand All @@ -72,7 +74,7 @@ struct CachingApp : public uWS::TemplatedApp<SSL, CachingApp<SSL>> {

// variant 1: only taking URL into account
CachingApp &&get(const std::string& url, uWS::MoveOnlyFunction<void(CachingHttpResponse*, uWS::HttpRequest*)> &&handler, unsigned int secondsToExpiry) {
((uWS::TemplatedApp<SSL, CachingApp<SSL>> *)this)->get(url, [this, handler = std::move(handler), secondsToExpiry](auto* res, auto* req) mutable {
((uWS::TemplatedAppBase<SSL, CachingApp<SSL>> *)this)->get(url, [this, handler = std::move(handler), secondsToExpiry](auto* res, auto* req) mutable {
/* We need to know the cache key and the time of now */
std::string_view cache_key = req->getFullUrl();
time_t now = static_cast<LoopData *>(us_loop_ext((us_loop_t *)uWS::Loop::get()))->cacheTimepoint;
Expand Down
2 changes: 1 addition & 1 deletion src/HttpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ template<bool> struct HttpResponse;

template <bool SSL>
struct HttpContext {
template<bool, typename> friend struct TemplatedApp;
template<bool, typename> friend struct TemplatedAppBase;
template<bool> friend struct HttpResponse;
private:
HttpContext() = delete;
Expand Down
2 changes: 1 addition & 1 deletion src/HttpContextData.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ template <bool SSL>
struct alignas(16) HttpContextData {
template <bool> friend struct HttpContext;
template <bool> friend struct HttpResponse;
template <bool, typename> friend struct TemplatedApp;
template <bool, typename> friend struct TemplatedAppBase;
private:
std::vector<MoveOnlyFunction<void(HttpResponse<SSL> *, int)>> filterHandlers;

Expand Down
2 changes: 1 addition & 1 deletion src/HttpResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static const int HTTP_TIMEOUT_S = 10;
template <bool SSL>
struct HttpResponse : public AsyncSocket<SSL> {
/* Solely used for getHttpResponseData() */
template <bool, typename> friend struct TemplatedApp;
template <bool, typename> friend struct TemplatedAppBase;
typedef AsyncSocket<SSL> Super;
private:
HttpResponseData<SSL> *getHttpResponseData() {
Expand Down
2 changes: 2 additions & 0 deletions src/LocalCluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ struct LocalCluster {

app->preOpen([](struct us_socket_context_t *context, LIBUS_SOCKET_DESCRIPTOR fd) -> LIBUS_SOCKET_DESCRIPTOR {

std::ignore = context;

/* Distribute this socket in round robin fashion */
//std::cout << "About to load balance " << fd << " to " << roundRobin << std::endl;

Expand Down
2 changes: 1 addition & 1 deletion src/WebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace uWS {

template <bool SSL, bool isServer, typename USERDATA>
struct WebSocket : AsyncSocket<SSL> {
template <bool, typename> friend struct TemplatedApp;
template <bool, typename> friend struct TemplatedAppBase;
template <bool> friend struct HttpResponse;
private:
typedef AsyncSocket<SSL> Super;
Expand Down
2 changes: 1 addition & 1 deletion src/WebSocketContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace uWS {

template <bool SSL, bool isServer, typename USERDATA>
struct WebSocketContext {
template <bool, typename> friend struct TemplatedApp;
template <bool, typename> friend struct TemplatedAppBase;
template <bool, typename> friend struct WebSocketProtocol;
private:
WebSocketContext() = delete;
Expand Down

0 comments on commit c3051f9

Please sign in to comment.