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.6)
project(Candy VERSION 6.1.7)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
93 changes: 55 additions & 38 deletions candy/src/peer/manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,32 +108,40 @@ std::string PeerManager::getPassword() {

int PeerManager::handlePeerQueue() {
Msg msg = getClient().getPeerMsgQueue().read();
switch (msg.kind) {
case MsgKind::TIMEOUT:
break;
case MsgKind::PACKET:
handlePacket(std::move(msg));
break;
case MsgKind::TUNADDR:
if (startTickThread()) {
return -1;
}
if (handleTunAddr(std::move(msg))) {
return -1;
try {
switch (msg.kind) {
case MsgKind::TIMEOUT:
break;
case MsgKind::PACKET:
handlePacket(std::move(msg));
break;
case MsgKind::TUNADDR:
if (startTickThread()) {
return -1;
}
if (handleTunAddr(std::move(msg))) {
return -1;
}
break;
case MsgKind::SYSRT:
this->localP2PDisabled = true;
break;
case MsgKind::TRYP2P:
handleTryP2P(std::move(msg));
break;
case MsgKind::PUBINFO:
handlePubInfo(std::move(msg));
break;
default:
spdlog::warn("unexcepted peer message type: {}", static_cast<int>(msg.kind));
break;
}
break;
case MsgKind::SYSRT:
this->localP2PDisabled = true;
break;
case MsgKind::TRYP2P:
handleTryP2P(std::move(msg));
break;
case MsgKind::PUBINFO:
handlePubInfo(std::move(msg));
break;
default:
spdlog::warn("unexcepted peer message type: {}", static_cast<int>(msg.kind));
break;
} catch (const Poco::Exception &e) {
spdlog::warn("peer manager handle queue failed: msg_kind={}, error={}", static_cast<int>(msg.kind), e.message());
return 0;
} catch (const std::exception &e) {
spdlog::warn("peer manager handle queue failed: msg_kind={}, error={}", static_cast<int>(msg.kind), e.what());
return 0;
}
return 0;
}
Expand Down Expand Up @@ -247,23 +255,32 @@ int PeerManager::handlePubInfo(Msg msg) {
return 0;
}

{
try {
{

std::shared_lock lock(this->ipPeerMutex);
auto it = this->ipPeerMap.find(info->src);
if (it != this->ipPeerMap.end()) {
it->second.handlePubInfo(info->ip, info->port, info->local);
std::shared_lock lock(this->ipPeerMutex);
auto it = this->ipPeerMap.find(info->src);
if (it != this->ipPeerMap.end()) {
it->second.handlePubInfo(info->ip, info->port, info->local);
}
}
}

{
std::unique_lock lock(this->ipPeerMutex);
auto it = this->ipPeerMap.emplace(std::piecewise_construct, std::forward_as_tuple(info->src),
std::forward_as_tuple(info->src, this));
if (it.second) {
it.first->second.handlePubInfo(info->ip, info->port, info->local);
return 0;
{
std::unique_lock lock(this->ipPeerMutex);
auto it = this->ipPeerMap.emplace(std::piecewise_construct, std::forward_as_tuple(info->src),
std::forward_as_tuple(info->src, this));
if (it.second) {
it.first->second.handlePubInfo(info->ip, info->port, info->local);
return 0;
}
}
} catch (const Poco::Exception &e) {
spdlog::warn("peer manager handle pubinfo failed: src={}, ip={}, port={}, error={}", info->src.toString(),
info->ip.toString(), info->port, e.message());
return 0;
} catch (const std::exception &e) {
spdlog::warn("peer manager handle pubinfo failed: src={}, error={}", info->src.toString(), e.what());
return 0;
}

return 0;
Expand Down
5 changes: 4 additions & 1 deletion candy/src/peer/peer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,17 @@ std::string Peer::stateString(PeerState state) const {
}

void Peer::handlePubInfo(IP4 ip, uint16_t port, bool local) {
{
try {
std::unique_lock lock(this->socketAddressMutex);
if (local) {
this->local = SocketAddress(ip.toString(), port);
return;
}

this->wide = SocketAddress(ip.toString(), port);
} catch (const Poco::Exception &e) {
spdlog::warn("peer handle pubinfo failed: ip={}, port={}, error={}", ip.toString(), port, e.message());
return;
}

if (this->state == PeerState::CONNECTED) {
Expand Down
2 changes: 1 addition & 1 deletion candy/src/websocket/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ int WebSocketClient::handleWsConn() {
return 0;
}
if ((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) == Poco::Net::WebSocket::FRAME_OP_CLOSE) {
spdlog::info("websocket close: {}", buffer);
spdlog::info("websocket close: {:n}", spdlog::to_hex(buffer));
return -1;
}
if (length > 0) {
Expand Down