Remote Call Framework 3.2
ServerTransport.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2020, 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: 3.2
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 <cstdint>
28 
29 #include <RCF/Enums.hpp>
30 #include <RCF/Export.hpp>
31 #include <RCF/RcfFwd.hpp>
32 #include <RCF/ThreadLibrary.hpp>
33 
34 namespace RCF {
35 
38  {
39  public:
40  virtual ~RemoteAddress()
41  {}
42 
43  virtual std::string string() const
44  {
45  return "";
46  }
47  };
48 
49  enum TransportProtocol;
50  enum TransportType;
51 
54  {};
55 
56  class RCF_EXPORT NetworkSession : public std::enable_shared_from_this<NetworkSession>
57  {
58  public:
59 
60  NetworkSession();
61  virtual ~NetworkSession();
62 
63  virtual void postRead() = 0;
64  virtual ByteBuffer getReadByteBuffer() = 0;
65  virtual void postWrite(std::vector<ByteBuffer> &byteBuffers) = 0;
66  virtual void postClose() = 0;
67 
68  virtual ServerTransport &
69  getServerTransport() = 0;
70 
71  virtual const RemoteAddress &
72  getRemoteAddress() = 0;
73 
74  virtual bool isConnected() = 0;
75 
76 
77  virtual void setTransportFilters(const std::vector<FilterPtr> &filters) = 0;
78  virtual void getTransportFilters(std::vector<FilterPtr> &filters) = 0;
79  void setEnableReconnect(bool enableReconnect);
80  bool getEnableReconnect();
81 
82  virtual void getWireFilters(std::vector<FilterPtr> &filters);
83 
84 
85 
86  std::uint64_t getTotalBytesReceived() const;
87  std::uint64_t getTotalBytesSent() const;
88 
89  RcfSessionPtr getSessionPtr() const;
90 
91  std::uint32_t getLastActivityTimestamp() const;
92  void setLastActivityTimestamp();
93 
94  protected:
95  bool mEnableReconnect;
96  std::uint64_t mBytesReceivedCounter;
97  std::uint64_t mBytesSentCounter;
98  std::uint32_t mLastActivityTimestampMs;
99 
100  RcfSessionPtr mRcfSessionPtr;
101 
102  };
103 
104  typedef std::shared_ptr<NetworkSession> NetworkSessionPtr;
105  typedef std::weak_ptr<NetworkSession> NetworkSessionWeakPtr;
106 
107  class RcfSession;
108  typedef std::shared_ptr<RcfSession> RcfSessionPtr;
109 
110  typedef RcfSessionPtr SessionPtr;
111 
112  class ThreadPool;
113  typedef std::shared_ptr<ThreadPool> ThreadPoolPtr;
114 
115  enum RpcProtocol
116  {
117  Rp_Rcf = 0,
118  Rp_JsonRpc = 1,
119  };
120 
122  class RCF_EXPORT ServerTransport
123  {
124  public:
125  ServerTransport();
126 
127  virtual ~ServerTransport() {}
128 
129  virtual ServerTransportPtr
130  clone() = 0;
131 
132  // *** SWIG BEGIN ***
133 
135  virtual TransportType getTransportType() = 0;
136 
139  void setMaxIncomingMessageLength(std::size_t maxMessageLength);
140 
142  std::size_t getMaxIncomingMessageLength() const;
143 
145  void setConnectionLimit(std::size_t connectionLimit);
146 
148  std::size_t getConnectionLimit() const;
149 
151  void setInitialNumberOfConnections(std::size_t initialNumberOfConnections);
152 
154  std::size_t getInitialNumberOfConnections() const;
155 
157  void setThreadPool(ThreadPoolPtr threadPoolPtr);
158 
162  void setSupportedProtocols(const std::vector<TransportProtocol> & protocols);
163 
165  const std::vector<TransportProtocol> & getSupportedProtocols() const;
166 
167  // *** SWIG END ***
168 
169  void setRpcProtocol(RpcProtocol rpcProtocol);
170  RpcProtocol getRpcProtocol() const;
171 
172 
173  protected:
174 
175  RpcProtocol mRpcProtocol;
176  bool mCustomFraming;
177 
178  private:
179 
180  mutable ReadWriteMutex mReadWriteMutex;
181  std::size_t mMaxMessageLength;
182  std::size_t mConnectionLimit;
183  std::size_t mInitialNumberOfConnections;
184 
185  std::vector<TransportProtocol> mSupportedProtocols;
186 
187  protected:
188 
189  Mutex mSessionsMutex;
190  std::set<NetworkSessionWeakPtr> mSessions;
191 
192  public:
193 
194  template<typename Iter>
195  void enumerateSessions(const Iter & iter)
196  {
197  Lock lock(mSessionsMutex);
198  std::copy(mSessions.begin(), mSessions.end(), iter);
199  }
200 
201  };
202 
203  class ServerTransportEx
204  {
205  public:
206 
207  virtual ~ServerTransportEx() {}
208 
209  virtual ClientTransportUniquePtr
210  createClientTransport(
211  const Endpoint &endpoint) = 0;
212 
213  virtual SessionPtr
214  createServerSession(
215  ClientTransportUniquePtr & clientTransportUniquePtr,
216  RcfClientPtr stubEntryPtr,
217  bool keepClientConnection) = 0;
218 
219  virtual ClientTransportUniquePtr
220  createClientTransport(
221  SessionPtr sessionPtr) = 0;
222 
223  };
224 
225  RCF_EXPORT std::size_t getDefaultMaxMessageLength();
226 
227  RCF_EXPORT void setDefaultMaxMessageLength(
228  std::size_t maxMessageLength);
229 
230 } // namespace RCF
231 
232 #endif // ! INCLUDE_RCF_SERVERTRANSPORT_HPP
Describes the network address of a remote peer.
Definition: ServerTransport.hpp:37
Represents a server side session, associated with a client connection.
Definition: RcfSession.hpp:65
Indicates that no remote address is available.
Definition: ServerTransport.hpp:53
std::unique_ptr< ClientTransport > ClientTransportUniquePtr
Unique pointer wrapper for RCF::ClientTransport.
Definition: RcfFwd.hpp:44
Base class for all server transports.
Definition: ServerTransport.hpp:122
Base class for all network endpoint types.
Definition: Endpoint.hpp:41
Represents a server-side thread pool.
Definition: ThreadPool.hpp:79
Definition: ByteBuffer.hpp:40
TransportProtocol
Describes the transport protocols used by a RCF connection. Transport protocols are layered on top of...
Definition: Enums.hpp:63
Definition: AmiIoHandler.hpp:24
TransportType
Describes the transport types used by a RCF connection.
Definition: Enums.hpp:34
std::shared_ptr< ServerTransport > ServerTransportPtr
Unique pointer wrapper for RCF::ServerTransport.
Definition: RcfFwd.hpp:47