COVISE Core
UDPComm.h
Go to the documentation of this file.
1/* This file is part of COVISE.
2
3 You can use it under the terms of the GNU Lesser General Public License
4 version 2.1 or later, see lgpl-2.1.txt.
5
6 * License: LGPL 2+ */
7
8#ifndef _UDPCOMM_H_
9#define _UDPCOMM_H_
10
11#include <util/common.h>
12
13#ifndef _WIN32
14#include <netinet/in.h>
15#else
16#include <winsock2.h>
17#endif
18#ifndef NULL
19#define NULL 0
20#endif
21#define UDP_COMM_MAX_SIZE 1024
27{
28public:
29 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30 // ++ Constructors / Destructor
31 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
36 UDPComm(int localPort);
37
38 // send udp packets only
39 UDPComm(int port, const char *hostname);
40
44 UDPComm(const char *hostname, int port, int localPort,
45 const char *mcastif = NULL, int mcastttl = -1);
46
50 UDPComm(const char *hostPort, int localPort,
51 const char *mcastif = NULL, int mcastttl = -1);
52
54 virtual ~UDPComm();
55
56 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57 // ++ Operations
58 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
59
60 // send a UDP packet: return #bytes sent, error ret=-1, error string set
61 int send(const void *buffer, int numsendBytes);
62
63 // send \0-terminated string
64 int send(const char *string);
65
66 int receive(void *buffer, int numBytes, double timeout=2.0); // wait by default 2 seconds for data
67 // read into buffer
68 // returns number of bytes read into buffer or -1
69 int readMessage(); // read a Datagram with a maximum size of UDP_COMM_MAX_SIZE;
70 int getMessagePart(void *buf, int size); // get part of this message, returns the number of bytes copied to buf or -1
71
72 int enableBroadcast(bool state);
73
74 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
75 // ++ Attribute request/set functions
76 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
77
78 // return true if bad
79 bool isBad();
80
81 // get error string of last error
82 const char *errorMessage();
83
84 const char *rawBuffer()
85 {
86 return (const char *)msgBuf;
87 };
89 {
90 return msgSize;
91 };
93 {
94 return d_rsocket;
95 };
96
97 // Funktion für Ein-/Ausblenden der Netzwerkfehler
98 static void errorStatus_SW();
99 static bool getError_SW();
100 static void setError_SW(bool);
101 sockaddr_in& getRemoteAddess() { return remote_adr; };
102
103protected:
104 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
105 // ++ Attributes
106 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
107
108 // address info to send to
109 struct sockaddr_in d_address;
110
111 // error buffer: contains non-empty string if bad things happen
112 char d_error[1024];
113 char msgBuf[UDP_COMM_MAX_SIZE];
116
117 // Socket number
119 int d_rsocket; // receive socket
120
121private:
122 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
123 // ++ Internally used functions
124 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
125
126 // Try to get IP, use no nameserver if dot-notation
127 unsigned long getIP(const char *hostname);
128
129 // set up server data (used by constructors)
130 void setup(const char *hostname, int port, int localPort,
131 const char *mcastif = NULL, int mcastttl = -1);
132
133 int openReceivePort(int portnumber);
134 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
135 // ++ prevent auto-generated bit copy routines by default
136 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
137
139 UDPComm(const UDPComm &);
140
142 UDPComm &operator=(const UDPComm &);
143
145 UDPComm();
146
147 // Variable für Ein-/Ausblenden der Netzwerkfehler
148 static bool error_SW;
149
150 sockaddr_in remote_adr;
151 socklen_t rlen;
152};
153#endif
#define UTILEXPORT
Definition: coExport.h:206
#define NULL
Definition: UDPComm.h:19
#define UDP_COMM_MAX_SIZE
Definition: UDPComm.h:21
GLuint buffer
Definition: khronos-glext.h:6606
GLsizeiptr size
Definition: khronos-glext.h:6610
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: khronos-glext.h:8469
GLbitfield GLuint64 timeout
Definition: khronos-glext.h:7882
Definition: UDPComm.h:27
const char * rawBuffer()
Definition: UDPComm.h:84
int d_rsocket
Definition: UDPComm.h:119
sockaddr_in remote_adr
Definition: UDPComm.h:150
char * currMsgPtr
Definition: UDPComm.h:114
int messageSize()
Definition: UDPComm.h:88
int d_socket
Definition: UDPComm.h:118
int getReceiveSocket()
Definition: UDPComm.h:92
socklen_t rlen
Definition: UDPComm.h:151
int msgSize
Definition: UDPComm.h:115
sockaddr_in & getRemoteAddess()
Definition: UDPComm.h:101
static bool error_SW
Definition: UDPComm.h:148