2017-03-23 12:14:51 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* 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-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
2017-03-23 12:14:51 -05:00
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <net/DelaySocket.hpp>
|
2017-03-23 12:14:51 -05:00
|
|
|
|
2020-05-24 08:10:18 -05:00
|
|
|
#define DELAY_LOG(X) std::cerr << X << '\n';
|
2017-04-22 12:37:38 -05:00
|
|
|
|
2017-03-23 12:14:51 -05:00
|
|
|
class Delayer;
|
|
|
|
|
|
|
|
// FIXME: TerminatingPoll ?
|
|
|
|
static SocketPoll DelayPoll("delay_poll");
|
|
|
|
|
|
|
|
/// Reads from fd, delays that and then writes to _dest.
|
|
|
|
class DelaySocket : public Socket {
|
|
|
|
int _delayMs;
|
2017-04-22 12:30:37 -05:00
|
|
|
enum State { ReadWrite, // normal socket
|
|
|
|
EofFlushWrites, // finish up writes and close
|
|
|
|
Closed };
|
|
|
|
State _state;
|
|
|
|
std::shared_ptr<DelaySocket> _dest; // our writing twin.
|
2017-03-23 12:14:51 -05:00
|
|
|
|
|
|
|
const size_t WindowSize = 64 * 1024;
|
|
|
|
|
2017-04-21 16:37:55 -05:00
|
|
|
/// queued up data - sent to us by our opposite twin.
|
|
|
|
struct WriteChunk {
|
2019-01-17 01:46:01 -06:00
|
|
|
private:
|
2017-03-23 12:14:51 -05:00
|
|
|
std::chrono::steady_clock::time_point _sendTime;
|
|
|
|
std::vector<char> _data;
|
2019-01-17 01:46:01 -06:00
|
|
|
public:
|
2017-04-21 16:37:55 -05:00
|
|
|
WriteChunk(int delayMs)
|
2017-03-23 12:14:51 -05:00
|
|
|
{
|
|
|
|
_sendTime = std::chrono::steady_clock::now() +
|
|
|
|
std::chrono::milliseconds(delayMs);
|
|
|
|
}
|
|
|
|
bool isError() { return _data.size() == 0; }
|
2019-01-17 01:46:01 -06:00
|
|
|
std::chrono::steady_clock::time_point getSendTime() const { return _sendTime; }
|
|
|
|
std::vector<char>& getData() { return _data; }
|
2017-03-23 12:14:51 -05:00
|
|
|
private:
|
2017-04-21 16:37:55 -05:00
|
|
|
WriteChunk();
|
2017-03-23 12:14:51 -05:00
|
|
|
};
|
|
|
|
|
2017-04-21 16:37:55 -05:00
|
|
|
std::vector<std::shared_ptr<WriteChunk>> _chunks;
|
2017-03-23 12:14:51 -05:00
|
|
|
public:
|
|
|
|
DelaySocket(int delayMs, int fd) :
|
2017-04-22 12:30:37 -05:00
|
|
|
Socket (fd), _delayMs(delayMs),
|
|
|
|
_state(ReadWrite)
|
2017-03-23 12:14:51 -05:00
|
|
|
{
|
|
|
|
// setSocketBufferSize(Socket::DefaultSendBufferSize);
|
|
|
|
}
|
2017-04-21 16:37:55 -05:00
|
|
|
void setDestination(const std::shared_ptr<DelaySocket> &dest)
|
2017-03-23 12:14:51 -05:00
|
|
|
{
|
|
|
|
_dest = dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dumpState(std::ostream& os) override
|
|
|
|
{
|
|
|
|
os << "\tfd: " << getFD()
|
2020-05-24 08:10:18 -05:00
|
|
|
<< "\n\tqueue: " << _chunks.size() << '\n';
|
2017-03-23 12:14:51 -05:00
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
for (auto &chunk : _chunks)
|
|
|
|
{
|
|
|
|
os << "\t\tin: " <<
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(
|
2019-01-17 01:46:01 -06:00
|
|
|
chunk->getSendTime() - now).count() << "ms - "
|
|
|
|
<< chunk->getData().size() << "bytes\n";
|
2017-03-23 12:14:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME - really need to propagate 'noDelay' etc.
|
|
|
|
// have a debug only lookup of delayed sockets for this case ?
|
|
|
|
|
2020-04-10 06:34:15 -05:00
|
|
|
int getPollEvents(std::chrono::steady_clock::time_point now,
|
|
|
|
int64_t &timeoutMaxMicroS) override
|
2017-03-23 12:14:51 -05:00
|
|
|
{
|
2017-04-21 16:37:55 -05:00
|
|
|
if (_chunks.size() > 0)
|
2017-03-23 12:14:51 -05:00
|
|
|
{
|
2020-04-09 08:43:51 -05:00
|
|
|
int64_t remainingMicroS = std::chrono::duration_cast<std::chrono::microseconds>(
|
2019-01-17 01:46:01 -06:00
|
|
|
(*_chunks.begin())->getSendTime() - now).count();
|
2020-04-09 08:43:51 -05:00
|
|
|
if (remainingMicroS < timeoutMaxMicroS)
|
2020-05-24 08:10:18 -05:00
|
|
|
DELAY_LOG('#' << getFD() << " reset timeout max to " << remainingMicroS
|
2020-04-09 08:43:51 -05:00
|
|
|
<< "us from " << timeoutMaxMicroS << "us\n");
|
|
|
|
timeoutMaxMicroS = std::min(timeoutMaxMicroS, remainingMicroS);
|
2017-03-23 12:14:51 -05:00
|
|
|
}
|
|
|
|
|
2017-04-21 16:37:55 -05:00
|
|
|
if (_chunks.size() > 0 &&
|
2019-01-17 01:46:01 -06:00
|
|
|
now > (*_chunks.begin())->getSendTime())
|
2017-04-21 16:37:55 -05:00
|
|
|
return POLLIN | POLLOUT;
|
|
|
|
else
|
|
|
|
return POLLIN;
|
2017-03-23 12:14:51 -05:00
|
|
|
}
|
|
|
|
|
2017-04-22 12:30:37 -05:00
|
|
|
void pushCloseChunk()
|
2017-03-23 12:14:51 -05:00
|
|
|
{
|
2017-04-21 16:37:55 -05:00
|
|
|
_chunks.push_back(std::make_shared<WriteChunk>(_delayMs));
|
2017-03-23 12:14:51 -05:00
|
|
|
}
|
|
|
|
|
2017-04-22 12:30:37 -05:00
|
|
|
void changeState(State newState)
|
|
|
|
{
|
|
|
|
switch (newState)
|
|
|
|
{
|
|
|
|
case ReadWrite:
|
|
|
|
assert (false);
|
|
|
|
break;
|
|
|
|
case EofFlushWrites:
|
|
|
|
assert (_state == ReadWrite);
|
|
|
|
assert (_dest);
|
|
|
|
_dest->pushCloseChunk();
|
|
|
|
_dest = nullptr;
|
|
|
|
break;
|
|
|
|
case Closed:
|
|
|
|
if (_dest && _state == ReadWrite)
|
|
|
|
_dest->pushCloseChunk();
|
|
|
|
_dest = nullptr;
|
|
|
|
shutdown();
|
|
|
|
break;
|
|
|
|
}
|
2020-05-24 08:10:18 -05:00
|
|
|
DELAY_LOG('#' << getFD() << " changed to state " << newState << '\n');
|
2017-04-22 12:30:37 -05:00
|
|
|
_state = newState;
|
|
|
|
}
|
|
|
|
|
2017-05-05 05:51:43 -05:00
|
|
|
void handlePoll(SocketDisposition &disposition,
|
|
|
|
std::chrono::steady_clock::time_point now, int events) override
|
2017-03-23 12:14:51 -05:00
|
|
|
{
|
2017-04-22 12:30:37 -05:00
|
|
|
if (_state == ReadWrite && (events & POLLIN))
|
2017-03-23 12:14:51 -05:00
|
|
|
{
|
2017-04-21 16:37:55 -05:00
|
|
|
auto chunk = std::make_shared<WriteChunk>(_delayMs);
|
2017-03-23 12:14:51 -05:00
|
|
|
|
|
|
|
char buf[64 * 1024];
|
|
|
|
ssize_t len;
|
2017-04-21 16:37:55 -05:00
|
|
|
size_t toRead = sizeof(buf); //std::min(sizeof(buf), WindowSize - _chunksSize);
|
2017-03-23 12:14:51 -05:00
|
|
|
do {
|
|
|
|
len = ::read(getFD(), buf, toRead);
|
|
|
|
} while (len < 0 && errno == EINTR);
|
|
|
|
|
2017-04-22 12:30:37 -05:00
|
|
|
if (len == 0) // EOF.
|
|
|
|
changeState(EofFlushWrites);
|
|
|
|
else if (len >= 0)
|
2017-03-23 12:14:51 -05:00
|
|
|
{
|
2020-05-24 08:10:18 -05:00
|
|
|
DELAY_LOG('#' << getFD() << " read " << len
|
|
|
|
<< " to queue: " << _chunks.size() << '\n');
|
2019-01-17 01:46:01 -06:00
|
|
|
chunk->getData().insert(chunk->getData().end(), &buf[0], &buf[len]);
|
2017-04-21 16:37:55 -05:00
|
|
|
if (_dest)
|
|
|
|
_dest->_chunks.push_back(chunk);
|
|
|
|
else
|
2017-04-22 12:30:37 -05:00
|
|
|
assert("no destination for data" && false);
|
2017-03-23 12:14:51 -05:00
|
|
|
}
|
|
|
|
else if (errno != EAGAIN && errno != EWOULDBLOCK)
|
|
|
|
{
|
2020-05-24 08:10:18 -05:00
|
|
|
DELAY_LOG('#' << getFD() << " error : " << Util::symbolicErrno(errno) << ": " << strerror(errno) << '\n');
|
2017-04-22 12:30:37 -05:00
|
|
|
changeState(Closed); // FIXME - propagate the error ?
|
2017-03-23 12:14:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-22 12:30:37 -05:00
|
|
|
if (_chunks.size() == 0)
|
|
|
|
{
|
|
|
|
if (_state == EofFlushWrites)
|
|
|
|
changeState(Closed);
|
|
|
|
}
|
|
|
|
else // Write if we have delayed enough.
|
2017-03-23 12:14:51 -05:00
|
|
|
{
|
2017-04-21 16:37:55 -05:00
|
|
|
std::shared_ptr<WriteChunk> chunk = *_chunks.begin();
|
2017-03-23 12:14:51 -05:00
|
|
|
if (std::chrono::duration_cast<std::chrono::milliseconds>(
|
2019-01-17 01:46:01 -06:00
|
|
|
now - chunk->getSendTime()).count() >= 0)
|
2017-03-23 12:14:51 -05:00
|
|
|
{
|
2019-01-17 01:46:01 -06:00
|
|
|
if (chunk->getData().size() == 0)
|
2017-03-23 12:14:51 -05:00
|
|
|
{ // delayed error or close
|
2020-05-24 08:10:18 -05:00
|
|
|
DELAY_LOG('#' << getFD() << " handling delayed close\n");
|
2017-04-22 12:30:37 -05:00
|
|
|
changeState(Closed);
|
2017-03-23 12:14:51 -05:00
|
|
|
}
|
2017-04-22 12:30:37 -05:00
|
|
|
else
|
2017-03-23 12:14:51 -05:00
|
|
|
{
|
2017-04-22 12:30:37 -05:00
|
|
|
ssize_t len;
|
|
|
|
do {
|
2019-01-17 01:46:01 -06:00
|
|
|
len = ::write(getFD(), &chunk->getData()[0], chunk->getData().size());
|
2017-04-22 12:30:37 -05:00
|
|
|
} while (len < 0 && errno == EINTR);
|
|
|
|
|
|
|
|
if (len < 0)
|
2017-03-23 12:14:51 -05:00
|
|
|
{
|
2017-04-22 12:30:37 -05:00
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
{
|
2020-05-24 08:10:18 -05:00
|
|
|
DELAY_LOG('#' << getFD() << " full - waiting for write\n");
|
2017-04-22 12:30:37 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-24 08:10:18 -05:00
|
|
|
DELAY_LOG('#' << getFD() << " failed onwards write "
|
2017-04-22 12:37:38 -05:00
|
|
|
<< len << "bytes of "
|
2019-01-17 01:46:01 -06:00
|
|
|
<< chunk->getData().size()
|
2018-07-11 04:58:10 -05:00
|
|
|
<< " queue: " << _chunks.size() << " error: "
|
2020-05-24 08:10:18 -05:00
|
|
|
<< Util::symbolicErrno(errno) << ": " << strerror(errno) << '\n');
|
2017-04-22 12:30:37 -05:00
|
|
|
changeState(Closed);
|
|
|
|
}
|
2017-03-23 12:14:51 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-24 08:10:18 -05:00
|
|
|
DELAY_LOG('#' << getFD() << " written onwards " << len << "bytes of "
|
2019-01-17 01:46:01 -06:00
|
|
|
<< chunk->getData().size()
|
2020-05-24 08:10:18 -05:00
|
|
|
<< " queue: " << _chunks.size() << '\n');
|
2017-04-22 12:30:37 -05:00
|
|
|
if (len > 0)
|
2019-01-17 01:46:01 -06:00
|
|
|
chunk->getData().erase(chunk->getData().begin(), chunk->getData().begin() + len);
|
2017-03-23 12:14:51 -05:00
|
|
|
|
2019-01-17 01:46:01 -06:00
|
|
|
if (chunk->getData().size() == 0)
|
2017-04-22 12:30:37 -05:00
|
|
|
_chunks.erase(_chunks.begin(), _chunks.begin() + 1);
|
|
|
|
}
|
2017-03-23 12:14:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events & (POLLERR | POLLHUP | POLLNVAL))
|
|
|
|
{
|
2020-05-24 08:10:18 -05:00
|
|
|
DELAY_LOG('#' << getFD() << " error events: " << events << '\n');
|
2017-04-22 12:30:37 -05:00
|
|
|
changeState(Closed);
|
2017-03-23 12:14:51 -05:00
|
|
|
}
|
2017-04-22 12:30:37 -05:00
|
|
|
|
|
|
|
if (_state == Closed)
|
2017-05-05 05:51:43 -05:00
|
|
|
disposition.setClosed();
|
2017-03-23 12:14:51 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Delayer:
|
|
|
|
///
|
|
|
|
/// Some terminology:
|
|
|
|
/// physical socket (DelaySocket's own fd) - what we accepted.
|
|
|
|
/// internalFd - the internal side of the socket-pair
|
|
|
|
/// delayFd - what we hand on to our un-suspecting wrapped socket
|
|
|
|
/// which looks like an external socket - but delayed.
|
|
|
|
namespace Delay {
|
|
|
|
int create(int delayMs, int physicalFd)
|
|
|
|
{
|
|
|
|
int pair[2];
|
|
|
|
int rc = socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, pair);
|
2017-04-24 05:35:13 -05:00
|
|
|
assert (rc == 0); (void)rc;
|
2017-03-23 12:14:51 -05:00
|
|
|
int internalFd = pair[0];
|
|
|
|
int delayFd = pair[1];
|
|
|
|
|
|
|
|
auto physical = std::make_shared<DelaySocket>(delayMs, physicalFd);
|
|
|
|
auto internal = std::make_shared<DelaySocket>(delayMs, internalFd);
|
|
|
|
physical->setDestination(internal);
|
|
|
|
internal->setDestination(physical);
|
|
|
|
|
|
|
|
DelayPoll.startThread();
|
|
|
|
DelayPoll.insertNewSocket(physical);
|
|
|
|
DelayPoll.insertNewSocket(internal);
|
|
|
|
|
|
|
|
return delayFd;
|
|
|
|
}
|
|
|
|
void dumpState(std::ostream &os)
|
|
|
|
{
|
|
|
|
if (DelayPoll.isAlive())
|
|
|
|
{
|
|
|
|
os << "Delay poll:\n";
|
|
|
|
DelayPoll.dumpState(os);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|