Remote Call Framework 3.3
AsioServerTransport.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_ASIOSERVERTRANSPORT_HPP
19 #define INCLUDE_RCF_ASIOSERVERTRANSPORT_HPP
20 
21 #include <memory>
22 #include <set>
23 #include <vector>
24 
25 #include <RCF/Asio.hpp>
26 #include <RCF/AsioBuffers.hpp>
27 #include <RCF/ByteBuffer.hpp>
28 #include <RCF/Export.hpp>
29 #include <RCF/ServerTransport.hpp>
30 #include <RCF/Service.hpp>
31 #include <RCF/ThreadLibrary.hpp>
32 #include <RCF/Tools.hpp>
33 
34 namespace RCF {
35 
36  class RcfServer;
37  class TcpClientTransport;
38  class AsioNetworkSession;
39  class AsioServerTransport;
40 
41  typedef std::shared_ptr<AsioNetworkSession> AsioNetworkSessionPtr;
42  typedef std::weak_ptr<AsioNetworkSession> AsioNetworkSessionWeakPtr;
43 
44  class ReallocBuffer;
45  typedef std::shared_ptr<ReallocBuffer> ReallocBufferPtr;
46 
47  class Exception;
48 
49  enum WireProtocol;
50 
51  class I_RcfClient;
52  typedef std::shared_ptr<I_RcfClient> RcfClientPtr;
53 
54  class AsioAcceptor
55  {
56  public:
57  virtual ~AsioAcceptor();
58  };
59 
60  typedef std::unique_ptr<AsioAcceptor> AsioAcceptorPtr;
61 
62  class RCF_EXPORT AsioServerTransport :
63  public ServerTransport,
64  public ServerTransportEx,
65  public I_Service
66  {
67  private:
68 
69  // Needs to call open().
70  friend class TcpTransportFactory;
71  friend class HttpTransportFactory;
72  friend class HttpsTransportFactory;
73 
74  friend class Win32NamedPipeNetworkSession;
75 
76  typedef std::weak_ptr<RcfSession> SessionWeakPtr;
77 
78  AsioNetworkSessionPtr createNetworkSession();
79 
80  protected:
81 
82  // I_ServerTransportEx implementation
84  createClientTransport(
85  const Endpoint &endpoint);
86 
87  SessionPtr createServerSession(
88  ClientTransportUniquePtr & clientTransportUniquePtr,
89  RcfClientPtr stubEntryPtr,
90  bool keepClientConnection);
91 
93  createClientTransport(
94  SessionPtr sessionPtr);
95 
96  private:
97 
98  // I_Service implementation
99  void open();
100  void close();
101  void stop();
102 
103  void onServiceAdded( RcfServer & server);
104  void onServiceRemoved( RcfServer & server);
105 
106  protected:
107 
108  void onServerStart( RcfServer & server);
109  void onServerStop( RcfServer & server);
110  void setServer( RcfServer & server);
111 
112  void startAccepting();
113 
114  private:
115 
116  void startAcceptingThread(Exception & eRet);
117 
118  public:
119 
120  RcfServer & getServer();
121  RcfServer & getSessionManager();
122 
123  private:
124 
125  void registerSession(NetworkSessionWeakPtr session);
126  void unregisterSession(NetworkSessionWeakPtr session);
127  void cancelOutstandingIo();
128 
129  friend class AsioNetworkSession;
130  friend class FilterAdapter;
131  friend class ServerTcpFrame;
132  friend class ServerHttpFrame;
133 
134  protected:
135 
136  AsioServerTransport();
137  ~AsioServerTransport();
138 
139  AsioIoService * mpIoService;
140  AsioAcceptorPtr mAcceptorPtr;
141 
142  WireProtocol mWireProtocol;
143 
144  private:
145 
146  volatile bool mStopFlag;
147  RcfServer * mpServer;
148 
149  private:
150 
151  virtual AsioNetworkSessionPtr implCreateNetworkSession() = 0;
152  virtual void implOpen() = 0;
153 
154  virtual ClientTransportUniquePtr implCreateClientTransport(
155  const Endpoint &endpoint) = 0;
156 
157  public:
158 
159  AsioAcceptor & getAcceptor();
160 
161  AsioIoService & getIoService();
162  };
163 
164  class ReadHandler
165  {
166  public:
167  ReadHandler(AsioNetworkSessionPtr networkSessionPtr);
168  void operator()(AsioErrorCode err, std::size_t bytes);
169  void * allocate(std::size_t size);
170  AsioNetworkSessionPtr mNetworkSessionPtr;
171  };
172 
173  class WriteHandler
174  {
175  public:
176  WriteHandler(AsioNetworkSessionPtr networkSessionPtr);
177  void operator()(AsioErrorCode err, std::size_t bytes);
178  void * allocate(std::size_t size);
179  AsioNetworkSessionPtr mNetworkSessionPtr;
180  };
181 
182  void * asio_handler_allocate(std::size_t size, ReadHandler * pHandler);
183  void asio_handler_deallocate(void * pointer, std::size_t size, ReadHandler * pHandler);
184  void * asio_handler_allocate(std::size_t size, WriteHandler * pHandler);
185  void asio_handler_deallocate(void * pointer, std::size_t size, WriteHandler * pHandler);
186 
187  class RCF_EXPORT AsioNetworkSession :
188  public NetworkSession,
189  Noncopyable
190  {
191  public:
192 
193  friend class ReadHandler;
194  friend class WriteHandler;
195  friend class ServerTcpFrame;
196  friend class ServerHttpFrame;
197 
198 
199  typedef std::weak_ptr<AsioNetworkSession> AsioNetworkSessionWeakPtr;
200  typedef std::shared_ptr<AsioNetworkSession> AsioNetworkSessionPtr;
201 
202  AsioNetworkSession(
203  AsioServerTransport &transport,
204  AsioIoService & ioService);
205 
206  virtual ~AsioNetworkSession();
207 
208  AsioNetworkSessionPtr sharedFromThis();
209 
210  void close();
211 
212  AsioErrorCode getLastError();
213 
214  void setCloseAfterWrite();
215 
216  protected:
217  AsioIoService & mIoService;
218 
219  std::vector<char> mReadHandlerBuffer;
220  std::vector<char> mWriteHandlerBuffer;
221 
222  AsioErrorCode mLastError;
223 
224  void runOnDestroyCallbacks();
225 
226  private:
227 
228  // read()/write() are used to connect with the filter sequence.
229  void read(
230  const ByteBuffer &byteBuffer,
231  std::size_t bytesRequested);
232 
233  void write(
234  const std::vector<ByteBuffer> &byteBuffers);
235 
236  public:
237 
238  void setTransportFilters(
239  const std::vector<FilterPtr> &filters);
240 
241  void getTransportFilters(
242  std::vector<FilterPtr> &filters);
243 
244  void setWireFilters(
245  const std::vector<FilterPtr> &filters);
246 
247  void getWireFilters(
248  std::vector<FilterPtr> &filters);
249 
250  AsioServerTransport & getAsioServerTransport();
251 
252  private:
253 
254  void beginAccept();
255  void beginRead();
256  void beginWrite();
257 
258  void onAcceptCompleted(const AsioErrorCode & error);
259 
260  void onNetworkReadCompleted(
261  AsioErrorCode error,
262  size_t bytesTransferred);
263 
264  void onNetworkWriteCompleted(
265  AsioErrorCode error,
266  size_t bytesTransferred);
267 
268  friend class HttpSessionFilter;
269  void onAppReadWriteCompleted(
270  size_t bytesTransferred);
271 
272  void sendServerError(int error);
273 
274  void doCustomFraming(size_t bytesTransferred);
275  void doRegularFraming(size_t bytesTransferred);
276 
277  // TODO: too many friends
278  friend class AsioServerTransport;
279  friend class TcpNetworkSession;
280  friend class UnixLocalNetworkSession;
281  friend class Win32NamedPipeNetworkSession;
282  friend class FilterAdapter;
283 
284  enum State
285  {
286  Ready,
287  Accepting,
288  ReadingData,
289  ReadingDataCount,
290  WritingData
291  };
292 
293  State mState;
294  bool mIssueZeroByteRead;
295  std::size_t mReadBufferRemaining;
296  std::size_t mWriteBufferRemaining;
297 
298  std::vector<FilterPtr> mTransportFilters;
299 
300  friend class HttpServerTransport;
301  friend class PublishingService;
302  std::vector<FilterPtr> mWireFilters;
303 
304  AsioServerTransport & mTransport;
305 
306  std::vector<ByteBuffer> mWriteByteBuffers;
307  std::vector<ByteBuffer> mSlicedWriteByteBuffers;
308 
309  ReallocBufferPtr mAppReadBufferPtr;
310  ByteBuffer mAppReadByteBuffer;
311 
312  ReallocBufferPtr mNetworkReadBufferPtr;
313  ByteBuffer mNetworkReadByteBuffer;
314 
315  // So we can connect our read()/write() functions to the filter sequence.
316  FilterPtr mFilterAdapterPtr;
317 
318  bool mCloseAfterWrite;
319 
320  NetworkSessionWeakPtr mWeakThisPtr;
321 
322  AsioBuffers mBufs;
323 
324  std::shared_ptr<Mutex> mSocketOpsMutexPtr;
325 
326  // I_NetworkSession
327 
328  private:
329 
330  void postRead();
331  ByteBuffer getReadByteBuffer();
332  void postWrite(std::vector<ByteBuffer> &byteBuffers);
333  void postClose();
334  ServerTransport & getServerTransport();
335  const RemoteAddress & getRemoteAddress();
336  bool isConnected();
337 
338  private:
339 
340  virtual const RemoteAddress & implGetRemoteAddress() = 0;
341  virtual void implRead(char * buffer, std::size_t bufferLen) = 0;
342  virtual void implWrite(const std::vector<ByteBuffer> & buffers) = 0;
343  virtual void implAccept() = 0;
344  virtual bool implOnAccept() = 0;
345  virtual bool implIsConnected() = 0;
346  virtual void implClose() = 0;
347  virtual void implCloseAfterWrite() {}
348  virtual void implTransferNativeFrom(ClientTransport & clientTransport) = 0;
349  virtual ClientTransportUniquePtr implCreateClientTransport() = 0;
350  };
351 
352 } // namespace RCF
353 
354 
355 #endif // ! INCLUDE_RCF_ASIOSERVERTRANSPORT_HPP
std::unique_ptr< ClientTransport > ClientTransportUniquePtr
Unique pointer wrapper for RCF::ClientTransport.
Definition: RcfFwd.hpp:43
Definition: AmiIoHandler.hpp:23