Collage  1.7.0
High-performance C++ library for developing object-oriented distributed applications.
connection.h
1 
2 /* Copyright (c) 2005-2017, Stefan Eilemann <eile@equalizergraphics.com>
3  * Daniel Nachbaur <danielnachbaur@gmail.com>
4  *
5  * This file is part of Collage <https://github.com/Eyescale/Collage>
6  *
7  * This library is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Lesser General Public License version 2.1 as published
9  * by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #ifndef CO_CONNECTION_H
22 #define CO_CONNECTION_H
23 
24 #include <co/api.h>
25 #include <co/types.h>
26 
27 #include <boost/noncopyable.hpp> // base class
28 #include <lunchbox/bitOperation.h> // used inline
29 #include <lunchbox/referenced.h> // base class
30 
31 #include <string.h>
32 #include <sys/types.h>
33 #include <vector>
34 
35 namespace co
36 {
37 namespace detail
38 {
39 class Connection;
40 }
41 
59 class Connection : public lunchbox::Referenced, public boost::noncopyable
60 {
61 public:
62  enum State
63  {
68  STATE_CLOSING
69  };
70 
81  CO_API static ConnectionPtr create(ConnectionDescriptionPtr desc);
82 
86  CO_API State getState() const;
87 
89  bool isClosed() const { return getState() == STATE_CLOSED; }
91  bool isClosing() const { return getState() == STATE_CLOSING; }
93  bool isConnected() const { return getState() == STATE_CONNECTED; }
95  bool isListening() const { return getState() == STATE_LISTENING; }
97  CO_API bool isMulticast() const;
98 
100  CO_API ConstConnectionDescriptionPtr getDescription() const;
101 
103  bool operator==(const Connection& rhs) const;
105 
118  virtual bool connect() { return false; }
129  virtual bool listen() { return false; }
131  virtual void close() {}
133 
137  void addListener(ConnectionListener* listener);
138 
140  void removeListener(ConnectionListener* listener);
142 
156  virtual void acceptNB() { LBUNIMPLEMENTED; }
164  {
165  LBUNIMPLEMENTED;
166  return 0;
167  }
169 
186  CO_API void recvNB(BufferPtr buffer, const uint64_t bytes);
187 
203  CO_API bool recvSync(BufferPtr& buffer, const bool block = true);
204 
205  BufferPtr resetRecvData();
206 
207 
225  CO_API bool send(const void* buffer, const uint64_t bytes,
226  const bool isLocked = false);
227 
229  CO_API void lockSend() const;
230 
232  CO_API void unlockSend() const;
233 
235  virtual void finish() {}
237 
242 #ifdef _WIN32
243  typedef void* Notifier;
244 #else
245  typedef int Notifier;
246 #endif
247 
248  virtual Notifier getNotifier() const = 0;
249 
250 protected:
252  Connection();
253 
255  virtual ~Connection();
256 
260  {
261  READ_TIMEOUT = -2,
262  READ_ERROR = -1
263  // >= 0: nBytes read
264  };
265 
280  virtual void readNB(void* buffer, const uint64_t bytes) = 0;
281 
294  virtual int64_t readSync(void* buffer, const uint64_t bytes,
295  const bool block) = 0;
296 
307  virtual int64_t write(const void* buffer, const uint64_t bytes) = 0;
309 
313  CO_API void _setDescription(ConnectionDescriptionPtr description);
314 
315  CO_API void _setState(const State state);
316 
318  CO_API ConnectionDescriptionPtr _getDescription();
320 
321 private:
322  detail::Connection* const _impl;
323 };
324 
325 CO_API std::ostream& operator<<(std::ostream&, const Connection&);
326 }
327 
328 #endif // CO_CONNECTION_H
State
The current state of the Connection.
Definition: connection.h:62
Defines export visibility macros for library Collage.
bool isConnected() const
Definition: connection.h:93
The connection has been connected and is open.
Definition: connection.h:66
bool isListening() const
Definition: connection.h:95
bool isClosing() const
Definition: connection.h:91
Closed, initial state.
Definition: connection.h:64
The connection is listening for connects.
Definition: connection.h:67
A connect() or listen() is in progress.
Definition: connection.h:65
virtual ConnectionPtr acceptSync()
Complete an accept operation.
Definition: connection.h:163
int Notifier
The Notifier used by the ConnectionSet to detect readiness of a Connection.
Definition: connection.h:245
Object-oriented network library.
Definition: barrier.h:27
lunchbox::RefPtr< const ConnectionDescription > ConstConnectionDescriptionPtr
A reference pointer for const ConnectionDescription pointers.
Definition: types.h:98
virtual void close()
Close a connected or listening connection.
Definition: connection.h:131
virtual bool connect()
Connect to the remote peer.
Definition: connection.h:118
virtual bool listen()
Put the connection into the listening state.
Definition: connection.h:129
bool isClosed() const
Definition: connection.h:89
lunchbox::RefPtr< ConnectionDescription > ConnectionDescriptionPtr
A reference pointer for ConnectionDescription pointers.
Definition: types.h:95
lunchbox::RefPtr< Connection > ConnectionPtr
A reference pointer for Connection pointers.
Definition: types.h:93
virtual void acceptNB()
Start an accept operation.
Definition: connection.h:156
An interface definition for communication between hosts.
Definition: connection.h:59
ReadStatus
< error codes for readSync()
Definition: connection.h:259