14 #include <sys/resource.h> 22 : _introspectionEnabled(false),
27 _accept_retry_time_sec(0.0)
29 struct rlimit limit = { .rlim_cur = 0, .rlim_max = 0 };
30 unsigned int max_files = 1024;
32 if(getrlimit(RLIMIT_NOFILE, &limit) == 0) {
33 max_files = limit.rlim_max;
34 if( limit.rlim_max == RLIM_INFINITY ) {
41 for(
unsigned int i=0; i<max_files; i++) {
44 pollfds[i].events = POLLIN | POLLPRI | POLLOUT;
81 MethodMap::iterator i =
_methods.find(methodName);
91 MethodMap::const_iterator i =
_methods.find(name);
146 XmlRpcUtil::log(2,
"XmlRpcServer::bindAndListen: server listening on port %d fd %d",
_port, fd);
197 XmlRpcUtil::error(
"XmlRpcServer::acceptConnection: Rejecting client, not enough free file descriptors");
206 XmlRpcUtil::log(2,
"XmlRpcServer::acceptConnection: creating a connection");
220 struct rlimit limit = { .rlim_cur = 0, .rlim_max = 0 };
223 if(getrlimit(RLIMIT_NOFILE, &limit) == 0) {
226 if( limit.rlim_max == RLIM_INFINITY ) {
234 if(poll(&
pollfds[0], limit.rlim_cur, 1) >= 0) {
235 for(rlim_t i=0; i<limit.rlim_cur; i++) {
236 if(
pollfds[i].revents & POLLNVAL) {
252 "limit, getrlimit() failed: %s", strerror(errno));
293 static const std::string
LIST_METHODS(
"system.listMethods");
294 static const std::string
METHOD_HELP(
"system.methodHelp");
295 static const std::string
MULTICALL(
"system.multicall");
306 _server->listMethods(result);
309 std::string
help() {
return std::string(
"List all methods available on a server as an array of strings"); }
331 std::string
help() {
return std::string(
"Retrieve the help string for a named method"); }
369 result[i++] = it->first;
A class to handle XML RPC requests from a particular client.
virtual XmlRpcServerConnection * createConnection(int socket)
Create a new connection object for processing requests from a specific client.
static bool setReuseAddr(int socket)
static const double ACCEPT_RETRY_INTERVAL_SEC
RPC method arguments and results are represented by Values.
static const std::string METHOD_HELP("system.methodHelp")
std::vector< struct pollfd > pollfds
static int get_port(int socket)
void execute(XmlRpcValue &, XmlRpcValue &result)
Execute the method. Subclasses must provide a definition for this method.
static int accept(int socket)
Accept a client connection request.
void shutdown()
Close all connections with clients and the socket file descriptor.
void work(double msTime)
Process client requests for the specified time.
bool bindAndListen(int port, int backlog=5)
XmlRpcServer()
Create a server object.
void addMethod(XmlRpcServerMethod *method)
Add a command to the RPC server.
XmlRpcServerMethod * findMethod(const std::string &name) const
Look up a method by name.
void removeMethod(XmlRpcServerMethod *method)
Remove a command from the RPC server.
void removeSource(XmlRpcSource *source)
void setKeepOpen(bool b=true)
Specify whether the file descriptor should be kept open if it is no longer monitored.
static void error(const char *fmt,...)
Dump error messages somewhere.
virtual unsigned handleEvent(unsigned eventType)
Handle client connection requests.
void clear()
Clear all sources from the monitored sources list. Sources are closed.
int countFreeFDs()
Count number of free file descriptors.
virtual ~XmlRpcServer()
Destructor.
static std::string getErrorMsg()
Returns message corresponding to last error.
int getfd() const
Return the file descriptor being monitored.
Abstract class representing a single RPC method.
XmlRpcServerMethod * _listMethods
virtual void close()
Close the owned fd. If deleteOnClose was specified at construction, the object is deleted...
void setfd(int fd)
Specify the file descriptor to monitor.
static bool bind(int socket, int port)
Bind to a specified port.
virtual void removeConnection(XmlRpcServerConnection *)
Remove a connection from the dispatcher.
void setSize(int size)
Specify the size for array values. Array values will grow beyond this size if needed.
static const std::string MULTICALL("system.multicall")
void execute(XmlRpcValue ¶ms, XmlRpcValue &result)
Execute the method. Subclasses must provide a definition for this method.
void enableIntrospection(bool enabled=true)
Specify whether introspection is enabled or not. Default is not enabled.
A class to handle XML RPC requests.
static bool listen(int socket, int backlog)
Set socket in listen mode.
static const int FREE_FD_BUFFER
static int socket()
Creates a stream (TCP) socket. Returns -1 on failure.
static void close(int socket)
Closes a socket.
double _accept_retry_time_sec
XmlRpcServerMethod * _methodHelp
void addSource(XmlRpcSource *source, unsigned eventMask)
virtual std::string help()
static const std::string LIST_METHODS("system.listMethods")
virtual unsigned acceptConnection()
Accept a client connection request.
void exit()
Exit from work routine.
static bool setNonBlocking(int socket)
Sets a stream (TCP) socket to perform non-blocking IO. Returns false on failure.
ListMethods(XmlRpcServer *s)
void exit()
Temporarily stop processing client requests and exit the work() method.
void listMethods(XmlRpcValue &result)
Introspection support.
MethodHelp(XmlRpcServer *s)
bool _introspectionEnabled
static void log(int level, const char *fmt,...)
Dump messages somewhere.
std::string & name()
Returns the name of the method.