Remote Call Framework 3.4
RcfServer.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_RCFSERVER_HPP
21 #define INCLUDE_RCF_RCFSERVER_HPP
22 
23 #include <functional>
24 #include <map>
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 #include <RCF/Export.hpp>
30 #include <RCF/RcfClient.hpp>
31 #include <RCF/ServerTransport.hpp>
32 #include <RCF/ThreadLibrary.hpp>
33 
34 #if RCF_FEATURE_FILETRANSFER==1
35 #include <RCF/FileTransferService.hpp>
36 #endif
37 
38 #if RCF_FEATURE_PUBSUB==1
39 #include <RCF/PublishingService.hpp>
40 #include <RCF/SubscriptionService.hpp>
41 #endif
42 
43 #include <RCF/ServerObjectService.hpp>
44 #include <RCF/ServerStub.hpp>
45 
46 namespace RCF {
47 
49 
53  class RCF_EXPORT RcfServer : Noncopyable
54  {
55  public:
56 
57  typedef std::function<void(RcfServer&)> StartCallback;
58 
59  public:
60 
62 
64 
66  RcfServer();
67 
69  RcfServer(const Endpoint &endpoint);
70 
72  RcfServer(ServerTransportPtr serverTransportPtr);
73 
75  ~RcfServer();
76 
78 
81 
83 
85  ServerTransport & addEndpoint(const Endpoint & endpoint);
86 
88  void start();
89 
91  void stop();
92 
94  bool isStarted();
95 
97  void enumerateProxyEndpoints(std::vector<std::string>& endpoints);
98 
100 
104  void setSupportedTransportProtocols(
105  const std::vector<TransportProtocol> & protocols);
106 
108  const std::vector<TransportProtocol> &
109  getSupportedTransportProtocols() const;
110 
114  void setThreadPool(ThreadPoolPtr threadPoolPtr);
115 
117  ThreadPoolPtr getThreadPool();
118 
119 
121 
122  // Waits for the RcfServer to stop. Can be called from any thread.
123  void waitForStopEvent();
124 
125  // Waits for the RcfServer to start. Can be called from any thread.
126  void waitForStartEvent();
127 
129  ServerTransport & getServerTransport();
130 
132  IpServerTransport & getIpServerTransport();
133 
134 
140 
142 
144 
147  template<typename InterfaceT, typename ImplementationT>
148  ServerBindingPtr bind(ImplementationT & servantObj, const std::string &name = "")
149  {
150  std::reference_wrapper<ImplementationT> refWrapper(servantObj);
151 
152  RcfClientPtr rcfClientPtr = createServerStub(
153  (InterfaceT *) 0,
154  (ImplementationT *) 0, refWrapper);
155 
156  return bindImpl(
157  name.empty() ?
158  InterfaceT::getInterfaceName() :
159  name ,
160  rcfClientPtr);
161  }
162 
164  template<typename InterfaceT>
165  bool unbind(const std::string &name = "")
166  {
167  const std::string &name_ = (name == "") ?
168  getInterfaceName((InterfaceT *) NULL) :
169  name;
170 
171  WriteLock writeLock(mStubMapMutex);
172  mStubMap.erase(name_);
173  return true;
174  }
175 
177 
180 
182 
184  void setCertificate(CertificatePtr certificatePtr);
185 
187  CertificatePtr getCertificate();
188 
190  void setOpenSslCipherSuite(const std::string & cipherSuite);
191 
193  std::string getOpenSslCipherSuite() const;
194 
196  void setCaCertificate(CertificatePtr certificatePtr);
197 
199  CertificatePtr getCaCertificate();
200 
202  void setCertificateValidationCallback(CertificateValidationCallback certificateValidationCb);
203 
206  getCertificateValidationCallback() const;
207 
209  void setEnableSchannelCertificateValidation(const tstring & peerName);
210 
212  tstring getEnableSchannelCertificateValidation() const;
213 
215  void setSslImplementation(SslImplementation sslImplementation);
216 
218  SslImplementation getSslImplementation() const;
219 
220 #if RCF_FEATURE_SSPI==1
221  void setSchannelEnabledProtocols(DWORD enabledProtocols);
222  DWORD getSchannelEnabledProtocols() const;
223 
224  void setSchannelContextRequirements(ULONG contextRequirements);
225  ULONG getSchannelContextRequirements() const;
226 #endif
227 
229 
232 
234 
236 
238  void setConnectionIdleTimeoutMs(std::uint32_t idleConnectionTimeoutMs);
239 
241  std::uint32_t getConnectionIdleTimeoutMs();
242 
244 
246  void setConnectionIdleScanIntervalMs(std::uint32_t idleConnectionScanIntervalMs);
247 
249  std::uint32_t getConnectionIdleScanIntervalMs();
250 
252 
255  void setHttpSessionTimeoutMs(std::uint32_t httpSessionTimeoutMs);
256 
258  std::uint32_t getHttpSessionTimeoutMs();
259 
261  void setHttpServerHeader(const std::string & httpServerHeader);
262 
264  std::string getHttpServerHeader() const;
265 
266 #if RCF_FEATURE_HTTP==1
267  void setHttpMessageVerifier(HttpMessageVerifierPtr verifierPtr);
268  HttpMessageVerifierPtr getHttpMessageVerifier() const;
269 #endif
270 
271  void setHealthCheckResponse(int statusCode, std::string statusMessage, std::string content);
272  void getHealthCheckResponse(int& statusCode, std::string& statusMessage, std::string& content);
273 
275 
282 
284 
286  std::uint32_t getServerObjectHarvestingIntervalS() const;
287 
289  void setServerObjectHarvestingIntervalS(std::uint32_t harvestingIntervalS);
290 
292  template<typename T>
293  std::shared_ptr<T> queryServerObject(
294  const std::string & objectKey)
295  {
296  return mServerObjectServicePtr->queryServerObject<T>(objectKey);
297  }
298 
300  template<typename T>
301  std::shared_ptr<T> getServerObject(
302  const std::string & objectKey,
303  std::uint32_t timeoutMs)
304  {
305  return mServerObjectServicePtr->getServerObject<T>(objectKey, timeoutMs);
306  }
307 
309  void deleteServerObject(const std::string & objectKey);
310 
312 
313 #if RCF_FEATURE_PUBSUB==1
314 
317 
319 
321 
324  template<typename Interface>
325  std::shared_ptr< Publisher<Interface> > createPublisher()
326  {
327  PublisherParms parms;
328  return mPublishingServicePtr->createPublisher<Interface>(parms);
329  }
330 
332 
335  template<typename Interface>
336  std::shared_ptr< Publisher<Interface> > createPublisher(
337  const PublisherParms & parms)
338  {
339  return mPublishingServicePtr->createPublisher<Interface>(parms);
340  }
341 
343 
345  template<typename Interface, typename T>
346  std::shared_ptr< Subscription > createSubscription(
347  T & servantObj,
348  const RCF::Endpoint & publisherEp)
349  {
350  RCF_ASSERT(mStarted);
351  SubscriptionParms parms;
352  parms.setPublisherEndpoint(publisherEp);
353  return mSubscriptionServicePtr->createSubscription<Interface>(servantObj, publisherEp);
354  }
355 
357 
359  template<typename Interface, typename T>
360  std::shared_ptr< Subscription > createSubscription(
361  T & servantObj,
362  const SubscriptionParms & parms)
363  {
364  RCF_ASSERT(mStarted);
365  return mSubscriptionServicePtr->createSubscription<Interface>(servantObj, parms);
366  }
367 
369 
370 #endif
371 
373  void setEnableProxyEndpoints(bool enable);
374 
376  bool getEnableProxyEndpoints() const;
377 
378 #if RCF_FEATURE_FILETRANSFER==1
379 
382 
384 
386  void setUploadBandwidthLimit(std::uint32_t uploadQuotaBps);
387 
389  std::uint32_t getUploadBandwidthLimit() const;
390 
392  void setDownloadBandwidthLimit(std::uint32_t downloadQuotaBps);
393 
395  std::uint32_t getDownloadBandwidthLimit() const;
396 
398  void setUploadDirectory(const Path & uploadDir);
399 
401  Path getUploadDirectory() const;
402 
404  void setDownloadProgressCallback(DownloadProgressCallback downloadProgressCb);
405 
407  void setUploadProgressCallback(UploadProgressCallback uploadProgressCb);
408 
410  void setUploadBandwidthQuotaCallback(UploadBandwidthQuotaCallback uploadQuotaCb);
411 
413  void setDownloadBandwidthQuotaCallback(DownloadBandwidthQuotaCallback downloadQuotaCb);
414 
416 
417 #endif
418 
419 
420 
421  // For internal use.
422  std::uint32_t getRuntimeVersion();
423  void setRuntimeVersion(std::uint32_t version);
424 
426  std::uint32_t getArchiveVersion();
427 
429  void setArchiveVersion(std::uint32_t version);
430 
431  void setOnCallbackConnectionCreated(OnCallbackConnectionCreated onCallbackConnectionCreated);
432  OnCallbackConnectionCreated getOnCallbackConnectionCreated();
433 
434 
435  private:
436 
437  void init();
438 
439  public:
440 
441 
442  I_Service &
443  getServerTransportService();
444 
446  getServerTransportPtr();
447 
448  private:
449 
450  bool addService(
451  ServicePtr servicePtr);
452 
453  bool removeService(
454  ServicePtr servicePtr);
455 
456  public:
457 
458  PingBackServicePtr
459  getPingBackServicePtr();
460 
461  FileTransferServicePtr
462  getFileTransferServicePtr();
463 
464  SessionTimeoutServicePtr
465  getSessionTimeoutServicePtr();
466 
467  PublishingServicePtr
468  getPublishingServicePtr();
469 
470  SubscriptionServicePtr
471  getSubscriptionServicePtr();
472 
473  FilterServicePtr getFilterServicePtr();
474 
475  bool addServerTransport(
476  ServerTransportPtr serverTransportPtr);
477 
478  bool removeServerTransport(
479  ServerTransportPtr serverTransportPtr);
480 
481  ServerTransport *
482  findTransportCompatibleWith(ClientTransport & clientTransport);
483 
484  ServerTransport * queryForTransport(RCF::TransportType transportType);
485 
486  void setStartCallback(const StartCallback &startCallback);
487 
488  private:
489  void invokeStartCallback();
490 
491  private:
492  ServerBindingPtr bindImpl(
493  const std::string &name,
494  RcfClientPtr rcfClientPtr);
495 
496  //*************************************
497  // async io transport interface
498 
499  public:
500  SessionPtr createSession();
501  void onReadCompleted(SessionPtr sessionPtr);
502  void onWriteCompleted(SessionPtr sessionPtr);
503 
504 
505  //*************************************
506  // transports, queues and threads
507 
508  public:
509 
510 
511  template<typename Iter>
512  void enumerateSessions(const Iter & iter)
513  {
514  for (std::size_t i=0; i<mServerTransports.size(); ++i)
515  {
516  mServerTransports[i]->enumerateSessions(iter);
517  }
518  }
519 
520  //*************************************
521  // stub management
522 
523  private:
524  ReadWriteMutex mStubMapMutex;
525  typedef std::map<std::string, RcfClientPtr> StubMap;
526  StubMap mStubMap;
527 
528 
529  typedef std::function<void(const JsonRpcRequest &, JsonRpcResponse &)> JsonRpcMethod;
530  typedef std::map<std::string, JsonRpcMethod> JsonRpcMethods;
531  JsonRpcMethods mJsonRpcMethods;
532 
533  friend class RcfSession;
534 
535 
536 
537  //*************************************
538  // service management
539 
540  private:
541  std::vector<ServerTransportPtr> mServerTransports;
542  std::vector<ServicePtr> mServices;
543  FilterServicePtr mFilterServicePtr;
544  PingBackServicePtr mPingBackServicePtr;
545  FileTransferServicePtr mFileTransferServicePtr;
546  SessionTimeoutServicePtr mSessionTimeoutServicePtr;
547  PublishingServicePtr mPublishingServicePtr;
548  SubscriptionServicePtr mSubscriptionServicePtr;
549  CallbackConnectionServicePtr mCallbackConnectionServicePtr;
550  ProxyEndpointServicePtr mProxyEndpointServicePtr;
551  ServerObjectServicePtr mServerObjectServicePtr;
552 
553  void startService(ServicePtr servicePtr) const;
554  void stopService(ServicePtr servicePtr) const;
555  void resolveServiceThreadPools(ServicePtr servicePtr) const;
556 
557  friend class AsioNetworkSession;
558  FilterPtr createFilter(int filterId);
559 
560  // start/stop functionality
561  StartCallback mStartCallback;
562  Condition mStartEvent;
563  Condition mStopEvent;
564 
565  Mutex mStartStopMutex;
566  bool mStarted;
567 
568  private:
569  ThreadPoolPtr mThreadPoolPtr;
570 
571 
572  // TODO: get rid of this hack
573  private:
574  friend class MethodInvocationRequest;
575 
576  private:
577  std::uint32_t mRuntimeVersion;
578  std::uint32_t mArchiveVersion;
579 
580 
581  public:
582 
583 #if RCF_FEATURE_FILETRANSFER==1
584 
585 
586  Path getUploadPath(const std::string & uploadId);
587 
588  private:
589 
590  DownloadProgressCallback mOnFileDownloadProgress;
591  UploadProgressCallback mOnFileUploadProgress;
592 
593  Path mFileUploadDirectory;
594 
595  std::uint32_t mFileUploadQuota;
596  UploadBandwidthQuotaCallback mFileUploadQuotaCb;
597 
598  std::uint32_t mFileDownloadQuota;
599  DownloadBandwidthQuotaCallback mFileDownloadQuotaCb;
600 
601  friend class FileTransferService;
602 
603 #endif
604 
605  private:
606 
607  mutable ReadWriteMutex mPropertiesMutex;
608 
609  std::vector<TransportProtocol> mSupportedProtocols;
610  CertificatePtr mCertificatePtr;
611  std::string mOpenSslCipherSuite;
612 
613  CertificatePtr mCaCertificatePtr;
614  CertificateValidationCallback mCertificateValidationCb;
615  tstring mSchannelCertificateValidation;
616 
617  SslImplementation mSslImplementation;
618 
619  std::uint32_t mSessionTimeoutMs;
620  std::uint32_t mSessionHarvestingIntervalMs;
621 
622  std::uint32_t mHttpSessionTimeoutMs;
623 
624  std::string mHttpServerHeader;
625 
626  std::uint32_t mHttpHealthCheckStatus = 0;
627  std::string mHttpHealthCheckStatusMessage;
628  std::string mHttpHealthCheckContent;
629 
630  OnCallbackConnectionCreated mOnCallbackConnectionCreated;
631 
632 #if RCF_FEATURE_SSPI==1
633  DWORD mSchannelEnabledProtocols = 0;
634  ULONG mSchannelContextRequirements = 0;
635 #endif
636 
637  public:
638 
639 
640  private:
641 
642  std::uint32_t mServerObjectHarvestingIntervalS;
643 
644  friend class HttpSessionFilter;
645 
646  HttpSessionPtr attachHttpSession(const std::string & httpSessionId, bool allowCreate, ExceptionPtr & ePtr);
647  void detachHttpSession(HttpSessionPtr httpSessionPtr);
648 
649  friend class ServerObjectService;
650  void harvestHttpSessions();
651 
652 #if RCF_FEATURE_HTTP==1
653  Mutex mHttpSessionMapMutex;
654  std::map<std::string, HttpSessionPtr> mHttpSessionMap;
655 
656  HttpMessageVerifierPtr mHttpMessageVerifierPtr;
657 
658 #endif
659 
660  public:
661 
662 
663  private:
664  friend class ProxyEndpoint;
665  ClientTransportUniquePtr makeProxyEndpointConnection(const std::string& proxyEndpointName);
666 
667  bool mEnableProxyEndpoints = false;
668 
669  };
670 
671 } // namespace RCF
672 
673 #endif // ! INCLUDE_RCF_RCFSERVER_HPP
674 
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.
std::function< void(RcfSession &, FileDownloadInfo &)> DownloadProgressCallback
Describes user-provided callback functions for server-side monitoring of a file download.
Definition: RcfFwd.hpp:134
std::shared_ptr< Subscription > createSubscription(T &servantObj, const SubscriptionParms &parms)
Creates a subscription to a remote RCF publisher.
Definition: RcfServer.hpp:360
Represents a server side session, associated with a client connection.
Definition: RcfSession.hpp:64
std::shared_ptr< Certificate > CertificatePtr
Reference counted wrapper for RCF::Certificate.
Definition: RcfFwd.hpp:108
std::unique_ptr< ClientTransport > ClientTransportUniquePtr
Unique pointer wrapper for RCF::ClientTransport.
Definition: RcfFwd.hpp:43
std::shared_ptr< T > queryServerObject(const std::string &objectKey)
Queries for a server object under the given key. Returns an empty std::shared_ptr if no object is fou...
Definition: RcfServer.hpp:293
Base class for all client transports.
Definition: ClientTransport.hpp:74
Base class for all server transports.
Definition: ServerTransport.hpp:121
void setPublisherEndpoint(const Endpoint &publisherEp)
Sets the network endpoint of the publishing server.
BandwidthQuotaCallback DownloadBandwidthQuotaCallback
Describes user-provided callback functions for assigning custom bandwidth quotas to a RcfSession...
Definition: RcfFwd.hpp:146
std::function< bool(Certificate *)> CertificateValidationCallback
Describes user-provided callback functions for validating a certificate.
Definition: RcfFwd.hpp:114
std::shared_ptr< Publisher< Interface > > createPublisher()
Creates a publisher instance for the given RCF interface.
Definition: RcfServer.hpp:325
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...
std::shared_ptr< HttpMessageVerifier > HttpMessageVerifierPtr
Reference counted wrapper for RCF::HttpMessageVerifier.
Definition: HttpFrameFilter.hpp:60
RCF_EXPORT std::uint32_t getRuntimeVersion()
Gets the RCF runtime version number.
std::shared_ptr< T > getServerObject(const std::string &objectKey, std::uint32_t timeoutMs)
Queries or creates a server object under the given key. If creating the object, the timeout value is ...
Definition: RcfServer.hpp:301
BandwidthQuotaCallback UploadBandwidthQuotaCallback
Describes user-provided callback functions for assigning custom bandwidth quotas to a RcfSession...
Definition: RcfFwd.hpp:143
SslImplementation
Describes which SSL implementation to use.
Definition: Enums.hpp:84
ServerBindingPtr bind(ImplementationT &servantObj, const std::string &name="")
Creates a servant binding, exposing the servant object to remote calls through the RCF interface Inte...
Definition: RcfServer.hpp:148
std::shared_ptr< ServerBinding > ServerBindingPtr
Reference counted wrapper for RCF::ServerBinding.
Definition: RcfFwd.hpp:248
std::shared_ptr< Publisher< Interface > > createPublisher(const PublisherParms &parms)
Creates a publisher instance for the given RCF interface.
Definition: RcfServer.hpp:336
Provides RCF server-side functionality.
Definition: RcfServer.hpp:53
bool unbind(const std::string &name="")
Removes a servant binding from the RcfServer.
Definition: RcfServer.hpp:165
Base class for all network endpoint types.
Definition: Endpoint.hpp:40
std::function< void(RcfSession &, FileUploadInfo &)> UploadProgressCallback
Describes user-provided callback functions for server-side monitoring of a file upload.
Definition: RcfFwd.hpp:137
Base class for IP-based server transports. Provides IP-related functionality.
Definition: IpServerTransport.hpp:36
General configuration of a subscription.
Definition: SubscriptionService.hpp:95
Definition: AmiIoHandler.hpp:23
TransportType
Describes the transport types used by a RCF connection.
Definition: Enums.hpp:33
Represents a proxy endpoint.
Definition: ProxyEndpoint.hpp:45
General configuration of a publisher.
Definition: PublishingService.hpp:38
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...
std::shared_ptr< ServerTransport > ServerTransportPtr
Unique pointer wrapper for RCF::ServerTransport.
Definition: RcfFwd.hpp:46
RCF_EXPORT bool init(RcfConfigT *=nullptr)
Reference-counted initialization of RCF library. May be called multiple times (see deinit())...
std::shared_ptr< Subscription > createSubscription(T &servantObj, const RCF::Endpoint &publisherEp)
Creates a subscription to a remote RCF publisher.
Definition: RcfServer.hpp:346