Open
Description
All messages passing from client to server and back are very slow....about a second for one round trip. These should be less than a millisecond. You don't need a time stamp to see the delay because it is obvious when you see the messages come up in the log display.
I am using version 0.5.5
I am running on Visual Studio 2019, x64, both debug and release modes.
To demonstrate this I wrote a pair of routines that you can use to see the problem by hacking up your client and server examples.
In one Command prompt window, run the server. Then in another Command prompt window run the client.
Here is the Client
//
// client.cc
//
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
// MIT License
#include <httplib.h>
#include <iostream>
using namespace std;
int main(void) {
cout << "starting client." << endl;
httplib::Client cli("localhost", 8050);
// GET /hi
for (size_t i = 0; i < 5; i++) {
cout << "GET /hi" << endl;
auto res = cli.Get("/hi");
if (res) {
cout << res->status << endl;
cout << res->get_header_value("Content-Type") << endl;
cout << res->body << endl;
} else {
cout << "crashed";
return 1;
}
}
// POST /network
std::string config = R"(
{network: [
{addRegion: {name: "encoder", type: "RDSERegion", params: {size: 1000, sparsity: 0.2, radius: 0.03, seed: 2019, noise: 0.01}}},
{addRegion: {name: "sp", type: "SPRegion", params: {columnCount: 2048, globalInhibition: true}}},
{addRegion: {name: "tm", type: "TMRegion", params: {cellsPerColumn: 8, orColumnOutputs: true}}},
{addLink: {src: "encoder.encoded", dest: "sp.bottomUpIn"}},
{addLink: {src: "sp.bottomUpOut", dest: "tm.bottomUpIn"}}
]})";
cout << "POST /network" << endl;
auto res = cli.Post("/network", config, "application/json");
if (!res || res->status / 100 != 2)
cout << "Failed Response to POST /network request." << endl;
else
cout << res->body << endl;
return 0;
}
Here is the server:
//
// sample.cc
//
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
// MIT License
//
#include <chrono>
#include <cstdio>
#include <iostream>
#include <httplib.h>
#define PORT 8050
using namespace httplib;
std::string dump_headers(const Headers &headers) {
std::string s;
char buf[BUFSIZ];
for (auto it = headers.begin(); it != headers.end(); ++it) {
const auto &x = *it;
snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
s += buf;
}
return s;
}
std::string log(const Request &req, const Response &res) {
std::string s;
char buf[BUFSIZ];
s += "================================\n";
snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
req.version.c_str(), req.path.c_str());
s += buf;
std::string query;
for (auto it = req.params.begin(); it != req.params.end(); ++it) {
const auto &x = *it;
snprintf(buf, sizeof(buf), "%c%s=%s",
(it == req.params.begin()) ? '?' : '&', x.first.c_str(),
x.second.c_str());
query += buf;
}
snprintf(buf, sizeof(buf), "%s\n", query.c_str());
s += buf;
s += dump_headers(req.headers);
if (!req.body.empty()) { s += "Body: " + req.body; }
s += "--------------------------------\n";
snprintf(buf, sizeof(buf), "%d %s\n", res.status, res.version.c_str());
s += buf;
s += dump_headers(res.headers);
s += "\n";
if (!res.body.empty()) { s += "Body: " + res.body; }
s += "\n";
return s;
}
int main(void) {
Server svr;
if (!svr.is_valid()) {
std::cout << "server has an error...\n";
return -1;
}
svr.Get("/", [=](const Request & /*req*/, Response &res) {
res.set_redirect("/hi");
});
// GET /hi
svr.Get("/hi", [](const Request & /*req*/, Response &res) {
res.set_content("Hello World!\n", "text/plain");
});
// POST /network
// Configure a Network resource (with JSON configuration in POST body) ==> token
svr.Post("/network", [&](const Request &req, Response &res, const ContentReader &content_reader) {
content_reader([&](const char *data, size_t data_length) {
res.body.append(data, data_length);
return true;
});
// here is where I would normally create the resource.
std::cout << "Creating resource /network with configuration: " << res.body << std::endl;
std::string token = "0001";
res.set_content(token+"\n", "text/plain");
});
svr.Get("/stop", [&](const Request & /*req*/, Response & /*res*/) { svr.stop(); });
svr.set_error_handler([](const Request & /*req*/, Response &res) {
const char *fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), fmt, res.status);
res.set_content(buf, "text/html");
});
svr.set_logger([](const Request &req, const Response &res) {
std::cout << log(req, res);
});
std::cout << "Starting Server." << std::endl;
svr.listen("127.0.0.1", PORT);
std::cout << "Server stopped." << std::endl;
return 0;
}