1.6. Multi-Processing XML-RPC

1.6.1. The Multi-Threaded Server

Starting the multithreaded server is a bit more complicated than the single threaded one. First you have to create the server object and tell it how many threads you want to have. Then you start the dispatcher threads. Afterwards you can either wait for all the threads to terminate or do something more useful until the termination.


  MultiThreadRpcServer server(protocol, num_threads); 1
  server.addMethod(&testcall,    2
                    Struct::getValueName(),
                    "testcall",
                    Integer::getValueName() + "," + Integer::getValueName(),
                    "Testcase with a c-function");
  unsigned started = server.dispatchAsync(); 3
  do_some_work(); 4
  server.waitAsync(false, true); 5

1

Create the server object.

2

Add rpc methods.

3

Start all threads.

4

Do something useful in the meanwhile.

5

Wait for all threads to finish before terminating the application.

One problem is it currently to terminate the server. Since I have not yet found a portable and good solution to make the threads terminate regularly while waiting for connections I usually use a shutdown method which I invoke via XML-RPC. This method has to be sent to all threads. See the server example files. One big drawbacks is that you have to secure this method against attacks.