2017-02-13 21:16:35 -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/.
|
|
|
|
*/
|
|
|
|
|
2017-02-21 19:54:13 -06:00
|
|
|
#ifndef INCLUDED_SOCKET_HPP
|
|
|
|
#define INCLUDED_SOCKET_HPP
|
|
|
|
|
2017-02-13 21:16:35 -06:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <poll.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <atomic>
|
2017-02-23 10:57:59 -06:00
|
|
|
#include <cassert>
|
2017-02-13 21:16:35 -06:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2017-02-23 10:57:59 -06:00
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2017-02-13 21:16:35 -06:00
|
|
|
#include <sstream>
|
|
|
|
|
2017-02-24 16:56:20 -06:00
|
|
|
#include <Poco/Timespan.h>
|
|
|
|
#include <Poco/Timestamp.h>
|
2017-02-13 21:16:35 -06:00
|
|
|
#include <Poco/Net/SocketAddress.h>
|
|
|
|
|
2017-02-24 10:45:31 -06:00
|
|
|
#include "Log.hpp"
|
|
|
|
|
2017-02-13 21:16:35 -06:00
|
|
|
/// A non-blocking, streaming socket.
|
|
|
|
class Socket
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Socket() :
|
|
|
|
_fd(socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0))
|
|
|
|
{
|
2017-02-17 20:05:07 -06:00
|
|
|
setNoDelay();
|
2017-02-13 21:16:35 -06:00
|
|
|
}
|
|
|
|
|
2017-02-15 08:48:48 -06:00
|
|
|
virtual ~Socket()
|
2017-02-13 21:16:35 -06:00
|
|
|
{
|
|
|
|
//TODO: Should we shutdown here or up to the client?
|
|
|
|
|
|
|
|
// Doesn't block on sockets; no error handling needed.
|
|
|
|
close(_fd);
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:56:20 -06:00
|
|
|
/// Returns the OS native socket fd.
|
2017-02-14 05:10:52 -06:00
|
|
|
int getFD() const { return _fd; }
|
2017-02-13 21:16:35 -06:00
|
|
|
|
2017-02-15 08:48:48 -06:00
|
|
|
/// Return a mask of events we should be polling for
|
|
|
|
virtual int getPollEvents() = 0;
|
|
|
|
|
2017-02-24 16:56:20 -06:00
|
|
|
/// Contract the poll timeout to match our needs
|
|
|
|
virtual void updateTimeout(Poco::Timestamp &/*timeout*/) { /* do nothing */ }
|
|
|
|
|
2017-02-15 08:48:48 -06:00
|
|
|
/// Handle results of events returned from poll
|
2017-02-18 12:01:43 -06:00
|
|
|
enum class HandleResult { CONTINUE, SOCKET_CLOSED };
|
2017-02-24 16:56:20 -06:00
|
|
|
virtual HandleResult handlePoll(const Poco::Timestamp &now, int events) = 0;
|
2017-02-15 08:48:48 -06:00
|
|
|
|
2017-02-17 20:05:07 -06:00
|
|
|
/// manage latency issues around packet aggregation
|
|
|
|
void setNoDelay(bool noDelay = true)
|
|
|
|
{
|
|
|
|
int val = noDelay ? 1 : 0;
|
|
|
|
setsockopt (_fd, IPPROTO_TCP, TCP_NODELAY,
|
|
|
|
(char *) &val, sizeof(val));
|
|
|
|
}
|
|
|
|
|
2017-02-13 21:16:35 -06:00
|
|
|
/// Sets the send buffer in size bytes.
|
|
|
|
/// Must be called before accept or connect.
|
|
|
|
/// Note: TCP will allocate twice this size for admin purposes,
|
|
|
|
/// so a subsequent call to getSendBufferSize will return
|
|
|
|
/// the larger (actual) buffer size, if this succeeds.
|
|
|
|
/// Note: the upper limit is set via /proc/sys/net/core/wmem_max,
|
|
|
|
/// and there is an unconfigurable lower limit as well.
|
|
|
|
/// Returns true on success only.
|
|
|
|
bool setSendBufferSize(const int size)
|
|
|
|
{
|
|
|
|
constexpr unsigned int len = sizeof(size);
|
|
|
|
const int rc = ::setsockopt(_fd, SOL_SOCKET, SO_SNDBUF, &size, len);
|
|
|
|
return (rc == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the actual send buffer size in bytes, -1 for failure.
|
|
|
|
int getSendBufferSize() const
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
unsigned int len = sizeof(size);
|
|
|
|
const int rc = ::getsockopt(_fd, SOL_SOCKET, SO_SNDBUF, &size, &len);
|
|
|
|
return (rc == 0 ? size : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the receive buffer size in bytes.
|
|
|
|
/// Must be called before accept or connect.
|
|
|
|
/// Note: TCP will allocate twice this size for admin purposes,
|
|
|
|
/// so a subsequent call to getSendBufferSize will return
|
|
|
|
/// the larger (actual) buffer size, if this succeeds.
|
|
|
|
/// Note: the upper limit is set via /proc/sys/net/core/rmem_max,
|
|
|
|
/// and there is an unconfigurable lower limit as well.
|
|
|
|
/// Returns true on success only.
|
|
|
|
bool setReceiveBufferSize(const int size)
|
|
|
|
{
|
|
|
|
constexpr unsigned int len = sizeof(size);
|
|
|
|
const int rc = ::setsockopt(_fd, SOL_SOCKET, SO_RCVBUF, &size, len);
|
|
|
|
return (rc == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the actual receive buffer size in bytes, -1 on error.
|
|
|
|
int getReceiveBufferSize() const
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
unsigned int len = sizeof(size);
|
|
|
|
const int rc = ::getsockopt(_fd, SOL_SOCKET, SO_RCVBUF, &size, &len);
|
|
|
|
return (rc == 0 ? size : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the error code.
|
|
|
|
/// Sets errno on success and returns it.
|
|
|
|
/// Returns -1 on failure to get the error code.
|
|
|
|
int getError() const
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
unsigned int len = sizeof(error);
|
|
|
|
const int rc = ::getsockopt(_fd, SOL_SOCKET, SO_ERROR, &error, &len);
|
|
|
|
if (rc == 0)
|
|
|
|
{
|
|
|
|
// Set errno so client can use strerror etc.
|
|
|
|
errno = error;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-02-15 08:48:48 -06:00
|
|
|
protected:
|
|
|
|
|
|
|
|
/// Construct based on an existing socket fd.
|
|
|
|
/// Used by accept() only.
|
|
|
|
Socket(const int fd) :
|
|
|
|
_fd(fd)
|
2017-02-13 21:16:35 -06:00
|
|
|
{
|
2017-02-17 20:05:07 -06:00
|
|
|
setNoDelay();
|
2017-02-15 08:48:48 -06:00
|
|
|
}
|
2017-02-13 21:16:35 -06:00
|
|
|
|
2017-02-15 08:48:48 -06:00
|
|
|
private:
|
|
|
|
const int _fd;
|
|
|
|
};
|
2017-02-13 21:16:35 -06:00
|
|
|
|
2017-02-15 08:48:48 -06:00
|
|
|
|
|
|
|
/// Handles non-blocking socket event polling.
|
|
|
|
/// Only polls on N-Sockets and invokes callback and
|
|
|
|
/// doesn't manage buffers or client data.
|
|
|
|
/// Note: uses poll(2) since it has very good performance
|
|
|
|
/// compared to epoll up to a few hundred sockets and
|
|
|
|
/// doesn't suffer select(2)'s poor API. Since this will
|
|
|
|
/// be used per-document we don't expect to have several
|
|
|
|
/// hundred users on same document to suffer poll(2)'s
|
|
|
|
/// scalability limit. Meanwhile, epoll(2)'s high
|
|
|
|
/// overhead to adding/removing sockets is not helpful.
|
|
|
|
class SocketPoll
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SocketPoll()
|
|
|
|
{
|
|
|
|
// Create the wakeup fd.
|
|
|
|
if (::pipe2(_wakeup, O_CLOEXEC | O_NONBLOCK) == -1)
|
2017-02-13 21:16:35 -06:00
|
|
|
{
|
2017-02-20 22:43:05 -06:00
|
|
|
throw std::runtime_error("Failed to allocate pipe for SocketPoll waking.");
|
2017-02-13 21:16:35 -06:00
|
|
|
}
|
2017-02-15 08:48:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
~SocketPoll()
|
|
|
|
{
|
|
|
|
::close(_wakeup[0]);
|
|
|
|
::close(_wakeup[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Poll the sockets for available data to read or buffer to write.
|
2017-02-24 16:56:20 -06:00
|
|
|
void poll(const int timeoutMaxMs)
|
2017-02-15 08:48:48 -06:00
|
|
|
{
|
|
|
|
const size_t size = _pollSockets.size();
|
2017-02-13 21:16:35 -06:00
|
|
|
|
2017-02-24 16:56:20 -06:00
|
|
|
Poco::Timestamp now;
|
|
|
|
Poco::Timestamp timeout = now;
|
|
|
|
timeout += Poco::Timespan(0 /* s */, timeoutMaxMs * 1000 /* us */);
|
|
|
|
|
2017-02-15 08:48:48 -06:00
|
|
|
// The events to poll on change each spin of the loop.
|
2017-02-24 16:56:20 -06:00
|
|
|
setupPollFds(timeout);
|
2017-02-15 08:48:48 -06:00
|
|
|
|
|
|
|
int rc;
|
|
|
|
do
|
2017-02-13 21:16:35 -06:00
|
|
|
{
|
2017-02-24 16:56:20 -06:00
|
|
|
rc = ::poll(&_pollFds[0], size + 1, (timeout - now)/1000);
|
2017-02-13 21:16:35 -06:00
|
|
|
}
|
2017-02-15 08:48:48 -06:00
|
|
|
while (rc < 0 && errno == EINTR);
|
2017-02-13 21:16:35 -06:00
|
|
|
|
2017-02-15 08:48:48 -06:00
|
|
|
// Fire the callback and remove dead fds.
|
2017-02-24 16:56:20 -06:00
|
|
|
Poco::Timestamp newNow;
|
2017-02-15 08:48:48 -06:00
|
|
|
for (int i = static_cast<int>(size) - 1; i >= 0; --i)
|
2017-02-13 21:16:35 -06:00
|
|
|
{
|
2017-02-15 08:48:48 -06:00
|
|
|
if (_pollFds[i].revents)
|
2017-02-13 21:16:35 -06:00
|
|
|
{
|
2017-02-24 16:56:20 -06:00
|
|
|
if (_pollSockets[i]->handlePoll(newNow, _pollFds[i].revents) ==
|
2017-02-15 08:48:48 -06:00
|
|
|
Socket::HandleResult::SOCKET_CLOSED)
|
|
|
|
{
|
2017-02-25 09:54:51 -06:00
|
|
|
LOG_DBG("Removing client #" << _pollFds[i].fd);
|
2017-02-15 08:48:48 -06:00
|
|
|
_pollSockets.erase(_pollSockets.begin() + i);
|
|
|
|
// Don't remove from pollFds; we'll recreate below.
|
|
|
|
}
|
2017-02-13 21:16:35 -06:00
|
|
|
}
|
2017-02-15 08:48:48 -06:00
|
|
|
}
|
2017-02-13 21:16:35 -06:00
|
|
|
|
2017-02-15 08:48:48 -06:00
|
|
|
// Process the wakeup pipe (always the last entry).
|
|
|
|
if (_pollFds[size].revents)
|
|
|
|
{
|
2017-02-24 17:14:09 -06:00
|
|
|
std::vector<CallbackFn> invoke;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
|
|
|
|
// Clear the data.
|
|
|
|
int dump = ::read(_wakeup[0], &dump, sizeof(dump));
|
2017-02-13 21:16:35 -06:00
|
|
|
|
2017-02-24 17:14:09 -06:00
|
|
|
// Copy the new sockets over and clear.
|
|
|
|
_pollSockets.insert(_pollSockets.end(),
|
|
|
|
_newSockets.begin(), _newSockets.end());
|
|
|
|
_newSockets.clear();
|
|
|
|
|
|
|
|
// Extract list of callbacks to process
|
|
|
|
std::swap(_newCallbacks, invoke);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < invoke.size(); ++i)
|
|
|
|
invoke[i]();
|
2017-02-13 21:16:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 17:14:09 -06:00
|
|
|
/// Wakeup the main polling loop in another thread
|
|
|
|
void wakeup()
|
|
|
|
{
|
|
|
|
// wakeup the main-loop.
|
|
|
|
int rc;
|
|
|
|
do {
|
|
|
|
rc = ::write(_wakeup[1], "w", 1);
|
|
|
|
} while (rc == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
assert (rc != -1 || errno == EAGAIN || errno == EWOULDBLOCK);
|
|
|
|
}
|
|
|
|
|
2017-02-15 08:48:48 -06:00
|
|
|
/// Insert a new socket to be polled.
|
|
|
|
/// Sockets are removed only when the handler return false.
|
|
|
|
void insertNewSocket(const std::shared_ptr<Socket>& newSocket)
|
2017-02-13 21:16:35 -06:00
|
|
|
{
|
2017-02-15 08:48:48 -06:00
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
_newSockets.emplace_back(newSocket);
|
2017-02-24 17:14:09 -06:00
|
|
|
wakeup();
|
2017-02-13 21:16:35 -06:00
|
|
|
}
|
|
|
|
|
2017-02-24 17:14:09 -06:00
|
|
|
typedef std::function<void()> CallbackFn;
|
2017-02-15 08:48:48 -06:00
|
|
|
|
2017-02-24 17:14:09 -06:00
|
|
|
/// Add a callback to be invoked in the polling thread
|
|
|
|
void addCallback(CallbackFn fn)
|
2017-02-13 21:16:35 -06:00
|
|
|
{
|
2017-02-15 08:48:48 -06:00
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
2017-02-24 17:14:09 -06:00
|
|
|
_newCallbacks.emplace_back(fn);
|
|
|
|
wakeup();
|
2017-02-13 21:16:35 -06:00
|
|
|
}
|
|
|
|
|
2017-02-24 17:14:09 -06:00
|
|
|
private:
|
|
|
|
|
2017-02-15 08:48:48 -06:00
|
|
|
void removeSocketFromPoll(const std::shared_ptr<Socket>& socket)
|
|
|
|
{
|
|
|
|
auto it = std::find(_pollSockets.begin(), _pollSockets.end(), socket);
|
|
|
|
assert (it != _pollSockets.end());
|
|
|
|
_pollSockets.erase(it);
|
|
|
|
}
|
2017-02-13 21:16:35 -06:00
|
|
|
|
2017-02-15 08:48:48 -06:00
|
|
|
/// Initialize the poll fds array with the right events
|
2017-02-24 16:56:20 -06:00
|
|
|
void setupPollFds(Poco::Timestamp &timeout)
|
2017-02-13 21:16:35 -06:00
|
|
|
{
|
2017-02-15 08:48:48 -06:00
|
|
|
const size_t size = _pollSockets.size();
|
|
|
|
|
|
|
|
_pollFds.resize(size + 1); // + wakeup pipe
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
_pollFds[i].fd = _pollSockets[i]->getFD();
|
|
|
|
_pollFds[i].events = _pollSockets[i]->getPollEvents();
|
2017-02-24 16:56:20 -06:00
|
|
|
_pollSockets[i]->updateTimeout(timeout);
|
2017-02-15 08:48:48 -06:00
|
|
|
_pollFds[i].revents = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the read-end of the wake pipe.
|
|
|
|
_pollFds[size].fd = _wakeup[0];
|
|
|
|
_pollFds[size].events = POLLIN;
|
|
|
|
_pollFds[size].revents = 0;
|
2017-02-13 21:16:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-02-15 08:48:48 -06:00
|
|
|
/// main-loop wakeup pipe
|
|
|
|
int _wakeup[2];
|
|
|
|
/// The sockets we're controlling
|
|
|
|
std::vector<std::shared_ptr<Socket>> _pollSockets;
|
|
|
|
/// Protects _newSockets
|
|
|
|
std::mutex _mutex;
|
|
|
|
std::vector<std::shared_ptr<Socket>> _newSockets;
|
2017-02-24 17:14:09 -06:00
|
|
|
std::vector<CallbackFn> _newCallbacks;
|
2017-02-15 08:48:48 -06:00
|
|
|
/// The fds to poll.
|
|
|
|
std::vector<pollfd> _pollFds;
|
2017-02-13 21:16:35 -06:00
|
|
|
};
|
|
|
|
|
2017-02-22 10:12:12 -06:00
|
|
|
class StreamSocket;
|
|
|
|
|
|
|
|
/// Interface that handles the actual incoming message.
|
2017-02-23 06:14:01 -06:00
|
|
|
class SocketHandlerInterface
|
2017-02-22 10:12:12 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Set the socket associated with this ResponseClient.
|
|
|
|
virtual void setSocket(StreamSocket* socket) = 0;
|
|
|
|
|
|
|
|
/// Called after successful socket reads.
|
|
|
|
virtual void handleIncomingMessage() = 0;
|
|
|
|
};
|
|
|
|
|
2017-02-22 08:45:41 -06:00
|
|
|
/// A plain, non-blocking, data streaming socket.
|
|
|
|
class StreamSocket : public Socket
|
2017-02-13 21:16:35 -06:00
|
|
|
{
|
|
|
|
public:
|
2017-02-23 06:14:01 -06:00
|
|
|
StreamSocket(const int fd, SocketHandlerInterface* socketHandler) :
|
2017-02-22 10:12:12 -06:00
|
|
|
Socket(fd),
|
2017-02-23 06:14:01 -06:00
|
|
|
_socketHandler(socketHandler)
|
2017-02-22 10:12:12 -06:00
|
|
|
{
|
2017-02-23 06:14:01 -06:00
|
|
|
_socketHandler->setSocket(this);
|
2017-02-22 10:12:12 -06:00
|
|
|
}
|
2017-02-19 13:47:25 -06:00
|
|
|
|
|
|
|
/// Called when a polling event is received.
|
|
|
|
/// @events is the mask of events that triggered the wake.
|
2017-02-24 16:56:20 -06:00
|
|
|
HandleResult handlePoll(const Poco::Timestamp & /* now */,
|
|
|
|
const int events) override
|
2017-02-13 21:16:35 -06:00
|
|
|
{
|
2017-02-19 13:47:25 -06:00
|
|
|
// FIXME: need to close input, but not output (?)
|
2017-02-15 08:48:48 -06:00
|
|
|
bool closeSocket = false;
|
2017-02-13 21:16:35 -06:00
|
|
|
|
2017-02-19 13:47:25 -06:00
|
|
|
// Always try to read.
|
|
|
|
closeSocket = !readIncomingData();
|
|
|
|
|
2017-02-24 10:45:31 -06:00
|
|
|
auto& log = Log::logger();
|
|
|
|
if (log.trace()) {
|
|
|
|
LOG_TRC("Incoming data buffer " << _inBuffer.size() <<
|
|
|
|
" closeSocket? " << closeSocket << "\n");
|
|
|
|
log.dump("", &_inBuffer[0], _inBuffer.size());
|
|
|
|
}
|
|
|
|
|
2017-02-19 13:47:25 -06:00
|
|
|
// If we have data, allow the app to consume.
|
|
|
|
size_t oldSize = 0;
|
|
|
|
while (!_inBuffer.empty() && oldSize != _inBuffer.size())
|
2017-02-18 14:27:27 -06:00
|
|
|
{
|
2017-02-19 13:47:25 -06:00
|
|
|
oldSize = _inBuffer.size();
|
2017-02-23 06:14:01 -06:00
|
|
|
if (_socketHandler)
|
|
|
|
_socketHandler->handleIncomingMessage();
|
2017-02-18 14:27:27 -06:00
|
|
|
}
|
2017-02-13 21:16:35 -06:00
|
|
|
|
2017-02-19 13:47:25 -06:00
|
|
|
// SSL might want to do handshake,
|
|
|
|
// even if we have no data to write.
|
|
|
|
if ((events & POLLOUT) || !_outBuffer.empty())
|
|
|
|
{
|
2017-02-15 08:48:48 -06:00
|
|
|
writeOutgoingData();
|
2017-02-19 13:47:25 -06:00
|
|
|
}
|
2017-02-13 21:16:35 -06:00
|
|
|
|
2017-02-15 08:48:48 -06:00
|
|
|
if (events & (POLLHUP | POLLERR | POLLNVAL))
|
|
|
|
closeSocket = true;
|
|
|
|
|
|
|
|
return closeSocket ? HandleResult::SOCKET_CLOSED :
|
|
|
|
HandleResult::CONTINUE;
|
2017-02-13 21:16:35 -06:00
|
|
|
}
|
2017-02-15 08:48:48 -06:00
|
|
|
|
2017-02-18 14:35:29 -06:00
|
|
|
/// Reads data by invoking readData() and buffering.
|
2017-02-19 11:32:07 -06:00
|
|
|
/// Return false iff the socket is closed.
|
2017-02-18 14:35:29 -06:00
|
|
|
virtual bool readIncomingData()
|
2017-02-14 17:45:24 -06:00
|
|
|
{
|
2017-02-19 19:56:01 -06:00
|
|
|
// SSL decodes blocks of 16Kb, so for efficiency we use the same.
|
|
|
|
char buf[16 * 1024];
|
2017-02-14 17:45:24 -06:00
|
|
|
ssize_t len;
|
2017-02-18 12:48:44 -06:00
|
|
|
do
|
|
|
|
{
|
|
|
|
// Drain the read buffer.
|
|
|
|
// TODO: Cap the buffer size, lest we grow beyond control.
|
|
|
|
do
|
|
|
|
{
|
2017-02-20 22:28:20 -06:00
|
|
|
len = readData(buf, sizeof(buf));
|
2017-02-18 12:48:44 -06:00
|
|
|
}
|
|
|
|
while (len < 0 && errno == EINTR);
|
2017-02-18 14:27:27 -06:00
|
|
|
|
2017-02-18 12:48:44 -06:00
|
|
|
if (len > 0)
|
|
|
|
{
|
2017-02-20 22:28:20 -06:00
|
|
|
assert (len <= ssize_t(sizeof(buf)));
|
2017-02-18 12:48:44 -06:00
|
|
|
_inBuffer.insert(_inBuffer.end(), &buf[0], &buf[len]);
|
|
|
|
}
|
|
|
|
// else poll will handle errors.
|
|
|
|
}
|
2017-02-20 22:28:20 -06:00
|
|
|
while (len == (sizeof(buf)));
|
2017-02-14 17:59:28 -06:00
|
|
|
|
|
|
|
return len != 0; // zero is eof / clean socket close.
|
2017-02-14 17:45:24 -06:00
|
|
|
}
|
2017-02-13 21:16:35 -06:00
|
|
|
|
2017-02-18 14:35:29 -06:00
|
|
|
/// Override to write data out to socket.
|
|
|
|
virtual void writeOutgoingData()
|
2017-02-13 21:16:35 -06:00
|
|
|
{
|
2017-02-20 22:18:54 -06:00
|
|
|
assert(!_outBuffer.empty());
|
2017-02-21 19:34:37 -06:00
|
|
|
do
|
2017-02-18 14:35:29 -06:00
|
|
|
{
|
2017-02-19 13:47:05 -06:00
|
|
|
ssize_t len;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
len = writeData(&_outBuffer[0], _outBuffer.size());
|
2017-02-24 10:45:31 -06:00
|
|
|
|
|
|
|
auto& log = Log::logger();
|
|
|
|
if (log.trace()) {
|
|
|
|
LOG_TRC("Wrote outgoing data " << len << " bytes\n");
|
|
|
|
log.dump("", &_outBuffer[0], len);
|
|
|
|
}
|
2017-02-19 13:47:05 -06:00
|
|
|
}
|
|
|
|
while (len < 0 && errno == EINTR);
|
2017-02-18 14:35:29 -06:00
|
|
|
|
2017-02-19 13:47:05 -06:00
|
|
|
if (len > 0)
|
|
|
|
{
|
2017-02-21 19:34:37 -06:00
|
|
|
_outBuffer.erase(_outBuffer.begin(), _outBuffer.begin() + len);
|
2017-02-19 13:47:05 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Poll will handle errors.
|
|
|
|
break;
|
|
|
|
}
|
2017-02-14 17:45:24 -06:00
|
|
|
}
|
2017-02-21 19:34:37 -06:00
|
|
|
while (!_outBuffer.empty());
|
2017-02-13 21:16:35 -06:00
|
|
|
}
|
|
|
|
|
2017-02-18 14:35:29 -06:00
|
|
|
/// Override to handle reading of socket data differently.
|
2017-02-22 08:45:41 -06:00
|
|
|
virtual int readData(char* buf, int len)
|
|
|
|
{
|
|
|
|
return ::read(getFD(), buf, len);
|
|
|
|
}
|
2017-02-18 14:35:29 -06:00
|
|
|
|
|
|
|
/// Override to handle writing data to socket differently.
|
2017-02-22 08:45:41 -06:00
|
|
|
virtual int writeData(const char* buf, const int len)
|
|
|
|
{
|
|
|
|
return ::write(getFD(), buf, len);
|
|
|
|
}
|
2017-02-18 14:35:29 -06:00
|
|
|
|
|
|
|
int getPollEvents() override
|
|
|
|
{
|
2017-02-19 13:47:05 -06:00
|
|
|
// Only poll for read if we have nothing to write.
|
2017-02-18 14:35:29 -06:00
|
|
|
return (_outBuffer.empty() ? POLLIN : POLLIN | POLLOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2017-02-22 10:12:12 -06:00
|
|
|
/// Client handling the actual data.
|
2017-02-23 06:14:01 -06:00
|
|
|
std::unique_ptr<SocketHandlerInterface> _socketHandler;
|
2017-02-18 14:35:29 -06:00
|
|
|
|
|
|
|
std::vector< char > _inBuffer;
|
|
|
|
std::vector< char > _outBuffer;
|
|
|
|
|
2017-02-23 06:14:01 -06:00
|
|
|
// To be able to access _inBuffer and _outBuffer.
|
|
|
|
friend class WebSocketHandler;
|
2017-02-25 10:55:24 -06:00
|
|
|
friend class ClientRequestDispatcher;
|
2017-02-18 14:35:29 -06:00
|
|
|
};
|
|
|
|
|
2017-02-21 19:54:13 -06:00
|
|
|
#endif
|
2017-02-13 21:16:35 -06:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|