mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Migrate Timer from location/nearby to third_party/nearby
PiperOrigin-RevId: 501928835
This commit is contained in:
committed by
Copybara-Service
parent
868e42f5f2
commit
f94aa7548b
@@ -17,6 +17,7 @@ cc_library(
|
||||
name = "types",
|
||||
srcs = [
|
||||
"log_message.cc",
|
||||
"timer.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"atomic_boolean.h",
|
||||
@@ -33,13 +34,16 @@ cc_library(
|
||||
"scheduled_executor.h",
|
||||
"settable_future.h",
|
||||
"submittable_executor.h",
|
||||
"timer.h",
|
||||
"utils.h",
|
||||
],
|
||||
deps = [
|
||||
"//base",
|
||||
"//base:stringprintf",
|
||||
"//internal/platform:base",
|
||||
"//internal/platform:logging",
|
||||
"//internal/platform/implementation:types",
|
||||
"//internal/platform/implementation/windows/generated:types",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -221,6 +225,7 @@ cc_test(
|
||||
"scheduled_executor_test.cc",
|
||||
"submittable_executor_test.cc",
|
||||
"thread_pool_test.cc",
|
||||
"timer_test.cc",
|
||||
"utils_test.cc",
|
||||
],
|
||||
copts = ["-Ithird_party/nearby/internal/platform/implementation/windows/generated -DCORE_ADAPTER_DLL"],
|
||||
|
||||
@@ -56,6 +56,7 @@
|
||||
#include "internal/platform/implementation/windows/server_sync.h"
|
||||
#include "internal/platform/implementation/windows/settable_future.h"
|
||||
#include "internal/platform/implementation/windows/submittable_executor.h"
|
||||
#include "internal/platform/implementation/windows/timer.h"
|
||||
#include "internal/platform/implementation/windows/utils.h"
|
||||
#include "internal/platform/implementation/windows/webrtc.h"
|
||||
#include "internal/platform/implementation/windows/wifi.h"
|
||||
@@ -307,5 +308,9 @@ absl::StatusOr<WebResponse> ImplementationPlatform::SendRequest(
|
||||
return http_loader.GetResponse();
|
||||
}
|
||||
|
||||
std::unique_ptr<Timer> ImplementationPlatform::CreateTimer() {
|
||||
return std::make_unique<windows::Timer>();
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
} // namespace nearby
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
// Copyright 2021 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "internal/platform/implementation/windows/timer.h"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
|
||||
Timer::~Timer() { Stop(); }
|
||||
|
||||
bool Timer::Create(int delay, int interval, std::function<void()> callback) {
|
||||
if ((delay < 0) || (interval < 0)) {
|
||||
NEARBY_LOGS(WARNING) << "Delay and interval shouldn\'t be negative value.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (timer_queue_handle_ != nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
timer_queue_handle_ = CreateTimerQueue();
|
||||
if (timer_queue_handle_ == nullptr) {
|
||||
NEARBY_LOGS(ERROR) << "Failed to create timer queue.";
|
||||
return false;
|
||||
}
|
||||
|
||||
delay_ = delay;
|
||||
interval_ = interval;
|
||||
callback_ = std::move(callback);
|
||||
|
||||
if (!CreateTimerQueueTimer(&handle_, timer_queue_handle_,
|
||||
static_cast<WAITORTIMERCALLBACK>(TimerRoutine),
|
||||
&callback_, delay, interval, WT_EXECUTEDEFAULT)) {
|
||||
if (!DeleteTimerQueueEx(timer_queue_handle_, nullptr)) {
|
||||
NEARBY_LOGS(ERROR) << "Failed to create timer in timer queue.";
|
||||
}
|
||||
timer_queue_handle_ = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Timer::Stop() {
|
||||
if (timer_queue_handle_ == nullptr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!DeleteTimerQueueTimer(timer_queue_handle_, handle_, nullptr)) {
|
||||
if (GetLastError() != ERROR_IO_PENDING) {
|
||||
NEARBY_LOGS(ERROR) << "Failed to delete timer from timer queue.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
handle_ = nullptr;
|
||||
|
||||
if (!DeleteTimerQueueEx(timer_queue_handle_, nullptr)) {
|
||||
NEARBY_LOGS(ERROR) << "Failed to delete timer queue.";
|
||||
return false;
|
||||
}
|
||||
|
||||
timer_queue_handle_ = nullptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Timer::FireNow() {
|
||||
if (!timer_queue_handle_ || !callback_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
callback_();
|
||||
return true;
|
||||
}
|
||||
|
||||
void CALLBACK Timer::TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired) {
|
||||
std::function<void()>* callback =
|
||||
reinterpret_cast<std::function<void()>*>(lpParam);
|
||||
if (*callback != NULL) {
|
||||
(*callback)();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace windows
|
||||
} // namespace nearby
|
||||
@@ -0,0 +1,49 @@
|
||||
// Copyright 2021 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef PLATFORM_IMPL_WINDOWS_TIMER_H_
|
||||
#define PLATFORM_IMPL_WINDOWS_TIMER_H_
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "internal/platform/implementation/timer.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
|
||||
class Timer : public api::Timer {
|
||||
public:
|
||||
Timer() = default;
|
||||
~Timer() override;
|
||||
|
||||
bool Create(int delay, int interval, std::function<void()> callback) override;
|
||||
bool Stop() override;
|
||||
bool FireNow() override;
|
||||
|
||||
private:
|
||||
static void CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired);
|
||||
|
||||
int delay_;
|
||||
int interval_;
|
||||
std::function<void()> callback_;
|
||||
HANDLE handle_ = NULL;
|
||||
HANDLE timer_queue_handle_ = NULL;
|
||||
};
|
||||
|
||||
} // namespace windows
|
||||
} // namespace nearby
|
||||
|
||||
#endif // PLATFORM_IMPL_WINDOWS_TIMER_H_
|
||||
@@ -0,0 +1,69 @@
|
||||
// Copyright 2021 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "internal/platform/implementation/timer.h"
|
||||
|
||||
#include <chrono> // NOLINT
|
||||
#include <functional> // NOLINT
|
||||
#include <memory>
|
||||
#include <thread> // NOLINT
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "internal/platform/implementation/platform.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
namespace {
|
||||
|
||||
TEST(Timer, TestCreateTimer) {
|
||||
int count = 0;
|
||||
std::function<void()> callback = [&count]() { ++count; };
|
||||
|
||||
std::unique_ptr<nearby::api::Timer> timer =
|
||||
nearby::api::ImplementationPlatform::CreateTimer();
|
||||
|
||||
ASSERT_TRUE(timer != nullptr);
|
||||
EXPECT_FALSE(timer->Create(-100, 0, callback));
|
||||
EXPECT_TRUE(timer->Stop());
|
||||
}
|
||||
|
||||
// This test case cannot run on Google3
|
||||
TEST(Timer, DISABLED_TestRepeatTimer) {
|
||||
int count = 0;
|
||||
std::function<void()> callback = [&count]() { count++; };
|
||||
|
||||
std::unique_ptr<nearby::api::Timer> timer =
|
||||
nearby::api::ImplementationPlatform::CreateTimer();
|
||||
|
||||
ASSERT_TRUE(timer != nullptr);
|
||||
EXPECT_TRUE(timer->Create(300, 300, callback));
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
EXPECT_TRUE(timer->Stop());
|
||||
EXPECT_EQ(count, 3);
|
||||
}
|
||||
|
||||
TEST(Timer, DISABLED_TestFireNow) {
|
||||
int count = 0;
|
||||
std::function<void()> callback = [&count]() { ++count; };
|
||||
auto timer = nearby::api::ImplementationPlatform::CreateTimer();
|
||||
EXPECT_TRUE(timer != nullptr);
|
||||
EXPECT_TRUE(timer->Create(3000, 3000, callback));
|
||||
EXPECT_TRUE(timer->FireNow());
|
||||
EXPECT_TRUE(timer->Stop());
|
||||
EXPECT_EQ(count, 1);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace windows
|
||||
} // namespace nearby
|
||||
Reference in New Issue
Block a user