RCFProto
 All Classes Functions Typedefs
ServerTransport.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_SERVERTRANSPORT_HPP
20 #define INCLUDE_RCF_SERVERTRANSPORT_HPP
21 
22 #include <memory>
23 #include <set>
24 #include <string>
25 #include <vector>
26 
27 #include <boost/enable_shared_from_this.hpp>
28 #include <boost/shared_ptr.hpp>
29 
30 #include <RCF/ByteBuffer.hpp>
31 #include <RCF/Enums.hpp>
32 #include <RCF/Export.hpp>
33 #include <RCF/Filter.hpp>
34 #include <RCF/ThreadLibrary.hpp>
35 
36 namespace RCF {
37 
38  class Filter;
39  class Endpoint;
40  class ClientTransport;
41  class NetworkSession;
42  class StubEntry;
43  class RcfSession;
44 
45  typedef boost::shared_ptr<Filter> FilterPtr;
46  typedef std::auto_ptr<ClientTransport> ClientTransportAutoPtr;
47  typedef boost::shared_ptr<StubEntry> StubEntryPtr;
48 
49  class ServerTransport;
50  typedef boost::shared_ptr<ServerTransport> ServerTransportPtr;
51  typedef std::auto_ptr<ServerTransport> ServerTransportAutoPtr;
52 
53  typedef boost::shared_ptr<RcfSession> RcfSessionPtr;
54 
55  class RemoteAddress
56  {
57  public:
58  virtual ~RemoteAddress()
59  {}
60 
61  virtual std::string string() const
62  {
63  return "";
64  }
65  };
66 
67  class NoRemoteAddress : public RemoteAddress
68  {};
69 
70  class RCF_EXPORT NetworkSession : public boost::enable_shared_from_this<NetworkSession>
71  {
72  public:
73 
74  NetworkSession();
75  virtual ~NetworkSession();
76 
77  virtual void postRead() = 0;
78  virtual ByteBuffer getReadByteBuffer() = 0;
79  virtual void postWrite(std::vector<ByteBuffer> &byteBuffers) = 0;
80  virtual void postClose() = 0;
81 
82  virtual ServerTransport &
83  getServerTransport() = 0;
84 
85  virtual const RemoteAddress &
86  getRemoteAddress() = 0;
87 
88  virtual bool isConnected() = 0;
89 
90 
91  virtual void setTransportFilters(const std::vector<FilterPtr> &filters) = 0;
92  virtual void getTransportFilters(std::vector<FilterPtr> &filters) = 0;
93  void setEnableReconnect(bool enableReconnect);
94  bool getEnableReconnect();
95 
96  boost::uint64_t getTotalBytesReceived() const;
97  boost::uint64_t getTotalBytesSent() const;
98 
99  RcfSessionPtr getSessionPtr() const;
100 
101  boost::uint32_t getLastActivityTimestamp() const;
102  void setLastActivityTimestamp();
103 
104  protected:
105  bool mEnableReconnect;
106  boost::uint64_t mBytesReceivedCounter;
107  boost::uint64_t mBytesSentCounter;
108  boost::uint32_t mLastActivityTimestampMs;
109 
110  RcfSessionPtr mRcfSessionPtr;
111 
112  };
113 
114  typedef boost::shared_ptr<NetworkSession> NetworkSessionPtr;
115  typedef boost::weak_ptr<NetworkSession> NetworkSessionWeakPtr;
116 
117  class RcfSession;
118  typedef boost::shared_ptr<RcfSession> RcfSessionPtr;
119 
120  typedef RcfSession I_Session;
121  typedef RcfSessionPtr SessionPtr;
122 
123  class ThreadPool;
124  typedef boost::shared_ptr<ThreadPool> ThreadPoolPtr;
125 
126  enum RpcProtocol
127  {
128  Rp_Rcf = 0,
129  Rp_JsonRpc = 1,
130  };
131 
133  class RCF_EXPORT ServerTransport
134  {
135  public:
136  ServerTransport();
137 
138  virtual ~ServerTransport() {}
139 
140  virtual ServerTransportPtr
141  clone() = 0;
142 
143  // Deprecated - use setMaxIncomingMessageLength()/getMaxIncomingMessageLength() instead.
144  ServerTransport & setMaxMessageLength(std::size_t maxMessageLength);
145  std::size_t getMaxMessageLength() const;
146 
147  // *** SWIG BEGIN ***
148 
150  virtual TransportType getTransportType() = 0;
151 
154  ServerTransport & setMaxIncomingMessageLength(std::size_t maxMessageLength);
155 
157  std::size_t getMaxIncomingMessageLength() const;
158 
160  ServerTransport & setConnectionLimit(std::size_t connectionLimit);
161 
163  std::size_t getConnectionLimit() const;
164 
166  ServerTransport & setInitialNumberOfConnections(std::size_t initialNumberOfConnections);
167 
169  std::size_t getInitialNumberOfConnections() const;
170 
172  ServerTransport & setThreadPool(ThreadPoolPtr threadPoolPtr);
173 
177  ServerTransport & setSupportedProtocols(const std::vector<TransportProtocol> & protocols);
178 
180  const std::vector<TransportProtocol> & getSupportedProtocols() const;
181 
182  // *** SWIG END ***
183 
184  void setRpcProtocol(RpcProtocol rpcProtocol);
185  RpcProtocol getRpcProtocol() const;
186 
187 
188  protected:
189 
190  RpcProtocol mRpcProtocol;
191  bool mCustomFraming;
192 
193  private:
194 
195  mutable ReadWriteMutex mReadWriteMutex;
196  std::size_t mMaxMessageLength;
197  std::size_t mConnectionLimit;
198  std::size_t mInitialNumberOfConnections;
199 
200  std::vector<TransportProtocol> mSupportedProtocols;
201 
202  protected:
203 
204  Mutex mSessionsMutex;
205  std::set<NetworkSessionWeakPtr> mSessions;
206 
207  public:
208 
209  template<typename Iter>
210  void enumerateSessions(const Iter & iter)
211  {
212  Lock lock(mSessionsMutex);
213  std::copy(mSessions.begin(), mSessions.end(), iter);
214  }
215 
216  };
217 
218  class ServerTransportEx
219  {
220  public:
221 
222  virtual ~ServerTransportEx() {}
223 
224  virtual ClientTransportAutoPtr
225  createClientTransport(
226  const Endpoint &endpoint) = 0;
227 
228  virtual SessionPtr
229  createServerSession(
230  ClientTransportAutoPtr & clientTransportAutoPtr,
231  StubEntryPtr stubEntryPtr,
232  bool keepClientConnection) = 0;
233 
234  virtual ClientTransportAutoPtr
235  createClientTransport(
236  SessionPtr sessionPtr) = 0;
237 
238  };
239 
240  RCF_EXPORT std::size_t getDefaultMaxMessageLength();
241 
242  RCF_EXPORT void setDefaultMaxMessageLength(
243  std::size_t maxMessageLength);
244 
245 } // namespace RCF
246 
247 #endif // ! INCLUDE_RCF_SERVERTRANSPORT_HPP