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