COVISE Core
covise_connect.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 EC_CONNECTION_H
9#define EC_CONNECTION_H
10
11#include <iostream>
12#include <vector>
13#include <map>
14#include <functional>
15
16#include <fcntl.h>
17#ifdef _WIN32
18#include <io.h>
19#else
20#include <netinet/in.h>
21#endif
22
23#include <util/coExport.h>
24#include "message.h"
26
27#include <functional>
28
29typedef struct ssl_st SSL;
30typedef struct ssl_ctx_st SSL_CTX;
31typedef struct ssl_method_st SSL_METHOD;
32
33/*
34 $Log: $
35 * Revision 1.9 1994/03/24 16:56:26 zrf30125
36 * ConnectionList completed
37 *
38 * Revision 1.8 94/02/18 14:31:52 zrfg0125
39 * ~Connection bug
40 *
41 * Revision 1.7 94/02/18 14:19:44 zrfg0125
42 * ~Connection bug solved
43 *
44* Revision 1.6 94/02/18 14:08:18 zrfg0125
45 * read_buf no longer static
46 *
47 * Revision 1.5 93/12/10 14:19:06 zrfg0125
48 * modifications to speedup write and read calls
49 *
50 * Revision 1.4 93/10/20 15:14:18 zrhk0125
51 * socket cleanup improved
52 *
53 * Revision 1.3 93/10/08 19:26:59 zrhk0125
54 * shortened include filename
55 * conversion included as connection dependent
56 *
57 * Revision 1.2 93/09/30 17:09:04 zrhk0125
58 * basic modifications for CRAY
59 *
60 * Revision 1.1 93/09/25 20:39:31 zrhk0125
61 * Initial revision
62 *
63 */
64
65const int EC_SERVER = 0;
66const int EC_CLIENT = 1;
67
68namespace covise
69{
70
71class Host;
72class SimpleServerConnection;
73class SSLSocket;
74class UDPSocket;
75class UdpMessage;
76
77#ifdef CRAY
78#define WRITE_BUFFER_SIZE 393216
79#else
80#define WRITE_BUFFER_SIZE 64000
81#endif
82#define READ_BUFFER_SIZE WRITE_BUFFER_SIZE
83
84/***********************************************************************\
85 ** **
86 ** Connection classes Version: 1.1 **
87 ** **
88 ** **
89 ** Description : These classes present the user-seeable part of the **
90 ** socket communications (if necessary). **
91 ** Connection is the base class, ServerConecction **
92 ** and ClientConnection are subclasses tuned for the **
93 ** server and the client part of a socket. **
94 ** ControllerConnection and DataManagerConnection **
95 ** are mere functional subclasses without additional **
96 ** data. **
97 ** ConnectionList provides the data structures **
98 ** necessary to use the select UNIX system call **
99 ** that allows to listen to many connections at once **
100 ** **
101 ** Classes : Connection, ServerConnection, ClientConnection, **
102 ** ControllerConnection, DataManagerConnection, **
103 ** ConnectionList **
104 ** **
105 ** Copyright (C) 1993 by University of Stuttgart **
106 ** Computer Center (RUS) **
107 ** Allmandring 30 **
108 ** 7000 Stuttgart 80 **
109 ** **
110 ** **
111 ** Author : A. Wierse (RUS) **
112 ** **
113 ** History : **
114 ** 15.04.93 Ver 1.0 **
115 ** 26.05.93 Ver 1.1 sender_type and sender_id **
116 ** introduced **
117 ** **
118 ** **
119\***********************************************************************/
120
122{
123protected:
124 friend class ServerConnection;
125 friend class ConnectionList;
126 class Socket *sock = nullptr; // Socket for connection
127 int port; // port for connection
128 int sender_id; // id of the sending process
129 int send_type; // type of module for messages
130 int peer_id_; // id of the peer process
131 int peer_type_; // type of peer
132 mutable int message_to_do; // if more than one message has been read
133 mutable int bytes_to_process;
134 unsigned long tru;
135 char *read_buf = nullptr;
136 Host *other_host = nullptr;
137 int hostid; //hostid of remote host
138 mutable void (*remove_socket)(int);
139 int get_id() const;
141 bool sendMessage(int senderId, int senderType, const Message *msg) const;
142
143public:
144 char convert_to; // to what format do we need to convert data?
145 Connection();
146 Connection(int sfd);
147 virtual ~Connection(); // close connection (for subclasses)
148
150 {
151 return sock;
152 };
153 void set_peer(int id, int type);
154 int get_peer_id() const;
155 int get_peer_type() const;
156
157 int is_connected() const// return true if connection is established
158 {
159 if (sock == NULL)
160 return 0;
161 return (get_id() != -1);
162 };
163 virtual int receive(void *buf, unsigned nbyte) const; // receive from socket
164 virtual int send(const void *buf, unsigned nbyte) const; // send into socket
165 virtual int recv_msg(Message *msg, char *ip = nullptr) const; // receive Message, can set ip to the ip adresss of the sender(for udp msgs)
166 virtual int recv_msg_fast(Message *msg) const; // high-performace receive Message
167 virtual bool sendMessage(const Message *msg) const override; // send Message
168 virtual bool sendMessage(const UdpMessage *msg) const override; // send Message
169 virtual int send_msg_fast(const Message *msg); // high-performance send Message
170 int check_for_input(float time = 0.0) const; // issue select call and return TRUE if there is an event or 0L otherwise
171 int get_port() const // give port number
172 {
173 return port;
174 };
175 void set_hostid(int id);
176 int get_hostid() const
177 {
178 return hostid;
179 };
180 int get_sendertype() const
181 {
182 return (send_type);
183 };
184 int get_id(void (*remove_func)(int)) const; // give socket id
185 int get_sender_id() const
186 {
187 return sender_id;
188 };
189 void close(); // send close msg for partner and delete socket
190 void close_inform(); // close without msg for partner
191 int has_message() const
192 {
193 // if(message_to_do)
194 // LOGINFO( "message_to_do == 1");
195 // else
196 // LOGINFO( "message_to_do == 0");
197 return message_to_do; // message is already read
198 };
199 void print() const
200 {
201 std::cerr << "port: " << port << std::endl;
202 };
203 Host *get_host();
204 const Host *get_host() const;
205 const char *get_hostname() const;
206};
207
209{
210public:
211 UDPConnection(int id, int s_type, int p, const char* address);
212 //receive a udp message from socket, return true on succsess (deletes old data and creates new data)
213 bool recv_udp_msg(UdpMessage* msg) const;
214 //send udp message to ip, if no ip given use member address. Retun true on succsess
215 bool sendMessage(const UdpMessage* msg) const override;
216 bool send_udp_msg(const UdpMessage* msg, const char* ip = nullptr) const;
217};
218// Connection that acts as server
220{
221protected:
222 void get_dataformat();
223
224public:
225 // bind socket to port p
226 ServerConnection(int p, int id, int st);
227 // bind socket, port assigned automatically
228 ServerConnection(int *p, int id, int st);
230 {
231 sock = s;
232 };
233 virtual ~ServerConnection() // close connection
234 {};
235 int acceptOne(); // accept connection (after bind)
236 int acceptOne(float timeout); // accept connection (after bind) and wait int seconds
237 int listen(); // listen for connection (after bind)
238 std::unique_ptr<ServerConnection> spawn_connection() const; // accept after select for open socket
239 // accept after select for open socket
240 std::unique_ptr<SimpleServerConnection> spawnSimpleConnection() const;
241};
243{
244public:
246 bool sendMessageTo(Message* msg, const char* address);
247};
248
249// Connection that acts as server
251{
253 char buffer[10001];
254 size_t buflen;
255
256public:
257 // bind socket to port p
258 SimpleServerConnection(int p, int id, int st);
259 // bind socket, port assigned automatically
260 SimpleServerConnection(int *p, int id, int st);
262 const char *readLine();
263};
264
265// Connection that acts as client
267{
269
270public:
271 // connect to server at port p on host h
272 // ClientConnection(Host *h, int p, int id, int st);
273 ClientConnection(Host *host, int port, int id, int senderType, int retries = 20, double timeout = 0.0);
274 ~ClientConnection(); // close connection
275};
276
277// Connection that acts as client, does not send a byteorder Byte
279{
282 char buffer[10001];
283 size_t buflen;
284
285public:
286 // connect to server at port p on host h
287 SimpleClientConnection(Host *, int p, int retries = 20);
288 ~SimpleClientConnection(); // close connection
289 const char *readLine();
290 void get_dataformat();
291};
292
294{
295 // make connection to datamanager
296public:
297 //DataManagerConnection(Host *hp, int pp, int my_id, int st): ClientConnection((Host*)hp, (int)pp, (int)my_id, (int) st) {};
298 DataManagerConnection(Host *hp, int pp, int my_id, int st)
299 : ClientConnection(hp, pp, my_id, st){};
300 // contact datamanager on port p at host h
301
302 // !!! irgendwie mag die HP das nicht
303 // DataManagerConnection(int pp, int my_id, int st): ClientConnection(NULL,pp,my_id,st) {};
304 // !!! in der Version vor dem Linux Mergen war das so:
305 DataManagerConnection(int pp, int my_id, int st)
306 : ClientConnection((Host *)NULL, pp, my_id, st){};
307
308 // contact local datamanager on port p
309 ~DataManagerConnection() // close connection
310 {};
311};
312
314{
315 // make connection to controller
316public:
317 ControllerConnection(Host *h, int p, int id, int st)
318 : ClientConnection(h, p, id, st){};
319 // contact controller on port p at host h
320};
321
322#ifdef MULTICAST
323class NETEXPORT MulticastConnection : public Connection
324{
325public:
326 MulticastConnection(int id, int s_type, int p, char *MulticastGroup = "224.10.10.10", int ttl = 200);
327};
328#endif
329
330
331template<typename Conn, typename... Args>
332static std::unique_ptr<Conn> createConnectedConn(Args&&...args){
333 std::unique_ptr<Conn> conn{new Conn(std::forward<Args>(args)...)};
334 if (!conn->is_connected())
335 return nullptr;
336 return std::move(conn);
337}
338
339template<typename Conn, typename... Args>
340static std::unique_ptr<Conn> createListeningConn(Args&&...args){
341 std::unique_ptr<Conn> conn = createConnectedConn<Conn>(std::forward<Args>(args)...);
342 if (!conn)
343 return nullptr;
344 struct linger linger;
345 linger.l_onoff = 0;
346 linger.l_linger = 0;
347 setsockopt(conn->get_id(NULL), SOL_SOCKET, SO_LINGER, (char *)&linger, sizeof(linger));
348 conn->listen();
349 return std::move(conn);
350}
351
352std::unique_ptr<ServerConnection> NETEXPORT setupServerConnection(int id, int senderType, int timeoutstd, std::function<bool(const ServerConnection&)> informClient);
353
354class NETEXPORT ConnectionList // list connections in a way that select can be used
355{
356 friend struct ConnectionAdder;
357
358public:
359 ConnectionList(); // constructor
360 ConnectionList(ServerConnection *); // constructor (listens always at port)
361
366
367 ~ConnectionList(); // destructor
368
369template<typename Conn, typename ...Args>
370const Conn* addNewConn(Args&&...args){
371 std::unique_ptr<Conn> conn{new Conn(std::forward<Args>(args)...)};
372 return dynamic_cast<const Conn*>(add(std::move(conn)));
373}
374
375template<typename Conn, typename ...Args>
376const Conn* tryAddNewListeningConn(Args&&...args){
377 std::unique_ptr<Conn> conn = createListeningConn<Conn>(std::forward<Args>(args)...);
378 if (conn)
379 {
380 return dynamic_cast<const Conn*>(add(std::move(conn)));
381 }
382 return nullptr;
383}
384
385template<typename Conn, typename ...Args>
386const Conn* tryAddNewConnectedConn(Args&&...args){
387 std::unique_ptr<Conn> conn = createConnectedConn<Conn>(std::forward<Args>(args)...);
388 if (conn)
389 {
390 return dynamic_cast<const Conn*>(add(std::move(conn)));
391 }
392 return nullptr;
393}
394
395 void add_open_conn(ServerConnection *c);
396 const Connection *add(std::unique_ptr<Connection> &&conn);
397 void remove(const Connection *c); // remove connection
398 const Connection *get_last() const;
399 const Connection *wait_for_input(); // issue select call and return the
400 // connection that shows the first event
401 // issue select call and return a
402 const Connection *check_for_input(float time = 0.0);
403 // connection if there is an event or 0L otherwise
404 void reset();
405 const Connection *next();
406 //Connection at(int index); // get specific entry from listpos i
407 size_t count(); // returns the number of current elements
408 void addRemoveNotice(const Connection *conn, const std::function<void(void)> callback);
409
410private:
411 long curidx = -1; // current index into vector
412 std::vector<std::unique_ptr<Connection>> connlist; // list of
413 std::map<const Connection *, std::vector < std::function<void(void)>>> m_onRemoveCallbacks;
414 fd_set fdvar; // field for select call
415 int maxfd; // maximum socket id
416 ServerConnection *open_sock; // socket for listening
417};
418
419
420#ifdef HAVE_OPENSSL
421class NETEXPORT SSLConnection : public Connection
422{
423public:
424 typedef int(PasswordCallback)(char *buf, int size, int rwflag, void *userdata);
425
426 SSLConnection();
427 SSLConnection(int sfd);
428 ~SSLConnection();
429
430 enum SSL_STATE
431 {
432 SSL_READY,
433 SSL_CLOSED
434 };
435
436 int AttachSSLToSocket(Socket *sock);
437
438 int receive(void *buf, unsigned nbyte); // receive from socket
439 int send(const void *buf, unsigned nbyte); // send into socket
440 int recv_msg(Message *msg, char *ip = nullptr) const override; // receive Message
441 int send_msg(const Message *msg) const; // send Message
442 bool sendMessage(const Message *msg) const override;
443 const char *readLine(); // Read line
444 bool IsClosed() const;
445 std::string getPeerAddress();
446 SSL *mSSL;
447
448protected:
449 struct sockaddr_in mSA_client;
450 struct sockaddr_in mSA_server;
451
452 static SSL_CTX *mCTX;
453 int mSFD;
454 int mClieDsc;
455 mutable SSL_STATE mState;
456 size_t mBuflen;
457 char mBuffer[10001];
458
459 void setPasswdCallback(PasswordCallback *, void *userData);
460 void validateConnState() const;
461
462 PasswordCallback *mSSLCB;
463 void *mSSLUserData;
464
465private:
466};
467
468class NETEXPORT SSLServerConnection : public SSLConnection
469{
470public:
471 SSLServerConnection(PasswordCallback *cb, void *userData);
472 SSLServerConnection(int *p, int id, int s_type, PasswordCallback *cb, void *userData);
473 SSLServerConnection(int p, int id, int st, PasswordCallback *cb, void *userData);
474 SSLServerConnection(SSLSocket *socket, PasswordCallback *cb, void *userData);
475 ~SSLServerConnection();
476
477 int accept();
478 int sock_accept();
479 int listen();
480
481 std::string getSSLSubjectUID();
482 std::string getSSLSubjectName();
483
484 SSLServerConnection *spawnConnection();
485
486protected:
487private:
488};
489
490class NETEXPORT SSLClientConnection : public SSLConnection
491{
492public:
493 SSLClientConnection(Host *, int p, PasswordCallback *cb, void *userData /*,int retries = 20*/);
494 ~SSLClientConnection();
495
496 int connect();
497 void evaluate();
498 std::string getSSLSubject();
499
500 Host *host;
501 Host *lhost;
502
503protected:
504 void getErrorMessage(long result);
505};
506#endif
507}
508#endif
#define NETEXPORT
Definition: coExport.h:373
#define NULL
Definition: covise_list.h:22
GLsizei const GLchar *const * string
Definition: khronos-glext.h:6750
GLboolean reset
Definition: khronos-glext.h:6369
const GLubyte * c
Definition: khronos-glext.h:9864
GLuint address
Definition: khronos-glext.h:10368
GLuint64EXT * result
Definition: khronos-glext.h:12573
GLuint buffer
Definition: khronos-glext.h:6606
GLsizeiptr size
Definition: khronos-glext.h:6610
GLuint GLuint GLsizei count
Definition: khronos-glext.h:6343
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: khronos-glext.h:8469
GLfloat GLfloat p
Definition: khronos-glext.h:9861
GLdouble s
Definition: khronos-glext.h:6441
GLbitfield GLuint64 timeout
Definition: khronos-glext.h:7882
GLenum GLuint id
Definition: khronos-glext.h:6279
GLenum type
Definition: khronos-glext.h:6279
GLfloat GLfloat GLfloat GLfloat h
Definition: khronos-glext.h:8441
struct ssl_ctx_st SSL_CTX
Definition: covise_connect.h:30
const int EC_CLIENT
Definition: covise_connect.h:66
struct ssl_st SSL
Definition: covise_connect.h:29
struct ssl_method_st SSL_METHOD
Definition: covise_connect.h:31
const int EC_SERVER
Definition: covise_connect.h:65
struct in_addr ip
Definition: coSimClient.h:202
list of all chemical elements
Definition: coConfig.h:27
static std::unique_ptr< Conn > createConnectedConn(Args &&...args)
Definition: covise_connect.h:332
std::unique_ptr< ServerConnection > NETEXPORT setupServerConnection(int id, int senderType, int timeoutstd, std::function< bool(const ServerConnection &)> informClient)
Definition: covise_connect.cpp:1313
static std::unique_ptr< Conn > createListeningConn(Args &&...args)
Definition: covise_connect.h:340
Definition: covise_connect.h:122
Socket * getSocket() const
Definition: covise_connect.h:149
char convert_to
Definition: covise_connect.h:144
int get_hostid() const
Definition: covise_connect.h:176
int sender_id
Definition: covise_connect.h:128
int peer_type_
Definition: covise_connect.h:131
int peer_id_
Definition: covise_connect.h:130
int hostid
Definition: covise_connect.h:137
unsigned long tru
Definition: covise_connect.h:134
int has_message() const
Definition: covise_connect.h:191
int get_port() const
Definition: covise_connect.h:171
int get_sender_id() const
Definition: covise_connect.h:185
int get_sendertype() const
Definition: covise_connect.h:180
int port
Definition: covise_connect.h:127
void print() const
Definition: covise_connect.h:199
int * header_int
Definition: covise_connect.h:140
int send_type
Definition: covise_connect.h:129
int is_connected() const
Definition: covise_connect.h:157
int bytes_to_process
Definition: covise_connect.h:133
int message_to_do
Definition: covise_connect.h:132
Definition: covise_connect.h:209
Definition: covise_connect.h:220
ServerConnection(Socket *s)
Definition: covise_connect.h:229
virtual ~ServerConnection()
Definition: covise_connect.h:233
Definition: covise_connect.h:243
Definition: covise_connect.h:251
size_t buflen
Definition: covise_connect.h:254
Definition: covise_connect.h:267
Host * lhost
Definition: covise_connect.h:268
Definition: covise_connect.h:279
Host * lhost
Definition: covise_connect.h:281
Host * host
Definition: covise_connect.h:280
size_t buflen
Definition: covise_connect.h:283
Definition: covise_connect.h:294
~DataManagerConnection()
Definition: covise_connect.h:309
DataManagerConnection(Host *hp, int pp, int my_id, int st)
Definition: covise_connect.h:298
DataManagerConnection(int pp, int my_id, int st)
Definition: covise_connect.h:305
Definition: covise_connect.h:314
ControllerConnection(Host *h, int p, int id, int st)
Definition: covise_connect.h:317
Definition: covise_connect.h:355
ConnectionList(ConnectionList &&)=delete
int maxfd
Definition: covise_connect.h:415
ConnectionList(const ConnectionList &)=delete
fd_set fdvar
Definition: covise_connect.h:414
const Conn * tryAddNewListeningConn(Args &&...args)
Definition: covise_connect.h:376
ConnectionList & operator=(ConnectionList &&)=delete
const Conn * addNewConn(Args &&...args)
Definition: covise_connect.h:370
std::vector< std::unique_ptr< Connection > > connlist
Definition: covise_connect.h:412
ConnectionList & operator=(const ConnectionList &)=delete
ServerConnection * open_sock
Definition: covise_connect.h:416
const Conn * tryAddNewConnectedConn(Args &&...args)
Definition: covise_connect.h:386
std::map< const Connection *, std::vector< std::function< void(void)> > > m_onRemoveCallbacks
Definition: covise_connect.h:413
Definition: covise_host.h:19
Definition: covise_socket.h:129
Definition: covise_socket.h:248
Definition: message.h:76
Definition: message_sender_interface.h:20
Definition: udpMessage.h:32