Skip to content

Commit

Permalink
Small improvements to avoid ambiguous variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
nhjschulz committed Nov 21, 2024
1 parent 161a1e6 commit bfe2447
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
33 changes: 16 additions & 17 deletions lib/WiFiNative/src/WifiClientPosix.inl
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@
*****************************************************************************/

/**
* WifiClient instance to socket mapping.
* WifiClient instance to pollfd (socket data) mapping.
* Local definition as stored type is OS dependend, and WifiClient is generic.
*/
static std::map<const WiFiClient*, struct pollfd> gSockets;
static std::map<const WiFiClient*, struct pollfd> gConnections;

/**
* The invalid socket value.
Expand All @@ -78,9 +78,9 @@ WiFiClient::~WiFiClient()
uint8_t WiFiClient::connected() const
{
bool isConnected = false;
auto iterSock = gSockets.find(this);
auto iterSock = gConnections.find(this);

if (gSockets.end() != iterSock)
if (gConnections.end() != iterSock)
{
isConnected = (iterSock->second.fd != SOCK_INVAL);
}
Expand All @@ -91,7 +91,7 @@ uint8_t WiFiClient::connected() const
uint8_t WiFiClient::connect(const IPAddress& addr, uint16_t port)
{
uint8_t retval = 0U;
int socket = SOCK_INVAL;
int socketFd = SOCK_INVAL;

if (0U != connected())
{
Expand All @@ -101,8 +101,8 @@ uint8_t WiFiClient::connect(const IPAddress& addr, uint16_t port)
stop();
}

socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (SOCK_INVAL != socket)
socketFd = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (SOCK_INVAL != socketFd)
{
struct sockaddr_in serverAddr;

Expand All @@ -111,15 +111,15 @@ uint8_t WiFiClient::connect(const IPAddress& addr, uint16_t port)
serverAddr.sin_addr.s_addr = htonl(addr);
serverAddr.sin_port = htons(port);

if (0 == ::connect(socket, reinterpret_cast<struct sockaddr*>(&serverAddr), sizeof(serverAddr)))
if (0 == ::connect(socketFd, reinterpret_cast<struct sockaddr*>(&serverAddr), sizeof(serverAddr)))
{
const int one = 1;

if (-1 == ::setsockopt(socket, SOL_TCP, TCP_NODELAY, &one, sizeof(one)))
if (-1 == ::setsockopt(socketFd, SOL_TCP, TCP_NODELAY, &one, sizeof(one)))
{
LOG_ERROR("%s:%s", "setsockopt", strerror(errno));
}
else if (-1 == ::fcntl(socket, F_SETFL, ::fcntl(socket, F_GETFL) | O_NONBLOCK))
else if (-1 == ::fcntl(socketFd, F_SETFL, ::fcntl(socketFd, F_GETFL) | O_NONBLOCK))
{
LOG_ERROR("%s:%s", "fcntl", strerror(errno));
}
Expand All @@ -140,7 +140,7 @@ uint8_t WiFiClient::connect(const IPAddress& addr, uint16_t port)
else
{
/* Store poll data and listen to input data event. */
gSockets[this] = {.fd = socket, .events = POLLIN, .revents = 0};
gConnections[this] = {.fd = socketFd, .events = POLLIN, .revents = 0};
}
}
else
Expand All @@ -155,17 +155,16 @@ void WiFiClient::stop()
{
if (0U != connected())
{
auto iterSock = gSockets.find(this);
auto iterSock = gConnections.find(this);

if (gSockets.end() != iterSock)
if (gConnections.end() != iterSock)
{
if (0 != ::close(iterSock->second.fd))
{
LOG_ERROR("%s:%s", "close", strerror(errno));
}
gConnections.erase(iterSock);
}

gSockets.erase(iterSock);
}
}

Expand All @@ -176,7 +175,7 @@ size_t WiFiClient::write(const uint8_t* buffer, size_t size)

if ((0U < size) && (0U != connected()))
{
auto pollFd = gSockets[this];
auto pollFd = gConnections[this];

while ((0U < remaining) && (retries < SOCK_WRITE_RETRY))
{
Expand Down Expand Up @@ -211,7 +210,7 @@ int WiFiClient::read(uint8_t* buffer, size_t size)

if (0U != connected())
{
auto pollFd = gSockets[this];
auto pollFd = gConnections[this];

if (-1 != ::poll(&pollFd, 1, 10))
{
Expand Down
16 changes: 8 additions & 8 deletions lib/WiFiNative/src/WifiClientWinsock.inl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ uint8_t WiFiClient::connected() const
uint8_t WiFiClient::connect(const IPAddress& addr, uint16_t port)
{
uint8_t retval = 0U;
SOCKET socket = INVALID_SOCKET;
SOCKET socketHandle = INVALID_SOCKET;

if (0U != connected())
{
Expand All @@ -95,8 +95,8 @@ uint8_t WiFiClient::connect(const IPAddress& addr, uint16_t port)
stop();
}

socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET != socket)
socketHandle = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET != socketHandle)
{
struct sockaddr_in serverAddr;

Expand All @@ -105,20 +105,20 @@ uint8_t WiFiClient::connect(const IPAddress& addr, uint16_t port)
serverAddr.sin_addr.s_addr = htonl(addr);
serverAddr.sin_port = htons(port);

if (0 == ::connect(socket, reinterpret_cast<struct sockaddr*>(&serverAddr), sizeof(serverAddr)))
if (0 == ::connect(socketHandle, reinterpret_cast<struct sockaddr*>(&serverAddr), sizeof(serverAddr)))
{
const int one = 1;
DWORD nodelay = TRUE;

if (SOCKET_ERROR ==
setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)))
setsockopt(socketHandle, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)))
{
LOG_ERROR("setsockopt error %ld\n", WSAGetLastError());
}
else
{
u_long mode = 0U;
int rt = ioctlsocket(socket, FIONBIO, &mode);
int rt = ioctlsocket(socketHandle, FIONBIO, &mode); /* Enable non-blocking IO. */

if (rt != NO_ERROR)
{
Expand All @@ -141,7 +141,7 @@ uint8_t WiFiClient::connect(const IPAddress& addr, uint16_t port)
}
else
{
gSockets[this] = socket; /* Store open socket for this class. */
gSockets[this] = socketHandle; /* Store open socket for this class. */
}
}
else
Expand All @@ -157,7 +157,7 @@ void WiFiClient::stop()
if (0U != connected())
{
auto iterSock = gSockets.find(this);

if (gSockets.end() != iterSock)
{
if (SOCKET_ERROR == closesocket(iterSock->second))
Expand Down

0 comments on commit bfe2447

Please sign in to comment.