Lots of fixes, begin to think about timeouts

This commit is contained in:
Alex Hultman
2018-07-02 23:13:55 +02:00
parent 441d8bc4b4
commit a3dd3ef958
5 changed files with 67 additions and 19 deletions
+31
View File
@@ -71,6 +71,20 @@ protected:
if (appData->onHttpConnection) {
appData->onHttpConnection((HttpSocket<SSL> *) s);
}
// we should already be linked!
//static_dispatch(us_ssl_socket_context_link, us_socket_context_link)(httpServerContext, s);
// this should absolutely not be exposed like this!
// fix up timers!
// this goes hand in hand with fixing up shutdown also!
if constexpr (!SSL) {
us_socket_context_link(us_socket_get_context(s), s);
}
// start a timeout on this socket of 10 seconds
std::cout << "Arming socket timeout" << std::endl;
static_dispatch(us_ssl_socket_timeout, us_socket_timeout)(s, 10);
});
static_dispatch(us_ssl_socket_context_on_close, us_socket_context_on_close)(httpServerContext, [](auto *s) {
@@ -97,6 +111,10 @@ protected:
static_dispatch(us_ssl_socket_context_on_writable, us_socket_context_on_writable)(httpServerContext, [](auto *s) {
((HttpSocket<SSL> *) s)->onWritable();
});
static_dispatch(us_ssl_socket_context_on_timeout, us_socket_context_on_timeout)(httpServerContext, [](auto *s) {
std::cout << "Some socket timed out!" << std::endl;
});
}
public:
@@ -122,6 +140,19 @@ public:
return *this;
}
// why even bother with these?
AppBase &onHttpConnection(std::function<void(HttpSocket<SSL> *)> handler) {
data->onHttpConnection = handler;
return *this;
}
AppBase &onHttpDisconnection(std::function<void(HttpSocket<SSL> *)> handler) {
data->onHttpDisconnection = handler;
return *this;
}
// for client and server
AppBase &onWebSocket(std::string pattern, std::function<void()> handler) {
return *this;
+6 -1
View File
@@ -158,7 +158,12 @@ public:
}
void route(const char *method, unsigned int method_length, const char *url, unsigned int url_length, USERDATA userData) {
handlers[lookup(url, url_length)](userData, &params);
int index = lookup(url, url_length);
if (index != -1) {
handlers[index](userData, &params);
}
params.clear();
}
};
+2 -2
View File
@@ -133,7 +133,7 @@ struct HttpSocket {
// write that off!
if constexpr (SSL) {
httpData->offset = us_ssl_socket_write((SOCKET_TYPE *) this, chunk.data(), chunk.length());
} else {
httpData->offset = us_socket_write((SOCKET_TYPE *) this, chunk.data(), chunk.length(), 0);
}
@@ -155,7 +155,7 @@ struct HttpSocket {
// write that off!
if constexpr (SSL) {
httpData->offset += us_ssl_socket_write((SOCKET_TYPE *) this, chunk.data(), chunk.length());
} else {
httpData->offset += us_socket_write((SOCKET_TYPE *) this, chunk.data(), chunk.length(), 0);
}
+1 -1
View File
@@ -13,7 +13,7 @@ struct Loop {
us_loop *loop;
static const int CORK_BUFFER_SIZE = 16 * 1024;
static const int MAX_COPY_DISTANCE = 4 * 1024;
static const int MAX_COPY_DISTANCE = 4096;
struct Data {