Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include "seasocks/PrintfLogger.h"
  2. #include "seasocks/Server.h"
  3. #include "seasocks/Response.h"
  4. #include "seasocks/ResponseWriter.h"
  5. #include "seasocks/ResponseCode.h"
  6. #include "seasocks/util/RootPageHandler.h"
  7. #include "seasocks/util/PathHandler.h"
  8.  
  9. #include <cassert>
  10. #include <cstring>
  11. #include <iostream>
  12. #include <memory>
  13. #include <set>
  14. #include <sstream>
  15. #include <string>
  16. #include <thread>
  17. #include <unistd.h>
  18.  
  19. using namespace seasocks;
  20. using namespace std;
  21.  
  22. // The AsyncResponse does some long-lived "work" (in this case a big sleep...)
  23. // before responding to the ResponseWriter, in chunks. It uses a new thread to
  24. // perform this "work". As responses can be canceled before the work is
  25. // complete, we must ensure the ResponseWriter used to communicate the response
  26. // is kept alive long enough by holding its shared_ptr in the "work" thread.
  27. // Seasocks will tell the response it has been cancelled (if the connection
  28. // associated with the request is closed); but the ResponseWriter is safe in the
  29. // presence of a closed connection so for simplicity this example does nothing
  30. // in the cancel() method. It is assumed the lifetime of the Server object is
  31. // long enough for all requests to complete before it is destroyed.
  32. struct AsyncResponse : Response {
  33.     Server &_server;
  34.     explicit AsyncResponse(Server &server) : _server(server) {}
  35.  
  36.     // From Response:
  37.     virtual void handle(shared_ptr<ResponseWriter> writer) override {
  38.         auto &server = _server;
  39.         thread t([&server, writer] () mutable {
  40.             usleep(1000000); // A long database query...
  41.             string response = "some kind of response...beginning<br>";
  42.             server.execute([response, writer]{
  43.                 writer->begin(ResponseCode::Ok, TransferEncoding::Chunked);
  44.                 writer->header("Content-type", "application/html");
  45.                 writer->payload(response.data(), response.length());
  46.             });
  47.             response = "more data...<br>";
  48.             for (auto i = 0; i < 5; ++i) {
  49.                 usleep(1000000); // more data
  50.                 server.execute([response, writer]{
  51.                     writer->payload(response.data(), response.length());
  52.                 });
  53.             }
  54.             response = "Done!";
  55.             usleep(100000); // final data
  56.             server.execute([response, writer]{
  57.                 writer->payload(response.data(), response.length());
  58.                 writer->finish(true);
  59.             });
  60.         });
  61.         t.detach();
  62.     }
  63.     virtual void cancel() override {
  64.         // If we could cancel the thread, we would do so here. There's no need
  65.         // to invalidate the _writer; any writes to it after this will be
  66.         // silently dropped.
  67.     }
  68. };
  69.  
  70. struct DataHandler : CrackedUriPageHandler {
  71.     virtual std::shared_ptr<Response> handle(
  72.             const CrackedUri &/*uri*/, const Request &request) override {
  73.         return make_shared<AsyncResponse>(request.server());
  74.     }
  75. };
  76.  
  77. int main(int /*argc*/, const char* /*argv*/[]) {
  78.     shared_ptr<Logger> logger(new PrintfLogger(Logger::Level::DEBUG));
  79.  
  80.     Server server(logger);
  81.     auto root = make_shared<RootPageHandler>();
  82.     auto pathHandler = make_shared<PathHandler>("data", make_shared<DataHandler>());
  83.     root->add(pathHandler);
  84.     server.addPageHandler(root);
  85.  
  86.     server.serve("src/async_test_web", 9090);
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement