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