@@ -79,7 +79,7 @@ namespace uWS {
79
79
80
80
static_assert (sizeof (struct us_socket_context_options_t ) == sizeof (SocketContextOptions), " Mismatching uSockets/uWebSockets ABI" );
81
81
82
- template <bool SSL>
82
+ template <bool SSL, typename BuilderPatternReturnType >
83
83
struct TemplatedApp {
84
84
private:
85
85
/* The app always owns at least one http context, but creates websocket contexts on demand */
@@ -94,7 +94,7 @@ struct TemplatedApp {
94
94
TopicTree<TopicTreeMessage, TopicTreeBigMessage> *topicTree = nullptr ;
95
95
96
96
/* Server name */
97
- TemplatedApp &&addServerName(std::string hostname_pattern, SocketContextOptions options = {}) {
97
+ BuilderPatternReturnType &&addServerName(std::string hostname_pattern, SocketContextOptions options = {}) {
98
98
99
99
/* Do nothing if not even on SSL */
100
100
if constexpr (SSL) {
@@ -104,10 +104,10 @@ struct TemplatedApp {
104
104
us_socket_context_add_server_name (SSL, (struct us_socket_context_t *) httpContext, hostname_pattern.c_str (), options, domainRouter);
105
105
}
106
106
107
- return std::move (*this );
107
+ return std::move (static_cast <BuilderPatternReturnType &&>( *this ) );
108
108
}
109
109
110
- TemplatedApp &&removeServerName(std::string hostname_pattern) {
110
+ BuilderPatternReturnType &&removeServerName(std::string hostname_pattern) {
111
111
112
112
/* This will do for now, would be better if us_socket_context_remove_server_name returned the user data */
113
113
auto *domainRouter = us_socket_context_find_server_name_userdata (SSL, (struct us_socket_context_t *) httpContext, hostname_pattern.c_str ());
@@ -119,7 +119,7 @@ struct TemplatedApp {
119
119
return std::move (*this );
120
120
}
121
121
122
- TemplatedApp &&missingServerName(MoveOnlyFunction<void (const char *hostname)> handler) {
122
+ BuilderPatternReturnType &&missingServerName(MoveOnlyFunction<void (const char *hostname)> handler) {
123
123
124
124
if (!constructorFailed ()) {
125
125
httpContext->getSocketContextData ()->missingServerNameHandler = std::move (handler);
@@ -141,7 +141,7 @@ struct TemplatedApp {
141
141
}
142
142
143
143
/* Attaches a "filter" function to track socket connections/disconnections */
144
- TemplatedApp &&filter(MoveOnlyFunction<void (HttpResponse<SSL> *, int )> &&filterHandler) {
144
+ BuilderPatternReturnType &&filter(MoveOnlyFunction<void (HttpResponse<SSL> *, int )> &&filterHandler) {
145
145
httpContext->filter (std::move (filterHandler));
146
146
147
147
return std::move (*this );
@@ -259,7 +259,7 @@ struct TemplatedApp {
259
259
};
260
260
261
261
/* Closes all sockets including listen sockets. */
262
- TemplatedApp &&close() {
262
+ BuilderPatternReturnType &&close() {
263
263
us_socket_context_close (SSL, (struct us_socket_context_t *) httpContext);
264
264
for (void *webSocketContext : webSocketContexts) {
265
265
us_socket_context_close (SSL, (struct us_socket_context_t *) webSocketContext);
@@ -269,13 +269,13 @@ struct TemplatedApp {
269
269
}
270
270
271
271
template <typename UserData>
272
- TemplatedApp &&ws(std::string pattern, WebSocketBehavior<UserData> &&behavior) {
272
+ BuilderPatternReturnType &&ws(std::string pattern, WebSocketBehavior<UserData> &&behavior) {
273
273
/* Don't compile if alignment rules cannot be satisfied */
274
274
static_assert (alignof (UserData) <= LIBUS_EXT_ALIGNMENT,
275
275
" µWebSockets cannot satisfy UserData alignment requirements. You need to recompile µSockets with LIBUS_EXT_ALIGNMENT adjusted accordingly." );
276
276
277
277
if (!httpContext) {
278
- return std::move (*this );
278
+ return std::move (static_cast <BuilderPatternReturnType &&>( *this ) );
279
279
}
280
280
281
281
/* Terminate on misleading idleTimeout values */
@@ -438,11 +438,11 @@ struct TemplatedApp {
438
438
req->setYield (true );
439
439
}
440
440
}, true );
441
- return std::move(*this );
441
+ return std::move(static_cast <BuilderPatternReturnType &&>( *this ) );
442
442
}
443
443
444
444
/* Browse to a server name, changing the router to this domain */
445
- TemplatedApp &&domain(std::string serverName) {
445
+ BuilderPatternReturnType &&domain(std::string serverName) {
446
446
HttpContextData<SSL> *httpContextData = httpContext->getSocketContextData ();
447
447
448
448
void *domainRouter = us_socket_context_find_server_name_userdata (SSL, (struct us_socket_context_t *) httpContext, serverName.c_str ());
@@ -454,82 +454,82 @@ struct TemplatedApp {
454
454
httpContextData->currentRouter = &httpContextData->router ;
455
455
}
456
456
457
- return std::move (*this );
457
+ return std::move (static_cast <BuilderPatternReturnType &&>( *this ) );
458
458
}
459
459
460
- TemplatedApp &&get(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
460
+ BuilderPatternReturnType &&get(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
461
461
if (httpContext) {
462
462
httpContext->onHttp (" GET" , pattern, std::move (handler));
463
463
}
464
- return std::move (*this );
464
+ return std::move (static_cast <BuilderPatternReturnType &&>( *this ) );
465
465
}
466
466
467
- TemplatedApp &&post(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
467
+ BuilderPatternReturnType &&post(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
468
468
if (httpContext) {
469
469
httpContext->onHttp (" POST" , pattern, std::move (handler));
470
470
}
471
- return std::move (*this );
471
+ return std::move (static_cast <BuilderPatternReturnType &&>( *this ) );
472
472
}
473
473
474
- TemplatedApp &&options(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
474
+ BuilderPatternReturnType &&options(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
475
475
if (httpContext) {
476
476
httpContext->onHttp (" OPTIONS" , pattern, std::move (handler));
477
477
}
478
478
return std::move (*this );
479
479
}
480
480
481
- TemplatedApp &&del(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
481
+ BuilderPatternReturnType &&del(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
482
482
if (httpContext) {
483
483
httpContext->onHttp (" DELETE" , pattern, std::move (handler));
484
484
}
485
485
return std::move (*this );
486
486
}
487
487
488
- TemplatedApp &&patch(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
488
+ BuilderPatternReturnType &&patch(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
489
489
if (httpContext) {
490
490
httpContext->onHttp (" PATCH" , pattern, std::move (handler));
491
491
}
492
492
return std::move (*this );
493
493
}
494
494
495
- TemplatedApp &&put(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
495
+ BuilderPatternReturnType &&put(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
496
496
if (httpContext) {
497
497
httpContext->onHttp (" PUT" , pattern, std::move (handler));
498
498
}
499
499
return std::move (*this );
500
500
}
501
501
502
- TemplatedApp &&head(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
502
+ BuilderPatternReturnType &&head(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
503
503
if (httpContext) {
504
504
httpContext->onHttp (" HEAD" , pattern, std::move (handler));
505
505
}
506
506
return std::move (*this );
507
507
}
508
508
509
- TemplatedApp &&connect(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
509
+ BuilderPatternReturnType &&connect(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
510
510
if (httpContext) {
511
511
httpContext->onHttp (" CONNECT" , pattern, std::move (handler));
512
512
}
513
513
return std::move (*this );
514
514
}
515
515
516
- TemplatedApp &&trace(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
516
+ BuilderPatternReturnType &&trace(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
517
517
if (httpContext) {
518
518
httpContext->onHttp (" TRACE" , pattern, std::move (handler));
519
519
}
520
520
return std::move (*this );
521
521
}
522
522
523
523
/* This one catches any method */
524
- TemplatedApp &&any(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
524
+ BuilderPatternReturnType &&any(std::string pattern, MoveOnlyFunction<void (HttpResponse<SSL> *, HttpRequest *)> &&handler) {
525
525
if (httpContext) {
526
526
httpContext->onHttp (" *" , pattern, std::move (handler));
527
527
}
528
- return std::move (*this );
528
+ return std::move (static_cast <BuilderPatternReturnType &&>( *this ) );
529
529
}
530
530
531
531
/* Host, port, callback */
532
- TemplatedApp &&listen(std::string host, int port, MoveOnlyFunction<void (us_listen_socket_t *)> &&handler) {
532
+ BuilderPatternReturnType &&listen(std::string host, int port, MoveOnlyFunction<void (us_listen_socket_t *)> &&handler) {
533
533
if (!host.length ()) {
534
534
return listen (port, std::move (handler));
535
535
}
@@ -538,7 +538,7 @@ struct TemplatedApp {
538
538
}
539
539
540
540
/* Host, port, options, callback */
541
- TemplatedApp &&listen(std::string host, int port, int options, MoveOnlyFunction<void (us_listen_socket_t *)> &&handler) {
541
+ BuilderPatternReturnType &&listen(std::string host, int port, int options, MoveOnlyFunction<void (us_listen_socket_t *)> &&handler) {
542
542
if (!host.length ()) {
543
543
return listen (port, options, std::move (handler));
544
544
}
@@ -547,44 +547,44 @@ struct TemplatedApp {
547
547
}
548
548
549
549
/* Port, callback */
550
- TemplatedApp &&listen(int port, MoveOnlyFunction<void (us_listen_socket_t *)> &&handler) {
550
+ BuilderPatternReturnType &&listen(int port, MoveOnlyFunction<void (us_listen_socket_t *)> &&handler) {
551
551
handler (httpContext ? httpContext->listen (nullptr , port, 0 ) : nullptr );
552
- return std::move (*this );
552
+ return std::move (static_cast <BuilderPatternReturnType &&>( *this ) );
553
553
}
554
554
555
555
/* Port, options, callback */
556
- TemplatedApp &&listen(int port, int options, MoveOnlyFunction<void (us_listen_socket_t *)> &&handler) {
556
+ BuilderPatternReturnType &&listen(int port, int options, MoveOnlyFunction<void (us_listen_socket_t *)> &&handler) {
557
557
handler (httpContext ? httpContext->listen (nullptr , port, options) : nullptr );
558
558
return std::move (*this );
559
559
}
560
560
561
561
/* options, callback, path to unix domain socket */
562
- TemplatedApp &&listen(int options, MoveOnlyFunction<void (us_listen_socket_t *)> &&handler, std::string path) {
562
+ BuilderPatternReturnType &&listen(int options, MoveOnlyFunction<void (us_listen_socket_t *)> &&handler, std::string path) {
563
563
handler (httpContext ? httpContext->listen (path.c_str (), options) : nullptr );
564
564
return std::move (*this );
565
565
}
566
566
567
567
/* callback, path to unix domain socket */
568
- TemplatedApp &&listen(MoveOnlyFunction<void (us_listen_socket_t *)> &&handler, std::string path) {
568
+ BuilderPatternReturnType &&listen(MoveOnlyFunction<void (us_listen_socket_t *)> &&handler, std::string path) {
569
569
handler (httpContext ? httpContext->listen (path.c_str (), 0 ) : nullptr );
570
570
return std::move (*this );
571
571
}
572
572
573
573
/* Register event handler for accepted FD. Can be used together with adoptSocket. */
574
- TemplatedApp &&preOpen(LIBUS_SOCKET_DESCRIPTOR (*handler)(LIBUS_SOCKET_DESCRIPTOR)) {
574
+ BuilderPatternReturnType &&preOpen(LIBUS_SOCKET_DESCRIPTOR (*handler)(LIBUS_SOCKET_DESCRIPTOR)) {
575
575
httpContext->onPreOpen (handler);
576
- return std::move (*this );
576
+ return std::move (static_cast <BuilderPatternReturnType &&>( *this ) );
577
577
}
578
578
579
579
/* adopt an externally accepted socket */
580
- TemplatedApp &&adoptSocket(LIBUS_SOCKET_DESCRIPTOR accepted_fd) {
580
+ BuilderPatternReturnType &&adoptSocket(LIBUS_SOCKET_DESCRIPTOR accepted_fd) {
581
581
httpContext->adoptAcceptedSocket (accepted_fd);
582
- return std::move (*this );
582
+ return std::move (static_cast <BuilderPatternReturnType &&>( *this ) );
583
583
}
584
584
585
- TemplatedApp &&run() {
585
+ BuilderPatternReturnType &&run() {
586
586
uWS::run ();
587
- return std::move (*this );
587
+ return std::move (static_cast <BuilderPatternReturnType &&>( *this ) );
588
588
}
589
589
590
590
Loop *getLoop () {
@@ -593,9 +593,13 @@ struct TemplatedApp {
593
593
594
594
};
595
595
596
- typedef TemplatedApp<false > App;
597
- typedef TemplatedApp<true > SSLApp;
596
+ }
597
+
598
+ #include " CachingApp.h"
598
599
600
+ namespace uWS {
601
+ typedef uWS::CachingApp App;
602
+ typedef uWS::CachingApp SSLApp;
599
603
}
600
604
601
605
#endif // UWS_APP_H
0 commit comments