diff --git a/source/3ds/platform.cpp b/source/3ds/platform.cpp index b3f0517..4f2bacf 100644 --- a/source/3ds/platform.cpp +++ b/source/3ds/platform.cpp @@ -484,10 +484,13 @@ public: /// \param func_ Thread entry point privateData_t (std::function &&func_) : thread (nullptr), func (std::move (func_)) { + // use next-lower priority s32 priority = 0x30; svcGetThreadPriority (&priority, CUR_THREAD_HANDLE); + priority = std::clamp (priority, 0x18, 0x3F - 1) + 1; - thread = threadCreate (&privateData_t::threadFunc, this, STACK_SIZE, priority, -1, false); + // use appcore + thread = threadCreate (&privateData_t::threadFunc, this, STACK_SIZE, priority, 0, false); assert (thread); } diff --git a/source/ftpServer.cpp b/source/ftpServer.cpp index f471e68..307ff4c 100644 --- a/source/ftpServer.cpp +++ b/source/ftpServer.cpp @@ -299,6 +299,13 @@ void FtpServer::handleNetworkFound () void FtpServer::handleNetworkLost () { { + // destroy sessions + std::vector sessions; + LOCKED (sessions = std::move (m_sessions)); + } + + { + // destroy command socket UniqueSocket sock; LOCKED (sock = std::move (m_socket)); } @@ -427,7 +434,14 @@ void FtpServer::loop () if (m_socket) { Socket::PollInfo info{*m_socket, POLLIN, 0}; - if (Socket::poll (&info, 1, 0ms) > 0) + auto const rc = Socket::poll (&info, 1, 0ms); + if (rc < 0) + { + handleNetworkLost (); + return; + } + + if (rc > 0 && (info.revents & POLLIN)) { auto socket = m_socket->accept (); if (socket) @@ -436,7 +450,10 @@ void FtpServer::loop () LOCKED (m_sessions.emplace_back (std::move (session))); } else + { handleNetworkLost (); + return; + } } } diff --git a/source/ftpSession.cpp b/source/ftpSession.cpp index 8d36acb..9a7958d 100644 --- a/source/ftpSession.cpp +++ b/source/ftpSession.cpp @@ -525,7 +525,7 @@ bool FtpSession::poll (std::vector const &sessions_) switch (session->m_state) { case State::COMMAND: - assert (false); + std::abort (); break; case State::DATA_CONNECT: @@ -571,6 +571,7 @@ bool FtpSession::poll (std::vector const &sessions_) break; } } + break; } } } @@ -1535,15 +1536,14 @@ void FtpSession::sendResponse (char const *fmt_, ...) // try to write data immediately assert (m_commandSocket); - auto const bytes = - m_commandSocket->write (m_responseBuffer.usedArea (), m_responseBuffer.usedSize ()); - if (bytes < 0 && errno != EWOULDBLOCK) - closeCommand (); - else if (bytes > 0) + auto const bytes = m_commandSocket->write (m_responseBuffer); + if (bytes <= 0) { - m_responseBuffer.markFree (bytes); - m_responseBuffer.coalesce (); + if (bytes == 0 || errno != EWOULDBLOCK) + closeCommand (); } + else + m_responseBuffer.coalesce (); } void FtpSession::sendResponse (std::string_view const response_) @@ -1686,7 +1686,7 @@ bool FtpSession::listTransfer () } // send any pending data - auto const rc = m_dataSocket->write (m_xferBuffer.usedArea (), m_xferBuffer.usedSize ()); + auto const rc = m_dataSocket->write (m_xferBuffer); if (rc <= 0) { // error sending data @@ -1699,7 +1699,6 @@ bool FtpSession::listTransfer () } // we can try to send more data - m_xferBuffer.markFree (rc); return true; } @@ -1735,7 +1734,7 @@ bool FtpSession::retrieveTransfer () } // send any pending data - auto const rc = m_dataSocket->write (m_xferBuffer.usedArea (), m_xferBuffer.usedSize ()); + auto const rc = m_dataSocket->write (m_xferBuffer); if (rc <= 0) { // error sending data @@ -1749,7 +1748,6 @@ bool FtpSession::retrieveTransfer () // we can try to read/send more data LOCKED (m_filePosition += rc); - m_xferBuffer.markFree (rc); return true; } @@ -1759,11 +1757,8 @@ bool FtpSession::storeTransfer () { m_xferBuffer.clear (); - auto const buffer = m_xferBuffer.freeArea (); - auto const size = m_xferBuffer.freeSize (); - // we have written all the received data, so try to get some more - auto const rc = m_dataSocket->read (buffer, size); + auto const rc = m_dataSocket->read (m_xferBuffer); if (rc < 0) { // failed to read data @@ -1782,9 +1777,6 @@ bool FtpSession::storeTransfer () setState (State::COMMAND, true, true); return false; } - - // we received some data - m_xferBuffer.markUsed (rc); } // write any pending data @@ -1928,8 +1920,8 @@ void FtpSession::HELP (char const *args_) sendResponse ("214-\r\n" "The following commands are recognized\r\n" " ABOR ALLO APPE CDUP CWD DELE FEAT HELP LIST MDTM MKD MLSD MLST MODE\r\n" - " NLST NOOP OPTS PASS PASV PORT PWD QUIT REST RETR RMD RNFR RNTO STAT\r\n" - " STOR STOU STRU SYST TYPE USER XCUP XCWD XMKD XPWD XRMD\r\n" + " NLST NOOP OPTS PASS PASV PORT PWD QUIT REST RETR RMD RNFR RNTO SITE\r\n" + " SIZE STAT STOR STOU STRU SYST TYPE USER XCUP XCWD XMKD XPWD XRMD\r\n" "214 End\r\n"); } diff --git a/source/socket.cpp b/source/socket.cpp index 942f469..18f2bee 100644 --- a/source/socket.cpp +++ b/source/socket.cpp @@ -171,7 +171,7 @@ bool Socket::shutdown (int const how_) { if (::shutdown (m_fd, how_) != 0) { - info ("shutdown: %s\n", std::strerror (errno)); + error ("shutdown: %s\n", std::strerror (errno)); return false; } diff --git a/source/switch/init.c b/source/switch/init.c index ae9c6d9..eecea57 100644 --- a/source/switch/init.c +++ b/source/switch/init.c @@ -58,12 +58,10 @@ void userAppInit () plInitialize (); psmInitialize (); nifmInitialize (NifmServiceType_User); - - if (R_FAILED (socketInitialize (&s_socketInitConfig))) - return; + socketInitialize (&s_socketInitConfig); #ifndef NDEBUG - // s_fd = nxlinkStdioForDebug (); + // s_fd = nxlinkStdioForDebug (); #endif }