//If no server replies within this time, abandon request
#define GLOBAL_TIMEOUT3000//msecs//PING interval for servers we think are alive
#define PING_INTERVAL2000//msecs//Server considered dead if silent for this long
#define SERVER_TTL6000//msecs

//This API works in two halves, a common pattern for APIs that need to//run in the background. One half is an front-end object our application//creates and works with; the other half is a back-end "agent" that runs//in a background thread. The front-end talks to the back-end over an//inproc pipe socket:

//To implement the connect method, the front-end object sends a multi-part//message to the back-end agent. The first part is a string "CONNECT", and//the second part is the endpoint. It waits 100msec for the connection to//come up, which isn't pretty, but saves us from sending all requests to a//single server, at start-up time:

//Here we see the back-end agent. It runs as an attached thread, talking//to its parent over a pipe socket. It is a fairly complex piece of work//so we'll break it down into pieces. First, the agent manages a set of//servers, using our familiar class approach:

//---------------------------------------------------------------------//Simple class for one server we talk to

typedefstruct {char*endpoint;//Server identity/endpointuint alive;//1 if known to be aliveint64_t ping_at;//Next ping at this timeint64_t expires;//Expires at this time
} server_t;