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