2020-03-23 03:36:29 +01:00
|
|
|
/*
|
|
|
|
* IXUdpSocket.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "IXUdpSocket.h"
|
|
|
|
|
|
|
|
#include "IXNetSystem.h"
|
2020-03-23 05:57:58 +01:00
|
|
|
#include <cstring>
|
2020-03-23 23:21:53 +01:00
|
|
|
#include <sstream>
|
2020-03-23 03:36:29 +01:00
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
|
|
|
UdpSocket::UdpSocket(int fd)
|
|
|
|
: _sockfd(fd)
|
|
|
|
{
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
UdpSocket::~UdpSocket()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UdpSocket::close()
|
|
|
|
{
|
|
|
|
if (_sockfd == -1) return;
|
|
|
|
|
|
|
|
closeSocket(_sockfd);
|
|
|
|
_sockfd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int UdpSocket::getErrno()
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
err = WSAGetLastError();
|
|
|
|
#else
|
|
|
|
err = errno;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:47:41 +02:00
|
|
|
bool UdpSocket::isWaitNeeded()
|
|
|
|
{
|
|
|
|
int err = getErrno();
|
|
|
|
|
|
|
|
if (err == EWOULDBLOCK || err == EAGAIN || err == EINPROGRESS)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-23 03:36:29 +01:00
|
|
|
void UdpSocket::closeSocket(int fd)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
closesocket(fd);
|
|
|
|
#else
|
|
|
|
::close(fd);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UdpSocket::init(const std::string& host, int port, std::string& errMsg)
|
|
|
|
{
|
|
|
|
_sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
if (_sockfd < 0)
|
|
|
|
{
|
|
|
|
errMsg = "Could not create socket";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:47:41 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
unsigned long nonblocking = 1;
|
2020-05-05 17:29:39 +02:00
|
|
|
ioctlsocket(_sockfd, FIONBIO, &nonblocking);
|
2020-05-05 16:47:41 +02:00
|
|
|
#else
|
|
|
|
fcntl(_sockfd, F_SETFL, O_NONBLOCK); // make socket non blocking
|
|
|
|
#endif
|
|
|
|
|
2020-03-23 03:36:29 +01:00
|
|
|
memset(&_server, 0, sizeof(_server));
|
|
|
|
_server.sin_family = AF_INET;
|
|
|
|
_server.sin_port = htons(port);
|
|
|
|
|
|
|
|
// DNS resolution.
|
|
|
|
struct addrinfo hints, *result = nullptr;
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
|
|
|
|
int ret = getaddrinfo(host.c_str(), nullptr, &hints, &result);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
errMsg = strerror(UdpSocket::getErrno());
|
|
|
|
freeaddrinfo(result);
|
|
|
|
close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_in* host_addr = (struct sockaddr_in*) result->ai_addr;
|
|
|
|
memcpy(&_server.sin_addr, &host_addr->sin_addr, sizeof(struct in_addr));
|
|
|
|
freeaddrinfo(result);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t UdpSocket::sendto(const std::string& buffer)
|
|
|
|
{
|
|
|
|
return (ssize_t)::sendto(
|
|
|
|
_sockfd, buffer.data(), buffer.size(), 0, (struct sockaddr*) &_server, sizeof(_server));
|
|
|
|
}
|
2020-05-05 16:47:41 +02:00
|
|
|
|
2020-05-05 17:03:04 +02:00
|
|
|
ssize_t UdpSocket::recvfrom(char* buffer, size_t length)
|
2020-05-05 16:47:41 +02:00
|
|
|
{
|
2020-05-05 17:15:01 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
int addressLen = (int) sizeof(_server);
|
|
|
|
#else
|
|
|
|
socklen_t addressLen = (socklen_t) sizeof(_server);
|
|
|
|
#endif
|
2020-05-05 16:47:41 +02:00
|
|
|
return (ssize_t)::recvfrom(
|
2020-05-05 17:03:04 +02:00
|
|
|
_sockfd, buffer, length, 0, (struct sockaddr*) &_server, &addressLen);
|
2020-05-05 16:47:41 +02:00
|
|
|
}
|
2020-03-23 03:36:29 +01:00
|
|
|
} // namespace ix
|