Skip to content

Commit f00943c

Browse files
committed
Various improvements and fixes
1 parent 7db6055 commit f00943c

File tree

4 files changed

+95
-62
lines changed

4 files changed

+95
-62
lines changed

Test/Socks5Extender/Socks5Connection.cpp

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,7 @@ namespace QuantumGate::Socks5Extender
284284
}
285285
}
286286
}
287-
else
288-
{
289-
LogDbg(L"%s: send/receive failed on connection %llu",
290-
m_Extender.GetName().c_str(), GetID());
291-
292-
SetDisconnectCondition();
293-
}
287+
else SetDisconnectCondition();
294288

295289
if (ShouldDisconnect())
296290
{
@@ -533,6 +527,21 @@ namespace QuantumGate::Socks5Extender
533527
}
534528
}
535529
}
530+
else
531+
{
532+
if (WSAGetLastError() != 0)
533+
{
534+
LogErr(L"%s: receive failed on endpoint %s for connection %llu (%s)",
535+
m_Extender.GetName().c_str(), m_Socket.GetPeerEndpoint().GetString().c_str(),
536+
GetID(), GetLastSysErrorString().c_str());
537+
}
538+
else
539+
{
540+
LogInfo(L"%s: socket closed on endpoint %s for connection %llu",
541+
m_Extender.GetName().c_str(), m_Socket.GetPeerEndpoint().GetString().c_str(),
542+
GetID());
543+
}
544+
}
536545
}
537546

538547
if (!m_ReceiveBuffer.IsEmpty()) m_Extender.SetConnectionReceiveEvent();
@@ -543,6 +552,12 @@ namespace QuantumGate::Socks5Extender
543552
if (m_Socket.GetIOStatus().CanWrite() && !m_SendBuffer.IsEmpty())
544553
{
545554
success = m_Socket.Send(m_SendBuffer);
555+
if (!success)
556+
{
557+
LogErr(L"%s: send failed on endpoint %s for connection %llu (%s)",
558+
m_Extender.GetName().c_str(), m_Socket.GetPeerEndpoint().GetString().c_str(),
559+
GetID(), GetLastSysErrorString().c_str());
560+
}
546561

547562
if (!m_SendBuffer.IsEmpty()) m_Extender.SetConnectionSendEvent();
548563

@@ -553,6 +568,12 @@ namespace QuantumGate::Socks5Extender
553568
}
554569
else success = false;
555570

571+
if (!success)
572+
{
573+
LogDbg(L"%s: send/receive failed on endpoint %s for connection %llu",
574+
m_Extender.GetName().c_str(), m_Socket.GetPeerEndpoint().GetString().c_str(), GetID());
575+
}
576+
556577
return success;
557578
}
558579

@@ -1188,10 +1209,18 @@ namespace QuantumGate::Socks5Extender
11881209

11891210
m_SendBuffer += buffer;
11901211

1191-
if (m_Socket.GetIOStatus().CanWrite())
1212+
if (m_Socket.UpdateIOStatus(0ms) && !m_Socket.GetIOStatus().HasException())
11921213
{
1193-
success = m_Socket.Send(m_SendBuffer);
1214+
if (m_Socket.GetIOStatus().CanWrite())
1215+
{
1216+
success = m_Socket.Send(m_SendBuffer);
1217+
if (success)
1218+
{
1219+
m_LastActiveSteadyTime = Util::GetCurrentSteadyTime();
1220+
}
1221+
}
11941222
}
1223+
else success = false;
11951224

11961225
// Any remaining data will be sent later
11971226
if (success && !m_SendBuffer.IsEmpty())
@@ -1202,25 +1231,6 @@ namespace QuantumGate::Socks5Extender
12021231
return success;
12031232
}
12041233

