Skip to content

Commit

Permalink
use base::Time for all times in the API
Browse files Browse the repository at this point in the history
And deprecate the ones that were still using integers (milliseconds).
Timeout::timeLeft has been renamed into remaining in the Time-using
API to keep backward compatibility.
  • Loading branch information
doudou committed Aug 15, 2017
1 parent e3b958d commit 404eac1
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 88 deletions.
82 changes: 43 additions & 39 deletions src/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

using namespace std;
using namespace iodrivers_base;
using base::Time;

string Driver::printable_com(std::string const& str)
{ return printable_com(str.c_str(), str.size()); }
Expand Down Expand Up @@ -144,7 +145,7 @@ int Driver::flushReadBuffer(uint8_t* buffer, int bufsize)
do
{
try {
int packetSize = readPacket(buffer, bufsize, 0, 0, false);
int packetSize = readPacket(buffer, bufsize, Time(), Time(), false);
return packetSize;
}
catch(TimeoutError) {
Expand All @@ -161,7 +162,7 @@ void Driver::skipReadBytes(size_t byte_count)
if (byte_count > internal_buffer_size)
throw std::invalid_argument("skipReadBytes(): byte count provided is more than the amount of bytes in the internal read buffer");

m_stats.stamp = base::Time::now();
m_stats.stamp = Time::now();
m_stats.bad_rx += byte_count;
internal_buffer_size -= byte_count;
memmove(internal_buffer, internal_buffer + byte_count, internal_buffer_size);
Expand Down Expand Up @@ -582,7 +583,7 @@ std::pair<uint8_t const*, int> Driver::findPacket(uint8_t const* buffer, int buf

if (m_extract_last)
{
m_stats.stamp = base::Time::now();
m_stats.stamp = Time::now();
m_stats.bad_rx += packet_start;
m_stats.good_rx += packet_size;
}
Expand Down Expand Up @@ -622,7 +623,7 @@ int Driver::doPacketExtraction(uint8_t* buffer)
pair<uint8_t const*, int> packet = findPacket(internal_buffer, internal_buffer_size);
if (!m_extract_last)
{
m_stats.stamp = base::Time::now();
m_stats.stamp = Time::now();
m_stats.bad_rx += packet.first - internal_buffer;
m_stats.good_rx += packet.second;
}
Expand Down Expand Up @@ -713,35 +714,29 @@ bool Driver::hasPacket() const
return (packet.second > 0);
}

void Driver::setReadTimeout(base::Time const& timeout)
void Driver::setReadTimeout(Time const& timeout)
{ m_read_timeout = timeout; }
base::Time Driver::getReadTimeout() const
Time Driver::getReadTimeout() const
{ return m_read_timeout; }
int Driver::readPacket(uint8_t* buffer, int buffer_size)
{
return readPacket(buffer, buffer_size, getReadTimeout());
return readPacket(buffer, buffer_size, getReadTimeout(), Time(), getFlushOnTimeout());
}
int Driver::readPacket(uint8_t* buffer, int buffer_size,
base::Time const& packet_timeout)
int Driver::readPacket(uint8_t* buffer, int buffer_size, Time const& packet_timeout)
{
return readPacket(buffer, buffer_size, packet_timeout,
packet_timeout + base::Time::fromSeconds(1));
return readPacket(buffer, buffer_size, packet_timeout, Time(), getFlushOnTimeout());
}
int Driver::readPacket(uint8_t* buffer, int buffer_size,
base::Time const& packet_timeout, base::Time const& first_byte_timeout)
{
return readPacket(buffer, buffer_size, packet_timeout.toMilliseconds(),
first_byte_timeout.toMilliseconds());
}
int Driver::readPacket(uint8_t* buffer, int buffer_size, int packet_timeout, int first_byte_timeout)
int Driver::readPacket(uint8_t* buffer, int buffer_size, Time const& packet_timeout, Time const& first_byte_timeout)
{
return readPacket(buffer, buffer_size, packet_timeout, first_byte_timeout, getFlushOnTimeout());
}

int Driver::readPacket(uint8_t* buffer, int buffer_size, int packet_timeout, int first_byte_timeout, bool flush_on_timeout)
int Driver::readPacket(uint8_t* buffer, int buffer_size, Time const& packet_timeout, Time const& first_byte_timeout_unvalidated, bool flush_on_timeout)
{
if (first_byte_timeout > packet_timeout)
first_byte_timeout = -1;
Time first_byte_timeout;
if (first_byte_timeout_unvalidated > packet_timeout)
first_byte_timeout = Time();
else
first_byte_timeout = first_byte_timeout_unvalidated;

if (buffer_size < MAX_PACKET_SIZE)
throw length_error("readPacket(): provided buffer too small (got "
Expand All @@ -760,24 +755,19 @@ int Driver::readPacket(uint8_t* buffer, int buffer_size, int packet_timeout, int
"readPacket(): no packet in the internal buffer and no FD to read from");
}

if(!m_stream)
throw std::runtime_error("Driver::writePacket : invalid stream, did you forget to call open ?");

Timeout time_out;
bool read_something = false;
while(true) {

pair<int, bool> read_state = readPacketInternal(buffer, buffer_size);

int packet_size = read_state.first;

read_something = read_something || read_state.second;

if (packet_size > 0)
return packet_size;

// if there was no data to read _and_ packet_timeout is zero, we'll throw
if (packet_timeout == 0)
if (packet_timeout.isNull())
{
if (flush_on_timeout && internal_buffer_size)
{
Expand All @@ -791,9 +781,9 @@ int Driver::readPacket(uint8_t* buffer, int buffer_size, int packet_timeout, int
"readPacket(): no data to read while a packet_timeout of 0 was given");
}

int timeout;
Time timeout;
TimeoutError::TIMEOUT_TYPE timeout_type;
if (first_byte_timeout != -1 && !read_something)
if (!first_byte_timeout.isNull() && !read_something)
{
timeout = first_byte_timeout;
timeout_type = TimeoutError::FIRST_BYTE;
Expand All @@ -812,11 +802,11 @@ int Driver::readPacket(uint8_t* buffer, int buffer_size, int packet_timeout, int
}

// we still have time left to wait for arriving data. see how much
int remaining_timeout = time_out.timeLeft(timeout);
Time remaining_timeout = time_out.remaining(timeout);
try {
// calls select and waits until a new read can be actually performed (in the next
// while-iteration)
m_stream->waitRead(base::Time::fromMicroseconds(remaining_timeout * 1000));
m_stream->waitRead(remaining_timeout);
}
catch(TimeoutError& e)
{
Expand All @@ -828,18 +818,33 @@ int Driver::readPacket(uint8_t* buffer, int buffer_size, int packet_timeout, int
}
}

void Driver::setWriteTimeout(base::Time const& timeout)



int Driver::readPacket(uint8_t* buffer, int buffer_size, int packet_timeout, int first_byte_timeout)
{
return readPacket(buffer, buffer_size,
Time::fromMilliseconds(packet_timeout),
Time::fromMilliseconds(first_byte_timeout),
getFlushOnTimeout());
}

void Driver::setWriteTimeout(Time const& timeout)
{ m_write_timeout = timeout; }
base::Time Driver::getWriteTimeout() const
Time Driver::getWriteTimeout() const
{ return m_write_timeout; }

bool Driver::writePacket(uint8_t const* buffer, int buffer_size)
{
return writePacket(buffer, buffer_size, getWriteTimeout());
}
bool Driver::writePacket(uint8_t const* buffer, int buffer_size, base::Time const& timeout)
{ return writePacket(buffer, buffer_size, timeout.toMilliseconds()); }

bool Driver::writePacket(uint8_t const* buffer, int buffer_size, int timeout)
{
return writePacket(buffer, buffer_size, Time::fromMilliseconds(timeout));
}

bool Driver::writePacket(uint8_t const* buffer, int buffer_size, Time const& timeout)
{
if(!m_stream)
throw std::runtime_error("Driver::writePacket : invalid stream, did you forget to call open ?");
Expand All @@ -853,16 +858,15 @@ bool Driver::writePacket(uint8_t const* buffer, int buffer_size, int timeout)
written += c;

if (written == buffer_size) {
m_stats.stamp = base::Time::now();
m_stats.stamp = Time::now();
m_stats.tx += buffer_size;
return true;
}

if (time_out.elapsed())
throw TimeoutError(TimeoutError::PACKET, "writePacket(): timeout");

int remaining_timeout = time_out.timeLeft();
m_stream->waitWrite(base::Time::fromMicroseconds(remaining_timeout * 1000));
m_stream->waitWrite(time_out.remaining());
}
}

23 changes: 11 additions & 12 deletions src/Driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,19 +410,11 @@ class Driver
*/
int readPacket(uint8_t* buffer, int bufsize, base::Time const& packet_timeout);

/** @overload @deprecated
*
* @arg packet_timeout in milliseconds, see readPacket for semantics
* @arg first_byte_timeout in milliseconds, see readPacket for semantics
*/
int readPacket(uint8_t* buffer, int bufsize, int packet_timeout, int first_byte_timeout = -1);

/** @overload @deprecated
/** @overload
*
* @arg packet_timeout in milliseconds, see readPacket for semantics
* @arg first_byte_timeout in milliseconds, see readPacket for semantics
* Calls readPacket without a first byte timeout
*/
int readPacket(uint8_t* buffer, int bufsize, int packet_timeout, int first_byte_timeout, bool flush_on_timeout);
int readPacket(uint8_t* buffer, int bufsize, base::Time const& packet_timeout, base::Time const& first_byte_timeout);

/** Tries to read a packet from the file descriptor and to save it in the
* provided buffer. +packet_timeout+ is the timeout to receive a complete
Expand All @@ -442,7 +434,14 @@ class Driver
* @throws TimeoutError on timeout or no data, and UnixError on reading problems
* @returns the size of the packet
*/
int readPacket(uint8_t* buffer, int bufsize, base::Time const& packet_timeout, base::Time const& first_byte_timeout);
int readPacket(uint8_t* buffer, int bufsize, base::Time const& packet_timeout, base::Time const& first_byte_timeout, bool flush_on_timeout);

/** @overload @deprecated
*
* @arg packet_timeout in milliseconds, see readPacket for semantics
* @arg first_byte_timeout in milliseconds, see readPacket for semantics
*/
int readPacket(uint8_t* buffer, int bufsize, int packet_timeout, int first_byte_timeout = -1);

/** @overload
*
Expand Down
59 changes: 35 additions & 24 deletions src/Timeout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,59 @@

using namespace iodrivers_base;

Timeout::Timeout(unsigned int timeout)
: timeout(timeout) {
gettimeofday(&start_time, 0);
Timeout::Timeout(base::Time timeout)
: timeout(timeout)
, start_time(base::Time::now())
{
}

void Timeout::restart() {
gettimeofday(&start_time, 0);
void Timeout::restart()
{
start_time = base::Time::now();
}

bool Timeout::elapsed() const
{
return elapsed(timeout);
}

bool Timeout::elapsed(unsigned int timeout) const
bool Timeout::elapsed(base::Time timeout) const
{
return timeout <= (base::Time::now() - start_time);
}

base::Time Timeout::remaining() const
{
return remaining(timeout);
}

base::Time Timeout::remaining(base::Time timeout) const
{
base::Time elapsed = base::Time::now() - start_time;
if (timeout < elapsed)
return base::Time();
else
return (timeout - elapsed);
}

Timeout::Timeout(unsigned int timeout)
: timeout(base::Time::fromMilliseconds(timeout))
, start_time(base::Time::now())
{
timeval current_time;
gettimeofday(&current_time, 0);
unsigned int elapsed =
(current_time.tv_sec - start_time.tv_sec) * 1000
+ (static_cast<int>(current_time.tv_usec) -
static_cast<int>(start_time.tv_usec)) / 1000;
return timeout < elapsed;
}

unsigned int Timeout::timeLeft() const
{
return timeLeft(timeout);
return remaining(timeout).toMilliseconds();
}

unsigned int Timeout::timeLeft(unsigned int timeout) const
{
timeval current_time;
gettimeofday(&current_time, 0);
int elapsed =
(current_time.tv_sec - start_time.tv_sec) * 1000
+ (static_cast<int>(current_time.tv_usec) -
static_cast<int>(start_time.tv_usec)) / 1000;
if ((int)timeout < elapsed)
return 0;
return timeout - elapsed;
return remaining(base::Time::fromMilliseconds(timeout)).toMilliseconds();
}


bool Timeout::elapsed(unsigned int timeout) const
{
return elapsed(base::Time::fromMilliseconds(timeout));
}

Loading

0 comments on commit 404eac1

Please sign in to comment.