Skip to content

Commit d640c88

Browse files
committed
refactor: replace uv_hrtime with std chono and remove libuv dependency
1 parent 7268bca commit d640c88

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

src/BluetoothHciL2Socket.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <sys/socket.h>
44
#include <sys/types.h>
55
#include <unistd.h>
6-
#include <uv.h>
76
#include <stdexcept>
87

98
#include "BluetoothHciL2Socket.h"

src/BluetoothHciSocket.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
#include <sys/socket.h>
44
#include <sys/types.h>
55
#include <unistd.h>
6-
#include <uv.h>
76
#include <stdexcept>
7+
#include <chrono>
88

99
#include "BluetoothHciSocket.h"
1010

11+
static uint64_t highResTimeNs() {
12+
auto now = std::chrono::steady_clock::now();
13+
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
14+
return static_cast<uint64_t>(ns);
15+
}
16+
1117
BluetoothHciSocket::BluetoothHciSocket(const Napi::CallbackInfo& info) :
1218
Napi::ObjectWrap<BluetoothHciSocket>(info),
1319
stopFlag(false),
@@ -344,13 +350,13 @@ bool BluetoothHciSocket::kernelConnectWorkArounds(char * data, int length) {
344350
l2socket_ptr = it_connecting->second;
345351
l2socket_ptr->disconnect();
346352
l2socket_ptr->connect();
347-
l2socket_ptr->setExpires(uv_hrtime() + L2_CONNECT_TIMEOUT);
353+
l2socket_ptr->setExpires(highResTimeNs() + L2_CONNECT_TIMEOUT);
348354
} else {
349355
// Create a new L2CAP socket and initiate connection
350356
bdaddr_t bdaddr_src = {};
351357
memcpy(bdaddr_src.b, _address, sizeof(bdaddr_src.b));
352358

353-
uint64_t expires = uv_hrtime() + L2_CONNECT_TIMEOUT;
359+
uint64_t expires = highResTimeNs() + L2_CONNECT_TIMEOUT;
354360

355361
l2socket_ptr = std::make_shared<BluetoothHciL2Socket> (
356362
this, & bdaddr_src, _addressType, & bdaddr_dst, dst_type, expires);
@@ -620,7 +626,7 @@ void BluetoothHciSocket::Cleanup(const Napi::CallbackInfo& info) {
620626
Napi::Env env = info.Env(); // Get the current efnvironment
621627
Napi::HandleScope scope(env); // Create a handle scope for memory management
622628

623-
auto now = uv_hrtime();
629+
auto now = highResTimeNs();
624630

625631
for (auto it = this->_l2sockets_connecting.cbegin(); it != this->_l2sockets_connecting.cend() /* not hoisted */; /* no increment */) {
626632
if (now < it->second->getExpires()) {

0 commit comments

Comments
 (0)