Skip to content

Commit

Permalink
Make SocketState a scoped enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
wilx committed Nov 8, 2024
1 parent 98834f1 commit 3ac0f53
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion include/log4cplus/helpers/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
namespace log4cplus {
namespace helpers {

enum SocketState { ok,
enum class SocketState { ok,
not_opened,
bad_address,
connection_failed,
Expand Down
21 changes: 12 additions & 9 deletions src/socket-unix.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ openSocket(tstring const & host, unsigned short port, bool udp, bool ipv6,
if (::listen(sock_holder.sock, 10))
return INVALID_SOCKET_VALUE;

state = ok;
state = SocketState::ok;
return to_log4cplus_socket (sock_holder.detach ());
}

Expand Down Expand Up @@ -243,7 +243,7 @@ connectSocket(const tstring& hostn, unsigned short port, bool udp, bool ipv6,
// No address succeeded.
return INVALID_SOCKET_VALUE;

state = ok;
state = SocketState::ok;
return to_log4cplus_socket (sock_holder.detach ());
}

Expand Down Expand Up @@ -338,7 +338,7 @@ acceptSocket(SOCKET_TYPE sock, SocketState& state)
;

if(clientSock != INVALID_OS_SOCKET_VALUE) {
state = ok;
state = SocketState::ok;
}

return to_log4cplus_socket (clientSock);
Expand Down Expand Up @@ -556,7 +556,7 @@ ServerSocket::ServerSocket(unsigned short port, bool udp /*= false*/,

error:;
err = get_last_socket_error ();
state = not_opened;
state = SocketState::not_opened;

if (sock != INVALID_SOCKET_VALUE)
closeSocket (sock);
Expand Down Expand Up @@ -598,7 +598,7 @@ ServerSocket::accept ()
continue;

set_last_socket_error (errno);
return Socket (INVALID_SOCKET_VALUE, not_opened, errno);
return Socket (INVALID_SOCKET_VALUE, SocketState::not_opened, errno);

// Timeout. This should not happen though.
case 0:
Expand All @@ -624,20 +624,22 @@ ServerSocket::accept ()
LOG4CPLUS_TEXT ("ServerSocket::accept- read() failed: ")
+ helpers::convertIntegerToString (eno));
set_last_socket_error (eno);
return Socket (INVALID_SOCKET_VALUE, not_opened, eno);
return Socket (INVALID_SOCKET_VALUE,
SocketState::not_opened, eno);
}

// Return Socket with state set to accept_interrupted.

return Socket (INVALID_SOCKET_VALUE, accept_interrupted, 0);
return Socket (INVALID_SOCKET_VALUE,
SocketState::accept_interrupted, 0);
}
else if ((accept_fd.revents & POLLIN) == POLLIN)
{
helpers::getLogLog ().debug (
LOG4CPLUS_TEXT ("ServerSocket::accept- ")
LOG4CPLUS_TEXT ("accepting connection"));

SocketState st = not_opened;
SocketState st = SocketState::not_opened;
SOCKET_TYPE clientSock = acceptSocket (sock, st);
int eno = 0;
if (clientSock == INVALID_SOCKET_VALUE)
Expand All @@ -646,7 +648,8 @@ ServerSocket::accept ()
return Socket (clientSock, st, eno);
}
else
return Socket (INVALID_SOCKET_VALUE, not_opened, 0);
return Socket (INVALID_SOCKET_VALUE, SocketState::not_opened,
0);
}
}
while (true);
Expand Down
13 changes: 7 additions & 6 deletions src/socket-win32.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ openSocket(tstring const & host, unsigned short port, bool udp, bool ipv6,
if (::listen(sock_holder.sock, 10) != 0)
goto error;

state = ok;
state = SocketState::ok;
return to_log4cplus_socket (sock_holder.detach());

error:
Expand Down Expand Up @@ -281,7 +281,7 @@ connectSocket(const tstring& hostn, unsigned short port, bool udp, bool ipv6,
return INVALID_SOCKET_VALUE;
}

state = ok;
state = SocketState::ok;
return to_log4cplus_socket (sock_holder.detach());
}

Expand All @@ -294,7 +294,7 @@ acceptSocket(SOCKET_TYPE sock, SocketState & state)
SOCKET connected_socket = ::accept (to_os_socket (sock), nullptr, nullptr);

if (connected_socket != INVALID_OS_SOCKET_VALUE)
state = ok;
state = SocketState::ok;
else
set_last_socket_error (WSAGetLastError ());

Expand Down Expand Up @@ -637,7 +637,8 @@ ServerSocket::accept ()

// Return Socket with state set to accept_interrupted.

return Socket (INVALID_SOCKET_VALUE, accept_interrupted, 0);
return Socket (INVALID_SOCKET_VALUE,
SocketState::accept_interrupted, 0);
}

// This is accept_ev.
Expand All @@ -649,7 +650,7 @@ ServerSocket::accept ()

// Finally, call accept().

SocketState st = not_opened;
SocketState st = SocketState::not_opened;
SOCKET_TYPE clientSock = acceptSocket (sock, st);
int eno = 0;
if (clientSock == INVALID_SOCKET_VALUE)
Expand Down Expand Up @@ -682,7 +683,7 @@ error:;
WSACloseEvent (accept_ev);

set_last_socket_error (eno);
return Socket (INVALID_SOCKET_VALUE, not_opened, eno);
return Socket (INVALID_SOCKET_VALUE, SocketState::not_opened, eno);
}


Expand Down
4 changes: 2 additions & 2 deletions src/socket.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extern LOG4CPLUS_EXPORT SOCKET_TYPE const INVALID_SOCKET_VALUE

AbstractSocket::AbstractSocket()
: sock(INVALID_SOCKET_VALUE),
state(not_opened),
state(SocketState::not_opened),
err(0)
{
}
Expand Down Expand Up @@ -84,7 +84,7 @@ AbstractSocket::close()
{
closeSocket(sock);
sock = INVALID_SOCKET_VALUE;
state = not_opened;
state = SocketState::not_opened;
}
}

Expand Down

0 comments on commit 3ac0f53

Please sign in to comment.