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