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