RCFProto
 All Classes Functions Typedefs
ClientStub.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_CLIENTSTUB_HPP
20 #define INCLUDE_RCF_CLIENTSTUB_HPP
21 
22 #include <string>
23 #include <vector>
24 #include <memory>
25 
26 #include <boost/scoped_ptr.hpp>
27 #include <boost/any.hpp>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/enable_shared_from_this.hpp>
30 
31 #include <RCF/ClientProgress.hpp>
32 #include <RCF/Filter.hpp>
33 #include <RCF/ClientTransport.hpp>
34 #include <RCF/Endpoint.hpp>
35 #include <RCF/Enums.hpp>
36 #include <RCF/Export.hpp>
37 #include <RCF/GetInterfaceName.hpp>
38 #include <RCF/MethodInvocation.hpp>
39 #include <RCF/OverlappedAmi.hpp>
40 #include <RCF/SerializationProtocol_Base.hpp>
41 #include <RCF/RecursionLimiter.hpp>
42 #include <RCF/SerializationProtocol.hpp>
43 #include <RCF/Token.hpp>
44 
45 #if RCF_FEATURE_FILETRANSFER==1
46 #include <RCF/FileStream.hpp>
47 #include <RCF/FileDownload.hpp>
48 #include <RCF/FileUpload.hpp>
49 #endif
50 
51 namespace RCF {
52 
53  template<typename T>
54  class FutureImpl;
55 
56  struct Void;
57 
58  class CallOptions;
59 
60  class ConnectionResetGuard;
61 
62  class I_Parameters;
63 
65  RCF_EXPORT void setDefaultSslImplementation(SslImplementation sslImplementation);
66 
68  RCF_EXPORT SslImplementation getDefaultSslImplementation();
69 
70  RCF_EXPORT void setDefaultConnectTimeoutMs(unsigned int connectTimeoutMs);
71  RCF_EXPORT unsigned int getDefaultConnectTimeoutMs();
72 
73  RCF_EXPORT void setDefaultRemoteCallTimeoutMs(unsigned int remoteCallTimeoutMs);
74  RCF_EXPORT unsigned int getDefaultRemoteCallTimeoutMs();
75 
76  RCF_EXPORT void setDefaultNativeWstringSerialization(bool enable);
77  RCF_EXPORT bool getDefaultNativeWstringSerialization();
78 
79  class ClientStub;
80 
81  typedef boost::shared_ptr<ClientStub> ClientStubPtr;
82 
83  typedef Token FileTransferToken;
84 
85  class ClientProgress;
86  typedef boost::shared_ptr<ClientProgress> ClientProgressPtr;
87 
88  class ClientTransport;
89  typedef std::auto_ptr<ClientTransport> ClientTransportAutoPtr;
90 
91  class I_RcfClient;
92  typedef boost::shared_ptr<I_RcfClient> RcfClientPtr;
93 
94  class I_Future;
95  class IpClientTransport;
96 
97  template<typename T>
98  class FutureImpl;
99 
100  class FileTransferProgress;
101 
102  class OpenSslEncryptionFilter;
103 
104  class SspiFilter;
105 
106  typedef boost::function1<void, const FileTransferProgress &> FileProgressCb;
107 
108  template<
109  typename R,
110  typename A1,
111  typename A2,
112  typename A3,
113  typename A4,
114  typename A5,
115  typename A6,
116  typename A7,
117  typename A8,
118  typename A9,
119  typename A10,
120  typename A11,
121  typename A12,
122  typename A13,
123  typename A14,
124  typename A15>
125  class AllocateClientParameters;
126 
127  template<
128  typename R,
129  typename A1,
130  typename A2,
131  typename A3,
132  typename A4,
133  typename A5,
134  typename A6,
135  typename A7,
136  typename A8,
137  typename A9,
138  typename A10,
139  typename A11,
140  typename A12,
141  typename A13,
142  typename A14,
143  typename A15>
144  class ClientParameters;
145 
146  class OverlappedAmi;
147  typedef boost::shared_ptr<OverlappedAmi> OverlappedAmiPtr;
148 
149  class InProcessTransport;
150 
151  class RCF_EXPORT CurrentClientStubSentry
152  {
153  public:
154  CurrentClientStubSentry(ClientStub & clientStub);
155  ~CurrentClientStubSentry();
156  };
157 
158  class RCF_EXPORT ClientStub :
159  public ClientTransportCallback,
160  public boost::enable_shared_from_this<ClientStub>
161  {
162  public:
163  ClientStub(const std::string &interfaceName);
164  ClientStub(const std::string &interfaceName, const std::string &objectName);
165  ClientStub(const ClientStub &rhs);
166  ~ClientStub();
167 
168  ClientStub &operator=(const ClientStub &rhs);
169 
170  void setEndpoint(const Endpoint &endpoint);
171  void setEndpoint(EndpointPtr endpointPtr);
172  EndpointPtr getEndpoint() const;
173  Token getTargetToken() const;
174  void setTargetToken(Token token);
175  const std::string & getTargetName() const;
176  void setTargetName(const std::string &targetName);
177  void setInterfaceName(const std::string & interfaceName);
178  const std::string & getInterfaceName();
179  RemoteCallSemantics getRemoteCallSemantics() const;
180  void setRemoteCallSemantics(RemoteCallSemantics defaultCallingSemantics);
181 
182  // Deprecated - use getRemoteCallSemantics()/setRemoteCallSemantics instead.
183  RemoteCallSemantics getDefaultCallingSemantics() const;
184  void setDefaultCallingSemantics(RemoteCallSemantics defaultCallingSemantics);
185 
186  void setSerializationProtocol(SerializationProtocol protocol);
187  SerializationProtocol getSerializationProtocol() const;
188 
189  void setMarshalingProtocol(MarshalingProtocol protocol);
190  MarshalingProtocol getMarshalingProtocol() const;
191 
192  bool getNativeWstringSerialization();
193  void setNativeWstringSerialization(bool enable);
194 
195  void setEnableSfPointerTracking(bool enable);
196  bool getEnableSfPointerTracking() const;
197 
198  void setTransport(ClientTransportAutoPtr transport);
199 
200  ClientTransport& getTransport();
201  IpClientTransport & getIpTransport();
202 
203  ClientTransportAutoPtr releaseTransport();
204 
205  void instantiateTransport();
206  void connect();
207  void connectAsync(boost::function0<void> onCompletion);
208  void wait(boost::function0<void> onCompletion, boost::uint32_t timeoutMs);
209  void disconnect();
210  bool isConnected();
211  void setConnected(bool connected);
212 
213  void setMessageFilters(const std::vector<FilterPtr> &filters);
214  void setMessageFilters();
215  void setMessageFilters(FilterPtr filterPtr);
216 
217  const std::vector<FilterPtr> &
218  getMessageFilters();
219 
220 
221  // Synchronous transport filter requests.
222  void requestTransportFilters(const std::vector<FilterPtr> &filters);
223  void requestTransportFilters(FilterPtr filterPtr);
224  void requestTransportFilters();
225 
226  void clearTransportFilters();
227 
228  // Asynchronous transport filter requests.
229  void requestTransportFiltersAsync(
230  const std::vector<FilterPtr> &filters,
231  boost::function0<void> onCompletion);
232 
233  void requestTransportFiltersAsync(
234  FilterPtr filterPtr,
235  boost::function0<void> onCompletion);
236 
237  void setRemoteCallTimeoutMs(unsigned int remoteCallTimeoutMs);
238  unsigned int getRemoteCallTimeoutMs() const;
239 
240  void setConnectTimeoutMs(unsigned int connectTimeoutMs);
241  unsigned int getConnectTimeoutMs() const;
242 
243  void setAutoReconnect(bool autoReconnect);
244  bool getAutoReconnect() const;
245 
246  void setAutoVersioning(bool autoVersioning);
247  bool getAutoVersioning() const;
248 
249  void setRuntimeVersion(boost::uint32_t version);
250  boost::uint32_t getRuntimeVersion() const;
251 
252  void setArchiveVersion(boost::uint32_t version);
253  boost::uint32_t getArchiveVersion() const;
254 
255  void setClientProgressPtr(ClientProgressPtr clientProgressPtr);
256 
257  ClientProgressPtr
258  getClientProgressPtr() const;
259 
260  typedef ClientProgress::ProgressCallback RemoteCallProgressCallback;
261 
262  void setRemoteCallProgressCallback(
263  RemoteCallProgressCallback cb,
264  boost::uint32_t callbackIntervalMs);
265 
266  void setTries(std::size_t tries);
267  std::size_t getTries() const;
268 
269  void setUserData(boost::any userData);
270  boost::any getUserData();
271 
272  //**********************************************************************
273  // These functions involve network calls.
274 
275  // Synchronous versions.
276 
277  void createRemoteObject(const std::string &objectName = "");
278  void deleteRemoteObject();
279 
280  void createRemoteSessionObject(const std::string &objectName = "");
281  void deleteRemoteSessionObject();
282 
283 #if RCF_FEATURE_FILETRANSFER==1
284  void setFileProgressCallback(FileProgressCb fileProgressCb);
285  void setFileProgressCallback() { setFileProgressCallback( FileProgressCb() ); }
286 
287  void uploadFiles(
288  const std::string & whichFile,
289  std::string & uploadId,
290  boost::uint32_t chunkSize,
291  boost::uint32_t transferRateBps,
292  boost::uint32_t sessionLocalId);
293 
294  void uploadFiles(
295  const FileManifest & whichFile,
296  std::string & uploadId,
297  boost::uint32_t chunkSize,
298  boost::uint32_t transferRateBps,
299  boost::uint32_t sessionLocalId);
300 
301  void downloadFiles(
302  const std::string & downloadLocation,
303  FileManifest & manifest,
304  boost::uint32_t chunkSize,
305  boost::uint32_t transferRateBps,
306  boost::uint32_t sessionLocalId);
307 
308  boost::uint32_t addUploadStream(FileUpload fileStream);
309  void processUploadStreams();
310 
311  boost::uint32_t addDownloadStream(FileDownload fileStream);
312 
313  // For testing.
314  void setTransferWindowS(boost::uint32_t transferWindowS);
315  boost::uint32_t getTransferWindowS();
316 #endif
317 
318  FutureImpl<Void> ping();
319  FutureImpl<Void> ping(const CallOptions & callOptions);
320 
321  //**********************************************************************
322 
323  void setPingBackIntervalMs(int pingBackIntervalMs);
324  int getPingBackIntervalMs();
325 
326  std::size_t getPingBackCount();
327  boost::uint32_t getPingBackTimeStamp();
328 
329  void clearParameters();
330 
331  SerializationProtocolIn & getSpIn();
332  SerializationProtocolOut & getSpOut();
333 
334  std::auto_ptr<Exception> getAsyncException();
335  void setAsyncException(std::auto_ptr<Exception>);
336  bool hasAsyncException();
337 
338  boost::uint32_t generatePollingTimeout(boost::uint32_t timeoutMs);
339  void onPollingTimeout();
340  void onUiMessage();
341 
342  friend class CallOptions;
343 
344  private:
345 
346  template<typename T>
347  friend class FutureImpl;
348 
349  template<
350  typename R,
351  typename A1,
352  typename A2,
353  typename A3,
354  typename A4,
355  typename A5,
356  typename A6,
357  typename A7,
358  typename A8,
359  typename A9,
360  typename A10,
361  typename A11,
362  typename A12,
363  typename A13,
364  typename A14,
365  typename A15>
366  friend class AllocateClientParameters;
367 
368  template<
369  typename R,
370  typename A1,
371  typename A2,
372  typename A3,
373  typename A4,
374  typename A5,
375  typename A6,
376  typename A7,
377  typename A8,
378  typename A9,
379  typename A10,
380  typename A11,
381  typename A12,
382  typename A13,
383  typename A14,
384  typename A15>
385  friend class ClientParameters;
386 
387  friend class InProcessTransport;
388 
389  Token mToken;
390  RemoteCallSemantics mDefaultCallingSemantics;
391  SerializationProtocol mProtocol;
392  MarshalingProtocol mMarshalingProtocol;
393  std::string mEndpointName;
394  std::string mObjectName;
395  std::string mInterfaceName;
396 
397  unsigned int mRemoteCallTimeoutMs;
398  unsigned int mConnectTimeoutMs;
399  bool mAutoReconnect;
400  bool mConnected;
401  std::size_t mTries;
402 
403  EndpointPtr mEndpoint;
404  ClientTransportAutoPtr mTransport;
405 
406  VectorFilter mMessageFilters;
407 
408  ClientProgressPtr mClientProgressPtr;
409 
410  bool mAutoVersioning;
411  boost::uint32_t mRuntimeVersion;
412  boost::uint32_t mArchiveVersion;
413 
414  bool mUseNativeWstringSerialization;
415  bool mEnableSfPointerTracking;
416 
417  std::vector<I_Future *> mFutures;
418  boost::any mUserData;
419 
420  MethodInvocationRequest mRequest;
421  SerializationProtocolIn mIn;
422  SerializationProtocolOut mOut;
423 
424  bool mAsync;
425  OverlappedAmi::AsyncOpType mAsyncOpType;
426  boost::function0<void> mAsyncCallback;
427  std::auto_ptr<Exception> mAsyncException;
428  unsigned int mEndTimeMs;
429  bool mRetry;
430  RemoteCallSemantics mRcs;
431  ByteBuffer mEncodedByteBuffer;
432  std::vector<ByteBuffer> mEncodedByteBuffers;
433 
434  std::vector<char> mRetValVec;
435  std::vector<char> mParametersVec;
436  I_Parameters * mpParameters;
437 
438  boost::uint32_t mPingBackIntervalMs;
439  boost::uint32_t mPingBackTimeStamp;
440  std::size_t mPingBackCount;
441 
442  boost::uint32_t mNextTimerCallbackMs;
443  boost::uint32_t mNextPingBackCheckMs;
444  boost::uint32_t mPingBackCheckIntervalMs;
445  boost::uint32_t mTimerIntervalMs;
446 
447  MutexPtr mSignalledMutexPtr;
448  ConditionPtr mSignalledConditionPtr;
449  LockPtr mSignalledLockPtr;
450 
451  bool mSignalled;
452 
453  Mutex mSubRcfClientMutex;
454  RcfClientPtr mSubRcfClientPtr;
455 
456  bool mBatchMode;
457  ReallocBufferPtr mBatchBufferPtr;
458  ReallocBuffer mBatchBufferTemp;
459  boost::uint32_t mBatchMaxMessageLength;
460  boost::uint32_t mBatchCount;
461  boost::uint32_t mBatchMessageCount;
462 
463  // Set to true if we need to set the transport protocol without disconnecting.
464  bool mSetTransportProtocol;
465 
466  void enrol(
467  I_Future *pFuture);
468 
469  void init(
470  const std::string & interfaceName,
471  int fnId,
472  RCF::RemoteCallSemantics rcs);
473 
474  void beginCall();
475 
476  void beginSend();
477 
478  void beginReceive();
479 
480  void call(
481  RCF::RemoteCallSemantics rcs);
482 
483  void onConnectCompleted(
484  bool alreadyConnected = false);
485 
486  void onRequestTransportFiltersCompleted();
487 
488  void doBatching();
489 
490  void onSendCompleted();
491 
492  void onReceiveCompleted();
493 
494  void onTimerExpired();
495 
496  void onError(
497  const std::exception &e);
498 
499  void setAsyncCallback(
500  boost::function0<void> callback);
501 
502  void onException(
503  const Exception & e);
504 
505  void scheduleAmiNotification();
506 
507  void createFilterSequence(std::vector<FilterPtr> & filters);
508 
509 
510  public:
511 
512  std::vector<char> & getRetValVec() { return mRetValVec; }
513 
514  // Batching
515 
516  void enableBatching();
517  void disableBatching(bool flush = true);
518  void flushBatch(unsigned int timeoutMs = 0);
519 
520  void setMaxBatchMessageLength(boost::uint32_t maxBatchMessageLength);
521  boost::uint32_t getMaxBatchMessageLength();
522 
523  boost::uint32_t getBatchesSent();
524  boost::uint32_t getMessagesInCurrentBatch();
525 
526  // Async
527 
528  void setAsync(bool async);
529  bool getAsync();
530 
531  bool ready();
532  void waitForReady(boost::uint32_t timeoutMs = 0);
533  void cancel();
534 
535  void setSubRcfClientPtr(RcfClientPtr clientStubPtr);
536  RcfClientPtr getSubRcfClientPtr();
537 
538  // User data
539 
540  void setRequestUserData(const std::string & userData);
541  std::string getRequestUserData();
542 
543  void setResponseUserData(const std::string & userData);
544  std::string getResponseUserData();
545 
546 #if RCF_FEATURE_FILETRANSFER==1
547  FileProgressCb mFileProgressCb;
548 
549  std::vector<FileUpload> mUploadStreams;
550  std::vector<FileDownload> mDownloadStreams;
551 #endif
552 
553  boost::uint32_t mTransferWindowS;
554 
555  RecursionState<int, int> mRecursionState;
556 
557  std::string mCurrentCallDesc;
558  bool mCallInProgress;
559 
560  public:
561 
562  void setHttpProxy(const std::string & httpProxy);
563  std::string getHttpProxy();
564 
565  void setHttpProxyPort(int httpProxyPort);
566  int getHttpProxyPort();
567 
568  void setTransportProtocol(TransportProtocol protocol);
569  TransportProtocol getTransportProtocol();
570 
571  TransportType getTransportType();
572 
573  void setUsername(const tstring & username);
574  tstring getUsername();
575 
576  void setPassword(const tstring & password);
577  tstring getPassword();
578 
579  void setKerberosSpn(const tstring & kerberosSpn);
580  tstring getKerberosSpn();
581 
582  void setEnableCompression(bool enableCompression);
583  bool getEnableCompression();
584 
585  void setCertificate(CertificatePtr certificatePtr);
586  CertificatePtr getCertificate();
587 
588  void setCaCertificate(CertificatePtr certificatePtr);
589  CertificatePtr getCaCertificate();
590 
591  void setOpenSslCipherSuite(const std::string & cipherSuite);
592  std::string getOpenSslCipherSuite() const;
593 
594  void setEnableSchannelCertificateValidation(const tstring & peerName);
595  tstring getEnableSchannelCertificateValidation() const;
596 
597  typedef boost::function<bool(Certificate *)> CertificateValidationCb;
598  void setCertificateValidationCallback(CertificateValidationCb certificateValidationCb);
599  const CertificateValidationCb & getCertificateValidationCallback() const;
600 
601  void setSslImplementation(SslImplementation sslImplementation);
602  SslImplementation getSslImplementation() const;
603 
604 #ifdef BOOST_WINDOWS
605  void setWindowsImpersonationToken(HANDLE hToken);
606  HANDLE getWindowsImpersonationToken() const;
607  private:
608  std::auto_ptr<HANDLE> mWindowsImpersonationToken;
609 #endif
610 
611  private:
612  std::string mHttpProxy;
613  int mHttpProxyPort;
614  TransportProtocol mTransportProtocol;
615  tstring mUsername;
616  tstring mPassword;
617  tstring mKerberosSpn;
618  bool mEnableCompression;
619 
620  CertificatePtr mCertificatePtr;
621  CertificatePtr mCaCertificatePtr;
622  CertificateValidationCb mCertificateValidationCb;
623  tstring mSchannelCertificateValidation;
624 
625  std::string mOpenSslCipherSuite;
626 
627  SslImplementation mSslImplementation;
628  };
629 
630  class CallOptions
631  {
632  public:
633  CallOptions() :
634  mAsync(false),
635  mRcsSpecified(false),
636  mRcs(Twoway),
637  mCallback()
638  {}
639 
640  CallOptions(RemoteCallSemantics rcs) :
641  mAsync(false),
642  mRcsSpecified(true),
643  mRcs(rcs),
644  mCallback()
645  {}
646 
647  CallOptions(RemoteCallSemantics rcs, const boost::function0<void> & callback) :
648  mAsync(true),
649  mRcsSpecified(true),
650  mRcs(rcs),
651  mCallback(callback)
652  {}
653 
654  CallOptions(boost::function0<void> callback) :
655  mAsync(true),
656  mRcsSpecified(false),
657  mRcs(Twoway),
658  mCallback(callback)
659  {}
660 
661  RemoteCallSemantics apply(ClientStub &clientStub) const
662  {
663  clientStub.setAsync(mAsync);
664  clientStub.setAsyncCallback(mCallback);
665  return mRcsSpecified ? mRcs : clientStub.getRemoteCallSemantics();
666  }
667 
668  private:
669  bool mAsync;
670  bool mRcsSpecified;
671  RemoteCallSemantics mRcs;
672  boost::function0<void> mCallback;
673  };
674 
675  class RCF_EXPORT AsyncTwoway : public CallOptions
676  {
677  public:
678  AsyncTwoway(const boost::function0<void> & callback) :
679  CallOptions(RCF::Twoway, callback)
680  {}
681  };
682 
683  class RCF_EXPORT AsyncOneway : public CallOptions
684  {
685  public:
686  AsyncOneway(const boost::function0<void> & callback) :
687  CallOptions(RCF::Oneway, callback)
688  {}
689  };
690 
691  class RestoreClientTransportGuard
692  {
693  public:
694 
695  RestoreClientTransportGuard(ClientStub &client, ClientStub &clientTemp) :
696  mClient(client),
697  mClientTemp(clientTemp)
698  {}
699 
700  ~RestoreClientTransportGuard()
701  {
702  RCF_DTOR_BEGIN
703  mClient.setTransport(mClientTemp.releaseTransport());
704  RCF_DTOR_END
705  }
706 
707  private:
708  ClientStub &mClient;
709  ClientStub &mClientTemp;
710  };
711 
712 
713 } // namespace RCF
714 
715 #endif // ! INCLUDE_RCF_CLIENTSTUB_HPP