RCFProto
 All Classes Functions Typedefs
RcfSession.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_RCFSESSION_HPP
20 #define INCLUDE_RCF_RCFSESSION_HPP
21 
22 #include <vector>
23 
24 #include <boost/any.hpp>
25 #include <boost/enable_shared_from_this.hpp>
26 #include <boost/function.hpp>
27 #include <boost/shared_ptr.hpp>
28 
29 #include <RCF/Filter.hpp>
30 #include <RCF/Export.hpp>
31 #include <RCF/MethodInvocation.hpp>
32 #include <RCF/SerializationProtocol.hpp>
33 #include <RCF/ServerTransport.hpp>
34 #include <RCF/StubEntry.hpp>
35 
36 #if RCF_FEATURE_FILETRANSFER==1
37 #include <RCF/FileDownload.hpp>
38 #include <RCF/FileUpload.hpp>
39 #endif
40 
41 #include <typeinfo>
42 
43 namespace RCF {
44 
45  class Filter;
46 
47  typedef boost::shared_ptr<Filter> FilterPtr;
48 
49  class RcfSession;
50 
51  typedef boost::shared_ptr<RcfSession> RcfSessionPtr;
52  typedef boost::weak_ptr<RcfSession> RcfSessionWeakPtr;
53 
54  class I_Future;
55 
56  class I_Parameters;
57 
58  class UdpServerTransport;
59  class UdpNetworkSession;
60 
61  class FileTransferService;
62  class FileUploadInfo;
63  class FileDownloadInfo;
64 
65  class FileStreamImpl;
66 
67  typedef boost::shared_ptr<FileUploadInfo> FileUploadInfoPtr;
68  typedef boost::shared_ptr<FileDownloadInfo> FileDownloadInfoPtr;
69 
70  typedef std::pair<boost::uint32_t, RcfSessionWeakPtr> PingBackTimerEntry;
71 
72  class Certificate;
73  typedef boost::shared_ptr<Certificate> CertificatePtr;
74 
75  class AsioNetworkSession;
76 
77  template<
78  typename R,
79  typename A1,
80  typename A2,
81  typename A3,
82  typename A4,
83  typename A5,
84  typename A6,
85  typename A7,
86  typename A8,
87  typename A9,
88  typename A10,
89  typename A11,
90  typename A12,
91  typename A13,
92  typename A14,
93  typename A15>
94  class AllocateServerParameters;
95 
96  template<
97  typename R,
98  typename A1,
99  typename A2,
100  typename A3,
101  typename A4,
102  typename A5,
103  typename A6,
104  typename A7,
105  typename A8,
106  typename A9,
107  typename A10,
108  typename A11,
109  typename A12,
110  typename A13,
111  typename A14,
112  typename A15>
113  class ServerParameters;
114 
115  class PingBackService;
116 
117  struct TypeInfoCompare
118  {
119  bool operator()(
120  const std::type_info* lhs,
121  const std::type_info* rhs) const
122  {
123  if (lhs->before(*rhs))
124  {
125  return true;
126  }
127  return false;
128  }
129  };
130 
131  class RCF_EXPORT RcfSession :
132  public boost::enable_shared_from_this<RcfSession>
133  {
134  public:
135  RcfSession(RcfServer &server);
136  ~RcfSession();
137 
138  typedef boost::function1<void, RcfSession&> OnWriteCompletedCallback;
139  typedef boost::function1<void, RcfSession&> OnWriteInitiatedCallback;
140  typedef boost::function1<void, RcfSession&> OnDestroyCallback;
141 
142  typedef std::map<const std::type_info *, boost::any, TypeInfoCompare> SessionObjectMap;
143  SessionObjectMap mSessionObjects;
144 
145  private:
146 
147  template<typename T>
148  T * getSessionObjectImpl(bool createIfDoesntExist)
149  {
150  typedef boost::shared_ptr<T> TPtr;
151 
152  const std::type_info & whichType = typeid(T);
153  const std::type_info * pWhichType = &whichType;
154 
155  SessionObjectMap::iterator iter = mSessionObjects.find(pWhichType);
156  if (iter != mSessionObjects.end())
157  {
158  boost::any & a = iter->second;
159  TPtr * ptPtr = boost::any_cast<TPtr>(&a);
160  RCF_ASSERT(ptPtr && *ptPtr);
161  return ptPtr->get();
162  }
163  else if (createIfDoesntExist)
164  {
165  TPtr tPtr( new T() );
166  mSessionObjects[pWhichType] = tPtr;
167  return tPtr.get();
168  }
169  else
170  {
171  return NULL;
172  }
173  }
174 
175  public:
176 
177  template<typename T>
178  void deleteSessionObject()
179  {
180  typedef boost::shared_ptr<T> TPtr;
181 
182  const std::type_info & whichType = typeid(T);
183  const std::type_info * pWhichType = &whichType;
184 
185  SessionObjectMap::iterator iter = mSessionObjects.find(pWhichType);
186  if (iter != mSessionObjects.end())
187  {
188  mSessionObjects.erase(iter);
189  }
190  }
191 
192  template<typename T>
193  T & createSessionObject()
194  {
195  deleteSessionObject<T>();
196  T * pt = getSessionObjectImpl<T>(true);
197  RCF_ASSERT(pt);
198  if ( !pt )
199  {
200  RCF_THROW(Exception(_RcfError_SessionObjectNotCreated(typeid(T).name())));
201  }
202  return *pt;
203  }
204 
205  template<typename T>
206  T & getSessionObject(bool createIfDoesntExist = false)
207  {
208  T * pt = getSessionObjectImpl<T>(createIfDoesntExist);
209  if (!pt)
210  {
211  RCF_THROW( Exception(_RcfError_SessionObjectDoesNotExist(typeid(T).name())));
212  }
213  return *pt;
214  }
215 
216  template<typename T>
217  T * querySessionObject()
218  {
219  T * pt = getSessionObjectImpl<T>(false);
220  return pt;
221  }
222 
223 
224 
225  //*******************************
226  // callback tables - synchronized
227 
228  // may well be called on a different thread than the one that executed the remote call
229  void addOnWriteCompletedCallback(
230  const OnWriteCompletedCallback & onWriteCompletedCallback);
231 
232  void extractOnWriteCompletedCallbacks(
233  std::vector<OnWriteCompletedCallback> & onWriteCompletedCallbacks);
234 
235  void setOnDestroyCallback(
236  OnDestroyCallback onDestroyCallback);
237 
238  //*******************************
239 
240  const RemoteAddress &
241  getClientAddress();
242 
243  RcfServer & getRcfServer();
244 
245  void disconnect();
246 
247  bool hasDefaultServerStub();
248  StubEntryPtr getDefaultStubEntryPtr();
249  void setDefaultStubEntryPtr(StubEntryPtr stubEntryPtr);
250  void setCachedStubEntryPtr(StubEntryPtr stubEntryPtr);
251 
252  void setEnableSfPointerTracking(bool enable);
253  bool getEnableSfPointerTracking() const;
254 
255  boost::uint32_t getRuntimeVersion();
256  void setRuntimeVersion(boost::uint32_t version);
257 
258  boost::uint32_t getArchiveVersion();
259  void setArchiveVersion(boost::uint32_t version);
260 
261  bool getNativeWstringSerialization();
262  void setNativeWstringSerialization(bool enable);
263 
264  void setUserData(const boost::any & userData);
265  boost::any & getUserData();
266 
267  void getMessageFilters(std::vector<FilterPtr> &filters);
268  void getTransportFilters(std::vector<FilterPtr> &filters);
269 
270  void lockTransportFilters();
271  void unlockTransportFilters();
272  bool transportFiltersLocked();
273 
274  SerializationProtocolIn & getSpIn();
275  SerializationProtocolOut & getSpOut();
276 
277  bool getFiltered();
278  void setFiltered(bool filtered);
279 
280  std::vector<FilterPtr> & getFilters();
281 
282  void setCloseSessionAfterWrite(bool close);
283 
284  boost::uint32_t getPingBackIntervalMs();
285 
286  boost::uint32_t getPingTimestamp();
287  void setPingTimestamp();
288 
289  boost::uint32_t getPingIntervalMs();
290  void setPingIntervalMs(boost::uint32_t pingIntervalMs);
291 
292  boost::uint32_t getTouchTimestamp();
293 
294  void touch();
295 
296  void sendPingBack();
297  bool getAutoSend();
298 
299  void setWeakThisPtr();
300 
301  void setRequestUserData(const std::string & userData);
302  std::string getRequestUserData();
303 
304  void setResponseUserData(const std::string & userData);
305  std::string getResponseUserData();
306 
307  bool isOneway();
308 
309  void cancelDownload();
310 
311 #if RCF_FEATURE_FILETRANSFER==1
312 
313  void addDownloadStream(
314  boost::uint32_t sessionLocalId,
315  FileStream fileStream);
316 
317 #endif
318 
319  Mutex mStopCallInProgressMutex;
320  bool mStopCallInProgress;
321 
322  private:
323 
324  template<
325  typename R,
326  typename A1,
327  typename A2,
328  typename A3,
329  typename A4,
330  typename A5,
331  typename A6,
332  typename A7,
333  typename A8,
334  typename A9,
335  typename A10,
336  typename A11,
337  typename A12,
338  typename A13,
339  typename A14,
340  typename A15>
341  friend class AllocateServerParameters;
342 
343  template<
344  typename R,
345  typename A1,
346  typename A2,
347  typename A3,
348  typename A4,
349  typename A5,
350  typename A6,
351  typename A7,
352  typename A8,
353  typename A9,
354  typename A10,
355  typename A11,
356  typename A12,
357  typename A13,
358  typename A14,
359  typename A15>
360  friend class ServerParameters;
361 
362  friend class PingBackService;
363  friend class FilterService;
364 
365  friend class StubAccess;
366 
367  RcfServer & mRcfServer;
368 
369  Mutex mMutex;
370  std::vector<OnWriteCompletedCallback> mOnWriteCompletedCallbacks;
371  std::vector<OnWriteInitiatedCallback> mOnWriteInitiatedCallbacks;
372  OnDestroyCallback mOnDestroyCallback;
373 
374  boost::uint32_t mRuntimeVersion;
375  boost::uint32_t mArchiveVersion;
376 
377  bool mUseNativeWstringSerialization;
378  bool mEnableSfPointerTracking;
379 
380  bool mTransportFiltersLocked;
381 
382  SerializationProtocolIn mIn;
383  SerializationProtocolOut mOut;
384 
385  // message filters
386  std::vector<FilterPtr> mFilters;
387  bool mFiltered;
388 
389  MethodInvocationRequest mRequest;
390 
391  bool mCloseSessionAfterWrite;
392  boost::uint32_t mPingTimestamp;
393  boost::uint32_t mPingIntervalMs;
394  boost::uint32_t mTouchTimestamp;
395  ByteBuffer mPingBackByteBuffer;
396  PingBackTimerEntry mPingBackTimerEntry;
397 
398  Mutex mIoStateMutex;
399  bool mWritingPingBack;
400  std::vector<ByteBuffer> mQueuedSendBuffers;
401 
402  void clearParameters();
403 
404  void onReadCompleted();
405  void onWriteCompleted();
406 
407  void processJsonRpcRequest();
408 
409  void processRequest();
410  void processOobMessages();
411  void invokeServant();
412 
413  void sendResponse();
414  void sendResponseException(const std::exception &e);
415  void sendResponseUncaughtException();
416 
417  void encodeRemoteException(
418  SerializationProtocolOut & out,
419  const RemoteException & e);
420 
421  void sendSessionResponse();
422 
423  void registerForPingBacks();
424  void unregisterForPingBacks();
425 
426  void verifyTransportProtocol(RCF::TransportProtocol protocol);
427 
428  friend class RcfServer;
429  friend class RemoteCallContextImpl;
430 
431  I_Parameters * mpParameters;
432  std::vector<char> mParametersVec;
433 
434  // For individual parameters.
435  std::vector< std::vector<char> > mParmsVec;
436 
437  bool mAutoSend;
438 
439  RcfSessionWeakPtr mWeakThisPtr;
440 
441  private:
442 
443  // UdpServerTransport needs to explicitly set mIoState to Reading,
444  // since it doesn't use async I/O with callbacks to RcfServer.
445  friend class UdpServerTransport;
446  friend class UdpNetworkSession;
447  friend class FileStreamImpl;
448 
449 #if RCF_FEATURE_FILETRANSFER==1
450 
451  private:
452 
453  friend class FileTransferService;
454 
455  FileDownloadInfoPtr mDownloadInfoPtr;
456  FileUploadInfoPtr mUploadInfoPtr;
457 
458  typedef std::map<boost::uint32_t, FileUploadInfoPtr> SessionUploads;
459  typedef std::map<boost::uint32_t, FileDownload> SessionDownloads;
460 
461  SessionUploads mSessionUploads;
462  SessionDownloads mSessionDownloads;
463 
464 #endif
465 
466  private:
467 
468  boost::any mUserData;
469  StubEntryPtr mDefaultStubEntryPtr;
470  StubEntryPtr mCachedStubEntryPtr;
471 
472  public:
473  NetworkSession & getNetworkSession() const;
474  void setNetworkSession(NetworkSession & networkSession);
475 
476  private:
477  friend class HttpSessionFilter;
478  NetworkSession * mpNetworkSession;
479 
480  public:
481  std::string mCurrentCallDesc;
482 
483  public:
484 
485  tstring getClientUsername();
486  TransportProtocol getTransportProtocol();
487  TransportType getTransportType();
488 
489  bool getEnableCompression();
490 
491  CertificatePtr getClientCertificatePtr();
492 
493  bool getIsCallbackSession() const;
494  void setIsCallbackSession(bool isCallbackSession);
495 
496  RemoteCallRequest getRemoteCallRequest() const;
497 
498  time_t getConnectedAtTime() const;
499 
500  std::size_t getConnectionDuration() const;
501 
502  std::size_t getRemoteCallCount() const;
503  boost::uint64_t getTotalBytesReceived() const;
504  boost::uint64_t getTotalBytesSent() const;
505 
506  bool isConnected() const;
507 
508  private:
509 
510  void setConnectedAtTime(time_t connectedAtTime);
511 
512  friend class SspiServerFilter;
513  friend class Win32NamedPipeNetworkSession;
514 
515  tstring mClientUsername;
516  TransportProtocol mTransportProtocol;
517  bool mEnableCompression;
518 
519  bool mTransportProtocolVerified;
520  bool mIsCallbackSession;
521 
522  time_t mConnectedAtTime;
523 
524  std::size_t mRemoteCallCount;
525  };
526 
527 } // namespace RCF
528 
529 #endif // ! INCLUDE_RCF_RCFSESSION_HPP