General system stability improvements to enhance the user's experience

This commit is contained in:
Michael Theall 2020-04-25 15:49:50 -05:00
parent 3f31371193
commit d237b04b3b
5 changed files with 38 additions and 28 deletions

View File

@ -484,10 +484,13 @@ public:
/// \param func_ Thread entry point /// \param func_ Thread entry point
privateData_t (std::function<void ()> &&func_) : thread (nullptr), func (std::move (func_)) privateData_t (std::function<void ()> &&func_) : thread (nullptr), func (std::move (func_))
{ {
// use next-lower priority
s32 priority = 0x30; s32 priority = 0x30;
svcGetThreadPriority (&priority, CUR_THREAD_HANDLE); svcGetThreadPriority (&priority, CUR_THREAD_HANDLE);
priority = std::clamp<s32> (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); assert (thread);
} }

View File

@ -299,6 +299,13 @@ void FtpServer::handleNetworkFound ()
void FtpServer::handleNetworkLost () void FtpServer::handleNetworkLost ()
{ {
{ {
// destroy sessions
std::vector<UniqueFtpSession> sessions;
LOCKED (sessions = std::move (m_sessions));
}
{
// destroy command socket
UniqueSocket sock; UniqueSocket sock;
LOCKED (sock = std::move (m_socket)); LOCKED (sock = std::move (m_socket));
} }
@ -427,7 +434,14 @@ void FtpServer::loop ()
if (m_socket) if (m_socket)
{ {
Socket::PollInfo info{*m_socket, POLLIN, 0}; 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 (); auto socket = m_socket->accept ();
if (socket) if (socket)
@ -436,7 +450,10 @@ void FtpServer::loop ()
LOCKED (m_sessions.emplace_back (std::move (session))); LOCKED (m_sessions.emplace_back (std::move (session)));
} }
else else
{
handleNetworkLost (); handleNetworkLost ();
return;
}
} }
} }

View File

@ -525,7 +525,7 @@ bool FtpSession::poll (std::vector<UniqueFtpSession> const &sessions_)
switch (session->m_state) switch (session->m_state)
{ {
case State::COMMAND: case State::COMMAND:
assert (false); std::abort ();
break; break;
case State::DATA_CONNECT: case State::DATA_CONNECT:
@ -571,6 +571,7 @@ bool FtpSession::poll (std::vector<UniqueFtpSession> const &sessions_)
break; break;
} }
} }
break;
} }
} }
} }
@ -1535,15 +1536,14 @@ void FtpSession::sendResponse (char const *fmt_, ...)
// try to write data immediately // try to write data immediately
assert (m_commandSocket); assert (m_commandSocket);
auto const bytes = auto const bytes = m_commandSocket->write (m_responseBuffer);
m_commandSocket->write (m_responseBuffer.usedArea (), m_responseBuffer.usedSize ()); if (bytes <= 0)
if (bytes < 0 && errno != EWOULDBLOCK)
closeCommand ();
else if (bytes > 0)
{ {
m_responseBuffer.markFree (bytes); if (bytes == 0 || errno != EWOULDBLOCK)
m_responseBuffer.coalesce (); closeCommand ();
} }
else
m_responseBuffer.coalesce ();
} }
void FtpSession::sendResponse (std::string_view const response_) void FtpSession::sendResponse (std::string_view const response_)
@ -1686,7 +1686,7 @@ bool FtpSession::listTransfer ()
} }
// send any pending data // 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) if (rc <= 0)
{ {
// error sending data // error sending data
@ -1699,7 +1699,6 @@ bool FtpSession::listTransfer ()
} }
// we can try to send more data // we can try to send more data
m_xferBuffer.markFree (rc);
return true; return true;
} }
@ -1735,7 +1734,7 @@ bool FtpSession::retrieveTransfer ()
} }
// send any pending data // 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) if (rc <= 0)
{ {
// error sending data // error sending data
@ -1749,7 +1748,6 @@ bool FtpSession::retrieveTransfer ()
// we can try to read/send more data // we can try to read/send more data
LOCKED (m_filePosition += rc); LOCKED (m_filePosition += rc);
m_xferBuffer.markFree (rc);
return true; return true;
} }
@ -1759,11 +1757,8 @@ bool FtpSession::storeTransfer ()
{ {
m_xferBuffer.clear (); 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 // 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) if (rc < 0)
{ {
// failed to read data // failed to read data
@ -1782,9 +1777,6 @@ bool FtpSession::storeTransfer ()
setState (State::COMMAND, true, true); setState (State::COMMAND, true, true);
return false; return false;
} }
// we received some data
m_xferBuffer.markUsed (rc);
} }
// write any pending data // write any pending data
@ -1928,8 +1920,8 @@ void FtpSession::HELP (char const *args_)
sendResponse ("214-\r\n" sendResponse ("214-\r\n"
"The following commands are recognized\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" " 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" " NLST NOOP OPTS PASS PASV PORT PWD QUIT REST RETR RMD RNFR RNTO SITE\r\n"
" STOR STOU STRU SYST TYPE USER XCUP XCWD XMKD XPWD XRMD\r\n" " SIZE STAT STOR STOU STRU SYST TYPE USER XCUP XCWD XMKD XPWD XRMD\r\n"
"214 End\r\n"); "214 End\r\n");
} }

View File

@ -171,7 +171,7 @@ bool Socket::shutdown (int const how_)
{ {
if (::shutdown (m_fd, how_) != 0) if (::shutdown (m_fd, how_) != 0)
{ {
info ("shutdown: %s\n", std::strerror (errno)); error ("shutdown: %s\n", std::strerror (errno));
return false; return false;
} }

View File

@ -58,9 +58,7 @@ void userAppInit ()
plInitialize (); plInitialize ();
psmInitialize (); psmInitialize ();
nifmInitialize (NifmServiceType_User); nifmInitialize (NifmServiceType_User);
socketInitialize (&s_socketInitConfig);
if (R_FAILED (socketInitialize (&s_socketInitConfig)))
return;
#ifndef NDEBUG #ifndef NDEBUG
// s_fd = nxlinkStdioForDebug (); // s_fd = nxlinkStdioForDebug ();