Skip to content

Commit

Permalink
修复 Poco SSL 锁导致的无法在多个线程同时读写的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthora committed May 13, 2024
1 parent a0c187a commit 1dc543d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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")

Expand Down
15 changes: 7 additions & 8 deletions src/core/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/core/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <functional>
#include <map>
Expand Down Expand Up @@ -138,7 +139,7 @@ class Client {
std::map<uint32_t, PeerInfo> ipPeerMap;
std::thread udpThread;
std::thread tickThread;
uint64_t tickTick = std::rand();
uint64_t tickTick = randomUint32();
uint32_t discoveryInterval;

// Route
Expand Down
3 changes: 2 additions & 1 deletion src/peer/peer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#ifndef CANDY_PEER_PEER_H
#define CANDY_PEER_PEER_H

#include "utility/random.h"
#include <any>
#include <cstdint>
#include <string>
Expand Down Expand Up @@ -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;

Expand Down
8 changes: 7 additions & 1 deletion src/websocket/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1dc543d

Please sign in to comment.