Skip to content

erijo/xsonrpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

xsonrpc

An LGPL licensed XML/JSON RPC library for C++11.

Examples

A simple server handling both XML-RPC and JSON-RPC:

std::string Concat(const std::string& a, const std::string& b)
{
  return a + b;
}

int main()
{
  xsonrpc::Server server(8080);

  xsonrpc::JsonFormatHandler jsonFormatHandler;
  xsonrpc::XmlFormatHandler xmlFormatHandler;
  server.RegisterFormatHandler(jsonFormatHandler);
  server.RegisterFormatHandler(xmlFormatHandler);

  auto& dispatcher = server.GetDispatcher();
  dispatcher.AddMethod("concat", &Concat);

  bool run = true;
  dispatcher.AddMethod("exit", [&] () { run = false; }).SetHidden();

  server.Run();

  pollfd fd;
  fd.fd = server.GetFileDescriptor();
  fd.events = POLLIN;

  while (run && poll(&fd, 1, -1) == 1) {
    server.OnReadableFileDescriptor();
  }

  return 0;
}

A client capable of calling the server above could look like this:

int main(int argc, char** argv)
{
  xsonrpc::Client::GlobalInit();

  std::unique_ptr<xsonrpc::FormatHandler> formatHandler;
  for (int i = 1; i < argc; ++i) {
    if (strcmp(argv[i], "json") == 0) {
      std::cout << "Using JSON format\n";
      formatHandler.reset(new xsonrpc::JsonFormatHandler());
      break;
    }
  }
  if (!formatHandler) {
    std::cout << "Using XML format\n";
    formatHandler.reset(new xsonrpc::XmlFormatHandler());
  }
  xsonrpc::Client client("localhost", 8080, *formatHandler);

  std::cout << client.Call("concat", "Hello, ", "World!") << "\n";
  client.Call("exit");
  return 0;
}

Build instructions

To build xsonrpc you need:

mkdir build
cd build
cmake path/to/xsonrpc
make

About

An XML/JSON RPC library for C++11

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published