giuliomoro@213: /* giuliomoro@213: * udpServer.h giuliomoro@213: * giuliomoro@213: * Created on: 19 May 2015 giuliomoro@213: * Author: giulio moro giuliomoro@213: */ giuliomoro@213: giuliomoro@213: #ifndef UDPSERVER_H_ giuliomoro@213: #define UDPSERVER_H_ giuliomoro@213: giuliomoro@213: #include giuliomoro@213: #include giuliomoro@213: #include giuliomoro@213: #include giuliomoro@213: #include giuliomoro@213: #include giuliomoro@213: #include giuliomoro@213: #include giuliomoro@213: #include giuliomoro@213: #include giuliomoro@213: giuliomoro@213: class UdpServer{ giuliomoro@213: private: giuliomoro@213: int port; giuliomoro@213: int enabled; giuliomoro@213: int inSocket; giuliomoro@213: struct sockaddr_in server; giuliomoro@213: struct timeval stTimeOut; giuliomoro@213: struct timeval stZeroTimeOut; giuliomoro@213: fd_set stReadFDS; giuliomoro@213: int size; giuliomoro@213: void *wasteBuffer; giuliomoro@213: int wasteBufferSize; giuliomoro@213: int length; giuliomoro@213: socklen_t fromLength; giuliomoro@213: struct sockaddr_in from; giuliomoro@213: public: giuliomoro@213: UdpServer(); giuliomoro@213: UdpServer(int aPort); giuliomoro@213: ~UdpServer(); giuliomoro@213: bool init(int aPort); giuliomoro@213: bool bindToPort(int aPort); giuliomoro@213: int getBoundPort() const; giuliomoro@213: /* giuliomoro@213: * Reads bytes from the socket. giuliomoro@213: * giuliomoro@213: * Drop-in replacement for JUCE DatagramSocket::read() giuliomoro@213: * giuliomoro@213: If blockUntilSpecifiedAmountHasArrived is true, the method will block until maxBytesToRead giuliomoro@213: bytes have been read, (or until an error occurs). If this flag is false, the method will giuliomoro@213: return as much data as is currently available without blocking. giuliomoro@213: */ giuliomoro@213: int read(void* destBuffer, int maxBytesToRead, bool blockUntilSpecifiedAmountHasArrived); giuliomoro@213: void close(); giuliomoro@213: int empty(); giuliomoro@213: int empty(int maxCount); giuliomoro@213: /* giuliomoro@213: * Waits until the socket is ready for reading or writing. giuliomoro@213: * giuliomoro@213: Drop-in replacement for JUCE DatagramSocket::waitUntilReady. giuliomoro@213: If readyForReading is true, it will wait until the socket is ready for reading; if false, it will wait until it's ready for writing. giuliomoro@213: If the timeout is < 0, it will wait forever, or else will give up after the specified time. giuliomoro@213: If the socket is ready on return, this returns 1. If it times-out before the socket becomes ready, it returns 0. If an error occurs, it returns -1. giuliomoro@213: */ giuliomoro@213: int waitUntilReady(bool readyForReading, int timeoutMsecs); giuliomoro@213: }; giuliomoro@213: giuliomoro@213: giuliomoro@213: giuliomoro@213: #endif /* UDPSERVER_H_ */