Remote Call Framework 3.4
RcfSession.hpp
Go to the documentation of this file.
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2023, Delta V Software. All rights reserved.
6 // https://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 under GPL terms.
12 //
13 // Version: 3.4
14 // Contact: support <at> deltavsoft.com
15 //
16 //******************************************************************************
17 
19 
20 #ifndef INCLUDE_RCF_RCFSESSION_HPP
21 #define INCLUDE_RCF_RCFSESSION_HPP
22 
23 #include <vector>
24 #include <functional>
25 #include <memory>
26 #include <typeinfo>
27 
28 #include <RCF/Any.hpp>
29 #include <RCF/Export.hpp>
30 #include <RCF/MethodInvocation.hpp>
31 #include <RCF/RcfFwd.hpp>
32 #include <RCF/SerializationProtocol.hpp>
33 #include <RCF/Tchar.hpp>
34 
35 #if RCF_FEATURE_FILETRANSFER==1
36 #include <RCF/FileSystem.hpp>
37 #endif
38 
39 #if RCF_FEATURE_SSPI==1
40 #include <wincred.h>
41 #endif
42 
43 namespace RCF {
44 
45 
46  struct TypeInfoCompare
47  {
48  bool operator()(
49  const std::type_info* lhs,
50  const std::type_info* rhs) const
51  {
52  if (lhs->before(*rhs))
53  {
54  return true;
55  }
56  return false;
57  }
58  };
59 
61  RCF_EXPORT RcfSession & getCurrentRcfSession();
62 
64  class RCF_EXPORT RcfSession :
65  public std::enable_shared_from_this<RcfSession>
66  {
67  public:
68 
69  RcfSession(RcfServer &server);
70  ~RcfSession();
71 
72  typedef std::function<void(RcfSession&)> OnWriteCompletedCallback;
73  typedef std::function<void(RcfSession&)> OnWriteInitiatedCallback;
74  typedef std::function<void(RcfSession&)> OnDestroyCallback;
75 
76  typedef std::map<const std::type_info *, Any, TypeInfoCompare> SessionObjectMap;
77  SessionObjectMap mSessionObjects;
78 
79  private:
80 
81  template<typename T>
82  T * getSessionObjectImpl(bool createIfDoesntExist)
83  {
84  typedef std::shared_ptr<T> TPtr;
85 
86  const std::type_info & whichType = typeid(T);
87  const std::type_info * pWhichType = &whichType;
88 
89  SessionObjectMap::iterator iter = mSessionObjects.find(pWhichType);
90  if (iter != mSessionObjects.end())
91  {
92  Any & a = iter->second;
93  TPtr & tPtr = a.get<TPtr>();
94  RCF_ASSERT(tPtr.get());
95  return tPtr.get();
96  }
97  else if (createIfDoesntExist)
98  {
99  TPtr tPtr( new T() );
100  mSessionObjects[pWhichType] = tPtr;
101  return tPtr.get();
102  }
103  else
104  {
105  return NULL;
106  }
107  }
108 
109  public:
110 
117 
119 
121  template<typename T>
123  {
124  typedef std::shared_ptr<T> TPtr;
125 
126  const std::type_info & whichType = typeid(T);
127  const std::type_info * pWhichType = &whichType;
128 
129  SessionObjectMap::iterator iter = mSessionObjects.find(pWhichType);
130  if (iter != mSessionObjects.end())
131  {
132  mSessionObjects.erase(iter);
133  }
134  }
135 
137  template<typename T>
139  {
140  deleteSessionObject<T>();
141  T * pt = getSessionObjectImpl<T>(true);
142  RCF_ASSERT(pt);
143  if ( !pt )
144  {
145  RCF_THROW(Exception(RcfError_SessionObjectNotCreated, typeid(T).name()));
146  }
147  return *pt;
148  }
149 
151  template<typename T>
152  T & getSessionObject(bool createIfDoesntExist = false)
153  {
154  T * pt = getSessionObjectImpl<T>(createIfDoesntExist);
155  if (!pt)
156  {
157  RCF_THROW(Exception(RcfError_SessionObjectDoesNotExist, typeid(T).name()));
158  }
159  return *pt;
160  }
161 
163  template<typename T>
165  {
166  T * pt = getSessionObjectImpl<T>(false);
167  return pt;
168  }
169 
171 
173  RcfServer & getRcfServer();
174 
176  void disconnect();
177 
178  RcfClientPtr getDefaultStubEntryPtr();
179  void setDefaultStubEntryPtr(RcfClientPtr stubEntryPtr);
180  void setCachedStubEntryPtr(RcfClientPtr stubEntryPtr);
181 
186 
188  void setRequestUserData(const std::string & userData);
189 
191  std::string getRequestUserData();
192 
194  void setResponseUserData(const std::string & userData);
195 
197  std::string getResponseUserData();
198 
200 
204 
206  std::uint32_t getRuntimeVersion();
207 
209  void setRuntimeVersion(std::uint32_t version);
210 
212  std::uint32_t getArchiveVersion();
213 
215  void setArchiveVersion(std::uint32_t version);
216 
218  const RemoteAddress &
219  getClientAddress();
220 
222  bool isOneway();
223 
225  std::uint32_t getPingBackIntervalMs();
226 
228  tstring getClientUserName();
229 
232  getTransportProtocol();
233 
235  TransportType getTransportType();
236 
237 #if RCF_FEATURE_SSPI==1
238 
241  PCtxtHandle getTransportSecurityContext() const;
242 
245  PCtxtHandle getTransportProtocolSecurityContext() const;
246 
247 #endif
248 
250  bool getEnableCompression();
251 
253  CertificatePtr getClientCertificatePtr();
254 
256  RemoteCallInfo getRemoteCallRequest() const;
257 
259  time_t getConnectedAtTime() const;
260 
262  std::size_t getConnectionDuration() const;
263 
265  std::size_t getRemoteCallCount() const;
266 
268  std::uint64_t getTotalBytesReceived() const;
269 
271  std::uint64_t getTotalBytesSent() const;
272 
274  void setEnableSfPointerTracking(bool enable);
275 
277  bool getEnableSfPointerTracking() const;
278 
279 
281 
282 #if RCF_FEATURE_FILETRANSFER==1
283 
284  // RCF3 file API
285 
289 
291  std::string configureDownload(const Path& downloadPath, BandwidthQuotaPtr quotaPtr = nullptr);
292 
294  void setAllowUploads(bool allowUploads);
295 
297  bool getAllowUploads() const;
298 
300  void setUploadBandwidthQuota(BandwidthQuotaPtr quotaPtr);
301 
303  BandwidthQuotaPtr getUploadBandwidthQuota() const;
304 
306  Path getUploadPath(const std::string& uploadId);
307 
309 
310 #endif
311 
312  //*******************************
313  // callback tables - synchronized
314 
315  // may well be called on a different thread than the one that executed the remote call
316  void addOnWriteCompletedCallback(
317  const OnWriteCompletedCallback & onWriteCompletedCallback);
318 
319  void extractOnWriteCompletedCallbacks(
320  std::vector<OnWriteCompletedCallback> & onWriteCompletedCallbacks);
321 
322  void setOnDestroyCallback(
323  OnDestroyCallback onDestroyCallback);
324 
325  //*******************************
326 
327  void setEnableNativeWstringSerialization(bool enable);
328  bool getEnableNativeWstringSerialization() const;
329 
330  void getMessageFilters(std::vector<FilterPtr> &filters) const;
331  void getTransportFilters(std::vector<FilterPtr> &filters) const;
332 
333  void lockTransportFilters();
334  void unlockTransportFilters();
335  bool transportFiltersLocked();
336 
337  SerializationProtocolIn & getSpIn();
338  SerializationProtocolOut & getSpOut();
339 
340  bool getFiltered();
341  void setFiltered(bool filtered);
342 
343  std::vector<FilterPtr> & getFilters();
344 
345  void setCloseSessionAfterWrite(bool close);
346 
347 
348 
349  std::uint32_t getPingTimestamp();
350  void setPingTimestamp();
351 
352  std::uint32_t getPingIntervalMs();
353  void setPingIntervalMs(std::uint32_t pingIntervalMs);
354 
355  std::uint32_t getTouchTimestamp();
356  bool getCallInProgress();
357 
358  void touch();
359  void setCallInProgress(bool callInProgress);
360 
361  void sendPingBack();
362  bool getAutoSend();
363 
364  void setWeakThisPtr();
365 
366  void cancelDownload();
367 
368 #if RCF_FEATURE_FILETRANSFER==1
369 
370  void addDownloadStream(
371  std::uint32_t sessionLocalId,
372  FileStream fileStream);
373 
374 #endif
375 
376  Mutex mStopCallInProgressMutex;
377  bool mStopCallInProgress;
378 
379  private:
380 
381  template<
382  typename R,
383  typename A1,
384  typename A2,
385  typename A3,
386  typename A4,
387  typename A5,
388  typename A6,
389  typename A7,
390  typename A8,
391  typename A9,
392  typename A10,
393  typename A11,
394  typename A12,
395  typename A13,
396  typename A14,
397  typename A15>
398  friend class AllocateServerParameters;
399 
400  template<
401  typename R,
402  typename A1,
403  typename A2,
404  typename A3,
405  typename A4,
406  typename A5,
407  typename A6,
408  typename A7,
409  typename A8,
410  typename A9,
411  typename A10,
412  typename A11,
413  typename A12,
414  typename A13,
415  typename A14,
416  typename A15>
417  friend class ServerParameters;
418 
419  friend class PingBackService;
420  friend class FilterService;
421 
422  friend class StubAccess;
423 
424  RcfServer & mRcfServer;
425 
426  Mutex mMutex;
427  std::vector<OnWriteCompletedCallback> mOnWriteCompletedCallbacks;
428  std::vector<OnWriteInitiatedCallback> mOnWriteInitiatedCallbacks;
429  OnDestroyCallback mOnDestroyCallback;
430 
431  std::uint32_t mRuntimeVersion;
432  std::uint32_t mArchiveVersion;
433 
434  bool mEnableSfPointerTracking;
435 
436  bool mTransportFiltersLocked;
437 
438  bool mEnableNativeWstringSerialization = false;
439 
440  SerializationProtocolIn mIn;
441  SerializationProtocolOut mOut;
442 
443  // message filters
444  std::vector<FilterPtr> mFilters;
445  bool mFiltered;
446 
447  MethodInvocationRequest mRequest;
448 
449  bool mCallInProgress = false;
450  bool mCloseSessionAfterWrite;
451  std::uint32_t mPingTimestamp;
452  std::uint32_t mPingIntervalMs;
453  std::uint32_t mTouchTimestamp;
454  ByteBuffer mPingBackByteBuffer;
455  PingBackTimerEntry mPingBackTimerEntry;
456 
457  Mutex mIoStateMutex;
458  bool mWritingPingBack;
459  std::vector<ByteBuffer> mQueuedSendBuffers;
460 
461  void clearParameters();
462 
463  void onReadCompleted();
464  void onWriteCompleted();
465 
466  void processRequest();
467 
468  void processOob_RequestTransportFilters(OobMessage& msg);
469  void processOob_CreateCallbackConnection(OobMessage& msg);
470  void processOob_RequestSubscription(OobMessage& msg);
471  void processOob_RequestProxyConnection(OobMessage& msg);
472  void processOobMessages();
473 
474  void callServant();
475 
476  void sendResponse();
477  void sendResponseException(const std::exception &e);
478  void sendResponseUncaughtException();
479 
480  void encodeRemoteException(
481  SerializationProtocolOut & out,
482  const RemoteException & e);
483 
484  void sendSessionResponse();
485 
486  void registerForPingBacks();
487  void unregisterForPingBacks();
488 
489  void verifyTransportProtocol(RCF::TransportProtocol protocol);
490 
491  friend class RcfServer;
492  friend class RemoteCallContextImpl;
493 
494  I_Parameters * mpParameters;
495  std::vector<char> mParametersVec;
496 
497  // For individual parameters.
498  std::vector< std::vector<char> > mParmsVec;
499 
500  bool mAutoSend;
501 
502  RcfSessionWeakPtr mWeakThisPtr;
503 
504  private:
505 
506  // UdpServerTransport needs to explicitly set mIoState to Reading,
507  // since it doesn't use async I/O with callbacks to RcfServer.
508  friend class UdpServerTransport;
509  friend class UdpNetworkSession;
510  friend class FileStreamImpl;
511 
512  friend class AsioNetworkSession;
513  void runOnDestroyCallbacks();
514 
515 #if RCF_FEATURE_FILETRANSFER==1
516 
517  private:
518 
519  friend class FileTransferService;
520 
521  FileDownloadInfoPtr mDownloadInfoPtr;
522  FileUploadInfoPtr mUploadInfoPtr;
523 
524  typedef std::map<std::uint32_t, FileUploadInfoPtr> SessionUploads;
525  typedef std::map<std::uint32_t, FileDownload> SessionDownloads;
526 
527  SessionUploads mSessionUploads;
528  SessionDownloads mSessionDownloads;
529 
530 #endif
531 
532  private:
533 
534  RcfClientPtr mDefaultStubEntryPtr;
535  RcfClientPtr mCachedStubEntryPtr;
536 
537  public:
538  NetworkSession & getNetworkSession() const;
539  void setNetworkSession(NetworkSession & networkSession);
540 
541 #if RCF_FEATURE_HTTP==1
542 
543  void getHttpFrameInfo(
544  std::string& requestLine,
545  std::vector< std::pair<std::string, std::string> >& headers);
546 
547 #endif
548 
549  private:
550  friend class HttpSessionFilter;
551  NetworkSession * mpNetworkSession;
552 
553  public:
554  std::string mCurrentCallDesc;
555 
556  public:
557 
558  bool getIsCallbackSession() const;
559  void setIsCallbackSession(bool isCallbackSession);
560 
561  bool isConnected() const;
562 
563  private:
564 
565  void setConnectedAtTime(time_t connectedAtTime);
566 
567  friend class SspiServerFilter;
568  friend class Win32NamedPipeNetworkSession;
569 
570  tstring mClientUsername;
571  TransportProtocol mTransportProtocol;
572  bool mEnableCompression;
573 
574  bool mTransportProtocolVerified;
575  bool mIsCallbackSession;
576 
577  time_t mConnectedAtTime;
578 
579  std::size_t mRemoteCallCount;
580  };
581 
582 } // namespace RCF
583 
584 #endif // ! INCLUDE_RCF_RCFSESSION_HPP
Describes the network address of a remote peer.
Definition: ServerTransport.hpp:36
Generic container type used to hold arbitrary objects.
Definition: RCF/Any.hpp:63
RCF_FILESYSTEM_NS::path Path
Typedef for standard C++ path type.
Definition: FileSystem.hpp:31
RCF_EXPORT std::uint32_t getArchiveVersion()
Gets the RCF archive version number.
Contains details about the currently executing remote call.
Definition: MethodInvocation.hpp:62
Represents a server side session, associated with a client connection.
Definition: RcfSession.hpp:64
T & get()
Type-safe retrieval of the contained value. Throws an exception if T does not match the type of the c...
Definition: RCF/Any.hpp:103
std::shared_ptr< BandwidthQuota > BandwidthQuotaPtr
Reference counted wrapper for RCF::BandwidthQuota.
Definition: RcfFwd.hpp:127
Base class of RemoteCallContext.
Definition: RemoteCallContext.hpp:38
std::shared_ptr< Certificate > CertificatePtr
Reference counted wrapper for RCF::Certificate.
Definition: RcfFwd.hpp:108
RCF_EXPORT RcfSession & getCurrentRcfSession()
Can only be called from within the server-side implementation of a remote call. Returns a reference t...
Base class for all RCF exceptions.
Definition: Exception.hpp:67
RCF_EXPORT void setArchiveVersion(std::uint32_t version)
Sets the RCF archive version number. Applies to all RCF clients and servers within the current proces...
RCF_EXPORT std::uint32_t getRuntimeVersion()
Gets the RCF runtime version number.
T & createSessionObject()
Creates a session object.
Definition: RcfSession.hpp:138
void deleteSessionObject()
Deletes a session object.
Definition: RcfSession.hpp:122
Provides RCF server-side functionality.
Definition: RcfServer.hpp:53
Represents an error that occurs on a RCF server and is transmitted back to the client.
Definition: Exception.hpp:168
Definition: ByteBuffer.hpp:39
TransportProtocol
Describes the transport protocols used by a RCF connection. Transport protocols are layered on top of...
Definition: Enums.hpp:62
Definition: AmiIoHandler.hpp:23
TransportType
Describes the transport types used by a RCF connection.
Definition: Enums.hpp:33
T * querySessionObject()
Queries for the existence of a session object.
Definition: RcfSession.hpp:164
RCF_EXPORT void setRuntimeVersion(std::uint32_t version)
Sets the RCF runtime version number. Applies to all RCF clients and servers within the current proces...
T & getSessionObject(bool createIfDoesntExist=false)
Retrieves a session object, and optionally creates it.
Definition: RcfSession.hpp:152