19 #ifndef INCLUDE_RCF_ASIOSERVERTRANSPORT_HPP
20 #define INCLUDE_RCF_ASIOSERVERTRANSPORT_HPP
25 #include <boost/shared_ptr.hpp>
26 #include <boost/weak_ptr.hpp>
28 #include <RCF/Asio.hpp>
29 #include <RCF/Enums.hpp>
30 #include <RCF/Export.hpp>
31 #include <RCF/IpAddress.hpp>
32 #include <RCF/IpServerTransport.hpp>
33 #include <RCF/ServerTransport.hpp>
34 #include <RCF/Service.hpp>
35 #include <RCF/ThreadLibrary.hpp>
40 class TcpClientTransport;
41 class AsioSessionState;
42 class AsioServerTransport;
44 typedef boost::shared_ptr<AsioSessionState> AsioSessionStatePtr;
45 typedef boost::weak_ptr<AsioSessionState> AsioSessionStateWeakPtr;
50 virtual ~AsioAcceptor()
54 typedef boost::scoped_ptr<AsioAcceptor> AsioAcceptorPtr;
56 class RCF_EXPORT AsioServerTransport :
57 public ServerTransport,
58 public ServerTransportEx,
64 friend class TcpAsioTransportFactory;
66 typedef boost::weak_ptr<I_Session> SessionWeakPtr;
68 AsioSessionStatePtr createSessionState();
73 ClientTransportAutoPtr
74 createClientTransport(
75 const Endpoint &endpoint);
77 SessionPtr createServerSession(
78 ClientTransportAutoPtr & clientTransportAutoPtr,
79 StubEntryPtr stubEntryPtr,
80 bool keepClientConnection);
82 ClientTransportAutoPtr
83 createClientTransport(
84 SessionPtr sessionPtr);
87 const SessionPtr &sessionPtr1,
88 const SessionPtr &sessionPtr2);
95 void onServiceAdded( RcfServer & server);
96 void onServiceRemoved( RcfServer & server);
100 void onServerStart( RcfServer & server);
101 void onServerStop( RcfServer & server);
102 void setServer( RcfServer & server);
104 void startAccepting();
108 void startAcceptingThread(Exception & eRet);
110 RcfServer & getServer();
111 RcfServer & getSessionManager();
115 void registerSession(AsioSessionStateWeakPtr session);
116 void unregisterSession(AsioSessionStateWeakPtr session);
117 void cancelOutstandingIo();
119 friend class AsioSessionState;
120 friend class FilterAdapter;
121 friend class ServerTcpFrame;
122 friend class ServerHttpFrame;
126 AsioServerTransport();
127 ~AsioServerTransport();
129 AsioIoService * mpIoService;
130 AsioAcceptorPtr mAcceptorPtr;
132 WireProtocol mWireProtocol;
136 volatile bool mStopFlag;
137 RcfServer * mpServer;
141 virtual AsioSessionStatePtr implCreateSessionState() = 0;
142 virtual void implOpen() = 0;
144 virtual ClientTransportAutoPtr implCreateClientTransport(
145 const Endpoint &endpoint) = 0;
149 AsioAcceptor & getAcceptor();
151 AsioIoService & getIoService();
157 ReadHandler(AsioSessionStatePtr sessionStatePtr);
158 void operator()(AsioErrorCode err, std::size_t bytes);
159 void * allocate(std::size_t size);
160 AsioSessionStatePtr mSessionStatePtr;
166 WriteHandler(AsioSessionStatePtr sessionStatePtr);
167 void operator()(AsioErrorCode err, std::size_t bytes);
168 void * allocate(std::size_t size);
169 AsioSessionStatePtr mSessionStatePtr;
172 void * asio_handler_allocate(std::size_t size, ReadHandler * pHandler);
173 void asio_handler_deallocate(
void * pointer, std::size_t size, ReadHandler * pHandler);
174 void * asio_handler_allocate(std::size_t size, WriteHandler * pHandler);
175 void asio_handler_deallocate(
void * pointer, std::size_t size, WriteHandler * pHandler);
177 class RCF_EXPORT AsioSessionState :
183 friend class ReadHandler;
184 friend class WriteHandler;
185 friend class ServerTcpFrame;
186 friend class ServerHttpFrame;
189 typedef boost::weak_ptr<AsioSessionState> AsioSessionStateWeakPtr;
190 typedef boost::shared_ptr<AsioSessionState> AsioSessionStatePtr;
193 AsioServerTransport &transport,
194 AsioIoService & ioService);
196 virtual ~AsioSessionState();
198 AsioSessionStatePtr sharedFromThis();
202 AsioErrorCode getLastError();
205 AsioIoService & mIoService;
207 std::vector<char> mReadHandlerBuffer;
208 std::vector<char> mWriteHandlerBuffer;
210 AsioErrorCode mLastError;
216 const ByteBuffer &byteBuffer,
217 std::size_t bytesRequested);
220 const std::vector<ByteBuffer> &byteBuffers);
222 void setTransportFilters(
223 const std::vector<FilterPtr> &filters);
225 void getTransportFilters(
226 std::vector<FilterPtr> &filters);
232 void onAcceptCompleted(
const AsioErrorCode & error);
234 void onNetworkReadCompleted(
236 size_t bytesTransferred);
238 void onNetworkWriteCompleted(
240 size_t bytesTransferred);
242 void onAppReadWriteCompleted(
243 size_t bytesTransferred);
245 void onReflectedReadWriteCompleted(
246 const AsioErrorCode & error,
247 size_t bytesTransferred);
249 void sendServerError(
int error);
251 void doCustomFraming(
size_t bytesTransferred);
252 void doRegularFraming(
size_t bytesTransferred);
255 friend class AsioServerTransport;
256 friend class TcpAsioSessionState;
257 friend class UnixLocalSessionState;
258 friend class Win32NamedPipeSessionState;
259 friend class FilterAdapter;
271 bool mIssueZeroByteRead;
272 std::size_t mReadBufferRemaining;
273 std::size_t mWriteBufferRemaining;
275 std::vector<FilterPtr> mTransportFilters;
276 std::vector<FilterPtr> mWireFilters;
278 AsioServerTransport & mTransport;
280 std::vector<ByteBuffer> mWriteByteBuffers;
281 std::vector<ByteBuffer> mSlicedWriteByteBuffers;
283 ReallocBufferPtr mAppReadBufferPtr;
284 ByteBuffer mAppReadByteBuffer;
286 ReallocBufferPtr mNetworkReadBufferPtr;
287 ByteBuffer mNetworkReadByteBuffer;
290 FilterPtr mFilterAdapterPtr;
292 bool mCloseAfterWrite;
293 AsioSessionStateWeakPtr mReflecteeWeakPtr;
294 AsioSessionStatePtr mReflecteePtr;
297 AsioSessionStateWeakPtr mWeakThisPtr;
301 boost::shared_ptr<Mutex> mSocketOpsMutexPtr;
308 ByteBuffer getReadByteBuffer();
309 void postWrite(std::vector<ByteBuffer> &byteBuffers);
311 ServerTransport & getServerTransport();
312 const RemoteAddress & getRemoteAddress();
317 virtual const RemoteAddress & implGetRemoteAddress() = 0;
318 virtual void implRead(
char * buffer, std::size_t bufferLen) = 0;
319 virtual void implWrite(
const std::vector<ByteBuffer> & buffers) = 0;
320 virtual void implWrite(AsioSessionState &toBeNotified,
const char * buffer, std::size_t bufferLen) = 0;
321 virtual void implAccept() = 0;
322 virtual bool implOnAccept() = 0;
323 virtual bool implIsConnected() = 0;
324 virtual void implClose() = 0;
325 virtual void implCloseAfterWrite() {}
326 virtual void implTransferNativeFrom(ClientTransport & clientTransport) = 0;
327 virtual ClientTransportAutoPtr implCreateClientTransport() = 0;
333 #endif // ! INCLUDE_RCF_ASIOSERVERTRANSPORT_HPP