Skip to content

Commit

Permalink
deadline_timer の非推奨に対応し steady_timer へ切り替える
Browse files Browse the repository at this point in the history
  • Loading branch information
torikizi committed Dec 16, 2024
1 parent f56f811 commit 05f9789
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion include/sora/data_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class DataChannel : public std::enable_shared_from_this<DataChannel> {
labels_;
std::weak_ptr<DataChannelObserver> observer_;
std::function<void(boost::system::error_code)> on_close_;
boost::asio::deadline_timer timer_;
boost::asio::steady_timer timer_;
};

} // namespace sora
Expand Down
4 changes: 2 additions & 2 deletions include/sora/sora_signaling.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ class SoraSignaling : public std::enable_shared_from_this<SoraSignaling>,
std::string video_mid_;
std::string audio_mid_;

boost::asio::deadline_timer connection_timeout_timer_;
boost::asio::deadline_timer closing_timeout_timer_;
boost::asio::steady_timer connection_timeout_timer_;
boost::asio::steady_timer closing_timeout_timer_;
std::function<void(boost::system::error_code ec)> on_ws_close_;
webrtc::PeerConnectionInterface::IceConnectionState ice_state_ =
webrtc::PeerConnectionInterface::kIceConnectionNew;
Expand Down
2 changes: 1 addition & 1 deletion include/sora/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class Websocket {
};
std::vector<std::unique_ptr<WriteData>> write_data_;

boost::asio::deadline_timer close_timeout_timer_;
boost::asio::steady_timer close_timeout_timer_;

http_header_value user_agent_;

Expand Down
4 changes: 2 additions & 2 deletions src/data_channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void DataChannel::Close(const webrtc::DataBuffer& disconnect_message,
return;
}