1205-
bool Connection::Send(Buffer&& buffer)
1206-
{
1207-
auto success = true;
1208-
1209-
if (m_Socket.GetIOStatus().CanWrite())
1210-
{
1211-
success = m_Socket.Send(buffer);
1212-
}
1213-
1214-
// Add any remaining data to be sent later
1215-
if (success && !buffer.IsEmpty())
1216-
{
1217-
m_SendBuffer += buffer;
1218-
m_Extender.SetConnectionSendEvent();
1219-
}
1220-
1221-
return success;
1222-
}
1223-
12241234
bool Connection::RelayReceivedData(const Size max_send, Size& sent)
12251235
{
12261236
assert(IsReady());

Test/Socks5Extender/Socks5Connection.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ namespace QuantumGate::Socks5Extender
7373
void FlushBuffers();
7474

7575
[[nodiscard]] bool Send(const BufferView buffer);
76-
[[nodiscard]] bool Send(Buffer&& buffer);
7776

7877
[[nodiscard]] bool DetermineProtocolVersion() noexcept;
7978

Test/Socks5Extender/Socks5Extender.cpp

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -834,41 +834,46 @@ namespace QuantumGate::Socks5Extender
834834

835835
if (success)
836836
{
837+
success = false;
838+
837839
// Remove if we fail
838840
auto sg = MakeScopeGuard([&]() noexcept { RemoveConnection(key); });
839841

840-
m_AllConnectionFDs.WithUniqueLock()->emplace_back(WSAPOLLFD{ .fd = shandle, .events = POLLRDNORM });
841-
842-
success = false;
843-
844-
const auto peer_ths = GetPeer(pluid);
845-
if (peer_ths)
842+
if (AddConnectionFD(shandle))
846843
{
847-
peer_ths->WithUniqueLock([&](Peer& peer)
844+
// Remove if we fail
845+
auto sg2 = MakeScopeGuard([&]() noexcept { RemoveConnectionFD(shandle); });
846+
847+
const auto peer_ths = GetPeer(pluid);
848+
if (peer_ths)
848849
{
849-
[[maybe_unused]] const auto [it, inserted] = peer.Connections.insert({ key, c});
850+
peer_ths->WithUniqueLock([&](Peer& peer)
851+
{
852+
[[maybe_unused]] const auto [it, inserted] = peer.Connections.insert({ key, c });
850853

851-
assert(inserted);
852-
success = inserted;
854+
assert(inserted);
855+
success = inserted;
853856

854-
peer.CalcMaxSndRcvSize();
857+
peer.CalcMaxSndRcvSize();
855858

856-
LogDbg(L"%s: MaxSndRcv size for peer %llu: %zu", GetName().c_str(), peer.ID, peer.MaxSndRcvSize);
857-
});
858-
}
859-
860-
if (success)
861-
{
862-
sg.Deactivate();
859+
LogDbg(L"%s: MaxSndRcv size for peer %llu: %zu", GetName().c_str(), peer.ID, peer.MaxSndRcvSize);
860+
});
861+
}
863862

864-
// Start doing some processing for speed
865-
c->WithUniqueLock([](Connection& connection)
863+
if (success)
866864
{
867-
auto didwork = false;
868-
if (connection.IsActive()) connection.ProcessEvents(didwork);
869-
});
865+
sg.Deactivate();
866+
sg2.Deactivate();
867+
868+
// Start doing some processing for speed
869+
c->WithUniqueLock([](Connection& connection)
870+
{
871+
auto didwork = false;
872+
if (connection.IsActive()) connection.ProcessEvents(didwork);
873+
});
870874

871-
SetConnectionSendEvent();
875+
SetConnectionSendEvent();
876+
}
872877
}
873878
}
874879

@@ -904,14 +909,7 @@ namespace QuantumGate::Socks5Extender
904909

905910
if (socket)
906911
{
907-
m_AllConnectionFDs.WithUniqueLock([&](auto& value)
908-
{
909-
const auto it = std::find_if(value.begin(), value.end(), [&](const auto& wsapollfd)
910-
{
911-
return (wsapollfd.fd == socket);
912-
});
913-
if (it != value.end()) value.erase(it);
914-
});
912+
RemoveConnectionFD(*socket);
915913
}
916914

917915
if (pluid)
@@ -933,6 +931,29 @@ namespace QuantumGate::Socks5Extender
933931
}
934932
}
935933

934+
[[nodiscard]] bool Extender::AddConnectionFD(const SOCKET s) noexcept
935+
{
936+
try
937+
{
938+
m_AllConnectionFDs.WithUniqueLock()->emplace_back(WSAPOLLFD{ .fd = s, .events = POLLRDNORM });
939+
}
940+
catch (...) { return false; }
941+
942+
return true;
943+
}
944+
945+
void Extender::RemoveConnectionFD(const SOCKET s) noexcept
946+
{
947+
m_AllConnectionFDs.WithUniqueLock([&](auto& value)
948+
{
949+
const auto it = std::find_if(value.begin(), value.end(), [&](const auto& wsapollfd)
950+
{
951+
return (wsapollfd.fd == s);
952+
});
953+
if (it != value.end()) value.erase(it);
954+
});
955+
}
956+
936957
void Extender::RemoveConnections(const std::vector<Connection::Key>& conn_list) noexcept
937958
{
938959
for (const auto key : conn_list)
@@ -1092,7 +1113,7 @@ namespace QuantumGate::Socks5Extender
10921113

10931114
if (connection.IsTimedOut())
10941115
{
1095-
LogDbg(L"%s: connection %llu timed out", GetName().c_str(), connection.GetID());
1116+
LogInfo(L"%s: connection %llu timed out; will disconnect", GetName().c_str(), connection.GetID());
10961117

10971118
connection.SetDisconnectCondition();
10981119
}

Test/Socks5Extender/Socks5Extender.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ namespace QuantumGate::Socks5Extender
140140
void RemoveConnections(const std::vector<Connection::Key>& conn_list) noexcept;
141141
[[nodiscard]] std::shared_ptr<Connection_ThS> GetConnection(const PeerLUID pluid, const Connection::ID cid) const noexcept;
142142

143+
[[nodiscard]] bool AddConnectionFD(const SOCKET s) noexcept;
144+
void RemoveConnectionFD(const SOCKET s) noexcept;
145+
143146
void Disconnect(Connection_ThS& c);
144147
void Disconnect(Connection& c);
145148
void DisconnectFor(const PeerLUID pluid);

0 commit comments

Comments
 (0)