Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unix-Socket Configuration #1013

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions websocketpp/config/asio_unix_no_tls.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

#ifndef WEBSOCKETPP_CONFIG_ASIO_UNIX_HPP
#define WEBSOCKETPP_CONFIG_ASIO_UNIX_HPP

#include <websocketpp/config/core.hpp>
#include <websocketpp/transport/asio/endpoint.hpp>

namespace websocketpp {
namespace config {

/// Server config with asio transport and TLS disabled
struct asio_unix : public core {
typedef asio_unix type;
typedef core base;

typedef base::concurrency_type concurrency_type;

typedef base::request_type request_type;
typedef base::response_type response_type;

typedef base::message_type message_type;
typedef base::con_msg_manager_type con_msg_manager_type;
typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;

typedef base::alog_type alog_type;
typedef base::elog_type elog_type;

typedef base::rng_type rng_type;

struct transport_config : public base::transport_config {
typedef type::concurrency_type concurrency_type;
typedef type::alog_type alog_type;
typedef type::elog_type elog_type;
typedef type::request_type request_type;
typedef type::response_type response_type;
typedef websocketpp::transport::asio::basic_socket::generic::endpoint<lib::asio::local::stream_protocol>
socket_type;
};

typedef websocketpp::transport::asio::endpoint<transport_config>
transport_type;
};

} // namespace config
} // namespace websocketpp

