Comments (7)
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.
@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.
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.
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.
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.
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.
I see you point now, and yes my last attempt is flawed. I'll edit it.
from simple-web-server.
Related Issues (20)
- Server 125 errors HOT 3
- Fully loads one kernel HOT 2
- Should we be using map instead of multimap for headers? HOT 1
- glaceful close ssl socket HOT 3
- x32 libs and x64 includes HOT 3
- How do you get the port number and detect when a connection has closed? HOT 2
- C++17 string_view HOT 8
- httpclient :Can i read the response in the same thread which sent request. HOT 1
- http_Server revice unidentifiable Chinese code? HOT 3
- "accept: Already open" error when using HTTP-Client within cucumber test framework HOT 2
- multithreads in http HOT 25
- C2039 'tlsv12': is not a member of 'boost::asio::ssl::context' server_https.hpp HOT 8
- The Networking TS HOT 1
- HttpClient::request() HOT 1
- On timeout in client, the error code is operation_aborted instead of the more appropriate asio::error::timed_out HOT 2
- 【warning C4996】Why is there such a warning? HOT 5
- Http client with partial results HOT 2
- SSL Pinning
- Multi-part support HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simple-web-server.