Skip to content

Commit

Permalink
Improve socket handling
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Feb 1, 2025
1 parent 8b17e0f commit 46bc92f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions cpp/s2p/s2p_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,26 @@ bool S2pThread::IsRunning() const

void S2pThread::Execute() const
{
int fd = -1;
while (server.IsRunning()) {
if (const int fd = server.Accept(); fd != -1) {
ExecuteCommand(fd);
if (fd == -1) {
fd = server.Accept();
}

if (fd != -1 && !ExecuteCommand(fd)) {
close(fd);
fd = -1;
}
}
}

void S2pThread::ExecuteCommand(int fd) const
bool S2pThread::ExecuteCommand(int fd) const
{
CommandContext context(fd, *s2p_logger);
try {
if (context.ReadCommand()) {
exec(context);
return true;
}
}
catch (const IoException &e) {
Expand All @@ -73,4 +79,6 @@ void S2pThread::ExecuteCommand(int fd) const
// Ignore
}
}

return false;
}
2 changes: 1 addition & 1 deletion cpp/s2p/s2p_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class S2pThread
private:

void Execute() const;
void ExecuteCommand(int) const;
bool ExecuteCommand(int) const;

callback exec;

Expand Down

0 comments on commit 46bc92f

Please sign in to comment.