diff --git a/CMakeLists.txt b/CMakeLists.txt index aad2649..ddcebb4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.18.4) -project(candy LANGUAGES C CXX VERSION 5.8.1) +project(candy LANGUAGES C CXX VERSION 5.8.2) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wfatal-errors") diff --git a/src/core/client.cc b/src/core/client.cc index 78ec0d3..f8083c1 100644 --- a/src/core/client.cc +++ b/src/core/client.cc @@ -174,6 +174,12 @@ int Client::startWsThread() { ws.setPingMessage(fmt::format("candy::{}::{}", CANDY_SYSTEM, CANDY_VERSION)); + // 只需要开 wsThread, 执行过程中会设置 tun 并开 tunThread. + this->wsThread = std::thread([&] { + this->handleWebSocketMessage(); + spdlog::debug("websocket client thread exit"); + }); + sendVirtualMacMessage(); if (!this->tunAddress.empty()) { @@ -193,13 +199,6 @@ int Client::startWsThread() { } sendDynamicAddressMessage(); } - - // 只需要开 wsThread, 执行过程中会设置 tun 并开 tunThread. - this->wsThread = std::thread([&] { - this->handleWebSocketMessage(); - spdlog::debug("websocket client thread exit"); - }); - return 0; } @@ -515,7 +514,7 @@ void Client::handlePeerConnMessage(WebSocketMessage &message) { return; } - if (peer.getState() != PeerState::CONNECTING) { + if (peer.getState() != PeerState::CONNECTING && peer.getState() != PeerState::PREPARING) { peer.updateState(PeerState::PREPARING); sendLocalPeerConnMessage(peer); return; diff --git a/src/core/client.h b/src/core/client.h index 18517e0..bd1884a 100644 --- a/src/core/client.h +++ b/src/core/client.h @@ -5,6 +5,7 @@ #include "core/message.h" #include "peer/peer.h" #include "tun/tun.h" +#include "utility/random.h" #include "websocket/client.h" #include #include @@ -138,7 +139,7 @@ class Client { std::map ipPeerMap; std::thread udpThread; std::thread tickThread; - uint64_t tickTick = std::rand(); + uint64_t tickTick = randomUint32(); uint32_t discoveryInterval; // Route diff --git a/src/peer/peer.h b/src/peer/peer.h index a1124be..f8c8e10 100644 --- a/src/peer/peer.h +++ b/src/peer/peer.h @@ -2,6 +2,7 @@ #ifndef CANDY_PEER_PEER_H #define CANDY_PEER_PEER_H +#include "utility/random.h" #include #include #include @@ -29,7 +30,7 @@ class PeerInfo { } wide, local, real; uint8_t ack = 0; uint32_t count = 0; - uint32_t tick = std::rand(); + uint32_t tick = randomUint32(); uint32_t retry = RETRY_MIN; int32_t delay = DELAY_LIMIT; diff --git a/src/websocket/client.cc b/src/websocket/client.cc index 4cc522b..758565d 100644 --- a/src/websocket/client.cc +++ b/src/websocket/client.cc @@ -35,7 +35,6 @@ int WebSocketClient::connect(const std::string &address) { spdlog::critical("invalid websocket scheme: {}", address); return -1; } - this->ws->setReceiveTimeout(Poco::Timespan(this->timeout, 0)); this->timestamp = Time::bootTime(); return 0; } catch (std::exception &e) { @@ -63,6 +62,13 @@ int WebSocketClient::read(WebSocketMessage &message) { } try { + Poco::Net::Socket::SocketList readList, writeList, exceptList; + readList.push_back(*this->ws.get()); + if (Poco::Net::Socket::select(readList, writeList, exceptList, Poco::Timespan(this->timeout, 0)) <= 0 || + readList.empty()) { + return 0; + } + char buffer[1500] = {0}; int flags = 0; int length = this->ws->receiveFrame(buffer, sizeof(buffer), flags);