annotate include/UdpServer.h @ 213:c083217c4195 robbie

Got UdpServer from ClockSync
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 09 Feb 2016 15:52:35 +0000
parents
children
rev   line source
giuliomoro@213 1 /*
giuliomoro@213 2 * udpServer.h
giuliomoro@213 3 *
giuliomoro@213 4 * Created on: 19 May 2015
giuliomoro@213 5 * Author: giulio moro
giuliomoro@213 6 */
giuliomoro@213 7
giuliomoro@213 8 #ifndef UDPSERVER_H_
giuliomoro@213 9 #define UDPSERVER_H_
giuliomoro@213 10
giuliomoro@213 11 #include <sys/types.h>
giuliomoro@213 12 #include <sys/socket.h>
giuliomoro@213 13 #include <netinet/in.h>
giuliomoro@213 14 #include <arpa/inet.h>
giuliomoro@213 15 #include <errno.h>
giuliomoro@213 16 #include <netdb.h>
giuliomoro@213 17 #include <stdio.h>
giuliomoro@213 18 #include <stdlib.h>
giuliomoro@213 19 #include <unistd.h>
giuliomoro@213 20 #include <string.h>
giuliomoro@213 21
giuliomoro@213 22 class UdpServer{
giuliomoro@213 23 private:
giuliomoro@213 24 int port;
giuliomoro@213 25 int enabled;
giuliomoro@213 26 int inSocket;
giuliomoro@213 27 struct sockaddr_in server;
giuliomoro@213 28 struct timeval stTimeOut;
giuliomoro@213 29 struct timeval stZeroTimeOut;
giuliomoro@213 30 fd_set stReadFDS;
giuliomoro@213 31 int size;
giuliomoro@213 32 void *wasteBuffer;
giuliomoro@213 33 int wasteBufferSize;
giuliomoro@213 34 int length;
giuliomoro@213 35 socklen_t fromLength;
giuliomoro@213 36 struct sockaddr_in from;
giuliomoro@213 37 public:
giuliomoro@213 38 UdpServer();
giuliomoro@213 39 UdpServer(int aPort);
giuliomoro@213 40 ~UdpServer();
giuliomoro@213 41 bool init(int aPort);
giuliomoro@213 42 bool bindToPort(int aPort);
giuliomoro@213 43 int getBoundPort() const;
giuliomoro@213 44 /*
giuliomoro@213 45 * Reads bytes from the socket.
giuliomoro@213 46 *
giuliomoro@213 47 * Drop-in replacement for JUCE DatagramSocket::read()
giuliomoro@213 48 *
giuliomoro@213 49 If blockUntilSpecifiedAmountHasArrived is true, the method will block until maxBytesToRead
giuliomoro@213 50 bytes have been read, (or until an error occurs). If this flag is false, the method will
giuliomoro@213 51 return as much data as is currently available without blocking.
giuliomoro@213 52 */
giuliomoro@213 53 int read(void* destBuffer, int maxBytesToRead, bool blockUntilSpecifiedAmountHasArrived);
giuliomoro@213 54 void close();
giuliomoro@213 55 int empty();
giuliomoro@213 56 int empty(int maxCount);
giuliomoro@213 57 /*
giuliomoro@213 58 * Waits until the socket is ready for reading or writing.
giuliomoro@213 59 *
giuliomoro@213 60 Drop-in replacement for JUCE DatagramSocket::waitUntilReady.
giuliomoro@213 61 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 62 If the timeout is < 0, it will wait forever, or else will give up after the specified time.
giuliomoro@213 63 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 64 */
giuliomoro@213 65 int waitUntilReady(bool readyForReading, int timeoutMsecs);
giuliomoro@213 66 };
giuliomoro@213 67
giuliomoro@213 68
giuliomoro@213 69
giuliomoro@213 70 #endif /* UDPSERVER_H_ */