Add multi_port to server-framework

This commit is contained in:
Vinnie Falco
2017-06-18 06:51:51 -07:00
parent f6e0e5ef47
commit a4a4121444
22 changed files with 937 additions and 301 deletions

View File

@@ -14,6 +14,7 @@
#if BEAST_USE_OPENSSL
#include "https_ports.hpp"
#include "multi_port.hpp"
#include "wss_ports.hpp"
#include "ssl_certificate.hpp"
#endif
@@ -79,11 +80,11 @@ main(
int ac,
char const* av[])
{
// Helper for reporting failures
//
using namespace framework;
using namespace beast::http;
// Helper for reporting failures
//
auto const fail =
[&](
std::string const& what,
@@ -141,17 +142,75 @@ main(
//--------------------------------------------------------------------------
//
// Asynchronous WebSocket HTTP
// Synchronous WebSocket HTTP
//
// port port + 1
// port + 0 port + 1
//
//--------------------------------------------------------------------------
{
// Install an asynchronous WebSocket echo port handler
// Create a WebSocket port
//
auto wsp = instance.make_port<ws_sync_port>(
ec,
endpoint_type{addr,static_cast<unsigned short>(port + 0)},
instance,
std::cout,
set_ws_options{pmd});
if(ec)
return fail("ws_sync_port", ec);
// Create an HTTP port
//
auto sp = instance.make_port<http_sync_port<
ws_upgrade_service<ws_sync_port>,
file_service
>>(
ec,
endpoint_type{addr,static_cast<unsigned short>(port + 1)},
instance,
std::cout);
if(ec)
return fail("http_sync_port", ec);
// Init the ws_upgrade_service to
// forward upgrades to the WebSocket port.
//
sp->template init<0>(
ec,
*wsp // The WebSocket port handler
);
if(ec)
return fail("http_sync_port/ws_upgrade_service", ec);
// Init the file_service to point to the root path.
//
sp->template init<1>(
ec,
root, // The root path
"http_sync_port" // The value for the Server field
);
if(ec)
return fail("http_sync_port/file_service", ec);
}
//--------------------------------------------------------------------------
//
// Asynchronous WebSocket HTTP
//
// port + 2 port + 3
//
//--------------------------------------------------------------------------
{
// Create a WebSocket port
//
auto wsp = instance.make_port<ws_async_port>(
ec,
endpoint_type{addr, port},
endpoint_type{addr,
static_cast<unsigned short>(port + 2)},
instance,
std::cout,
set_ws_options{pmd}
@@ -160,106 +219,47 @@ main(
if(ec)
return fail("ws_async_port", ec);
// Install an asynchronous HTTP port handler
// Create an HTTP port
//
auto sp = instance.make_port<http_async_port<
ws_upgrade_service<ws_async_port>,
file_service
>>(
ec,
endpoint_type{addr,
static_cast<unsigned short>(port + 1)},
instance,
std::cout);
if(ec)
return fail("http_async_port", ec);
// Set up the ws_upgrade_service. We will route upgrade
// requests to the websocket port handler created earlier.
//
sp->template init<0>(
ec,
wsp // The websocket port handler
);
if(ec)
return fail("http_async_port/ws_upgrade_service", ec);
// Set up the file_service to point to the root path.
//
sp->template init<1>(
ec,
root, // The root path
"http_async_port" // The value for the Server field
);
if(ec)
return fail("http_async_port/file_service", ec);
}
//--------------------------------------------------------------------------
//
// Synchronous WebSocket HTTP
//
// port + 2 port + 3
//
//--------------------------------------------------------------------------
{
// Install a synchronous WebSocket echo port handler
//
auto wsp = instance.make_port<ws_sync_port>(
ec,
endpoint_type{addr,
static_cast<unsigned short>(port + 2)},
instance,
std::cout,
set_ws_options{pmd});
if(ec)
return fail("ws_sync_port", ec);
// Install a synchronous HTTP port handler
//
auto sp = instance.make_port<http_sync_port<
ws_upgrade_service<ws_sync_port>,
file_service
>>(
ec,
endpoint_type{addr,
static_cast<unsigned short>(port + 3)},
instance,
std::cout);
if(ec)
return fail("http_sync_port", ec);
return fail("http_async_port", ec);
// Set up the ws_upgrade_service. We will route upgrade
// requests to the websocket port handler created earlier.
// Init the ws_upgrade_service to
// forward upgrades to the WebSocket port.
//
sp->template init<0>(
ec,
wsp
*wsp // The websocket port handler
);
if(ec)
return fail("http_sync_port/ws_upgrade_service", ec);
return fail("http_async_port/ws_upgrade_service", ec);
// Set up the file_service to point to the root path.
// Init the file_service to point to the root path.
//
sp->template init<1>(
ec,
root,
"http_sync_port"
root, // The root path
"http_async_port" // The value for the Server field
);
if(ec)
return fail("http_sync_port/file_service", ec);
return fail("http_async_port/file_service", ec);
}
//
// If OpenSSL is available then install some SSL-enabled ports
// The next section supports encrypted connections and requires
// an installed and configured OpenSSL as part of the build.
//
#if BEAST_USE_OPENSSL
@@ -268,31 +268,30 @@ main(
//--------------------------------------------------------------------------
//
// Asynchronous Secure WebSocket HTTPS
// Synchronous Secure WebSocket HTTPS
//
// port + 4 port + 5
//
//--------------------------------------------------------------------------
{
// Install an asynchronous Secure WebSocket echo port handler
// Create a WebSocket port
//
auto wsp = instance.make_port<wss_async_port>(
auto wsp = instance.make_port<wss_sync_port>(
ec,
endpoint_type{addr,
static_cast<unsigned short>(port + 4)},
instance,
std::cout,
cert.get(),
set_ws_options{pmd}
);
set_ws_options{pmd});
if(ec)
return fail("ws_async_port", ec);
return fail("wss_sync_port", ec);
// Install an asynchronous HTTPS port handler
// Create an HTTP port
//
auto sp = instance.make_port<https_async_port<
ws_upgrade_service<wss_async_port>,
auto sp = instance.make_port<https_sync_port<
ws_upgrade_service<wss_sync_port>,
file_service
>>(
ec,
@@ -303,20 +302,82 @@ main(
cert.get());
if(ec)
return fail("https_async_port", ec);
return fail("https_sync_port", ec);
// Set up the ws_upgrade_service. We will route upgrade
// requests to the websocket port handler created earlier.
// Init the ws_upgrade_service to
// forward upgrades to the WebSocket port.
//
sp->template init<0>(
ec,
wsp // The websocket port handler
*wsp // The websocket port handler
);
if(ec)
return fail("http_sync_port/ws_upgrade_service", ec);
// Init the file_service to point to the root path.
//
sp->template init<1>(
ec,
root, // The root path
"http_sync_port" // The value for the Server field
);
if(ec)
return fail("https_sync_port/file_service", ec);
}
//--------------------------------------------------------------------------
//
// Asynchronous Secure WebSocket HTTPS
//
// port + 6 port + 7
//
//--------------------------------------------------------------------------
{
// Create a WebSocket port
//
auto wsp = instance.make_port<wss_async_port>(
ec,
endpoint_type{addr,
static_cast<unsigned short>(port + 6)},
instance,
std::cout,
cert.get(),
set_ws_options{pmd}
);
if(ec)
return fail("ws_async_port", ec);
// Create an HTTP port
//
auto sp = instance.make_port<https_async_port<
ws_upgrade_service<wss_async_port>,
file_service
>>(
ec,
endpoint_type{addr,
static_cast<unsigned short>(port + 7)},
instance,
std::cout,
cert.get());
if(ec)
return fail("https_async_port", ec);
// Init the ws_upgrade_service to
// forward upgrades to the WebSocket port.
//
sp->template init<0>(
ec,
*wsp // The websocket port handler
);
if(ec)
return fail("https_async_port/ws_upgrade_service", ec);
// Set up the file_service to point to the root path.
// Init the file_service to point to the root path.
//
sp->template init<1>(
ec,
@@ -330,63 +391,53 @@ main(
//--------------------------------------------------------------------------
//
// Synchronous Secure WebSocket HTTPS
// Multi-Port HTTP, WebSockets,
// HTTPS Secure WebSockets
//
// port + 6 port + 7
// Asynchronous, all on the same port!
//
// port + 8
//
//--------------------------------------------------------------------------
{
// Install a synchronous Secure WebSocket echo port handler
// Create a multi_port
//
auto wsp = instance.make_port<wss_sync_port>(
auto sp = instance.make_port<multi_port<
ws_upgrade_service<multi_port_base>,
file_service
>>(
ec,
endpoint_type{addr,
static_cast<unsigned short>(port + 6)},
static_cast<unsigned short>(port + 8)},
instance,
std::cout,
cert.get(),
set_ws_options{pmd});
if(ec)
return fail("wss_sync_port", ec);
return fail("multi_port", ec);
// Install a synchronous HTTPS port handler
//
auto sp = instance.make_port<https_sync_port<
ws_upgrade_service<wss_sync_port>,
file_service
>>(
ec,
endpoint_type{addr,
static_cast<unsigned short>(port + 7)},
instance,
std::cout,
cert.get());
if(ec)
return fail("https_sync_port", ec);
// Set up the ws_upgrade_service. We will route upgrade
// requests to the websocket port handler created earlier.
// Init the ws_upgrade_service to forward requests to the multi_port.
//
sp->template init<0>(
ec,
wsp // The websocket port handler
*sp // The websocket port handler
);
if(ec)
return fail("http_sync_port/ws_upgrade_service", ec);
return fail("multi_port/ws_upgrade_service", ec);
// Set up the file_service to point to the root path.
// Init the ws_upgrade_service to
// forward upgrades to the Multi port.
//
sp->template init<1>(
ec,
root,
"https_sync_port"
root, // The root path
"multi_port" // The value for the Server field
);
if(ec)
return fail("https_sync_port/file_service", ec);
return fail("multi_port/file_service", ec);
}
#endif