#endif // WEBSOCKETPP_CONFIG_ASIO_UNIX_HPP
56 changes: 35 additions & 21 deletions websocketpp/transport/asio/endpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ class endpoint : public config::socket_type {

/// Type of a pointer to the ASIO io_service being used
typedef lib::asio::io_service * io_service_ptr;

typedef typename config::socket_type::socket_con_type::socket_suite socket_suite;

/// Type of a shared pointer to the acceptor being used
typedef lib::shared_ptr<lib::asio::ip::tcp::acceptor> acceptor_ptr;
typedef lib::shared_ptr<typename socket_suite::acceptor> acceptor_ptr;
/// Type of a shared pointer to the resolver being used
typedef lib::shared_ptr<lib::asio::ip::tcp::resolver> resolver_ptr;
/// Type of timer handle
Expand Down Expand Up @@ -161,7 +164,7 @@ class endpoint : public config::socket_type {
rhs.m_acceptor = NULL;
rhs.m_listen_backlog = lib::asio::socket_base::max_connections;
rhs.m_state = UNINITIALIZED;

// TODO: this needs to be updated
}
return *this;
Expand Down Expand Up @@ -195,7 +198,7 @@ class endpoint : public config::socket_type {

m_io_service = ptr;
m_external_io_service = true;
m_acceptor.reset(new lib::asio::ip::tcp::acceptor(*m_io_service));
m_acceptor.reset(new typename socket_suite::acceptor(*m_io_service));

m_state = READY;
ec = lib::error_code();
Expand Down Expand Up @@ -225,7 +228,7 @@ class endpoint : public config::socket_type {
* @param ec Set to indicate what error occurred, if any.
*/
void init_asio(lib::error_code & ec) {
// Use a smart pointer until the call is successful and ownership has
// Use a smart pointer until the call is successful and ownership has
// successfully been taken. Use unique_ptr when available.
// TODO: remove the use of auto_ptr when C++98/03 support is no longer
// necessary.
Expand All @@ -247,7 +250,7 @@ class endpoint : public config::socket_type {
* @see init_asio(io_service_ptr ptr)
*/
void init_asio() {
// Use a smart pointer until the call is successful and ownership has
// Use a smart pointer until the call is successful and ownership has
// successfully been taken. Use unique_ptr when available.
// TODO: remove the use of auto_ptr when C++98/03 support is no longer
// necessary.
Expand Down Expand Up @@ -378,26 +381,26 @@ class endpoint : public config::socket_type {
lib::asio::io_service & get_io_service() {
return *m_io_service;
}

/// Get local TCP endpoint
/**
* Extracts the local endpoint from the acceptor. This represents the
* address that WebSocket++ is listening on.
*
* Sets a bad_descriptor error if the acceptor is not currently listening
* or otherwise unavailable.
*
*
* @since 0.7.0
*
* @param ec Set to indicate what error occurred, if any.
* @return The local endpoint
*/
lib::asio::ip::tcp::endpoint get_local_endpoint(lib::asio::error_code & ec) {
typename socket_suite::endpoint get_local_endpoint(lib::asio::error_code & ec) {
if (m_acceptor) {
return m_acceptor->local_endpoint(ec);
} else {
ec = lib::asio::error::make_error_code(lib::asio::error::bad_descriptor);
return lib::asio::ip::tcp::endpoint();
return socket_suite::endpoint();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return socket_suite::endpoint();
return typename socket_suite::endpoint();

}
}

Expand All @@ -409,7 +412,7 @@ class endpoint : public config::socket_type {
* @param ep An endpoint to read settings from
* @param ec Set to indicate what error occurred, if any.
*/
void listen(lib::asio::ip::tcp::endpoint const & ep, lib::error_code & ec)
void listen(typename socket_suite::endpoint const & ep, lib::error_code & ec)
{
if (m_state != READY) {
m_elog->write(log::elevel::library,
Expand All @@ -425,10 +428,10 @@ class endpoint : public config::socket_type {

m_acceptor->open(ep.protocol(),bec);
if (bec) {ec = clean_up_listen_after_error(bec);return;}

m_acceptor->set_option(lib::asio::socket_base::reuse_address(m_reuse_addr),bec);
if (bec) {ec = clean_up_listen_after_error(bec);return;}

// if a TCP pre-bind handler is present, run it
if (m_tcp_pre_bind_handler) {
ec = m_tcp_pre_bind_handler(m_acceptor);
Expand All @@ -437,13 +440,13 @@ class endpoint : public config::socket_type {
return;
}
}

m_acceptor->bind(ep,bec);
if (bec) {ec = clean_up_listen_after_error(bec);return;}

m_acceptor->listen(m_listen_backlog,bec);
if (bec) {ec = clean_up_listen_after_error(bec);return;}

// Success
m_state = LISTENING;
ec = lib::error_code();
Expand All @@ -457,7 +460,7 @@ class endpoint : public config::socket_type {
*
* @param ep An endpoint to read settings from
*/
void listen(lib::asio::ip::tcp::endpoint const & ep) {
void listen(typename socket_suite::endpoint const & ep) {
lib::error_code ec;
listen(ep,ec);
if (ec) { throw exception(ec); }
Expand Down Expand Up @@ -498,11 +501,20 @@ class endpoint : public config::socket_type {
* @param internet_protocol The internet protocol to use.
* @param port The port to listen on.
*/
template <typename InternetProtocol>
void listen(InternetProtocol const & internet_protocol, uint16_t port)
{
lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
template <typename InternetProtocol, typename SockSuite = socket_suite>
typename std::enable_if<std::is_same<SockSuite, lib::asio::ip::tcp>::value, void>::type
listen(InternetProtocol const &internet_protocol, uint16_t port) {
typename lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
listen(ep);
}

template <typename SockSuite = socket_suite>
typename std::enable_if<std::is_same<SockSuite, lib::asio::local::stream_protocol>::value, void>::type
listen(const char* path) {
::unlink(path);
lib::asio::local::stream_protocol::endpoint ep(path);
listen(ep);
chmod(path, S_IRWXG ^ S_IRWXU ^ S_IRWXO);
}

/// Set up endpoint for listening on a port (exception free)
Expand Down Expand Up @@ -553,7 +565,9 @@ class endpoint : public config::socket_type {
* descriptive name or a numeric string corresponding to a port number.
* @param ec Set to indicate what error occurred, if any.
*/
void listen(std::string const & host, std::string const & service,
template <typename SockSuite = socket_suite>
typename std::enable_if<std::is_same<SockSuite, lib::asio::ip::tcp>::value, void>::type
listen(std::string const & host, std::string const & service,
lib::error_code & ec)
{
using lib::asio::ip::tcp;
Expand Down
Loading