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