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