Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
UdpServer.h
1 /*
2  * udpServer.h
3  *
4  * Created on: 19 May 2015
5  * Author: giulio moro
6  */
7 
8 #ifndef UDPSERVER_H_
9 #define UDPSERVER_H_
10 
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <errno.h>
16 #include <netdb.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21 
22 class UdpServer{
23  private:
24  int port;
25  int enabled;
26  int inSocket;
27  struct sockaddr_in server;
28  struct timeval stTimeOut;
29  struct timeval stZeroTimeOut;
30  fd_set stReadFDS;
31  int size;
32  void *wasteBuffer;
33  int wasteBufferSize;
34  int length;
35  socklen_t fromLength;
36  struct sockaddr_in from;
37  public:
38  UdpServer();
39  UdpServer(int aPort);
40  ~UdpServer();
41  bool setup(int aPort);
42  void cleanup();
43  bool bindToPort(int aPort);
44  int getBoundPort() const;
45  /*
46  * Reads bytes from the socket.
47  *
48  * Drop-in replacement for JUCE DatagramSocket::read()
49  *
50  If blockUntilSpecifiedAmountHasArrived is true, the method will block until maxBytesToRead
51  bytes have been read, (or until an error occurs). If this flag is false, the method will
52  return as much data as is currently available without blocking.
53  */
54  int read(void* destBuffer, int maxBytesToRead, bool blockUntilSpecifiedAmountHasArrived);
55  void close();
56  int empty();
57  int empty(int maxCount);
58  /*
59  * Waits until the socket is ready for reading or writing.
60  *
61  Drop-in replacement for JUCE DatagramSocket::waitUntilReady.
62  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.
63  If the timeout is < 0, it will wait forever, or else will give up after the specified time.
64  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.
65  */
66  int waitUntilReady(bool readyForReading, int timeoutMsecs);
67  int getLastRecvPort();
68  const char* getLastRecvAddr();
69 };
70 
71 
72 
73 #endif /* UDPSERVER_H_ */
Definition: UdpServer.h:22
void cleanup(BelaContext *context, void *userData)
User-defined cleanup function which runs when the program finishes.
Definition: render.cpp:96
bool setup(BelaContext *context, void *userData)
User-defined initialisation function which runs before audio rendering begins.
Definition: render.cpp:51