Wolframe, 0.0.3

connectionHandler.hpp
Go to the documentation of this file.
1 /************************************************************************
2 
3  Copyright (C) 2011 - 2014 Project Wolframe.
4  All rights reserved.
5 
6  This file is part of Project Wolframe.
7 
8  Commercial Usage
9  Licensees holding valid Project Wolframe Commercial licenses may
10  use this file in accordance with the Project Wolframe
11  Commercial License Agreement provided with the Software or,
12  alternatively, in accordance with the terms contained
13  in a written agreement between the licensee and Project Wolframe.
14 
15  GNU General Public License Usage
16  Alternatively, you can redistribute this file and/or modify it
17  under the terms of the GNU General Public License as published by
18  the Free Software Foundation, either version 3 of the License, or
19  (at your option) any later version.
20 
21  Wolframe is distributed in the hope that it will be useful,
22  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  GNU General Public License for more details.
25 
26  You should have received a copy of the GNU General Public License
27  along with Wolframe. If not, see <http://www.gnu.org/licenses/>.
28 
29  If you have questions regarding the use of this file, please contact
30  Project Wolframe.
31 
32 ************************************************************************/
35 
36 #ifndef _CONNECTION_HANDLER_HPP_INCLUDED
37 #define _CONNECTION_HANDLER_HPP_INCLUDED
38 
39 #include <string>
40 #include <cstddef>
41 #include <cstring>
42 
44 
45 #include <boost/noncopyable.hpp>
46 
47 namespace _Wolframe {
48 namespace net {
49 
53 {
54  template< typename T > friend class ConnectionBase;
55 
56 protected:
57  enum Operation {
62  };
63 
64  explicit NetworkOperation( Operation op, void* d = NULL, std::size_t s = 0,
65  unsigned to = 0 )
66  { operation_ = op, data_ = d; size_ = s; timeout_ = to; }
67 
68  unsigned timeout() const { return timeout_; }
69  const void* data() const { return (const void*)data_; }
70  void* buffer() { return data_; }
71  std::size_t size() const { return size_; }
72  Operation operation() const { return operation_; }
73 
74 private:
76  unsigned timeout_;
77  void* data_;
78  std::size_t size_;
79 };
80 
82 class ReadData : public NetworkOperation
83 {
84 public:
85  explicit ReadData( void* d, std::size_t s, unsigned to = 0 )
86  : NetworkOperation( READ, d, s, to ) {}
87 };
88 
90 class SendData : public NetworkOperation
91 {
92 public:
93  explicit SendData( const void* d, std::size_t s, unsigned to = 0 )
94  : NetworkOperation( WRITE, const_cast<void*>( d ), s, to ) {}
95 };
96 
100 {
101 public:
102  explicit SendString( const std::string& s, unsigned to = 0 )
103  : NetworkOperation( WRITE, const_cast<char*>( s.c_str() ), s.length(), to ) {}
104  explicit SendString( const char *s, unsigned to = 0 )
105  : NetworkOperation( WRITE, const_cast<char*>( s ), strlen( s ), to ) {}
106 };
107 
110 {
111 public:
113 };
114 
116 class NoOp : public NetworkOperation
117 {
118 public:
120 };
121 
124 {
125  template< typename socketType > friend class ConnectionBase;
126 protected:
128  virtual ~ConnectionHandler() {}
129 
130 private:
133 
134 public:
143  };
144 
145 
147  virtual void networkInput( const void* buffer, std::size_t bytesTransferred ) = 0;
148 
150  virtual const NetworkOperation nextOperation() = 0;
151 
152 
154  virtual void signalOccured( NetworkSignal ) {}
155 
157  virtual void setPeer( const RemoteEndpointR& remote ) = 0;
158 };
159 
160 } // namespace net
161 
162 
163 struct HandlerConfiguration;
164 namespace module { class ModulesDirectory; }
165 
168 class ServerHandler : private boost::noncopyable
169 {
170 public:
171  ServerHandler( const HandlerConfiguration* conf,
172  const module::ModulesDirectory* modules );
173  ~ServerHandler();
174 
177 
178 private:
179  class ServerHandlerImpl;
180  ServerHandlerImpl *m_impl;
181 };
182 
183 } // namespace _Wolframe
184 
185 #endif // _CONNECTION_HANDLER_HPP_INCLUDED
unsigned timeout_
Definition: connectionHandler.hpp:76
Definition: connectionHandler.hpp:139
Definition: connectionHandler.hpp:138
Network operation: asynchronously read a block of data.
Definition: connectionHandler.hpp:82
NetworkSignal
Definition: connectionHandler.hpp:135
ServerHandler(const HandlerConfiguration *conf, const module::ModulesDirectory *modules)
unsigned timeout() const
Definition: connectionHandler.hpp:68
Classes for network endpoints.
The modules directory used by the constructors of the providers to build themselves.
Definition: moduleDirectory.hpp:48
Definition: connectionHandler.hpp:140
Definition: connectionHandler.hpp:60
ServerHandlerImpl * m_impl
Definition: connectionHandler.hpp:179
Network operation: no operation.
Definition: connectionHandler.hpp:116
std::size_t size_
Definition: connectionHandler.hpp:78
Definition: connectionHandler.hpp:136
friend class ConnectionBase
Definition: connectionHandler.hpp:125
The common handler for the connection status.
Definition: connectionHandler.hpp:123
Network operation: asynchronously send a block of data.
Definition: connectionHandler.hpp:90
boost::shared_ptr< LocalEndpoint > LocalEndpointR
Definition: connectionEndpoint.hpp:163
Operation operation() const
Definition: connectionHandler.hpp:72
Definition: connectionHandler.hpp:58
const void * data() const
Definition: connectionHandler.hpp:69
Definition: connectionHandler.hpp:142
NetworkOperation(Operation op, void *d=NULL, std::size_t s=0, unsigned to=0)
Definition: connectionHandler.hpp:64
Definition: connectionHandler.hpp:61
Definition: connectionHandler.hpp:59
virtual ~ConnectionHandler()
Definition: connectionHandler.hpp:128
SendString(const char *s, unsigned to=0)
Definition: connectionHandler.hpp:104
virtual const NetworkOperation nextOperation()=0
What should the network do next.
void * buffer()
Definition: connectionHandler.hpp:70
Definition: connectionHandler.hpp:137
virtual void networkInput(const void *buffer, std::size_t bytesTransferred)=0
Signal the incoming data. buffer is the buffer given to the read operation.
SendData(const void *d, std::size_t s, unsigned to=0)
Definition: connectionHandler.hpp:93
virtual void signalOccured(NetworkSignal)
A network error, timeout or signal occured.
Definition: connectionHandler.hpp:154
net::ConnectionHandler * newConnection(const net::LocalEndpointR &local)
Create a new connection handler and return a pointer to it.
Definition: connectionHandler.hpp:141
boost::shared_ptr< RemoteEndpoint > RemoteEndpointR
Definition: connectionEndpoint.hpp:208
SendString(const std::string &s, unsigned to=0)
Definition: connectionHandler.hpp:102
friend class ConnectionBase
Definition: connectionHandler.hpp:54
Operation
Definition: connectionHandler.hpp:57
void * data_
Definition: connectionHandler.hpp:77
Operation operation_
Definition: connectionHandler.hpp:75
Network operation: close the current network connection.
Definition: connectionHandler.hpp:109
CloseConnection()
Definition: connectionHandler.hpp:112
virtual void setPeer(const RemoteEndpointR &remote)=0
Set the remote peer. The connection is up now.
std::size_t size() const
Definition: connectionHandler.hpp:71
ConnectionHandler & operator=(const ConnectionHandler &)
Base class for a network operation. It should never be accessed directly by the user code...
Definition: connectionHandler.hpp:52
ConnectionHandler()
Definition: connectionHandler.hpp:127
Network operation: asynchronously send a string (message) This is just some syntactic sugar...
Definition: connectionHandler.hpp:99
The server main handler All it should do is to provide connection handlers.
Definition: connectionHandler.hpp:168
ReadData(void *d, std::size_t s, unsigned to=0)
Definition: connectionHandler.hpp:85
NoOp()
Definition: connectionHandler.hpp:119