Giter VIP home page Giter VIP logo

Comments (7)

eidheim avatar eidheim commented on June 1, 2024

I think these changes should work without creating a buffer for each send operation, although c++14 is required here:

diff --git a/http_examples.cpp b/http_examples.cpp
index 3e16a9c..39ad46c 100644
--- a/http_examples.cpp
+++ b/http_examples.cpp
@@ -173,16 +173,17 @@ int main() {
         // Trick to define a recursive function within this scope (for example purposes)
         class FileServer {
         public:
-          static void read_and_send(const shared_ptr<HttpServer::Response> &response, const shared_ptr<ifstream> &ifs) {
+          static void read_and_send(const shared_ptr<HttpServer::Response> &response,
+                                    const shared_ptr<ifstream> &ifs,
+                                    vector<char> &&buffer) {
             // Read and send 128 KB at a time
-            static vector<char> buffer(131072); // Safe when server is running on one thread
             streamsize read_length;
             if((read_length = ifs->read(&buffer[0], static_cast<streamsize>(buffer.size())).gcount()) > 0) {
               response->write(&buffer[0], read_length);
               if(read_length == static_cast<streamsize>(buffer.size())) {
-                response->send([response, ifs](const SimpleWeb::error_code &ec) {
+                response->send([ response, ifs, buffer = move(buffer) ](const SimpleWeb::error_code &ec) mutable {
                   if(!ec)
-                    read_and_send(response, ifs);
+                    read_and_send(response, ifs, move(buffer));
                   else
                     cerr << "Connection interrupted" << endl;
                 });
@@ -190,7 +191,7 @@ int main() {
             }
           }
         };
-        FileServer::read_and_send(response, ifs);
+        FileServer::read_and_send(response, ifs, vector<char>(131072));
       }
       else
         throw invalid_argument("could not read file");

from simple-web-server.

kinimodmeyer avatar kinimodmeyer commented on June 1, 2024

@eidheim nice that works great 👍 (maybe adding into the example?)

btw how is the threading working exactly in this lib?
is there a logic that every thread execute his own lambda-call or can multiple threads execute the same lambda-call?

from simple-web-server.

eidheim avatar eidheim commented on June 1, 2024

In general, it is best to use an event loop (one server thread) so that all the handlers that you define always will be run sequentially. But this does not mean that your application is running in only one thread, because Asio is using internal threads for for instance tcp/ip communication. In other words, the number of threads you define in the config object, is the number of threads that will be used to run your handlers such as the resource functions and the send-method argument.

from simple-web-server.

eidheim avatar eidheim commented on June 1, 2024

Here is an another attempt, where the number of buffers created equals the maximum number of threads the handlers are run in: 

edit: removed flawed diff

from simple-web-server.

kinimodmeyer avatar kinimodmeyer commented on June 1, 2024

that means, that one server.resource[xyz][xyz]-callback only getting executed by one thread
(no matter how much you defined in config)

with more threads in config, more threads can work on these callbacks but 2 threads not executing the exact same connection-callback?

from simple-web-server.

eidheim avatar eidheim commented on June 1, 2024

With 2 threads in config, 2 callbacks, any callbacks that is, similar or not, can be executed simultaneously, so if you use shared resources, keep in mind to protect them using mutexes or something similar.

With 1 thread, event loop that is, the handlers/callbacks are run sequentially, no need to think about mutexes in your callbacks.

from simple-web-server.

eidheim avatar eidheim commented on June 1, 2024

I see you point now, and yes my last attempt is flawed. I'll edit it.

from simple-web-server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.