2017-02-21 20:49:29 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_SERVERSOCKET_HPP
|
|
|
|
#define INCLUDED_SERVERSOCKET_HPP
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "memory"
|
|
|
|
|
|
|
|
#include "Socket.hpp"
|
2017-02-25 09:54:51 -06:00
|
|
|
#include "Log.hpp"
|
2017-02-21 20:49:29 -06:00
|
|
|
|
|
|
|
class SocketFactory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual std::shared_ptr<Socket> create(const int fd) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A non-blocking, streaming socket.
|
|
|
|
class ServerSocket : public Socket
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ServerSocket(SocketPoll& clientPoller, std::unique_ptr<SocketFactory> sockFactory) :
|
|
|
|
_clientPoller(clientPoller),
|
|
|
|
_sockFactory(std::move(sockFactory))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Binds to a local address (Servers only).
|
|
|
|
/// Does not retry on error.
|
|
|
|
/// Returns true on success only.
|
|
|
|
bool bind(const Poco::Net::SocketAddress& address)
|
|
|
|
{
|
|
|
|
// Enable address reuse to avoid stalling after
|
|
|
|
// recycling, when previous socket is TIME_WAIT.
|
|
|
|
//TODO: Might be worth refactoring out.
|
|
|
|
const int reuseAddress = 1;
|
|
|
|
constexpr unsigned int len = sizeof(reuseAddress);
|
|
|
|
::setsockopt(getFD(), SOL_SOCKET, SO_REUSEADDR, &reuseAddress, len);
|
|
|
|
|
|
|
|
const int rc = ::bind(getFD(), address.addr(), address.length());
|
|
|
|
return (rc == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Listen to incoming connections (Servers only).
|
|
|
|
/// Does not retry on error.
|
|
|
|
/// Returns true on success only.
|
|
|
|
bool listen(const int backlog = 64)
|
|
|
|
{
|
|
|
|
const int rc = ::listen(getFD(), backlog);
|
|
|
|
return (rc == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Accepts an incoming connection (Servers only).
|
|
|
|
/// Does not retry on error.
|
|
|
|
/// Returns a valid Socket shared_ptr on success only.
|
|
|
|
std::shared_ptr<Socket> accept()
|
|
|
|
{
|
|
|
|
// Accept a connection (if any) and set it to non-blocking.
|
|
|
|
// We don't care about the client's address, so ignored.
|
|
|
|
const int rc = ::accept4(getFD(), nullptr, nullptr, SOCK_NONBLOCK);
|
|
|
|
return (rc != -1 ? _sockFactory->create(rc) : std::shared_ptr<Socket>(nullptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
int getPollEvents() override
|
|
|
|
{
|
|
|
|
return POLLIN;
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:56:20 -06:00
|
|
|
HandleResult handlePoll(const Poco::Timestamp &/* now */, int events) override
|
2017-02-21 20:49:29 -06:00
|
|
|
{
|
|
|
|
if (events & POLLIN)
|
|
|
|
{
|
|
|
|
std::shared_ptr<Socket> clientSocket = accept();
|
|
|
|
if (!clientSocket)
|
|
|
|
{
|
|
|
|
const std::string msg = "Failed to accept. (errno: ";
|
|
|
|
throw std::runtime_error(msg + std::strerror(errno) + ")");
|
|
|
|
}
|
|
|
|
|
2017-02-27 20:40:44 -06:00
|
|
|
LOG_DBG("Accepted client #" << clientSocket->getFD());
|
2017-02-21 20:49:29 -06:00
|
|
|
_clientPoller.insertNewSocket(clientSocket);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Socket::HandleResult::CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SocketPoll& _clientPoller;
|
|
|
|
std::unique_ptr<SocketFactory> _sockFactory;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|