Fixed an issue where the recvaddr was not set on Windows

- Also improved logging in UDPSocket
 - Resolves nasa/XPlaneConnect#39
This commit is contained in:
Jason Watkins
2015-04-13 20:20:55 -07:00
parent 52bf998632
commit fe88204b57
2 changed files with 59 additions and 57 deletions

View File

@@ -8,7 +8,7 @@ namespace XPC
UDPSocket::UDPSocket(std::uint16_t recvPort) UDPSocket::UDPSocket(std::uint16_t recvPort)
{ {
#if LOG_VERBOSITY > 1 #if LOG_VERBOSITY > 1
Log::FormatLine("[UDPSocket] Opening socket (port:%d)", recvPort); Log::FormatLine("[SOCK] Opening socket (port:%d)", recvPort);
#endif #endif
// Setup Port // Setup Port
struct sockaddr_in localAddr; struct sockaddr_in localAddr;
@@ -23,7 +23,7 @@ namespace XPC
if (startResult != 0) if (startResult != 0)
{ {
#if LOG_VERBOSITY > 0 #if LOG_VERBOSITY > 0
Log::FormatLine("[UDPSocket] ERROR: WSAStartup failed with error code %i.", startResult); Log::FormatLine("[SOCK] ERROR: WSAStartup failed with error code %i.", startResult);
#endif #endif
return; return;
} }
@@ -31,14 +31,14 @@ namespace XPC
{ {
#if LOG_VERBOSITY > 0 #if LOG_VERBOSITY > 0
int err = WSAGetLastError(); int err = WSAGetLastError();
Log::FormatLine("[UDPSocket] ERROR: Failed to open socket. (Error code %i)", err); Log::FormatLine("[SOCK] ERROR: Failed to open socket. (Error code %i)", err);
#endif #endif
return; return;
} }
#elif (__APPLE__ || __linux) #elif (__APPLE__ || __linux)
if ((this->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) if ((this->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{ {
Log::WriteLine("[UDPSocket] ERROR: Failed to open socket"); Log::WriteLine("[SOCK] ERROR: Failed to open socket");
return; return;
} }
int optval = 1; int optval = 1;
@@ -49,7 +49,7 @@ namespace XPC
{ {
#if LOG_VERBOSITY > 0 #if LOG_VERBOSITY > 0
int err = WSAGetLastError(); int err = WSAGetLastError();
Log::FormatLine("[UDPSocket] ERROR: Failed to bind socket. (Error code %i)", err); Log::FormatLine("[SOCK] ERROR: Failed to bind socket. (Error code %i)", err);
#endif #endif
return; return;
} }
@@ -63,7 +63,7 @@ namespace XPC
{ {
#if LOG_VERBOSITY > 1 #if LOG_VERBOSITY > 1
int err = WSAGetLastError(); int err = WSAGetLastError();
Log::FormatLine("[UDPSocket] ERROR: Failed to set timeout. (Error code %i)", err); Log::FormatLine("[SOCK] ERROR: Failed to set timeout. (Error code %i)", err);
#endif #endif
} }
#else #else
@@ -77,7 +77,7 @@ namespace XPC
UDPSocket::~UDPSocket() UDPSocket::~UDPSocket()
{ {
#if LOG_VERBOSITY > 1 #if LOG_VERBOSITY > 1
Log::FormatLine("[UDPSocket] Closing socket (%d)", this->sock); Log::FormatLine("[SOCK] Closing socket (%d)", this->sock);
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
closesocket(this->sock); closesocket(this->sock);
@@ -86,7 +86,7 @@ namespace XPC
#endif #endif
} }
int UDPSocket::Read(std::uint8_t* dst, int maxLen, struct sockaddr* recvAddr) int UDPSocket::Read(std::uint8_t* dst, int maxLen, sockaddr* recvAddr)
{ {
socklen_t recvaddrlen = sizeof(*recvAddr); socklen_t recvaddrlen = sizeof(*recvAddr);
int status = 0; int status = 0;
@@ -98,8 +98,7 @@ namespace XPC
// Definitions // Definitions
FD_SET stReadFDS; FD_SET stReadFDS;
FD_SET stExceptFDS; FD_SET stExceptFDS;
struct timeval tv; timeval tv;
struct sockaddr_in sourceAddr;
// Setup for Select // Setup for Select
FD_ZERO(&stReadFDS); FD_ZERO(&stReadFDS);
@@ -115,7 +114,7 @@ namespace XPC
if (result == SOCKET_ERROR) if (result == SOCKET_ERROR)
{ {
int err = WSAGetLastError(); int err = WSAGetLastError();
Log::FormatLine("[UDPSocket] ERROR: Select failed. (Error code %i)", err); Log::FormatLine("[SOCK] ERROR: Select failed. (Error code %i)", err);
} }
#endif #endif
if (result <= 0) // No Data or error if (result <= 0) // No Data or error
@@ -124,8 +123,7 @@ namespace XPC
} }
// If no error: Read Data // If no error: Read Data
recvaddrlen = sizeof(sourceAddr); status = recvfrom(sock, (char*)dst, maxLen, 0, recvAddr, &recvaddrlen);
status = recvfrom(sock, (char*)dst, maxLen, 0, (SOCKADDR *)&sourceAddr, &recvaddrlen);
#else #else
// For apple or linux-just read - will timeout in 0.5 ms // For apple or linux-just read - will timeout in 0.5 ms
status = (int)recvfrom(recfd.sock, dataRef, 5000, 0, recvaddr, &recvaddrlen); status = (int)recvfrom(recfd.sock, dataRef, 5000, 0, recvaddr, &recvaddrlen);
@@ -133,8 +131,12 @@ namespace XPC
return status; return status;
} }
// TODO: Add IPV6 support
void UDPSocket::SendTo(std::uint8_t* buffer, std::size_t len, std::string remoteHost, std::uint16_t remotePort) void UDPSocket::SendTo(std::uint8_t* buffer, std::size_t len, std::string remoteHost, std::uint16_t remotePort)
{ {
#if LOG_VERBOSITY > 4
Log::FormatLine("[SOCK] Sending message to %s:%u (length=%u)", remoteHost.c_str(), remotePort, len);
#endif
if (remoteHost == "localhost") if (remoteHost == "localhost")
{ {
remoteHost = "127.0.0.1"; remoteHost = "127.0.0.1";
@@ -161,7 +163,7 @@ namespace XPC
if (sendto(sock, (char*)buffer, len, 0, (const struct sockaddr *) &remoteAddr, sizeof(remoteAddr)) < 0) if (sendto(sock, (char*)buffer, len, 0, (const struct sockaddr *) &remoteAddr, sizeof(remoteAddr)) < 0)
{ {
#if LOG_VERBOSITY > 0 #if LOG_VERBOSITY > 0
Log::FormatLine("[UDPSocket] Send failed. (remote: %s:%d)", remoteAddr, remotePort); Log::FormatLine("[SOCK] Send failed. (remote: %s:%d)", remoteAddr, remotePort);
#endif #endif
} }
} }

View File

@@ -49,7 +49,7 @@ namespace XPC
/// of the remote host. /// of the remote host.
/// \returns The number of bytes read, or a negative number if /// \returns The number of bytes read, or a negative number if
/// an error occurs. /// an error occurs.
int Read(std::uint8_t* buffer, int size, struct sockaddr* remoteAddr); int Read(std::uint8_t* buffer, int size, sockaddr* remoteAddr);
/// Sends data to the specified remote endpoint. /// Sends data to the specified remote endpoint.
/// ///