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 "memory"
|
|
|
|
|
|
|
|
#include "Socket.hpp"
|
2017-02-25 09:54:51 -06:00
|
|
|
#include "Log.hpp"
|
2017-02-21 20:49:29 -06:00
|
|
|
|
2017-03-16 12:43:33 -05:00
|
|
|
#include <Poco/Net/SocketAddress.h>
|
|
|
|
|
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:
|
2018-01-09 08:02:02 -06:00
|
|
|
ServerSocket(Socket::Type type, SocketPoll& clientPoller, std::shared_ptr<SocketFactory> sockFactory) :
|
|
|
|
Socket(type),
|
2019-04-19 04:10:51 -05:00
|
|
|
#if !MOBILEAPP
|
2018-01-09 08:02:02 -06:00
|
|
|
_type(type),
|
2019-04-19 04:10:51 -05:00
|
|
|
#endif
|
2017-02-21 20:49:29 -06:00
|
|
|
_clientPoller(clientPoller),
|
|
|
|
_sockFactory(std::move(sockFactory))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-30 09:06:16 -05:00
|
|
|
/// Control access to a bound TCP socket
|
2018-01-09 08:02:02 -06:00
|
|
|
enum Type { Local, Public };
|
|
|
|
|
2017-02-21 20:49:29 -06:00
|
|
|
/// Binds to a local address (Servers only).
|
|
|
|
/// Does not retry on error.
|
2018-01-09 08:02:02 -06:00
|
|
|
/// Returns true only on success.
|
2019-03-30 09:06:16 -05:00
|
|
|
virtual bool bind(Type type, int port);
|
2017-02-21 20:49:29 -06:00
|
|
|
|
|
|
|
/// Listen to incoming connections (Servers only).
|
|
|
|
/// Does not retry on error.
|
|
|
|
/// Returns true on success only.
|
|
|
|
bool listen(const int backlog = 64)
|
|
|
|
{
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2017-02-21 20:49:29 -06:00
|
|
|
const int rc = ::listen(getFD(), backlog);
|
2018-09-13 11:16:00 -05:00
|
|
|
#else
|
|
|
|
const int rc = fakeSocketListen(getFD());
|
|
|
|
#endif
|
2018-01-09 08:02:02 -06:00
|
|
|
if (rc)
|
|
|
|
LOG_SYS("Failed to listen");
|
2017-03-30 04:15:28 -05:00
|
|
|
return rc == 0;
|
2017-02-21 20:49:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Accepts an incoming connection (Servers only).
|
|
|
|
/// Does not retry on error.
|
|
|
|
/// Returns a valid Socket shared_ptr on success only.
|
2019-03-30 16:07:58 -05:00
|
|
|
virtual std::shared_ptr<Socket> accept();
|
2017-02-21 20:49:29 -06:00
|
|
|
|
2017-03-17 16:59:09 -05:00
|
|
|
int getPollEvents(std::chrono::steady_clock::time_point /* now */,
|
|
|
|
int & /* timeoutMaxMs */) override
|
2017-02-21 20:49:29 -06:00
|
|
|
{
|
|
|
|
return POLLIN;
|
|
|
|
}
|
|
|
|
|
2017-03-12 18:03:45 -05:00
|
|
|
void dumpState(std::ostream& os) override;
|
2017-03-06 09:45:34 -06:00
|
|
|
|
2017-05-05 05:51:43 -05:00
|
|
|
void handlePoll(SocketDisposition &,
|
|
|
|
std::chrono::steady_clock::time_point /* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-04-19 04:10:51 -05:00
|
|
|
#if !MOBILEAPP
|
2018-01-09 08:02:02 -06:00
|
|
|
Socket::Type _type;
|
2019-04-19 04:10:51 -05:00
|
|
|
#endif
|
2017-02-21 20:49:29 -06:00
|
|
|
SocketPoll& _clientPoller;
|
2019-03-30 16:07:58 -05:00
|
|
|
protected:
|
2017-03-06 13:50:06 -06:00
|
|
|
std::shared_ptr<SocketFactory> _sockFactory;
|
2017-02-21 20:49:29 -06:00
|
|
|
};
|
|
|
|
|
2019-03-31 06:34:24 -05:00
|
|
|
#if !MOBILEAPP
|
|
|
|
|
2019-03-30 09:06:16 -05:00
|
|
|
/// A non-blocking, streaming Unix Domain Socket for local use
|
|
|
|
class LocalServerSocket : public ServerSocket
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LocalServerSocket(SocketPoll& clientPoller, std::shared_ptr<SocketFactory> sockFactory) :
|
2019-05-22 06:55:55 -05:00
|
|
|
ServerSocket(Socket::Type::Unix, clientPoller, std::move(sockFactory))
|
2019-03-30 09:06:16 -05:00
|
|
|
{
|
|
|
|
}
|
2019-04-01 05:00:27 -05:00
|
|
|
virtual bool bind(Type, int) override { assert(false); return false; }
|
2019-03-30 16:07:58 -05:00
|
|
|
virtual std::shared_ptr<Socket> accept() override;
|
2019-03-30 09:06:16 -05:00
|
|
|
std::string bind();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string _name;
|
|
|
|
};
|
|
|
|
|
2017-02-21 20:49:29 -06:00
|
|
|
#endif
|
|
|
|
|
2019-03-31 06:34:24 -05:00
|
|
|
#endif
|
|
|
|
|
2017-02-21 20:49:29 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|