Remote Call Framework 3.0
RcfSession.hpp
Go to the documentation of this file.
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2018, 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.0
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/FileSystem.hpp>
32 #include <RCF/MethodInvocation.hpp>
33 #include <RCF/RcfFwd.hpp>
34 #include <RCF/SerializationProtocol.hpp>
35 #include <RCF/Tchar.hpp>
36 
37 #if RCF_FEATURE_FILETRANSFER==1
38 //#include <RCF/FileDownload.hpp>
39 //#include <RCF/FileUpload.hpp>
40 #endif
41 
42 #if RCF_FEATURE_SSPI==1
43 #include <wincred.h>
44 #endif
45 
46 namespace RCF {
47 
48 
49  struct TypeInfoCompare
50  {
51  bool operator()(
52  const std::type_info* lhs,
53  const std::type_info* rhs) const
54  {
55  if (lhs->before(*rhs))
56  {
57  return true;
58  }
59  return false;
60  }
61  };
62 
64  RCF_EXPORT RcfSession & getCurrentRcfSession();
65 
67  class RCF_EXPORT RcfSession :
68  public std::enable_shared_from_this<RcfSession>
69  {
70  public:
71 
72  RcfSession(RcfServer &server);
73  ~RcfSession();
74 
75  typedef std::function<void(RcfSession&)> OnWriteCompletedCallback;
76  typedef std::function<void(RcfSession&)> OnWriteInitiatedCallback;
77  typedef std::function<void(RcfSession&)> OnDestroyCallback;
78 
79  typedef std::map<const std::type_info *, Any, TypeInfoCompare> SessionObjectMap;
80  SessionObjectMap mSessionObjects;
81 
82  private:
83 
84  template<typename T>
85  T * getSessionObjectImpl(bool createIfDoesntExist)
86  {
87  typedef std::shared_ptr<T> TPtr;
88 
89  const std::type_info & whichType = typeid(T);
90  const std::type_info * pWhichType = &whichType;
91 
92  SessionObjectMap::iterator iter = mSessionObjects.find(pWhichType);
93  if (iter != mSessionObjects.end())
94  {
95  Any & a = iter->second;
96  TPtr & tPtr = a.get<TPtr>();
97  RCF_ASSERT(tPtr.get());
98  return tPtr.get();
99  }
100  else if (createIfDoesntExist)
101  {
102  TPtr tPtr( new T() );
103  mSessionObjects[pWhichType] = tPtr;
104  return tPtr.get();
105  }
106  else
107  {
108  return NULL;
109  }
110  }
111 
112  public:
113 
120 
122 
124  template<typename T>
126  {
127  typedef std::shared_ptr<T> TPtr;
128 
129  const std::type_info & whichType = typeid(T);
130  const std::type_info * pWhichType = &whichType;
131 
132  SessionObjectMap::iterator iter = mSessionObjects.find(pWhichType);
133  if (iter != mSessionObjects.end())
134  {
135  mSessionObjects.erase(iter);
136  }
137  }
138 
140  template<typename T>
142  {
143  deleteSessionObject<T>();
144  T * pt = getSessionObjectImpl<T>(true);
145  RCF_ASSERT(pt);
146  if ( !pt )
147  {
148  RCF_THROW(Exception(RcfError_SessionObjectNotCreated, typeid(T).name()));
149  }
150  return *pt;
151  }
152 
154  template<typename T>
155  T & getSessionObject(bool createIfDoesntExist = false)
156  {
157  T * pt = getSessionObjectImpl<T>(createIfDoesntExist);
158  if (!pt)
159  {
160  RCF_THROW(Exception(RcfError_SessionObjectDoesNotExist, typeid(T).name()));
161  }
162  return *pt;
163  }
164 
166  template<typename T>
168  {
169  T * pt = getSessionObjectImpl<T>(false);
170  return pt;
171  }
172 
174 
176  RcfServer & getRcfServer();
177 
179  void disconnect();
180 
181  RcfClientPtr getDefaultStubEntryPtr();
182  void setDefaultStubEntryPtr(RcfClientPtr stubEntryPtr);
183  void setCachedStubEntryPtr(RcfClientPtr stubEntryPtr);
184 
189 
191  void setRequestUserData(const std::string & userData);
192 
194  std::string getRequestUserData();
195 
197  void setResponseUserData(const std::string & userData);
198 
200  std::string getResponseUserData();
201 
203 
207 
209  std::uint32_t getRuntimeVersion();
210 
212  void setRuntimeVersion(std::uint32_t version);
213 
215  std::uint32_t getArchiveVersion();
216 
218  void setArchiveVersion(std::uint32_t version);
219 
221  const RemoteAddress &
222  getClientAddress();
223 
225  bool isOneway();
226 
228  std::uint32_t getPingBackIntervalMs();
229 
231  tstring getClientUserName();
232 
235  getTransportProtocol();
236 
238  TransportType getTransportType();
239 
240 #if RCF_FEATURE_SSPI==1
241 
244  PCtxtHandle getTransportSecurityContext() const;
245 
248  PCtxtHandle getTransportProtocolSecurityContext() const;
249 
250 #endif
251 
253  bool getEnableCompression();
254 
256  CertificatePtr getClientCertificatePtr();
257 
259  RemoteCallInfo getRemoteCallRequest() const;
260 
262  time_t getConnectedAtTime() const;
263 
265  std::size_t getConnectionDuration() const;
266 
268  std::size_t getRemoteCallCount() const;
269 
271  std::uint64_t getTotalBytesReceived() const;
272 
274  std::uint64_t getTotalBytesSent() const;
275 
277  void setEnableSfPointerTracking(bool enable);
278 
280  bool getEnableSfPointerTracking() const;
281 
282 
284 
285 #if RCF_FEATURE_FILETRANSFER==1
286 
287  // RCF3 file API
288 
292 
294  std::string configureDownload(const Path& downloadPath, BandwidthQuotaPtr quotaPtr = nullptr);
295 
297  void setAllowUploads(bool allowUploads);
298 
300  bool getAllowUploads() const;
301 
303  void setUploadBandwidthQuota(BandwidthQuotaPtr quotaPtr);
304 
306  BandwidthQuotaPtr getUploadBandwidthQuota() const;
307 
309  Path getUploadPath(const std::string& uploadId);
310 
312 
313 #endif
314 
315 
316  //*******************************
317  // callback tables - synchronized
318 
319  // may well be called on a different thread than the one that executed the remote call
320  void addOnWriteCompletedCallback(
321  const OnWriteCompletedCallback & onWriteCompletedCallback);
322 
323  void extractOnWriteCompletedCallbacks(
324  std::vector<OnWriteCompletedCallback> & onWriteCompletedCallbacks);
325 
326  void setOnDestroyCallback(
327  OnDestroyCallback onDestroyCallback);
328 
329  //*******************************
330 
331  void setEnableNativeWstringSerialization(bool enable);
332  bool getEnableNativeWstringSerialization() const;
333 
334  void getMessageFilters(std::vector<FilterPtr> &filters) const;
335  void getTransportFilters(std::vector<FilterPtr> &filters) const;
336 
337  void lockTransportFilters();
338  void unlockTransportFilters();
339  bool transportFiltersLocked();
340 
341  SerializationProtocolIn & getSpIn();
342  SerializationProtocolOut & getSpOut();
343 
344  bool getFiltered();
345  void setFiltered(bool filtered);
346 
347  std::vector<FilterPtr> & getFilters();
348 
349  void setCloseSessionAfterWrite(bool close);
350 
351 
352 
353  std::uint32_t getPingTimestamp();
354  void setPingTimestamp();
355 
356  std::uint32_t getPingIntervalMs();
357  void setPingIntervalMs(std::uint32_t pingIntervalMs);
358 
359  std::uint32_t getTouchTimestamp();
360 
361  void touch();
362 
363  void sendPingBack();
364  bool getAutoSend();
365 
366  void setWeakThisPtr();
367 
368  void cancelDownload();
369 
370 #if RCF_FEATURE_FILETRANSFER==1
371 
372  void addDownloadStream(
373  std::uint32_t sessionLocalId,
374  FileStream fileStream);
375 
376 #endif
377 
378  Mutex mStopCallInProgressMutex;
379  bool mStopCallInProgress;
380 
381  private:
382 
383  template<
384  typename R,
385  typename A1,
386  typename A2,
387  typename A3,
388  typename A4,
389  typename A5,
390  typename A6,
391  typename A7,
392  typename A8,
393  typename A9,
394  typename A10,
395  typename A11,
396  typename A12,
397  typename A13,
398  typename A14,
399  typename A15>
400  friend class AllocateServerParameters;
401 
402  template<
403  typename R,
404  typename A1,
405  typename A2,
406  typename A3,
407  typename A4,
408  typename A5,
409  typename A6,
410  typename A7,
411  typename A8,
412  typename A9,
413  typename A10,
414  typename A11,
415  typename A12,
416  typename A13,
417  typename A14,
418  typename A15>
419  friend class ServerParameters;
420 
421  friend class PingBackService;
422  friend class FilterService;
423 
424  friend class StubAccess;
425 
426  RcfServer & mRcfServer;
427 
428  Mutex mMutex;
429  std::vector<OnWriteCompletedCallback> mOnWriteCompletedCallbacks;
430  std::vector<OnWriteInitiatedCallback> mOnWriteInitiatedCallbacks;
431  OnDestroyCallback mOnDestroyCallback;
432 
433  std::uint32_t mRuntimeVersion;
434  std::uint32_t mArchiveVersion;
435 
436  bool mEnableSfPointerTracking;
437 
438  bool mTransportFiltersLocked;
439 
440  bool mEnableNativeWstringSerialization = false;
441 
442  SerializationProtocolIn mIn;
443  SerializationProtocolOut mOut;
444 
445  // message filters
446  std::vector<FilterPtr> mFilters;
447  bool mFiltered;
448 
449  MethodInvocationRequest mRequest;
450 
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  void processOobMessages();
469  void callServant();
470 
471  void sendResponse();
472  void sendResponseException(const std::exception &e);
473  void sendResponseUncaughtException();
474 
475  void encodeRemoteException(
476  SerializationProtocolOut & out,
477  const RemoteException & e);
478 
479  void sendSessionResponse();
480 
481  void registerForPingBacks();
482  void unregisterForPingBacks();
483 
484  void verifyTransportProtocol(RCF::TransportProtocol protocol);
485 
486  friend class RcfServer;
487  friend class RemoteCallContextImpl;
488 
489  I_Parameters * mpParameters;
490  std::vector<char> mParametersVec;
491 
492  // For individual parameters.
493  std::vector< std::vector<char> > mParmsVec;
494 
495  bool mAutoSend;
496 
497  RcfSessionWeakPtr mWeakThisPtr;
498 
499  private:
500 
501  // UdpServerTransport needs to explicitly set mIoState to Reading,
502  // since it doesn't use async I/O with callbacks to RcfServer.
503  friend class UdpServerTransport;
504  friend class UdpNetworkSession;
505  friend class FileStreamImpl;
506 
507 #if RCF_FEATURE_FILETRANSFER==1
508 
509  private:
510 
511  friend class FileTransferService;
512 
513  FileDownloadInfoPtr mDownloadInfoPtr;
514  FileUploadInfoPtr mUploadInfoPtr;
515 
516  typedef std::map<std::uint32_t, FileUploadInfoPtr> SessionUploads;
517  typedef std::map<std::uint32_t, FileDownload> SessionDownloads;
518 
519  SessionUploads mSessionUploads;
520  SessionDownloads mSessionDownloads;
521 
522 #endif
523 
524  private:
525 
526  RcfClientPtr mDefaultStubEntryPtr;
527  RcfClientPtr mCachedStubEntryPtr;
528 
529  public:
530  NetworkSession & getNetworkSession() const;
531  void setNetworkSession(NetworkSession & networkSession);
532 
533 #if RCF_FEATURE_HTTP==1
534 
535  void getHttpFrameInfo(
536  std::string& requestLine,
537  std::vector< std::pair<std::string, std::string> >& headers);
538 
539 #endif
540 
541  private:
542  friend class HttpSessionFilter;
543  NetworkSession * mpNetworkSession;
544 
545  public:
546  std::string mCurrentCallDesc;
547 
548  public:
549 
550  bool getIsCallbackSession() const;
551  void setIsCallbackSession(bool isCallbackSession);
552 
553  bool isConnected() const;
554 
555  private:
556 
557  void setConnectedAtTime(time_t connectedAtTime);
558 
559  friend class SspiServerFilter;
560  friend class Win32NamedPipeNetworkSession;
561 
562  tstring mClientUsername;
563  TransportProtocol mTransportProtocol;
564  bool mEnableCompression;
565 
566  bool mTransportProtocolVerified;
567  bool mIsCallbackSession;
568 
569  time_t mConnectedAtTime;
570 
571  std::size_t mRemoteCallCount;
572  };
573 
574 } // namespace RCF
575 
576 #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:35
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:67
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:127
Base class of RemoteCallContext.
Definition: RemoteCallContext.hpp:39
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: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:141
void deleteSessionObject()
Deletes a session object.
Definition: RcfSession.hpp:125
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:167
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:155