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