Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.16)
project(Candy VERSION 6.1.1)
project(Candy VERSION 6.1.2)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
36 changes: 23 additions & 13 deletions candy/src/peer/manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ int PeerManager::initSocket() {
this->pollThread = std::thread([&]() {
spdlog::debug("start thread: peer manager poll");
while (getClient().isRunning()) {
poll();
if (poll()) {
break;
}
}
getClient().shutdown();
spdlog::debug("stop thread: peer manager poll");
Expand Down Expand Up @@ -516,24 +518,32 @@ void PeerManager::handleRouteMessage(std::string buffer, const SocketAddress &ad
}
}

void PeerManager::poll() {
int PeerManager::poll() {
using Poco::Net::Socket;
using Poco::Net::SocketAddress;

if (this->socket.poll(Poco::Timespan(1, 0), Poco::Net::Socket::SELECT_READ)) {
std::string buffer(1500, 0);
SocketAddress address;
auto size = this->socket.receiveFrom(buffer.data(), buffer.size(), address);
if (size > 0) {
buffer.resize(size);

if (this->stun.address == address) {
handleStunResponse(buffer);
} else if (auto plaintext = decrypt(buffer)) {
handleMessage(std::move(*plaintext), address);
try {
if (this->socket.poll(Poco::Timespan(1, 0), Poco::Net::Socket::SELECT_READ)) {
std::string buffer(1500, 0);
SocketAddress address;
auto size = this->socket.receiveFrom(buffer.data(), buffer.size(), address);
if (size > 0) {
buffer.resize(size);

if (this->stun.address == address) {
handleStunResponse(buffer);
} else if (auto plaintext = decrypt(buffer)) {
handleMessage(std::move(*plaintext), address);
}
}
}
} catch (Poco::Net::ConnectionResetException &e) {
// 忽略 UDP 的连接 Reset, Windows 特有的问题
} catch (std::exception &e) {
spdlog::warn("peer_manager poll failed: {}", e.what());
return -1;
}
return 0;
}

std::optional<std::string> PeerManager::decrypt(const std::string &ciphertext) {
Expand Down
2 changes: 1 addition & 1 deletion candy/src/peer/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class PeerManager {
void handleForwardMessage(std::string buffer, const SocketAddress &address);
void handleDelayMessage(std::string buffer, const SocketAddress &address);
void handleRouteMessage(std::string buffer, const SocketAddress &address);
void poll();
int poll();

std::optional<std::string> decrypt(const std::string &ciphertext);
std::shared_ptr<EVP_CIPHER_CTX> decryptCtx;
Expand Down
Loading