From 4ee3e56db0a72f964122de88c67a52d8ecbbf612 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Fri, 24 Feb 2023 14:46:21 +0800 Subject: [PATCH] Handle login disconnection, ref ryzom/ryzomcore#628 --- ryzom/client/src/network_connection.cpp | 24 ++++++------- ryzom/client/src/quic_connection.cpp | 46 +++++++++++++------------ ryzom/client/src/quic_connection.h | 3 -- 3 files changed, 36 insertions(+), 37 deletions(-) diff --git a/ryzom/client/src/network_connection.cpp b/ryzom/client/src/network_connection.cpp index a1645092db..12d53ff433 100644 --- a/ryzom/client/src/network_connection.cpp +++ b/ryzom/client/src/network_connection.cpp @@ -891,13 +891,6 @@ bool CNetworkConnection::update() } } - if (m_UseQuic ? (m_QuicConnection.state() == CQuicConnection::Disconnected) : !m_Connection.connected()) - { - //if(!ClientCfg.Local) - // nlwarning("CNET[%p]: update() attempted whereas socket is not connected !", this); - return false; - } - try { // State automaton @@ -975,12 +968,19 @@ bool CNetworkConnection::update() } } - if (m_UseQuic && m_QuicConnection.state() == CQuicConnection::Disconnected) + if (m_UseQuic && (m_QuicConnection.state() == CQuicConnection::Disconnected)) { // Bye _ConnectionState = Disconnect; } + if (!m_UseQuic && !m_Connection.connected()) + { + //if(!ClientCfg.Local) + // nlwarning("CNET[%p]: update() attempted whereas socket is not connected !", this); + _ConnectionState = Disconnect; + } + //updateBufferizedPackets (); PacketLossGraph.addOneValue (getMeanPacketLoss ()); @@ -1230,12 +1230,12 @@ bool CNetworkConnection::stateLogin() { sendSystemLogin(); _LatestLoginTime = _UpdateTime; - // On UDP time out the login after 24 attempts (every 300ms, so after 7.2 seconds) - if ((!m_UseQuic) && (m_LoginAttempts > 24)) + // Time out the login after 24 attempts (every 300ms, so after 7.2 seconds) + if (m_LoginAttempts > 24) { m_LoginAttempts = 0; disconnect(); // will send disconnection message - nlwarning("CNET[%p]: Too many LOGIN attempts, connection problem", this); + nlwarning("CNET[%p]: Too many LOGIN attempts, connection problem (using %s)", this, m_UseQuic ? "QUIC" : "UDP"); return false; // exit now from loop, don't expect a new state } else @@ -3123,7 +3123,7 @@ void CNetworkConnection::reset() _TotalLostPackets = 0; _ConnectionQuality = false; - m_UseQuic = false; + m_UseQuic = true; _CurrentSmoothServerTick= 0; _SSTLastLocalTime= 0; diff --git a/ryzom/client/src/quic_connection.cpp b/ryzom/client/src/quic_connection.cpp index b2ce6a53e3..9797359a2b 100644 --- a/ryzom/client/src/quic_connection.cpp +++ b/ryzom/client/src/quic_connection.cpp @@ -52,18 +52,20 @@ class CQuicConnectionImpl Release, }; - CQuicConnectionImpl() - : Api(NULL) + CQuicConnectionImpl(CQuicConnection *self) + : Self(self) + , Api(NULL) , Registration(NULL) , Configuration(NULL) , Connection(NULL) , DesiredStateChange(NoChange) , BufferReads(0) , State(CQuicConnection::Disconnected) - , ReceiveEnable(0) + , ReceiveEnable(0) { } + CQuicConnection *Self; const QUIC_API_TABLE *Api; HQUIC Registration; HQUIC Configuration; @@ -101,13 +103,16 @@ class CQuicConnectionImpl void shutdown(); void close(); void release(); + + /// Received datagram (from quic threads) + void datagramReceived(const uint8 *buffer, uint32 length); }; CQuicConnection::CQuicConnection() #ifdef NL_CPP14 - : m(std::make_unique()) + : m(std::make_unique(this)) #else - : m(new CQuicConnectionImpl()) + : m(new CQuicConnectionImpl(this)) #endif { } @@ -121,17 +126,17 @@ CQuicConnection::~CQuicConnection() void CQuicConnectionImpl::init() { CQuicConnectionImpl *const m = this; - + if (MsQuic) { return; } - + // TestAndSet has acquire semantics, clear has release semantics, so we use clear to flag the events m->ConnectedFlag.testAndSet(); m->ShuttingDownFlag.testAndSet(); m->ShutdownFlag.testAndSet(); - + // Open library QUIC_STATUS status = MsQuicOpenVersion(QUIC_API_VERSION_2, (const void **)&MsQuic); if (QUIC_FAILED(status)) @@ -317,7 +322,7 @@ void CQuicConnectionImpl::update() // Asynchronous release if (!m->Connection - && m->DesiredStateChange == CQuicConnectionImpl::Release) + && m->DesiredStateChange == CQuicConnectionImpl::Release) { nlassert(m->State == CQuicConnection::Disconnected); m->release(); @@ -371,7 +376,8 @@ void CQuicConnectionImpl::update() // Shutting down, this is just a limbo state if (!m->ShuttingDownFlag.testAndSet()) { - if (m->State == CQuicConnection::Connected) + if (m->State == CQuicConnection::Connected + || m->State == CQuicConnection::Connecting) { m->State = CQuicConnection::Disconnecting; } @@ -406,7 +412,7 @@ void CQuicConnectionImpl::release() CBufFIFO *fifo = &access.value(); fifo->clear(); } - + // Close configuration if (m->Configuration) { @@ -452,8 +458,7 @@ _Function_class_(QUIC_CONNECTION_CALLBACK) #endif CQuicConnectionImpl::connectionCallback(HQUIC connection, void *context, QUIC_CONNECTION_EVENT *ev) { - CQuicConnection *const self = (CQuicConnection *)context; - CQuicConnectionImpl *const m = self->m.get(); + CQuicConnectionImpl *const m = (CQuicConnectionImpl *)context; QUIC_STATUS status = QUIC_STATUS_NOT_SUPPORTED; switch (ev->Type) { @@ -485,7 +490,7 @@ _Function_class_(QUIC_CONNECTION_CALLBACK) case QUIC_CONNECTION_EVENT_DATAGRAM_RECEIVED: nlinfo("Datagram received"); // YES PLEASE - self->datagramReceived(ev->DATAGRAM_RECEIVED.Buffer->Buffer, ev->DATAGRAM_RECEIVED.Buffer->Length); + m->datagramReceived(ev->DATAGRAM_RECEIVED.Buffer->Buffer, ev->DATAGRAM_RECEIVED.Buffer->Length); status = QUIC_STATUS_SUCCESS; break; case QUIC_CONNECTION_EVENT_DATAGRAM_STATE_CHANGED: @@ -541,12 +546,12 @@ bool CQuicConnection::receiveDatagram(NLMISC::CBitMemStream &msgin) { if (!m->ReceiveEnable) return false; - + int writes = m->BufferWrites; int reads = m->BufferReads; if (writes != reads) { - CFifoAccessor access(&m->Buffer); + CFifoAccessor access(&m->Buffer); // This block is mutex'd now CBufFIFO *fifo = &access.value(); if (fifo->empty()) { @@ -568,9 +573,10 @@ bool CQuicConnection::receiveDatagram(NLMISC::CBitMemStream &msgin) return false; } -void CQuicConnection::datagramReceived(const uint8 *buffer, uint32 length) +void CQuicConnectionImpl::datagramReceived(const uint8 *buffer, uint32 length) { - CFifoAccessor access(&m->Buffer); + CQuicConnectionImpl *const m = this; + CFifoAccessor access(&m->Buffer); // This block is mutex'd now CBufFIFO *fifo = &access.value(); fifo->push(buffer, length); ++m->BufferWrites; @@ -631,10 +637,6 @@ bool CQuicConnection::receiveDatagram(NLMISC::CBitMemStream &msgin) return false; } -void CQuicConnection::datagramReceived(const uint8 *buffer, uint32 length) -{ -} - #endif /* end of file */ diff --git a/ryzom/client/src/quic_connection.h b/ryzom/client/src/quic_connection.h index 7521325c2c..de622ede70 100644 --- a/ryzom/client/src/quic_connection.h +++ b/ryzom/client/src/quic_connection.h @@ -104,9 +104,6 @@ class CQuicConnection /// Internal implementation specific CUniquePtr m; - - /// Received datagram - void datagramReceived(const uint8 *buffer, uint32 length); }; #endif /* NL_QUIC_TRANSCEIVER_H */