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)
203 newUdpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
218 bindAddress.sin_family = AF_INET;
219 bindAddress.sin_addr.s_addr = INADDR_ANY;
220 bindAddress.sin_port = htons((uint16_t)localPort);
235 pHandle->
handle = pNewUdpHandle;
398 broadcastMode = (allowBroadcast?
true:
false);
403 if (setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST, (
const char*)&broadcastMode,
sizeof(broadcastMode)) !=
NO_ERROR)
439 int partialWriteSize;
459 outAddr.sin_family = AF_INET;
460 outAddr.sin_addr.s_addr = pUdpHandle->
remoteAddr;
461 outAddr.sin_port = htons((uint16_t)pUdpHandle->
remotePort);
471 partialWriteSize = (int)bytesToWrite;
487 numBytesSent = sendto(udpSocket, (
const char*)pBuffer, partialWriteSize, 0, (
SOCKADDR*)&outAddr,
sizeof(outAddr));
492 if (numBytesSent != partialWriteSize)
503 bytesToWrite -= (size_t)partialWriteSize;
504 pBuffer = (
uint8*)pBuffer + partialWriteSize;
554 retValue = recvfrom(udpSocket, (
char*)pBuffer, (
int)bytesToRead, 0,
NULL, 0);
564 *pReadBytes = (size_t)retValue;
572 retValue = WSAGetLastError();
574 if (retValue== WSAEWOULDBLOCK)
SbgErrorCode sbgInterfaceUdpWrite(SbgInterface *pHandle, const void *pBuffer, size_t bytesToWrite)
#define SBG_IF_TYPE_ETH_UDP
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()
SbgErrorCode sbgInterfaceUdpAllowBroadcast(SbgInterface *pHandle, bool allowBroadcast)
SbgInterfaceWriteFunc pWriteFunc
enum _SbgErrorCode SbgErrorCode
SbgErrorCode sbgInterfaceUdpDestroy(SbgInterface *pHandle)