Files
XPlaneConnectCSP/xpcPlugin/UDPSocket.h
Chris Teubert e60dde457c Changes to work with Mac
Changes include:
- Updating Project
- fixing sendto declaration
- Fixed some bugs in #if Mac sections
- Removed unneeded #includes (my compiler didn’t like them)
2015-04-16 12:58:49 -07:00

78 lines
2.3 KiB
C++

//Copyright (c) 2013-2015 United States Government as represented by the Administrator of the
//National Aeronautics and Space Administration. All Rights Reserved.
#ifndef XPC_SOCKET_H
#define XPC_SOCKET_H
#include <cstdlib>
#include <string>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#elif (__APPLE__ || __linux)
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
namespace XPC
{
/// Represents a UDP datagram socket used for reading data from and sending
/// data to XPC clients.
///
/// \author Jason Watkins
/// \version 1.0
/// \since 1.0
/// \date Intial Version: 2015-04-10
/// \date Last Updated: 2015-04-11
class UDPSocket
{
public:
/// Initializes a new instance of the XPCSocket class bound to the
/// specified receive port.
///
/// \param recvPort The port on which this instance will receive data.
UDPSocket(unsigned short recvPort);
/// Closes the underlying socket for this instance.
~UDPSocket();
/// Reads the specified number of bytes into the data buffer and stores
/// the remote endpoint.
///
/// \param buffer The array to copy the data into.
/// \param size The number of bytes to read.
/// \param remoteAddr When at least one byte is read, contains the address
/// of the remote host.
/// \returns The number of bytes read, or a negative number if
/// an error occurs.
int Read(unsigned char* buffer, int size, sockaddr* remoteAddr);
/// Sends data to the specified remote endpoint.
///
/// \param data The data to be sent.
/// \param len The number of bytes to send.
/// \param remoteHost The hostname of the destination client.
/// \param remotePort The port of the destination client.
void SendTo(unsigned char* buffer, std::size_t len, std::string remoteHost, unsigned short remotePort);
/// Sends data to the specified remote endpoint.
///
/// \param data The data to be sent.
/// \param len The number of bytes to send.
/// \param remoteHost The hostname of the destination client.
/// \param remotePort The port of the destination client.
void SendTo(unsigned char* buffer, std::size_t len, unsigned long remoteIP, unsigned short remotePort);
private:
#ifdef _WIN32
SOCKET sock;
#else
int sock;
#endif
};
}
#endif