Add comments and AsyncSocketData
This commit is contained in:
@@ -26,7 +26,8 @@ HEADERS += \
|
|||||||
src/new_design/HttpResponse.h \
|
src/new_design/HttpResponse.h \
|
||||||
src/new_design/StaticDispatch.h \
|
src/new_design/StaticDispatch.h \
|
||||||
src/new_design/LoopData.h \
|
src/new_design/LoopData.h \
|
||||||
src/new_design/AsyncSocket.h
|
src/new_design/AsyncSocket.h \
|
||||||
|
src/new_design/AsyncSocketData.h
|
||||||
|
|
||||||
INCLUDEPATH += uSockets/src src
|
INCLUDEPATH += uSockets/src src
|
||||||
#QMAKE_CXXFLAGS += -fsanitize=address
|
#QMAKE_CXXFLAGS += -fsanitize=address
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#ifndef HUB_H
|
#ifndef HUB_H
|
||||||
#define HUB_H
|
#define HUB_H
|
||||||
|
|
||||||
|
// this header needs fixing! should not depend on source files!
|
||||||
|
|
||||||
#include "new_design/LoopData.h"
|
#include "new_design/LoopData.h"
|
||||||
|
|
||||||
#include <libusockets.h>
|
#include <libusockets.h>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "StaticDispatch.h"
|
#include "StaticDispatch.h"
|
||||||
#include "LoopData.h"
|
#include "LoopData.h"
|
||||||
|
#include "AsyncSocketData.h"
|
||||||
|
|
||||||
// todo: this is where the magic happens
|
// todo: this is where the magic happens
|
||||||
|
|
||||||
@@ -10,18 +11,14 @@ namespace uWS {
|
|||||||
|
|
||||||
template <bool SSL>
|
template <bool SSL>
|
||||||
struct AsyncSocket : StaticDispatch<SSL> {
|
struct AsyncSocket : StaticDispatch<SSL> {
|
||||||
|
protected:
|
||||||
|
// this will probably be different on ssl and non-ssl
|
||||||
|
static const int MAX_COPY_DISTANCE = 4096;
|
||||||
|
|
||||||
using SOCKET_TYPE = typename StaticDispatch<SSL>::SOCKET_TYPE;
|
using SOCKET_TYPE = typename StaticDispatch<SSL>::SOCKET_TYPE;
|
||||||
using StaticDispatch<SSL>::static_dispatch;
|
using StaticDispatch<SSL>::static_dispatch;
|
||||||
|
|
||||||
// control everything with write, cork, uncork, close
|
// this will have to belong here for now
|
||||||
// have HttpResponseData derive from AsyncSocketData?
|
|
||||||
|
|
||||||
// HttpResponse will only need one buffer for outgoing - the corked up
|
|
||||||
|
|
||||||
// maybe better name would be CorkableSocket?
|
|
||||||
|
|
||||||
// this does not belong here!
|
|
||||||
int u32toa(uint32_t value, char *dst) {
|
int u32toa(uint32_t value, char *dst) {
|
||||||
char temp[10];
|
char temp[10];
|
||||||
char *p = temp;
|
char *p = temp;
|
||||||
@@ -39,15 +36,43 @@ struct AsyncSocket : StaticDispatch<SSL> {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cork() {
|
LoopData *getLoopData() {
|
||||||
LoopData *loopData = (LoopData *) us_loop_ext(us_socket_context_loop(us_socket_get_context((us_socket *) this)));
|
return (LoopData *) us_loop_ext(us_socket_context_loop(us_socket_get_context((SOCKET_TYPE *) this)));
|
||||||
|
|
||||||
loopData->corked = true;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *getExt() {
|
||||||
|
return static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
// cork should bascially mark corked
|
||||||
|
// if already corked, crash and burn!
|
||||||
|
void cork() {
|
||||||
|
LoopData *loopData = getLoopData();
|
||||||
|
loopData->corked = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if not in corked mode, write & buffer. if in corked mode: either write & buffer or buffer
|
||||||
|
// that is, corking is optional
|
||||||
|
void write(const char *src, int length) {
|
||||||
|
LoopData *loopData = getLoopData();
|
||||||
|
|
||||||
|
// append it
|
||||||
|
memcpy(loopData->corkBuffer + loopData->corkOffset, src, length);
|
||||||
|
loopData->corkOffset += length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// should follow same rules as for write
|
||||||
|
void writeUnsigned(unsigned int value) {
|
||||||
|
LoopData *loopData = getLoopData();
|
||||||
|
|
||||||
|
loopData->corkOffset += u32toa(value, loopData->corkBuffer + loopData->corkOffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
// uncork should always send and clean the loop's cork buffer. anything that is not able to send, buffer it up in the socket
|
||||||
void uncork() {
|
void uncork() {
|
||||||
LoopData *loopData = (LoopData *) us_loop_ext(us_socket_context_loop(us_socket_get_context((us_socket *) this)));
|
LoopData *loopData = getLoopData();
|
||||||
|
|
||||||
loopData->corked = false;
|
loopData->corked = false;
|
||||||
|
|
||||||
@@ -60,19 +85,12 @@ struct AsyncSocket : StaticDispatch<SSL> {
|
|||||||
// buffer the rest up in the outbuffer of this asynsocket!
|
// buffer the rest up in the outbuffer of this asynsocket!
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeUnsigned(unsigned int value) {
|
// when we are writable AND have buffer length, that's a really bad situation that should not happen!
|
||||||
LoopData *loopData = (LoopData *) us_loop_ext(us_socket_context_loop(us_socket_get_context((us_socket *) this)));
|
void drain() {
|
||||||
|
|
||||||
loopData->corkOffset += u32toa(value, loopData->corkBuffer + loopData->corkOffset);
|
|
||||||
}
|
|
||||||
|
|
||||||
void write(const char *src, int length) {
|
|
||||||
LoopData *loopData = (LoopData *) us_loop_ext(us_socket_context_loop(us_socket_get_context((us_socket *) this)));
|
|
||||||
|
|
||||||
memcpy(loopData->corkBuffer + loopData->corkOffset, src, length);
|
|
||||||
loopData->corkOffset += length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this one will write up to length and simply leave things be
|
||||||
int writeOptionally(const char *src, int length) {
|
int writeOptionally(const char *src, int length) {
|
||||||
|
|
||||||
// not optional for now
|
// not optional for now
|
||||||
@@ -111,10 +129,6 @@ struct AsyncSocket : StaticDispatch<SSL> {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void *getExt() {
|
|
||||||
return static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void close() {
|
void close() {
|
||||||
static_dispatch(us_ssl_socket_close, us_socket_close)((SOCKET_TYPE *) this);
|
static_dispatch(us_ssl_socket_close, us_socket_close)((SOCKET_TYPE *) this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef ASYNCSOCKETDATA_H
|
||||||
|
#define ASYNCSOCKETDATA_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
// todo: think about chains of AsyncSocketData too!
|
||||||
|
// we want to buffer things up in one buffer, or in many separate ones (like with websockets)
|
||||||
|
|
||||||
|
template <bool SSL>
|
||||||
|
struct AsyncSocketData {
|
||||||
|
|
||||||
|
// we need a buffer
|
||||||
|
std::string buffer;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // ASYNCSOCKETDATA_H
|
||||||
@@ -4,12 +4,15 @@
|
|||||||
/* This data belongs to the HttpResponse */
|
/* This data belongs to the HttpResponse */
|
||||||
|
|
||||||
#include "HttpParser.h"
|
#include "HttpParser.h"
|
||||||
|
#include "AsyncSocketData.h"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
namespace uWS {
|
namespace uWS {
|
||||||
|
|
||||||
template <bool SSL>
|
template <bool SSL>
|
||||||
struct HttpResponseData : HttpParser {
|
struct HttpResponseData : HttpParser, AsyncSocketData<SSL> {
|
||||||
|
|
||||||
|
// asyncsocketdata will hold the outgoing buffer to hold the header if not sent off in one go
|
||||||
|
|
||||||
// inStream, outStream
|
// inStream, outStream
|
||||||
std::function<void(std::string_view)> inStream;
|
std::function<void(std::string_view)> inStream;
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ private:
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
static const int CORK_BUFFER_SIZE = 16 * 1024;
|
static const int CORK_BUFFER_SIZE = 16 * 1024;
|
||||||
static const int MAX_COPY_DISTANCE = 4096;
|
|
||||||
|
|
||||||
char *corkBuffer = new char[CORK_BUFFER_SIZE];
|
char *corkBuffer = new char[CORK_BUFFER_SIZE];
|
||||||
int corkOffset = 0;
|
int corkOffset = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user