Collage  1.0.1
Object-Oriented C++ Network Library
connection.h
1 
2 /* Copyright (c) 2005-2013, Stefan Eilemann <eile@equalizergraphics.com>
3  * 2012, 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/referenced.h> // base class
28 
29 #include <sys/types.h>
30 #include <string.h>
31 #include <vector>
32 
33 #ifdef _WIN32
34 # define EQ_DEFAULT_PORT (4242)
35 #else
36 # define EQ_DEFAULT_PORT (4242 + getuid())
37 #endif
38 
39 namespace co
40 {
41 namespace detail { class Connection; }
42 
60  class Connection : public lunchbox::Referenced, public lunchbox::NonCopyable
61  {
62  public:
63  enum State
64  {
70  };
71 
82  CO_API static ConnectionPtr create( ConnectionDescriptionPtr desc );
83 
87  CO_API State getState() const;
88 
90  bool isClosed() const { return getState() == STATE_CLOSED; }
91 
93  bool isClosing() const { return getState() == STATE_CLOSING; }
94 
96  bool isConnected() const { return getState() == STATE_CONNECTED; }
97 
99  bool isListening() const { return getState() == STATE_LISTENING; }
100 
102  CO_API bool isMulticast() const;
103 
106 
108  bool operator == ( const Connection& rhs ) const;
110 
123  virtual bool connect() { return false; }
124 
135  virtual bool listen() { return false; }
136 
138  virtual void close() {}
140 
144  void addListener( ConnectionListener* listener );
145 
147  void removeListener( ConnectionListener* listener );
149 
162  virtual void acceptNB() { LBUNIMPLEMENTED; }
163 
170  virtual ConnectionPtr acceptSync() { LBUNIMPLEMENTED; return 0; }
172 
187  CO_API void recvNB( BufferPtr buffer, const uint64_t bytes );
188 
204  CO_API bool recvSync( BufferPtr& buffer, const bool block = true );
205 
206  BufferPtr resetRecvData();
207 
208 
226  CO_API bool send( const void* buffer, const uint64_t bytes,
227  const bool isLocked = false );
228 
230  CO_API void lockSend() const;
231 
233  CO_API void unlockSend() const;
234 
236  virtual void finish() {}
238 
243 #ifdef _WIN32
244  typedef void* Notifier;
245 #else
246  typedef int Notifier;
247 #endif
248 
249  virtual Notifier getNotifier() const = 0;
250 
251  protected:
253  Connection();
254 
256  virtual ~Connection();
257 
261  {
262  READ_TIMEOUT = -2,
263  READ_ERROR = -1
264  // >= 0: nBytes read
265  };
266 
281  virtual void readNB( void* buffer, const uint64_t bytes ) = 0;
282 
296  virtual int64_t readSync( void* buffer, const uint64_t bytes,
297  const bool block ) = 0;
298 
309  virtual int64_t write( const void* buffer, const uint64_t bytes ) = 0;
311 
315  CO_API void _setDescription( ConnectionDescriptionPtr description );
316 
317  CO_API void _setState( const State state );
318 
322 
323  private:
324  detail::Connection* const _impl;
325  };
326 
327  CO_API std::ostream& operator << ( std::ostream&, const Connection& );
328 }
329 
330 namespace lunchbox
331 {
332 template<> inline void byteswap( co::Connection*& ) { /*NOP*/ }
333 }
334 
335 #endif //CO_CONNECTION_H
CO_API void unlockSend() const
Unlock the connection.
virtual int64_t readSync(void *buffer, const uint64_t bytes, const bool block)=0
Finish reading data from the connection.
virtual void readNB(void *buffer, const uint64_t bytes)=0
Start a read operation on the connection.
Closed, initial state.
Definition: connection.h:65
virtual void acceptNB()
Start an accept operation.
Definition: connection.h:162
virtual ~Connection()
Destruct this connection.
lunchbox::RefPtr< ConnectionDescription > ConnectionDescriptionPtr
A reference pointer for ConnectionDescription pointers.
Definition: types.h:90
static CO_API ConnectionPtr create(ConnectionDescriptionPtr desc)
Create a new connection.
bool isClosed() const
Definition: connection.h:90
virtual bool connect()
Connect to the remote peer.
Definition: connection.h:123
bool isClosing() const
Definition: connection.h:93
An interface definition for communication between hosts.
Definition: connection.h:60
virtual ConnectionPtr acceptSync()
Complete an accept operation.
Definition: connection.h:170
CO_API State getState() const
CO_API bool recvSync(BufferPtr &buffer, const bool block=true)
Finish reading data from the connection.
State
The current state of the Connection.
Definition: connection.h:63
CO_API ConnectionDescriptionPtr _getDescription()
A close() is in progress.
Definition: connection.h:69
ReadStatus
&lt; error codes for readSync()
Definition: connection.h:260
virtual void close()
Close a connected or listening connection.
Definition: connection.h:138
bool isConnected() const
Definition: connection.h:96
A connect() or listen() is in progress.
Definition: connection.h:66
The connection is listening for connects.
Definition: connection.h:68
CO_API void lockSend() const
Lock the connection, no other thread can send data.
CO_API void recvNB(BufferPtr buffer, const uint64_t bytes)
Start a read operation on the connection.
bool isListening() const
Definition: connection.h:99
int Notifier
The Notifier used by the ConnectionSet to detect readiness of a Connection.
Definition: connection.h:246
CO_API bool isMulticast() const
virtual Notifier getNotifier() const =0
virtual bool listen()
Put the connection into the listening state.
Definition: connection.h:135
Connection()
Construct a new connection.
CO_API bool send(const void *buffer, const uint64_t bytes, const bool isLocked=false)
Send data using the connection.
CO_API ConstConnectionDescriptionPtr getDescription() const
lunchbox::RefPtr< const ConnectionDescription > ConstConnectionDescriptionPtr
A reference pointer for const ConnectionDescription pointers.
Definition: types.h:93
The connection has been connected and is open.
Definition: connection.h:67
virtual int64_t write(const void *buffer, const uint64_t bytes)=0
Write data to the connection.
lunchbox::RefPtr< Connection > ConnectionPtr
A reference pointer for Connection pointers.
Definition: types.h:88