Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use early return when initialising socket #76

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading