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