Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is not real code. Fantasy pseudo
- // Read the following functions in this order:
- // main(), onClient(), Route(), then aboutFunc()
- #include "node_native_amalgamation.c"
- // aboutFunc
- // Optional function - Created by the user of our library
- static int aboutFunc(pClient)
- {
- // we could simply reply with a file from the RAM Cache
- // pClient->Write(MyCachedHTMLFile);
- // return INT_HANDLED;
- // or we need to run a long process. So, let's add our function to a message (threaded) queue.
- // 1. setup a new queue item
- NNQUEUE * pQueueItem = new NNQUEUE;
- // 2. Optionally add a custom object to it
- Custom_t * pCustom = new Custom_t;
- pCustom.str = "something";
- pQueueItem.parm = pCustom;
- // 3. Add the lengthy function to be called in a thread. Required
- pQueueItem.function = aboutFunc2();
- // 4. Add to the message queue. Returns immediately. Will clean up itself.
- pClient.AddQueuePool(pQueueItem);
- // return this so that the caller does not call pClient->Wait();
- return INT_HANDLED_VIA_THREAD;
- }
- // Route
- // Required function - Maps a URL to a function
- static int NNHTTP::Route(NNCLIENT* pClient)
- {
- // read in the data
- std::string sResponse = pClient->Read();
- if(sResponse == "/about")
- {
- return aboutFunc(pClient);
- }
- // etc ...
- }
- // onClient
- // Required function - Respond to a new client. Called automatically via the handleClients() thread
- static void NNHTTP::onClient(NNHTTP* pMyServer)
- {
- // Accept the connection
- NNCLIENT * pClient = new NNCLIENT;
- pMyServer->Accept(pClient);
- // You could parse the string manually. Instead we'll see if we have a route for it and let it
- // call our mapped function.
- if(pMyServer->Route(pClient) != INT_HANDLED)
- {
- // we don't recognize the URL. We could close the connection, call a 40* function
- // or just keep it persistent.
- // pClient->Close();
- }
- // processed Client command. Keep it persistent by waiting for more URL commands.
- if(pClient->Wait() == true)
- {
- pMyServer->Route(pClient);
- }
- else
- {
- pClient->Close();
- }
- }
- int main()
- {
- // instantiate node.native server and read in configuration
- myServer NNHTTP;
- myServer.loadCfg("myDomain2.com","/path/config.ini");
- // optionally run as service or daemon
- myServer.asService("MySvcName");
- // Handle clients. This call actually runs our NNHTTP in a separate thread. It will call
- // onClient() for every client event. Easy API.
- myServer.handleClients();
- // wait for the signal to shut down
- myServer.asServiceStop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment