27 #ifndef _REC_RPC_SERVER_H_ 28 #define _REC_RPC_SERVER_H_ 55 virtual serialization::SerializablePtr createParam()
const = 0;
58 virtual serialization::SerializablePtr createResult()
const = 0;
88 virtual void invoke(
const QUrl& url,
const QString& host, QByteArray& resultPage, QString& contentType,
const rec::rpc::ClientInfo& client ) = 0;
107 virtual void invoke(
const QByteArray& request, QByteArray& response,
const rec::rpc::ClientInfo& client ) = 0;
111 typedef QSharedPointer< RPCFunctionBase > RPCFunctionBasePtr;
112 typedef QSharedPointer< HTTPGetHandlerBase > HTTPGetHandlerBasePtr;
113 typedef QSharedPointer< CustomRequestHandlerBase > CustomRequestHandlerBasePtr;
117 template<
typename Parent_t,
typename Param_t,
typename Result_t >
122 RPCFunction( Parent_t* parent, Invoke_f
function ) : _parent( parent ), _function(
function ) { }
123 serialization::SerializablePtr createParam()
const {
return createSerializable< Param_t >(); }
124 serialization::SerializablePtr createResult()
const {
return createSerializable< Result_t >(); }
128 if (
typeid( param ) !=
typeid( Param_t ) ||
typeid( result ) !=
typeid( Result_t ) )
130 ( _parent->*_function )( static_cast< const Param_t& >( param ),
static_cast< Result_t&
>( result ), client );
137 template<
typename Parent_t >
140 typedef void( Parent_t::*Invoke_f )(
const QUrl&,
const QString&, QByteArray&, QString&,
const rec::rpc::ClientInfo& );
142 HTTPGetHandler( Parent_t* parent, Invoke_f
function ) : _parent( parent ), _function(
function ) { }
144 void invoke(
const QUrl& url,
const QString& host, QByteArray& resultPage, QString& contentType,
const rec::rpc::ClientInfo& client )
146 ( _parent->*_function )( url, host, resultPage, contentType, client );
153 template<
typename Parent_t >
156 typedef void( Parent_t::*Invoke_f )(
const QByteArray&, QByteArray& response,
const rec::rpc::ClientInfo& );
158 CustomRequestHandler( Parent_t* parent, Invoke_f
function ) : _parent( parent ), _function(
function ) { }
160 void invoke(
const QByteArray& request, QByteArray& response,
const rec::rpc::ClientInfo& client )
162 ( _parent->*_function )( request, response, client );
181 static const int DefaultClientMsgWaitTime = 5000;
184 static const int DefaultHttpKeepAliveTimeout = 20;
187 static const int DefaultHttpKeepAliveMaxRequests = 10;
190 static const int DefaultCustomTimeout = 2;
202 Server( QObject* parent = 0 );
211 bool isMultiThreadedSerializationEnabled()
const;
223 bool isLocalIPCEnabled()
const;
229 int clientMsgWaitTime()
const;
235 int httpKeepAliveTimeout()
const;
241 int httpKeepAliveMaxRequests()
const;
247 int customTimeout()
const;
253 bool isListening()
const;
261 unsigned short serverPort()
const;
272 QString greeting()
const;
278 int numClientsConnected()
const;
290 bool listen(
bool blocking =
false );
300 void close(
bool blocking =
false );
312 void disconnectAllClients();
317 void disconnectClient(
const QHostAddress& peerAddress, quint16 peerPort );
324 void setMultiThreadedSerializationEnabled(
bool enabled );
338 void setLocalIPCEnabled(
bool enabled );
345 void setClientMsgWaitTime(
int clientMsgWaitTime = DefaultClientMsgWaitTime );
352 void setHttpKeepAliveTimeout(
int httpKeepAliveTimeout = DefaultHttpKeepAliveTimeout );
359 void setHttpKeepAliveMaxRequests(
int httpKeepAliveMaxRequests = DefaultHttpKeepAliveMaxRequests );
366 void setCustomTimeout(
int customTimeout = DefaultCustomTimeout );
377 void setGreeting(
const QString& greeting );
401 void serverError( QAbstractSocket::SocketError error,
const QString& errorString );
409 void clientError( QAbstractSocket::SocketError error,
const QString& errorString );
446 void numClientsConnectedChanged(
int num );
454 void log(
const QString& message,
int level = 1 );
470 void registerFunction(
const QString& name, RPCFunctionBasePtr
function );
482 void unregisterFunction(
const QString& name );
493 bool isFunctionRegistered(
const QString& name )
const;
508 void registerTopicListener(
const QString& name, TopicListenerBasePtr listener );
520 void unregisterTopicListener(
const QString& name );
531 bool isTopicListenerRegistered(
const QString& name )
const;
549 void addTopic(
const QString& name,
int sharedMemorySize = 0,
bool serverOnly =
false );
567 void addEnqueuedTopic(
const QString& name,
bool serverOnly =
false );
586 void addPermanentTopic(
const QString& name,
int sharedMemorySize = 0,
bool serverOnly =
false );
588 void beginAddTopicGroup();
589 void endAddTopicGroup();
604 void publishTopic(
const QString& name, serialization::SerializablePtrConst data );
616 void registerHttpGetHandler( HTTPGetHandlerBasePtr handler = HTTPGetHandlerBasePtr() );
628 void registerCustomRequestHandler( CustomRequestHandlerBasePtr handler = CustomRequestHandlerBasePtr() );
631 server::Server* _server;
644 #define DECLARE_FUNCTION( FUNCTIONNAME ) \ 646 rec::rpc::RPCFunctionBasePtr create##FUNCTIONNAME##Wrapper(); \ 647 void FUNCTIONNAME( const FUNCTIONNAME##Param& param, FUNCTIONNAME##Result& result, const rec::rpc::ClientInfo& client ); 660 #define BEGIN_FUNCTION_DEFINITION( CLASSNAME, FUNCTIONNAME ) \ 661 rec::rpc::RPCFunctionBasePtr CLASSNAME::create##FUNCTIONNAME##Wrapper() \ 663 return rec::rpc::RPCFunctionBasePtr( new rec::rpc::detail::RPCFunction< CLASSNAME, FUNCTIONNAME##Param, FUNCTIONNAME##Result >( this, &CLASSNAME::FUNCTIONNAME ) ); \ 665 void CLASSNAME::FUNCTIONNAME( const FUNCTIONNAME##Param& param, FUNCTIONNAME##Result& result, const rec::rpc::ClientInfo& client ) \ 671 #define END_FUNCTION_DEFINITION } 685 #define REGISTER_FUNCTION( FUNCTIONNAME ) registerFunction( #FUNCTIONNAME, create##FUNCTIONNAME##Wrapper() ); 696 #define UNREGISTER_FUNCTION( FUNCTIONNAME ) unregisterFunction( #FUNCTIONNAME ); 705 #define IS_FUNCTION_REGISTERED( FUNCTIONNAME ) isFunctionRegistered( #FUNCTIONNAME ); 719 #define ADD_TOPIC( TOPICNAME, SHAREDMEMSIZE ) addTopic( #TOPICNAME, SHAREDMEMSIZE ); 721 #define ADD_ENQUEUEDTOPIC( TOPICNAME ) addEnqueuedTopic( #TOPICNAME ); 735 #define ADD_SERVERONLY_TOPIC( TOPICNAME, SHAREDMEMSIZE ) addTopic( #TOPICNAME, SHAREDMEMSIZE, true ); 749 #define ADD_PERMANENT_TOPIC( TOPICNAME, SHAREDMEMSIZE ) addPermanentTopic( #TOPICNAME, SHAREDMEMSIZE ); 763 #define ADD_PERMANENT_SERVERONLY_TOPIC( TOPICNAME, SHAREDMEMSIZE ) addPermanentTopic( #TOPICNAME, SHAREDMEMSIZE, true ); 773 #define DECLARE_HTTP_GET_HANDLER( HANDLERNAME ) \ 775 rec::rpc::HTTPGetHandlerBasePtr create##HANDLERNAME##HttpGetHandler(); \ 776 void HANDLERNAME( const QUrl& url, const QString& host, QByteArray& resultPage, QString& contentType, const rec::rpc::ClientInfo& client ); 791 #define BEGIN_HTTP_GET_HANDLER_DEFINITION( CLASSNAME, HANDLERNAME ) \ 792 rec::rpc::HTTPGetHandlerBasePtr CLASSNAME::create##HANDLERNAME##HttpGetHandler() \ 794 return rec::rpc::HTTPGetHandlerBasePtr( new rec::rpc::detail::HTTPGetHandler< CLASSNAME >( this, &CLASSNAME::HANDLERNAME ) ); \ 796 void CLASSNAME::HANDLERNAME( const QUrl& url, const QString& host, QByteArray& resultPage, QString& contentType, const rec::rpc::ClientInfo& client ) \ 802 #define END_HTTP_GET_HANDLER_DEFINITION } 814 #define REGISTER_HTTP_GET_HANDLER( HANDLERNAME ) registerHttpGetHandler( create##HANDLERNAME##HttpGetHandler() ); 823 #define REMOVE_HTTP_GET_HANDLER registerHttpGetHandler( rec::rpc::HTTPGetHandlerBasePtr() ); 833 #define DECLARE_CUSTOM_REQUEST_HANDLER( HANDLERNAME ) \ 835 rec::rpc::CustomRequestHandlerBasePtr create##HANDLERNAME##CustomRequestHandler(); \ 836 void HANDLERNAME( const QByteArray& request, QByteArray& response, const rec::rpc::ClientInfo& client ); 848 #define BEGIN_CUSTOM_REQUEST_HANDLER_DEFINITION( CLASSNAME, HANDLERNAME ) \ 849 rec::rpc::CustomRequestHandlerBasePtr CLASSNAME::create##HANDLERNAME##CustomRequestHandler() \ 851 return rec::rpc::CustomRequestHandlerBasePtr( new rec::rpc::detail::CustomRequestHandler< CLASSNAME >( this, &CLASSNAME::HANDLERNAME ) ); \ 853 void CLASSNAME::HANDLERNAME( const QByteArray& request, QByteArray& response, const rec::rpc::ClientInfo& client ) \ 859 #define END_CUSTOM_REQUEST_HANDLER_DEFINITION } 871 #define REGISTER_CUSTOM_REQUEST_HANDLER( HANDLERNAME ) registerCustomRequestHandler( create##HANDLERNAME##CustomRequestHandler() ); 880 #define REMOVE_CUSTOM_REQUEST_HANDLER registerCustomRequestHandler( rec::rpc::CustomRequestHandlerBasePtr() ); 882 #endif //_REC_RPC_RPC_SERVER_H_
const int defaultPort
The TCP port which will be used by default if no other one is specified.
RPC function wrapper interface.
static int sendFailSocketTimeout
Close socket if sending fails for sendFailSocketTimeout() milli seconds.
Custom request handler interface.
Base class for all serializable data.
HTTP GET handler interface.