9 #include <netinet/in.h> 10 #include <sys/types.h> 11 #include <sys/socket.h> 16 #define SOCKADDR_IN struct sockaddr_in 17 #define SOCKADDR struct sockaddr 19 #define INVALID_SOCKET (SOCKET)(~0) 20 #define SOCKET_ERROR (-1) 24 #define closesocket(socket) close(socket); 44 if (WSAStartup(MAKEWORD(2, 2), &wsaData) ==
NO_ERROR)
100 blockingMode = (blocking ?0:1);
105 if (ioctlsocket(socketHandle, FIONBIO, &blockingMode) ==
NO_ERROR)
119 flags = fcntl(socketHandle, F_GETFL, 0);
129 flags = (blocking ? (flags&~O_NONBLOCK) : (flags|O_NONBLOCK));
134 if (fcntl(socketHandle, F_SETFL, flags) == 0)
206 newUdpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
221 bindAddress.sin_family = AF_INET;
222 bindAddress.sin_addr.s_addr = INADDR_ANY;
223 bindAddress.sin_port = htons((uint16_t)localPort);
238 pHandle->
handle = pNewUdpHandle;
407 broadcastMode = (allowBroadcast?
true:
false);
412 if (setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST, (
const char*)&broadcastMode,
sizeof(broadcastMode)) !=
NO_ERROR)
448 int partialWriteSize;
471 outAddr.sin_family = AF_INET;
472 outAddr.sin_addr.s_addr = pUdpHandle->
remoteAddr;
473 outAddr.sin_port = htons((uint16_t)pUdpHandle->
remotePort);
483 partialWriteSize = (int)bytesToWrite;
499 numBytesSent = sendto(udpSocket, (
const char*)pBuffer, partialWriteSize, 0, (
SOCKADDR*)&outAddr,
sizeof(outAddr));
504 if (numBytesSent != partialWriteSize)
515 bytesToWrite -= (size_t)partialWriteSize;
516 pBuffer = (
uint8*)pBuffer + partialWriteSize;
569 retValue = recvfrom(udpSocket, (
char*)pBuffer, (
int)bytesToRead, 0,
NULL, 0);
579 *pReadBytes = (size_t)retValue;
587 retValue = WSAGetLastError();
589 if (retValue== WSAEWOULDBLOCK)
SbgErrorCode sbgInterfaceUdpWrite(SbgInterface *pHandle, const void *pBuffer, size_t bytesToWrite)
SbgInterfaceHandle handle
SbgInterfaceReadFunc pReadFunc
#define closesocket(socket)
SbgErrorCode sbgInterfaceUdpCreate(SbgInterface *pHandle, sbgIpAddress remoteAddr, uint32 remotePort, uint32 localPort)
#define SBG_INTERFACE_UDP_PACKET_MAX_SIZE
SbgErrorCode sbgInterfaceUpdateCloseSockets(void)
SbgErrorCode sbgInterfaceUdpRead(SbgInterface *pHandle, void *pBuffer, size_t *pReadBytes, size_t bytesToRead)
This file implements an UDP interface.
SbgErrorCode sbgInterfaceUdpInitSockets(void)
SbgErrorCode sbgInterfaceUdpSetSocketBlocking(SOCKET socketHandle, bool blocking)
ROSCONSOLE_DECL void shutdown()
#define SBG_ASSERT(expression)
SbgErrorCode sbgInterfaceUdpAllowBroadcast(SbgInterface *pHandle, bool allowBroadcast)
SbgInterfaceWriteFunc pWriteFunc
enum _SbgErrorCode SbgErrorCode
SbgErrorCode sbgInterfaceUdpDestroy(SbgInterface *pHandle)