timer_.expires_from_now(
boost::posix_time::milliseconds((int)(disconnect_wait_timeout * 1000)));
timer_.expires_after(
std::chrono::milliseconds((int)(disconnect_wait_timeout * 1000)));
timer_.async_wait([on_close](boost::system::error_code ec) {
if (ec == boost::asio::error::operation_aborted) {
return;
Expand Down
21 changes: 10 additions & 11 deletions src/sora_signaling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,8 @@ void SoraSignaling::Redirect(std::string url) {
}

// 接続タイムアウト用の処理
self->connection_timeout_timer_.expires_from_now(
boost::posix_time::seconds(
self->config_.websocket_connection_timeout));
self->connection_timeout_timer_.expires_after(
std::chrono::seconds(self->config_.websocket_connection_timeout));
self->connection_timeout_timer_.async_wait(
[self](boost::system::error_code ec) {
if (ec) {
Expand Down Expand Up @@ -217,7 +216,7 @@ void SoraSignaling::OnRedirect(boost::system::error_code ec,
}

boost::system::error_code tec;
connection_timeout_timer_.cancel(tec);
connection_timeout_timer_.cancel();

state_ = State::Connected;
ws_ = ws;
Expand Down Expand Up @@ -692,8 +691,8 @@ void SoraSignaling::DoInternalDisconnect(
SendOnSignalingMessage(SoraSignalingType::DATACHANNEL,
SoraSignalingDirection::SENT, std::move(text));
} else if (!using_datachannel_ && ws_connected_) {
closing_timeout_timer_.expires_from_now(
boost::posix_time::seconds(config_.websocket_close_timeout));
closing_timeout_timer_.expires_after(
std::chrono::seconds(config_.websocket_close_timeout));
closing_timeout_timer_.async_wait(
[self = shared_from_this()](boost::system::error_code ec) {
if (ec) {
Expand All @@ -704,7 +703,7 @@ void SoraSignaling::DoInternalDisconnect(
on_ws_close_ = [self = shared_from_this(),
on_close](boost::system::error_code ec) {
boost::system::error_code tec;
self->closing_timeout_timer_.cancel(tec);
self->closing_timeout_timer_.cancel();
auto reason = self->ws_->reason();
if (ec == boost::asio::error::operation_aborted) {
// タイムアウトによる切断なので 4999 をコールバックする
Expand Down Expand Up @@ -798,7 +797,7 @@ void SoraSignaling::OnConnect(boost::system::error_code ec,
}

boost::system::error_code tec;
connection_timeout_timer_.cancel(tec);
connection_timeout_timer_.cancel();

RTC_LOG(LS_INFO) << "Signaling Websocket is connected: url=" << url;
state_ = State::Connected;
Expand Down Expand Up @@ -1173,7 +1172,7 @@ void SoraSignaling::DoConnect() {
dc_.reset(new DataChannel(*config_.io_context, shared_from_this()));

// 接続タイムアウト用の処理
connection_timeout_timer_.expires_from_now(boost::posix_time::seconds(30));
connection_timeout_timer_.expires_after(std::chrono::seconds(30));
connection_timeout_timer_.async_wait(
[self = shared_from_this()](boost::system::error_code ec) {
if (ec) {
Expand Down Expand Up @@ -1409,8 +1408,8 @@ bool SoraSignaling::SendDataChannel(const std::string& label,

void SoraSignaling::Clear() {
boost::system::error_code tec;
connection_timeout_timer_.cancel(tec);
closing_timeout_timer_.cancel(tec);
connection_timeout_timer_.cancel();
closing_timeout_timer_.cancel();
connecting_wss_.clear();
selected_signaling_url_.store("");
connected_signaling_url_.store("");
Expand Down
5 changes: 2 additions & 3 deletions src/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,7 @@ void Websocket::DoClose(close_callback_t on_close, int timeout_seconds) {
std::bind(&Websocket::OnClose, this, on_close, std::placeholders::_1));
}

close_timeout_timer_.expires_from_now(
boost::posix_time::seconds(timeout_seconds));
close_timeout_timer_.expires_after(std::chrono::seconds(timeout_seconds));
close_timeout_timer_.async_wait(
[on_close, timeout_seconds, this](boost::system::error_code ec) {
if (ec) {
Expand All @@ -566,7 +565,7 @@ void Websocket::OnClose(close_callback_t on_close,
<< " ec=" << ec.message() << " code=" << reason().code
<< " reason=" << reason().reason;
boost::system::error_code tec;
close_timeout_timer_.cancel(tec);
close_timeout_timer_.cancel();
on_close(ec);
}

Expand Down
6 changes: 3 additions & 3 deletions test/connect_disconnect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ class SoraClient : public std::enable_shared_from_this<SoraClient>,
signals.async_wait(
[this](const boost::system::error_code&, int) { conn_->Disconnect(); });

timer_.reset(new boost::asio::deadline_timer(*ioc_));
timer_->expires_from_now(boost::posix_time::seconds(1));
timer_.reset(new boost::asio::steady_timer(*ioc_));
timer_->expires_after(std::chrono::seconds(1));
timer_->async_wait([this](boost::system::error_code ec) {
if (ec) {
return;
Expand Down Expand Up @@ -124,7 +124,7 @@ class SoraClient : public std::enable_shared_from_this<SoraClient>,
rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track_;
std::shared_ptr<sora::SoraSignaling> conn_;
std::unique_ptr<boost::asio::io_context> ioc_;
std::unique_ptr<boost::asio::deadline_timer> timer_;
std::unique_ptr<boost::asio::steady_timer> timer_;
};

int main(int argc, char* argv[]) {
Expand Down
6 changes: 3 additions & 3 deletions test/datachannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class SoraClient : public std::enable_shared_from_this<SoraClient>,
signals.async_wait(
[this](const boost::system::error_code&, int) { conn_->Disconnect(); });

timer_.reset(new boost::asio::deadline_timer(*ioc_));
timer_->expires_from_now(boost::posix_time::seconds(3));
timer_.reset(new boost::asio::steady_timer(*ioc_));
timer_->expires_after(std::chrono::seconds(3));
timer_->async_wait([this, labels](boost::system::error_code ec) {
if (ec) {
return;
Expand Down Expand Up @@ -133,7 +133,7 @@ class SoraClient : public std::enable_shared_from_this<SoraClient>,
SoraClientConfig config_;
std::shared_ptr<sora::SoraSignaling> conn_;
std::unique_ptr<boost::asio::io_context> ioc_;
std::unique_ptr<boost::asio::deadline_timer> timer_;
std::unique_ptr<boost::asio::steady_timer> timer_;
std::set<std::string> opened_;
bool ok_ = false;
};
Expand Down
6 changes: 3 additions & 3 deletions test/e2e.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ class SoraClient : public std::enable_shared_from_this<SoraClient>,
signals.async_wait(
[this](const boost::system::error_code&, int) { conn_->Disconnect(); });

timer_.reset(new boost::asio::deadline_timer(*ioc_));
timer_->expires_from_now(boost::posix_time::seconds(5));
timer_.reset(new boost::asio::steady_timer(*ioc_));
timer_->expires_after(std::chrono::seconds(5));
timer_->async_wait([this](boost::system::error_code ec) {
if (ec) {
return;
Expand Down Expand Up @@ -150,7 +150,7 @@ class SoraClient : public std::enable_shared_from_this<SoraClient>,
std::shared_ptr<sora::SoraClientContext> context_;
std::shared_ptr<sora::SoraSignaling> conn_;
std::unique_ptr<boost::asio::io_context> ioc_;
std::unique_ptr<boost::asio::deadline_timer> timer_;
std::unique_ptr<boost::asio::steady_timer> timer_;
rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> video_source_;
rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track_;
std::atomic<bool> ok_{false};
Expand Down

0 comments on commit 05f9789

Please sign in to comment.