Collage  1.3.0
High-performance C++ library for developing object-oriented distributed applications.
connection.h
1 
2 /* Copyright (c) 2005-2014, Stefan Eilemann <eile@equalizergraphics.com>
3  * 2012-2014, 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 <lunchbox/bitOperation.h> // used inline
28 #include <lunchbox/referenced.h> // base class
29 #include <boost/noncopyable.hpp> // base class
30 
31 #include <sys/types.h>
32 #include <string.h>
33 #include <vector>
34 
35 namespace co
36 {
37 namespace detail { class Connection; }
38 
56 class Connection : public lunchbox::Referenced, public boost::noncopyable
57 {
58 public:
59  enum State
60  {
66  };
67 
78  CO_API static ConnectionPtr create( ConnectionDescriptionPtr desc );
79 
83  CO_API State getState() const;
84 
86  bool isClosed() const { return getState() == STATE_CLOSED; }
87 
89  bool isClosing() const { return getState() == STATE_CLOSING; }
90 
92  bool isConnected() const { return getState() == STATE_CONNECTED; }
93 
95  bool isListening() const { return getState() == STATE_LISTENING; }
96 
98  CO_API bool isMulticast() const;
99 
102 
104  bool operator == ( const Connection& rhs ) const;
106 
119  virtual bool connect() { return false; }
120 
131  virtual bool listen() { return false; }
132 
134  virtual void close() {}
136 
140  void addListener( ConnectionListener* listener );
141 
143  void removeListener( ConnectionListener* listener );
145 
159  virtual void acceptNB() { LBUNIMPLEMENTED; }
160 
167  virtual ConnectionPtr acceptSync() { LBUNIMPLEMENTED; return 0; }
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 
320 
321 private:
322  detail::Connection* const _impl;
323 };
324 
325 CO_API std::ostream& operator << ( std::ostream&, const Connection& );
326 }
327 
328 namespace lunchbox
329 {
330 template<> inline void byteswap( co::Connection*& ) { /*NOP*/ }
331 template<> inline void byteswap( co::ConnectionPtr& ) { /*NOP*/ }
332 }
333 
334 #endif //CO_CONNECTION_H
State
The current state of the Connection.
Definition: connection.h:59
Defines export visibility macros for library Collage.
Connection()
Construct a new connection.
bool isConnected() const
Definition: connection.h:92
lunchbox::RefPtr< ConnectionDescription > ConnectionDescriptionPtr
A reference pointer for ConnectionDescription pointers.
Definition: types.h:97
The connection has been connected and is open.
Definition: connection.h:63
bool isListening() const
Definition: connection.h:95
CO_API void recvNB(BufferPtr buffer, const uint64_t bytes)
Start a read operation on the connection.
bool isClosing() const
Definition: connection.h:89
CO_API bool recvSync(BufferPtr &buffer, const bool block=true)
Finish reading data from the connection.
Closed, initial state.
Definition: connection.h:61
virtual Notifier getNotifier() const =0
CO_API void lockSend() const
Lock the connection, no other thread can send data.
The connection is listening for connects.
Definition: connection.h:64
A connect() or listen() is in progress.
Definition: connection.h:62
virtual ConnectionPtr acceptSync()
Complete an accept operation.
Definition: connection.h:167
static CO_API ConnectionPtr create(ConnectionDescriptionPtr desc)
Create a new connection.
virtual ~Connection()
Destruct this connection.
virtual int64_t readSync(void *buffer, const uint64_t bytes, const bool block)=0
Finish reading data from the connection.
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
virtual void readNB(void *buffer, const uint64_t bytes)=0
Start a read operation on the connection.
CO_API State getState() const
virtual void close()
Close a connected or listening connection.
Definition: connection.h:134
CO_API bool isMulticast() const
virtual bool connect()
Connect to the remote peer.
Definition: connection.h:119
A close() is in progress.
Definition: connection.h:65
virtual int64_t write(const void *buffer, const uint64_t bytes)=0
Write data to the connection.
virtual bool listen()
Put the connection into the listening state.
Definition: connection.h:131
bool isClosed() const
Definition: connection.h:86
CO_API bool send(const void *buffer, const uint64_t bytes, const bool isLocked=false)
Send data using the connection.
CO_API void unlockSend() const
Unlock the connection.
virtual void acceptNB()
Start an accept operation.
Definition: connection.h:159
An interface definition for communication between hosts.
Definition: connection.h:56
ReadStatus
< error codes for readSync()
Definition: connection.h:259
lunchbox::RefPtr< const ConnectionDescription > ConstConnectionDescriptionPtr
A reference pointer for const ConnectionDescription pointers.
Definition: types.h:100
CO_API ConstConnectionDescriptionPtr getDescription() const
CO_API ConnectionDescriptionPtr _getDescription()
lunchbox::RefPtr< Connection > ConnectionPtr
A reference pointer for Connection pointers.
Definition: types.h:95