|
|
@ -17,7 +17,6 @@
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "UDPSocket.h"
|
|
|
|
#include "UDPSocket.h"
|
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
|
|
|
@ -26,26 +25,36 @@
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstring>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(HAVE_LOG_H)
|
|
|
|
|
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define LogError(fmt, ...) ::fprintf(stderr, fmt "\n", ## __VA_ARGS__)
|
|
|
|
|
|
|
|
#define LogInfo(fmt, ...) ::fprintf(stderr, fmt "\n", ## __VA_ARGS__)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
CUDPSocket::CUDPSocket(const std::string& address, unsigned int port) :
|
|
|
|
CUDPSocket::CUDPSocket(const std::string& address, unsigned int port) :
|
|
|
|
m_address(address),
|
|
|
|
m_address_save(address),
|
|
|
|
m_port(port),
|
|
|
|
m_port_save(port),
|
|
|
|
m_fd(-1)
|
|
|
|
m_counter(0U)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
assert(!address.empty());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
WSAData data;
|
|
|
|
WSAData data;
|
|
|
|
int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data);
|
|
|
|
int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data);
|
|
|
|
if (wsaRet != 0)
|
|
|
|
if (wsaRet != 0)
|
|
|
|
LogError("Error from WSAStartup");
|
|
|
|
LogError("Error from WSAStartup");
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (int i = 0; i < UDP_SOCKET_MAX; i++) {
|
|
|
|
|
|
|
|
m_address[i] = "";
|
|
|
|
|
|
|
|
m_port[i] = 0U;
|
|
|
|
|
|
|
|
m_af[i] = 0U;
|
|
|
|
|
|
|
|
m_fd[i] = -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CUDPSocket::CUDPSocket(unsigned int port) :
|
|
|
|
CUDPSocket::CUDPSocket(unsigned int port) :
|
|
|
|
m_address(),
|
|
|
|
m_address_save(),
|
|
|
|
m_port(port),
|
|
|
|
m_port_save(port),
|
|
|
|
m_fd(-1)
|
|
|
|
m_counter(0U)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
WSAData data;
|
|
|
|
WSAData data;
|
|
|
@ -53,6 +62,12 @@ m_fd(-1)
|
|
|
|
if (wsaRet != 0)
|
|
|
|
if (wsaRet != 0)
|
|
|
|
LogError("Error from WSAStartup");
|
|
|
|
LogError("Error from WSAStartup");
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (int i = 0; i < UDP_SOCKET_MAX; i++) {
|
|
|
|
|
|
|
|
m_address[i] = "";
|
|
|
|
|
|
|
|
m_port[i] = 0U;
|
|
|
|
|
|
|
|
m_af[i] = 0U;
|
|
|
|
|
|
|
|
m_fd[i] = -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CUDPSocket::~CUDPSocket()
|
|
|
|
CUDPSocket::~CUDPSocket()
|
|
|
@ -149,6 +164,11 @@ bool CUDPSocket::open(const sockaddr_storage& address)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CUDPSocket::open(unsigned int af)
|
|
|
|
bool CUDPSocket::open(unsigned int af)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return open(0, af, m_address_save, m_port_save);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std::string& address, const unsigned int port)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
sockaddr_storage addr;
|
|
|
|
sockaddr_storage addr;
|
|
|
|
unsigned int addrlen;
|
|
|
|
unsigned int addrlen;
|
|
|
@ -159,14 +179,14 @@ bool CUDPSocket::open(unsigned int af)
|
|
|
|
hints.ai_family = af;
|
|
|
|
hints.ai_family = af;
|
|
|
|
|
|
|
|
|
|
|
|
/* to determine protocol family, call lookup() first. */
|
|
|
|
/* to determine protocol family, call lookup() first. */
|
|
|
|
int err = lookup(m_address, m_port, addr, addrlen, hints);
|
|
|
|
int err = lookup(address, port, addr, addrlen, hints);
|
|
|
|
if (err != 0) {
|
|
|
|
if (err != 0) {
|
|
|
|
LogError("The local address is invalid - %s", m_address.c_str());
|
|
|
|
LogError("The local address is invalid - %s", address.c_str());
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
|
|
|
|
int fd = ::socket(addr.ss_family, SOCK_DGRAM, 0);
|
|
|
|
if (m_fd < 0) {
|
|
|
|
if (fd < 0) {
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
LogError("Cannot create the UDP socket, err: %lu", ::GetLastError());
|
|
|
|
LogError("Cannot create the UDP socket, err: %lu", ::GetLastError());
|
|
|
|
#else
|
|
|
|
#else
|
|
|
@ -175,9 +195,14 @@ bool CUDPSocket::open(unsigned int af)
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (m_port > 0U) {
|
|
|
|
m_address[index] = address;
|
|
|
|
|
|
|
|
m_port[index] = port;
|
|
|
|
|
|
|
|
m_af[index] = addr.ss_family;
|
|
|
|
|
|
|
|
m_fd[index] = fd;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (port > 0U) {
|
|
|
|
int reuse = 1;
|
|
|
|
int reuse = 1;
|
|
|
|
if (::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
|
|
|
|
if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) {
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
LogError("Cannot set the UDP socket option, err: %lu", ::GetLastError());
|
|
|
|
LogError("Cannot set the UDP socket option, err: %lu", ::GetLastError());
|
|
|
|
#else
|
|
|
|
#else
|
|
|
@ -186,7 +211,7 @@ bool CUDPSocket::open(unsigned int af)
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (::bind(m_fd, (sockaddr*)&addr, addrlen) == -1) {
|
|
|
|
if (::bind(fd, (sockaddr*)&addr, addrlen) == -1) {
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
LogError("Cannot bind the UDP address, err: %lu", ::GetLastError());
|
|
|
|
LogError("Cannot bind the UDP address, err: %lu", ::GetLastError());
|
|
|
|
#else
|
|
|
|
#else
|
|
|
@ -195,7 +220,7 @@ bool CUDPSocket::open(unsigned int af)
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LogInfo("Opening UDP port on %u", m_port);
|
|
|
|
LogInfo("Opening UDP port on %u", port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
@ -207,30 +232,43 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
|
|
|
|
assert(length > 0U);
|
|
|
|
assert(length > 0U);
|
|
|
|
|
|
|
|
|
|
|
|
// Check that the readfrom() won't block
|
|
|
|
// Check that the readfrom() won't block
|
|
|
|
fd_set readFds;
|
|
|
|
int i, n;
|
|
|
|
FD_ZERO(&readFds);
|
|
|
|
struct pollfd pfd[UDP_SOCKET_MAX];
|
|
|
|
|
|
|
|
for (i = n = 0; i < UDP_SOCKET_MAX; i++) {
|
|
|
|
|
|
|
|
if (m_fd[i] >= 0) {
|
|
|
|
|
|
|
|
pfd[n].fd = m_fd[i];
|
|
|
|
|
|
|
|
pfd[n].events = POLLIN;
|
|
|
|
|
|
|
|
n++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// no socket descriptor to receive
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Return immediately
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
FD_SET((unsigned int)m_fd, &readFds);
|
|
|
|
int ret = WSAPoll(pfd, n, 0);
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
FD_SET(m_fd, &readFds);
|
|
|
|
int ret = ::poll(pfd, n, 0);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// Return immediately
|
|
|
|
|
|
|
|
timeval tv;
|
|
|
|
|
|
|
|
tv.tv_sec = 0L;
|
|
|
|
|
|
|
|
tv.tv_usec = 0L;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ret = ::select(m_fd + 1, &readFds, NULL, NULL, &tv);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
if (ret < 0) {
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
LogError("Error returned from UDP select, err: %lu", ::GetLastError());
|
|
|
|
LogError("Error returned from UDP poll, err: %lu", ::GetLastError());
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
LogError("Error returned from UDP select, err: %d", errno);
|
|
|
|
LogError("Error returned from UDP poll, err: %d", errno);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
int index;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
|
|
|
|
// round robin
|
|
|
|
|
|
|
|
index = (i + m_counter) % n;
|
|
|
|
|
|
|
|
if (pfd[index].revents & POLLIN)
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == n)
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
@ -240,9 +278,9 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
int len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
|
|
|
int len = ::recvfrom(pfd[index].fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
ssize_t len = ::recvfrom(m_fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
|
|
|
ssize_t len = ::recvfrom(pfd[index].fd, (char*)buffer, length, 0, (sockaddr *)&address, &size);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
if (len <= 0) {
|
|
|
|
if (len <= 0) {
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
@ -253,8 +291,8 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_counter++;
|
|
|
|
address_length = size;
|
|
|
|
address_length = size;
|
|
|
|
|
|
|
|
|
|
|
|
return len;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -263,36 +301,52 @@ bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const s
|
|
|
|
assert(buffer != NULL);
|
|
|
|
assert(buffer != NULL);
|
|
|
|
assert(length > 0U);
|
|
|
|
assert(length > 0U);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < UDP_SOCKET_MAX; i++) {
|
|
|
|
|
|
|
|
if (m_fd[i] < 0 || m_af[i] != address.ss_family)
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
int ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
|
|
|
int ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
ssize_t ret = ::sendto(m_fd, (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
|
|
|
ssize_t ret = ::sendto(m_fd[i], (char *)buffer, length, 0, (sockaddr *)&address, address_length);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
if (ret < 0) {
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
LogError("Error returned from sendto, err: %lu", ::GetLastError());
|
|
|
|
LogError("Error returned from sendto, err: %lu", ::GetLastError());
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
LogError("Error returned from sendto, err: %d", errno);
|
|
|
|
LogError("Error returned from sendto, err: %d", errno);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
if (ret != int(length))
|
|
|
|
if (ret == int(length))
|
|
|
|
return false;
|
|
|
|
result = true;
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
if (ret != ssize_t(length))
|
|
|
|
if (ret == ssize_t(length))
|
|
|
|
return false;
|
|
|
|
result = true;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CUDPSocket::close()
|
|
|
|
void CUDPSocket::close()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
for (int i = 0; i < UDP_SOCKET_MAX; i++)
|
|
|
|
|
|
|
|
close(m_fd[i]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CUDPSocket::close(const unsigned int index)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (m_fd[index] >= 0) {
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
::closesocket(m_fd);
|
|
|
|
::closesocket(m_fd[index]);
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
::close(m_fd);
|
|
|
|
::close(m_fd[index]);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m_fd[index] = -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|