|
| 1 | +// Copyright (c) Stefano Cristiano |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +#include "HttpAsyncServer.h" |
| 4 | +#include "../Foundation/Deferred.h" |
| 5 | + |
| 6 | +namespace SC |
| 7 | +{ |
| 8 | +Result HttpAsyncServer::start(AsyncEventLoop& loop, StringSpan address, uint16_t port, HttpServer::Memory& memory) |
| 9 | +{ |
| 10 | + SocketIPAddress nativeAddress; |
| 11 | + SC_TRY(nativeAddress.fromAddressPort(address, port)); |
| 12 | + eventLoop = &loop; |
| 13 | + SC_TRY(eventLoop->createAsyncTCPSocket(nativeAddress.getAddressFamily(), serverSocket)); |
| 14 | + SocketServer socketServer(serverSocket); |
| 15 | + SC_TRY(socketServer.bind(nativeAddress)); |
| 16 | + SC_TRY(socketServer.listen(511)); |
| 17 | + |
| 18 | + asyncServerAccept.setDebugName("HttpServer"); |
| 19 | + asyncServerAccept.callback.bind<HttpAsyncServer, &HttpAsyncServer::onNewClient>(*this); |
| 20 | + SC_TRY(asyncServerAccept.start(*eventLoop, serverSocket)); |
| 21 | + started = true; |
| 22 | + |
| 23 | + return httpServer.start(memory); |
| 24 | +} |
| 25 | + |
| 26 | +Result HttpAsyncServer::stopAsync() |
| 27 | +{ |
| 28 | + stopping = true; |
| 29 | + auto deferStop = MakeDeferred([this]() { stopping = false; }); |
| 30 | + if (not asyncServerAccept.isFree()) |
| 31 | + { |
| 32 | + SC_TRY(asyncServerAccept.stop(*eventLoop)); |
| 33 | + } |
| 34 | + |
| 35 | + for (HttpServerClient& it : httpServer.clients) |
| 36 | + { |
| 37 | + if (it.state != HttpServerClient::State::Free) |
| 38 | + { |
| 39 | + closeAsync(it); |
| 40 | + } |
| 41 | + } |
| 42 | + return Result(true); |
| 43 | +} |
| 44 | + |
| 45 | +Result HttpAsyncServer::stopSync() |
| 46 | +{ |
| 47 | + stopping = true; |
| 48 | + auto deferStop = MakeDeferred([this]() { stopping = false; }); |
| 49 | + SC_TRY(stopAsync()); |
| 50 | + while (httpServer.getNumClients() > 0) |
| 51 | + { |
| 52 | + SC_TRY(eventLoop->runNoWait()); |
| 53 | + } |
| 54 | + while (not asyncServerAccept.isFree()) |
| 55 | + { |
| 56 | + SC_TRY(eventLoop->runNoWait()); |
| 57 | + } |
| 58 | + started = false; |
| 59 | + return Result(true); |
| 60 | +} |
| 61 | + |
| 62 | +void HttpAsyncServer::onNewClient(AsyncSocketAccept::Result& result) |
| 63 | +{ |
| 64 | + SocketDescriptor acceptedClient; |
| 65 | + if (not result.moveTo(acceptedClient)) |
| 66 | + { |
| 67 | + // TODO: Invoke an error |
| 68 | + return; |
| 69 | + } |
| 70 | + size_t idx = 0; |
| 71 | + // Allocate always succeeds because we pause asyncAccept when the arena is full |
| 72 | + SC_ASSERT_RELEASE(httpServer.allocateClient(idx)); |
| 73 | + |
| 74 | + HttpServerClient& client = httpServer.clients[idx]; |
| 75 | + |
| 76 | + client.socket = move(acceptedClient); |
| 77 | + client.asyncSend.setDebugName(client.debugName); |
| 78 | + client.asyncReceive.setDebugName(client.debugName); |
| 79 | + client.asyncReceive.callback.bind<HttpAsyncServer, &HttpAsyncServer::onReceive>(*this); |
| 80 | + |
| 81 | + // This cannot fail because start reports only incorrect API usage (AsyncRequest already in use etc.) |
| 82 | + SC_TRUST_RESULT(client.asyncReceive.start(*eventLoop, client.socket, client.request.availableHeader)); |
| 83 | + |
| 84 | + // Only reactivate asyncAccept if arena is not full (otherwise it's being reactivated in closeAsync) |
| 85 | + result.reactivateRequest(httpServer.canAcceptMoreClients()); |
| 86 | +} |
| 87 | + |
| 88 | +void HttpAsyncServer::onReceive(AsyncSocketReceive::Result& result) |
| 89 | +{ |
| 90 | + SC_COMPILER_WARNING_PUSH_OFFSETOF |
| 91 | + HttpServerClient& client = SC_COMPILER_FIELD_OFFSET(HttpServerClient, asyncReceive, result.getAsync()); |
| 92 | + SC_COMPILER_WARNING_POP |
| 93 | + SC_ASSERT_RELEASE(&client.asyncReceive == &result.getAsync()); |
| 94 | + Span<char> readData; |
| 95 | + if (not result.get(readData)) |
| 96 | + { |
| 97 | + // TODO: Invoke on error |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const size_t idx = static_cast<size_t>(&client - &httpServer.clients[0]); |
| 102 | + Span<char> outspan = httpServer.processClientReceivedData(idx, readData); |
| 103 | + if (not outspan.empty()) |
| 104 | + { |
| 105 | + client.asyncSend.setDebugName(client.debugName); |
| 106 | + client.asyncSend.callback.bind<HttpAsyncServer, &HttpAsyncServer::onAfterSend>(*this); |
| 107 | + auto res = client.asyncSend.start(*eventLoop, client.socket, outspan); |
| 108 | + if (not res) |
| 109 | + { |
| 110 | + // TODO: Invoke on error |
| 111 | + return; |
| 112 | + } |
| 113 | + } |
| 114 | + else if (not result.completionData.disconnected) |
| 115 | + { |
| 116 | + result.reactivateRequest(true); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +void HttpAsyncServer::onAfterSend(AsyncSocketSend::Result& result) |
| 121 | +{ |
| 122 | + if (result.isValid()) |
| 123 | + { |
| 124 | + SC_COMPILER_WARNING_PUSH_OFFSETOF |
| 125 | + HttpServerClient& requestClient = SC_COMPILER_FIELD_OFFSET(HttpServerClient, asyncSend, result.getAsync()); |
| 126 | + SC_COMPILER_WARNING_POP |
| 127 | + closeAsync(requestClient); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +void HttpAsyncServer::closeAsync(HttpServerClient& requestClient) |
| 132 | +{ |
| 133 | + if (not requestClient.asyncSend.isFree()) |
| 134 | + { |
| 135 | + (void)requestClient.asyncSend.stop(*eventLoop); |
| 136 | + } |
| 137 | + if (not requestClient.asyncReceive.isFree()) |
| 138 | + { |
| 139 | + (void)requestClient.asyncReceive.stop(*eventLoop); |
| 140 | + } |
| 141 | + SC_TRUST_RESULT(requestClient.socket.close()); |
| 142 | + const bool wasFull = not httpServer.canAcceptMoreClients(); |
| 143 | + |
| 144 | + SC_TRUST_RESULT(httpServer.deallocateClient(requestClient)); |
| 145 | + if (wasFull and not stopping) |
| 146 | + { |
| 147 | + // Arena was full and in onNewClient asyncAccept has been paused (by avoiding reactivation). |
| 148 | + // Now a slot has been freed so it's possible to start accepting clients again. |
| 149 | + SC_TRUST_RESULT(asyncServerAccept.start(*eventLoop, serverSocket)); |
| 150 | + } |
| 151 | +} |
| 152 | +} // namespace SC |
0 commit comments