RCFProto
 All Classes Functions Typedefs
ConnectedClientTransport.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, 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: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_CONNECTIONORIENTEDCLIENTTRANSPORT_HPP
20 #define INCLUDE_RCF_CONNECTIONORIENTEDCLIENTTRANSPORT_HPP
21 
22 #include <boost/enable_shared_from_this.hpp>
23 
24 #include <RCF/AmiThreadPool.hpp>
25 #include <RCF/AsioBuffers.hpp>
26 #include <RCF/AsioDeadlineTimer.hpp>
27 #include <RCF/Filter.hpp>
28 #include <RCF/ByteOrdering.hpp>
29 #include <RCF/ClientProgress.hpp>
30 #include <RCF/ClientTransport.hpp>
31 #include <RCF/Export.hpp>
32 #include <RCF/RecursionLimiter.hpp>
33 
34 namespace RCF {
35 
36  class ConnectedClientTransport;
37 
38  class ClientFilterProxy;
39 
40  class TcpClientTransport;
41  typedef boost::shared_ptr<TcpClientTransport> TcpClientTransportPtr;
42  class TcpClientFilterProxy;
43 
44  class OverlappedAmi;
45  typedef boost::shared_ptr<OverlappedAmi> OverlappedAmiPtr;
46 
47  class RCF_EXPORT ConnectedClientTransport :
48  public ClientTransport,
49  public WithProgressCallback
50  {
51  public:
52 
53  ConnectedClientTransport(const ConnectedClientTransport &rhs);
54  ConnectedClientTransport();
55  ~ConnectedClientTransport();
56 
57  void close();
58  void setMaxSendSize(std::size_t maxSendSize);
59  std::size_t getMaxSendSize();
60 
61  private:
62 
63  void read(const ByteBuffer &byteBuffer_, std::size_t bytesRequested);
64  void write(const std::vector<ByteBuffer> &byteBuffers);
65  std::size_t timedSend(const std::vector<ByteBuffer> &data);
66  std::size_t timedReceive(ByteBuffer &byteBuffer, std::size_t bytesRequested);
67 
68  void setTransportFilters(const std::vector<FilterPtr> &filters);
69  void getTransportFilters(std::vector<FilterPtr> &filters);
70  void connectTransportFilters();
71 
72  void connect(ClientTransportCallback &clientStub, unsigned int timeoutMs);
73  void disconnect(unsigned int timeoutMs);
74  int timedSend(const char *buffer, std::size_t bufferLen);
75  int timedReceive(char *buffer, std::size_t bufferLen);
76 
77  protected:
78 
79  void onReadCompleted(const ByteBuffer &byteBuffer);
80  void onWriteCompleted(std::size_t bytes);
81 
82  friend class HttpServerTransport;
83  friend class AsioServerTransport;
84  friend class PublishingService;
85  void setWireFilters(const std::vector<FilterPtr> & wireFilters);
86 
87  bool mOwn;
88  bool mClosed;
89  std::size_t mMaxSendSize;
90  std::size_t mBytesTransferred;
91  std::size_t mBytesSent;
92  std::size_t mBytesRead;
93  std::size_t mBytesTotal;
94  int mError;
95  bool mNoTimeout;
96  unsigned int mEndTimeMs;
97 
98  std::vector<FilterPtr> mTransportFilters;
99  std::vector<FilterPtr> mWireFilters;
100  std::vector<ByteBuffer> mByteBuffers;
101  std::vector<ByteBuffer> mSlicedByteBuffers;
102  ReallocBufferPtr mReadBufferPtr;
103  ReallocBufferPtr mReadBuffer2Ptr;
104 
105  friend class ClientFilterProxy;
106  friend class ClientTcpFrame;
107  friend class ClientHttpFrame;
108 
109  private:
110 
111  virtual std::size_t implRead(
112  const ByteBuffer &byteBuffer_,
113  std::size_t bytesRequested) = 0;
114 
115  virtual std::size_t implReadAsync(
116  const ByteBuffer &byteBuffer_,
117  std::size_t bytesRequested) = 0;
118 
119  virtual std::size_t implWrite(
120  const std::vector<ByteBuffer> &byteBuffers) = 0;
121 
122  virtual std::size_t implWriteAsync(
123  const std::vector<ByteBuffer> &byteBuffers) = 0;
124 
125  virtual void implConnect(
126  ClientTransportCallback &clientStub,
127  unsigned int timeoutMs) = 0;
128 
129  virtual void implConnectAsync(
130  ClientTransportCallback &clientStub,
131  unsigned int timeoutMs) = 0;
132 
133  virtual void implClose() = 0;
134 
135  public:
136 
137  enum State {
138  Connecting,
139  Reading,
140  Writing
141  };
142 
143  State mPreState;
144  State mPostState;
145  std::size_t mReadBufferPos;
146  std::size_t mWriteBufferPos;
147 
148  ClientTransportCallback * mpClientStub;
149 
150  ByteBuffer * mpClientStubReadBuffer;
151  ByteBuffer mReadBuffer;
152  std::size_t mBytesToRead;
153  std::size_t mBytesRequested;
154  ByteBuffer mByteBuffer;
155 
156  protected:
157  friend class Subscription;
158  OverlappedAmiPtr mOverlappedPtr;
159 
160 
161  MutexPtr mSocketOpsMutexPtr;
162 
163  AsioBuffers mAsioBuffers;
164 
165  public:
166 
167  typedef boost::shared_ptr<Lock> LockPtr;
168 
169  AsioDeadlineTimerPtr mAsioTimerPtr;
170 
171  friend class TcpClientFilterProxy;
172 
173  public:
174 
175  void cancel();
176 
177  void setTimer(
178  boost::uint32_t timeoutMs,
179  ClientTransportCallback *pClientStub);
180 
181  void onTimerExpired();
182 
183  private:
184  RecursionState<std::size_t, int> mRecursionState;
185 
186  // TODO: Access control.
187  public:
188 
189  void onTransitionCompleted(std::size_t bytesTransferred);
190 
191  void onCompletion(int bytesTransferred);
192  void onTimedRecvCompleted(int ret, int err);
193  void onTimedSendCompleted(int ret, int err);
194  void onConnectCompleted(int err);
195 
196  void transition();
197  void onTransitionCompleted_(std::size_t bytesTransferred);
198  void issueRead(const ByteBuffer &buffer, std::size_t bytesToRead);
199  void issueWrite(const std::vector<ByteBuffer> &byteBuffers);
200 
201  int send(
202  ClientTransportCallback &clientStub,
203  const std::vector<ByteBuffer> &data,
204  unsigned int timeoutMs);
205 
206  int receive(
207  ClientTransportCallback &clientStub,
208  ByteBuffer &byteBuffer,
209  unsigned int timeoutMs);
210 
211  void setSocketOpsMutex(MutexPtr mutexPtr);
212 
213  friend class OverlappedAmi;
214 
215  };
216 
217 } // namespace RCF
218 
219 #endif // ! INCLUDE_RCF_CONNECTIONORIENTEDCLIENTTRANSPORT_HPP