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 SessionState;
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 SessionState : public boost::enable_shared_from_this<SessionState>
71  {
72  public:
73 
74  SessionState();
75  virtual ~SessionState();
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  protected:
102  bool mEnableReconnect;
103  boost::uint64_t mBytesReceivedCounter;
104  boost::uint64_t mBytesSentCounter;
105 
106  RcfSessionPtr mSessionPtr;
107 
108  };
109 
110  typedef boost::shared_ptr<SessionState> SessionStatePtr;
111  typedef boost::weak_ptr<SessionState> SessionStateWeakPtr;
112 
113  class RcfSession;
114  typedef boost::shared_ptr<RcfSession> RcfSessionPtr;
115 
116  typedef RcfSession I_Session;
117  typedef RcfSessionPtr SessionPtr;
118 
119  class ThreadPool;
120  typedef boost::shared_ptr<ThreadPool> ThreadPoolPtr;
121 
122  enum RpcProtocol
123  {
124  Rp_Rcf = 0,
125  Rp_JsonRpc = 1,
126  };
127 
129  class RCF_EXPORT ServerTransport
130  {
131  public:
132  ServerTransport();
133 
134  virtual ~ServerTransport() {}
135 
136  virtual ServerTransportPtr
137  clone() = 0;
138 
139  // Deprecated - use setMaxIncomingMessageLength()/getMaxIncomingMessageLength() instead.
140  ServerTransport & setMaxMessageLength(std::size_t maxMessageLength);
141  std::size_t getMaxMessageLength() const;
142 
143  // *** SWIG BEGIN ***
144 
146  virtual TransportType getTransportType() = 0;
147 
150  ServerTransport & setMaxIncomingMessageLength(std::size_t maxMessageLength);
151 
153  std::size_t getMaxIncomingMessageLength() const;
154 
156  ServerTransport & setConnectionLimit(std::size_t connectionLimit);
157 
159  std::size_t getConnectionLimit() const;
160 
162  ServerTransport & setInitialNumberOfConnections(std::size_t initialNumberOfConnections);
163 
165  std::size_t getInitialNumberOfConnections() const;
166 
168  ServerTransport & setThreadPool(ThreadPoolPtr threadPoolPtr);
169 
173  ServerTransport & setSupportedProtocols(const std::vector<TransportProtocol> & protocols);
174 
176  const std::vector<TransportProtocol> & getSupportedProtocols() const;
177 
178  // *** SWIG END ***
179 
180  void setRpcProtocol(RpcProtocol rpcProtocol);
181  RpcProtocol getRpcProtocol() const;
182 
183 
184  protected:
185 
186  RpcProtocol mRpcProtocol;
187  bool mCustomFraming;
188 
189  private:
190 
191  mutable ReadWriteMutex mReadWriteMutex;
192  std::size_t mMaxMessageLength;
193  std::size_t mConnectionLimit;
194  std::size_t mInitialNumberOfConnections;
195 
196  std::vector<TransportProtocol> mSupportedProtocols;
197 
198  protected:
199 
200  Mutex mSessionsMutex;
201  std::set<SessionStateWeakPtr> mSessions;
202 
203  public:
204 
205  template<typename Iter>
206  void enumerateSessions(const Iter & iter)
207  {
208  Lock lock(mSessionsMutex);
209  std::copy(mSessions.begin(), mSessions.end(), iter);
210  }
211 
212  };
213 
214  class ServerTransportEx
215  {
216  public:
217 
218  virtual ~ServerTransportEx() {}
219 
220  virtual ClientTransportAutoPtr
221  createClientTransport(
222  const Endpoint &endpoint) = 0;
223 
224  virtual SessionPtr
225  createServerSession(
226  ClientTransportAutoPtr & clientTransportAutoPtr,
227  StubEntryPtr stubEntryPtr,
228  bool keepClientConnection) = 0;
229 
230  virtual ClientTransportAutoPtr
231  createClientTransport(
232  SessionPtr sessionPtr) = 0;
233 
234  virtual bool
235  reflect(
236  const SessionPtr &sessionPtr1,
237  const SessionPtr &sessionPtr2) = 0;
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