Fix up async file streaming

This commit is contained in:
Alex Hultman
2018-09-29 19:42:35 +02:00
parent c79f68605a
commit 5a1de2039c
6 changed files with 143 additions and 127 deletions
+31 -22
View File
@@ -57,34 +57,37 @@ protected:
AsyncSocketData<SSL> *asyncSocketData = (AsyncSocketData<SSL> *) getExt();
/* Do nothing for a null sized chunk */
if (length == 0 && !asyncSocketData->buffer.length()) {
//std::cout << "Write returned: 0" << std::endl;
return 0;
}
/* Do not write anything if we have a per-socket buffer */
/* We are limited if we have a per-socket buffer */
if (asyncSocketData->buffer.length()) {
std::cout << "WHAT THE FUCK WE HAVE BUFFER!" << std::endl;
// probably want to swap the hierarchy here to: if buffer, if length, if optionally
/* We only try and drain if we are in optional mode */
if (optionally) {
// we have buffer and we are optionally, if drain then drain else quit
// drain here
std::cout << "Drain path" << std::endl;
// will just end up in a loop!
int written = static_dispatch(us_ssl_socket_write, us_socket_write)((SOCKET_TYPE *) this, asyncSocketData->buffer.data(), asyncSocketData->buffer.length(), nextLength != 0);//write(asyncSocketData->buffer.data(), asyncSocketData->buffer.length(), optionally, 0, true);
// removeBuffer
asyncSocketData->buffer = asyncSocketData->buffer.substr(written);
// should we really return this here? should be 0 as we took 0 new data!
return 0;
/* Is this a merge drain or not? */
if (length) {
// merge drain may happen if the developer continues to write data despite having buffered up content
// for now we do not support this yet
return 0;
} else {
/* Write off as much as we can */
int written = static_dispatch(us_ssl_socket_write, us_socket_write)((SOCKET_TYPE *) this, asyncSocketData->buffer.data(), asyncSocketData->buffer.length(), nextLength != 0);
/* Update buffering (should probably have different allocation strategies here) */
asyncSocketData->buffer = asyncSocketData->buffer.substr(written);
/* We consumed no new data */
return 0;
}
} else {
/* Currently there is no drainage for non-optional writes */
std::cout << "Buffering at top of write (really bad)!" << std::endl;
/* At least we can reserve room for next chunk if we know it up front */
@@ -99,6 +102,12 @@ protected:
}
}
/* Do nothing for a null sized chunk */
if (!length) {
std::cout << "Trying to write 0 length!" << std::endl;
return 0;
}
if (loopData->corked) {
/* We are corked */
if (LoopData::CORK_BUFFER_SIZE - loopData->corkOffset >= length) {
@@ -130,7 +139,7 @@ protected:
if (written < length) {
/* If the write was optional then just bail out */
if (optionally) {
//std::cout << "Write returned: " << written << std::endl;
std::cout << "Write returned: " << written << std::endl;
return written;
}
@@ -147,7 +156,7 @@ protected:
}
}
//std::cout << "Write returned: " << length << std::endl;
std::cout << "Write returned: " << length << std::endl;
return length;
}
+17 -27
View File
@@ -60,15 +60,15 @@ private:
/* Get socket ext */
HttpResponseData<SSL> *httpResponseData = (HttpResponseData<SSL> *) static_dispatch(us_ssl_socket_ext, us_socket_ext)(s);
/* Signal broken out stream */
if (httpResponseData->outStream) {
httpResponseData->outStream(-1);
std::cout << "Close event!" << std::endl;
/* Signal broken HTTP request */
if (httpResponseData->onAborted) {
httpResponseData->onAborted();
}
/* Signal broken in stream */
if (httpResponseData->inStream) {
httpResponseData->inStream(std::string_view(nullptr, 0));
}
// we might want to also signal the read stream?
// smash onAborted together with read?
/* Destruct socket ext */
httpResponseData->~HttpResponseData<SSL>();
@@ -132,11 +132,10 @@ private:
return s;
});
/* Handle HTTP write out */
/* Handle HTTP write out (note: SSL_read may trigger this spuriously, the app need to handle spurious calls) */
static_dispatch(us_ssl_socket_context_on_writable, us_socket_context_on_writable)(getSocketContext(), [](auto *s) {
std::cout << "Writable event!" << std::endl;
std::cout << "HttpContext::onWritable event fired!" << std::endl;
/* Writing data should reset the timeout */
static_dispatch(us_ssl_socket_timeout, us_socket_timeout)(s, HTTP_IDLE_TIMEOUT_S);
@@ -146,33 +145,24 @@ private:
// if this, then it means it finished with no issues so we need to empty any buffers?
if (httpResponseData->onWritable) {
httpResponseData->onWritable(httpResponseData->offset);
} else {
// lets drain here
std::cout << "LEts drain!" << std::endl;
/* We expect the developer to return whether or not write was successful (true) */
bool success = httpResponseData->onWritable(httpResponseData->offset);
// mergeDrain
// on writable should return whether it wants more data or not
// but we don't need to know that here? we cannot drain because a sucessful write should mean there is no buffer to drain
} else {
/* This is used to drain any buffers we might have */
asyncSocket->write(nullptr, 0, true, 0);
}
// bascially just empty the buffer and if successful also call onWritable (bad strategy!)
//asyncSocket->mergeDrain();
// what we want is to immediately call onWritable and have AsyncSocket::write calls always try and empty any buffers at the same time?
// AsyncSocket::write can take boolean drain = true to know it should try and drain the buffers according to whatever strategy
// mergeDrain is basically AsyncSocket::write with boolean drain = true!
// on writable should return whether it wants more data or not?
return s;
});
/* Handle FIN, HTTP does not support half-closed sockets, so simply close */
static_dispatch(us_ssl_socket_context_on_end, us_socket_context_on_end)(getSocketContext(), [](auto *s) {
std::cout << "FIN sent" << std::endl;
/* We do not care for half closed sockets */
AsyncSocket<SSL> *asyncSocket = (AsyncSocket<SSL> *) s;
return asyncSocket->close();
+17 -4
View File
@@ -156,7 +156,7 @@ public:
if (!(httpResponseData->state & HttpResponseData<SSL>::HTTP_END_CALLED)) {
/* We have a known send size */
Super::write("Content-Length: ", 16);
writeUnsigned(data.length());
writeUnsigned(/*data.length()*/totalSize);
Super::write("\r\n\r\n", 4);
/* Mark end called */
@@ -164,14 +164,27 @@ public:
}
/* Write as much as possible without causing backpressure */
httpResponseData->offset += Super::write(data.data(), data.length(), true);
int written = Super::write(data.data(), data.length(), true);
httpResponseData->offset += written;
std::cout << "Offset is now: " << httpResponseData->offset << std::endl;
return written == data.length();
}
return httpResponseData->offset == totalSize;
// this path is completely wrong!
std::cout << "tryEnd returning " << (httpResponseData->offset == /*totalSize*/ data.length()) << std::endl;
return httpResponseData->offset == /*totalSize*/ data.length();
}
int getWriteOffset() {
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
return httpResponseData->offset;
}
/* Attach handler for writable HTTP response */
HttpResponse *onWritable(std::function<void(int)> handler) {
HttpResponse *onWritable(std::function<bool(int)> handler) {
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
httpResponseData->onWritable = handler;
+2 -1
View File
@@ -24,7 +24,8 @@ private:
};
/* Per socket event handlers */
std::function<void(int)> onWritable;
std::function<bool(int)> onWritable;
std::function<void()> onAborted;
//std::function<void()> onData;
std::function<void(std::string_view)> inStream;