Skip to content

Allow user to set socket flags for server creation #13

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

Merged
merged 1 commit into from
Aug 29, 2017
Merged
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
14 changes: 7 additions & 7 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class Server {
void set_error_handler(Handler handler);
void set_logger(Logger logger);

bool listen(const char* host, int port);
bool listen(const char* host, int port, int socket_flags = 0);
void stop();

protected:
Expand Down Expand Up @@ -316,7 +316,7 @@ inline int shutdown_socket(socket_t sock)
}

template <typename Fn>
socket_t create_socket(const char* host, int port, Fn fn)
socket_t create_socket(const char* host, int port, Fn fn, int socket_flags = 0)
{
#ifdef _MSC_VER
int opt = SO_SYNCHRONOUS_NONALERT;
Expand All @@ -330,7 +330,7 @@ socket_t create_socket(const char* host, int port, Fn fn)
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_flags = socket_flags;
hints.ai_protocol = 0;

auto service = std::to_string(port);
Expand Down Expand Up @@ -363,7 +363,7 @@ socket_t create_socket(const char* host, int port, Fn fn)
return -1;
}

inline socket_t create_server_socket(const char* host, int port)
inline socket_t create_server_socket(const char* host, int port, int socket_flags)
{
return create_socket(host, port, [](socket_t sock, struct addrinfo& ai) -> socket_t {
if (::bind(sock, ai.ai_addr, ai.ai_addrlen)) {
Expand All @@ -373,7 +373,7 @@ inline socket_t create_server_socket(const char* host, int port)
return false;
}
return true;
});
}, socket_flags);
}

inline socket_t create_client_socket(const char* host, int port)
Expand Down Expand Up @@ -854,9 +854,9 @@ inline void Server::set_logger(Logger logger)
logger_ = logger;
}

inline bool Server::listen(const char* host, int port)
inline bool Server::listen(const char* host, int port, int socket_flags)
{
svr_sock_ = detail::create_server_socket(host, port);
svr_sock_ = detail::create_server_socket(host, port, socket_flags);
if (svr_sock_ == -1) {
return false;
}
Expand Down