From 3e8b10d813659100593ec0da433c8aabb3fd1633 Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sun, 13 Jun 2021 17:29:43 +0200 Subject: [PATCH] zm::Select: Convert API to std::chrono --- src/zm_comms.cpp | 16 ++-------------- src/zm_comms.h | 11 ++++------- src/zm_rtp_ctrl.cpp | 2 +- src/zm_rtp_data.cpp | 2 +- src/zm_rtsp.cpp | 2 +- 5 files changed, 9 insertions(+), 24 deletions(-) diff --git a/src/zm_comms.cpp b/src/zm_comms.cpp index 17fdf8501..2f57f9506 100644 --- a/src/zm_comms.cpp +++ b/src/zm_comms.cpp @@ -615,19 +615,7 @@ bool zm::TcpUnixServer::accept(TcpUnixSocket *&newSocket) { return true; } -void zm::Select::setTimeout(int timeout) { - mTimeout.tv_sec = timeout; - mTimeout.tv_usec = 0; - mHasTimeout = true; -} - -void zm::Select::setTimeout(double timeout) { - mTimeout.tv_sec = int(timeout); - mTimeout.tv_usec = suseconds_t((timeout - mTimeout.tv_sec) * 1000000.0); - mHasTimeout = true; -} - -void zm::Select::setTimeout(timeval timeout) { +void zm::Select::setTimeout(Microseconds timeout) { mTimeout = timeout; mHasTimeout = true; } @@ -703,7 +691,7 @@ void zm::Select::clearWriters() { } int zm::Select::wait() { - timeval tempTimeout = mTimeout; + timeval tempTimeout = zm::chrono::duration_cast(mTimeout); timeval *selectTimeout = mHasTimeout ? &tempTimeout : nullptr; fd_set rfds; diff --git a/src/zm_comms.h b/src/zm_comms.h index af6459ca2..7e7329d5d 100644 --- a/src/zm_comms.h +++ b/src/zm_comms.h @@ -22,6 +22,7 @@ #include "zm_exception.h" #include "zm_logger.h" +#include "zm_time.h" #include #include #include @@ -560,13 +561,9 @@ class Select { typedef std::vector CommsList; Select() : mHasTimeout(false), mMaxFd(-1) {} - explicit Select(timeval timeout) : mMaxFd(-1) { setTimeout(timeout); } - explicit Select(int timeout) : mMaxFd(-1) { setTimeout(timeout); } - explicit Select(double timeout) : mMaxFd(-1) { setTimeout(timeout); } + explicit Select(Microseconds timeout) : mMaxFd(-1) { setTimeout(timeout); } - void setTimeout(int timeout); - void setTimeout(double timeout); - void setTimeout(timeval timeout); + void setTimeout(Microseconds timeout); void clearTimeout(); void calcMaxFd(); @@ -590,7 +587,7 @@ class Select { CommsList mReadable; CommsList mWriteable; bool mHasTimeout; - timeval mTimeout; + Microseconds mTimeout; int mMaxFd; }; diff --git a/src/zm_rtp_ctrl.cpp b/src/zm_rtp_ctrl.cpp index 2d1c8f303..265d594d4 100644 --- a/src/zm_rtp_ctrl.cpp +++ b/src/zm_rtp_ctrl.cpp @@ -269,7 +269,7 @@ void RtpCtrlThread::Run() { // The only reason I can think of why we would have a timeout period is so that we can regularly send RR packets. // Why 10 seconds? If anything I think this should be whatever timeout value was given in the DESCRIBE response - zm::Select select(10 ); + zm::Select select(Seconds(10)); select.addReader( &rtpCtrlServer ); unsigned char buffer[ZM_NETWORK_BUFSIZ]; diff --git a/src/zm_rtp_data.cpp b/src/zm_rtp_data.cpp index 3fcd558f8..e8ce628f2 100644 --- a/src/zm_rtp_data.cpp +++ b/src/zm_rtp_data.cpp @@ -76,7 +76,7 @@ void RtpDataThread::Run() { } Debug(3, "Bound to %s:%d", mRtpSource.getLocalHost().c_str(), mRtpSource.getLocalDataPort()); - zm::Select select(3); + zm::Select select(Seconds(3)); select.addReader(&rtpDataSocket); unsigned char buffer[ZM_NETWORK_BUFSIZ]; diff --git a/src/zm_rtsp.cpp b/src/zm_rtsp.cpp index 6eebd34e3..5f69e8374 100644 --- a/src/zm_rtsp.cpp +++ b/src/zm_rtsp.cpp @@ -621,7 +621,7 @@ void RtspThread::Run() { RtpDataThread rtpDataThread( *this, *source ); RtpCtrlThread rtpCtrlThread( *this, *source ); - zm::Select select(double(config.http_timeout)/1000.0 ); + zm::Select select(Milliseconds(config.http_timeout)); select.addReader( &mRtspSocket ); Buffer buffer( ZM_NETWORK_BUFSIZ );