Skip to content

Commit

Permalink
Use early return when initialising socket
Browse files Browse the repository at this point in the history
This is more in line with the modern Code HQ coding standard.
  • Loading branch information
Colin Ward authored and hitman-codehq committed Aug 6, 2024
1 parent cfde278 commit 061cb22
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions StdSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,49 +75,50 @@ int RSocket::open(const char *a_host, unsigned short a_port)

WSAData wsaData;

if (WSAStartup(MAKEWORD(2, 2), &wsaData) == 0)
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
return KErrGeneral;
}

#endif /* WIN32 */

if ((m_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET)
{
if ((m_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET)
if (a_host)
{
if (a_host)
if ((hostEnt = gethostbyname(a_host)) != nullptr)
{
if ((hostEnt = gethostbyname(a_host)) != nullptr)
inAddr = (struct in_addr *) hostEnt->h_addr_list[0];

sockAddr.sin_family = hostEnt->h_addrtype;
sockAddr.sin_port = htons(a_port);
sockAddr.sin_addr = *inAddr;

if (connect(m_socket, (struct sockaddr *) &sockAddr, sizeof(sockAddr)) >= 0)
{
inAddr = (struct in_addr *) hostEnt->h_addr_list[0];

sockAddr.sin_family = hostEnt->h_addrtype;
sockAddr.sin_port = htons(a_port);
sockAddr.sin_addr = *inAddr;

if (connect(m_socket, (struct sockaddr *) &sockAddr, sizeof(sockAddr)) >= 0)
{
retVal = KErrNone;
}
else
{
retVal = KErrNotOpen;

close();
}
retVal = KErrNone;
}
else
{
retVal = KErrHostNotFound;
retVal = KErrNotOpen;

close();
}
}
else
{
/* When running as a host, enable SO_LINGER to ensure that socket is cleanly closed and can thus be */
/* immediately reopened for the next client connection */
struct linger Linger = { 1, 0 };
retVal = KErrHostNotFound;
}
}
else
{
/* When running as a host, enable SO_LINGER to ensure that socket is cleanly closed and can thus be */
/* immediately reopened for the next client connection */
struct linger Linger = { 1, 0 };

if (setsockopt(m_socket, SOL_SOCKET, SO_LINGER, (const char *) &Linger, sizeof(Linger)) == 0)
{
retVal = KErrNone;
}
if (setsockopt(m_socket, SOL_SOCKET, SO_LINGER, (const char *) &Linger, sizeof(Linger)) == 0)
{
retVal = KErrNone;
}
}
}
Expand Down

0 comments on commit 061cb22

Please sign in to comment.