#include "socket.h"
-#include <stdio.h>
+#include <cstdio>
#include <iostream>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
+#include <cstdlib>
+#include <cstring>
+#include <cerrno>
#include <sstream>
#include <iomanip>
#include "util/string.h"
// Equality (address family, address and port must be equal)
bool Address::operator==(const Address &address)
{
- if(address.m_addr_family != m_addr_family || address.m_port != m_port)
+ if (address.m_addr_family != m_addr_family || address.m_port != m_port)
return false;
- else if(m_addr_family == AF_INET)
- {
+
+ if (m_addr_family == AF_INET) {
return m_address.ipv4.sin_addr.s_addr ==
address.m_address.ipv4.sin_addr.s_addr;
}
- else if(m_addr_family == AF_INET6)
- {
+
+ if (m_addr_family == AF_INET6) {
return memcmp(m_address.ipv6.sin6_addr.s6_addr,
address.m_address.ipv6.sin6_addr.s6_addr, 16) == 0;
}
- else
- return false;
+
+ return false;
}
bool Address::operator!=(const Address &address)
{
if (m_addr_family == AF_INET) {
return m_address.ipv4.sin_addr.s_addr == 0;
- } else if (m_addr_family == AF_INET6) {
+ }
+
+ if (m_addr_family == AF_INET6) {
static const char zero[16] = {0};
return memcmp(m_address.ipv6.sin6_addr.s6_addr,
zero, 16) == 0;
bool UDPSocket::init(bool ipv6, bool noExceptions)
{
- if (g_sockets_initialized == false) {
+ if (!g_sockets_initialized) {
dstream << "Sockets not initialized" << std::endl;
return false;
}
if (m_handle <= 0) {
if (noExceptions) {
return false;
- } else {
- throw SocketException(std::string("Failed to create socket: error ")
- + itos(LAST_SOCKET_ERR()));
}
+
+ throw SocketException(std::string("Failed to create socket: error ")
+ + itos(LAST_SOCKET_ERR()));
}
setTimeoutMs(0);
int UDPSocket::Receive(Address & sender, void *data, int size)
{
// Return on timeout
- if(WaitData(m_timeout_ms) == false)
+ if (!WaitData(m_timeout_ms))
return -1;
int received;
if (result == 0)
return false;
- else if (result < 0 && (errno == EINTR || errno == EBADF)) {
+
+ if (result < 0 && (errno == EINTR || errno == EBADF)) {
// N.B. select() fails when sockets are destroyed on Connection's dtor
// with EBADF. Instead of doing tricky synchronization, allow this
// thread to exit but don't throw an exception.
return false;
- } else if (result < 0) {
- dstream << (int) m_handle << ": Select failed: "
- << strerror(errno) << std::endl;
+ }
+
+ if (result < 0) {
+ dstream << m_handle << ": Select failed: " << strerror(errno) << std::endl;
#ifdef _WIN32
int e = WSAGetLastError();
#endif
throw SocketException("Select failed");
- } else if(FD_ISSET(m_handle, &readset) == false) {
+ } else if (!FD_ISSET(m_handle, &readset)) {
// No data
return false;
}