#pragma once
#include "ofConstants.h"
#include "ofxUDPSettings.h"
#include <string.h>
#include <wchar.h>
#include <stdio.h>
#ifndef TARGET_WIN32
#include <ctype.h>
#include <netdb.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <netinet/tcp.h>
#define SO_MAX_MSG_SIZE TCP_MAXSEG
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define FAR
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#define SOCKET_TIMEOUT SOCKET_ERROR - 1
class ofxUDPManager
{
public:
ofxUDPManager();
virtual ~ofxUDPManager()
{
if (HasSocket()) Close();
}
bool HasSocket() const { return (m_hSocket)&&(m_hSocket != INVALID_SOCKET); }
bool Close();
bool Setup(const ofxUDPSettings & settings);
bool Create();
bool Connect(const char *pHost, unsigned short usPort);
bool ConnectMcast(char *pMcast, unsigned short usPort);
bool Bind(unsigned short usPort);
bool BindMcast(char *pMcast, unsigned short usPort);
int Send(const char* pBuff, const int iSize);
int SendAll(const char* pBuff, const int iSize);
int PeekReceive();
int Receive(char* pBuff, const int iSize);
void SetTimeoutSend(int timeoutInSeconds);
void SetTimeoutReceive(int timeoutInSeconds);
int GetTimeoutSend();
int GetTimeoutReceive();
bool GetRemoteAddr(std::string& address,int& port) const;
bool GetListenAddr(std::string& address,int& port) const;
bool SetReceiveBufferSize(int sizeInByte);
bool SetSendBufferSize(int sizeInByte);
int GetReceiveBufferSize();
int GetSendBufferSize();
bool SetReuseAddress(bool allowReuse);
bool SetEnableBroadcast(bool enableBroadcast);
bool SetNonBlocking(bool useNonBlocking);
int GetMaxMsgSize();
int GetTTL();
bool SetTTL(int nTTL);
protected:
#ifdef TARGET_WIN32
SOCKET m_hSocket;
#else
int m_hSocket;
#endif
int WaitReceive(time_t timeoutSeconds, time_t timeoutMillis);
int WaitSend(time_t timeoutSeconds, time_t timeoutMillis);
unsigned long m_dwTimeoutReceive;
unsigned long m_dwTimeoutSend;
bool nonBlocking;
struct sockaddr_in saServer;
struct sockaddr_in saClient;
static bool m_bWinsockInit;
bool canGetRemoteAddress;
};
Comments