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