66
77namespace SC
88{
9+
10+ Result HttpAsyncServer::initInternal (AsyncBuffersPool& pool, SpanWithStride<HttpConnection> connectionsSpan,
11+ Span<char > headersMemory)
12+ {
13+ // TODO: Add some validation of minimum sizes for the queues and the buffers
14+ SC_TRY (connections.init (connectionsSpan, headersMemory));
15+ buffersPool = &pool;
16+ return Result (true );
17+ }
18+
919Result HttpAsyncServer::start (AsyncEventLoop& loop, StringSpan address, uint16_t port)
1020{
11- SC_TRY_MSG (memory , " HttpAsyncServer::start - init not called" );
21+ SC_TRY_MSG (buffersPool != nullptr , " HttpAsyncServer::start - init not called" );
1222 SocketIPAddress nativeAddress;
1323 SC_TRY (nativeAddress.fromAddressPort (address, port));
1424 eventLoop = &loop;
@@ -24,31 +34,11 @@ Result HttpAsyncServer::start(AsyncEventLoop& loop, StringSpan address, uint16_t
2434 return Result (true );
2535}
2636
27- Result HttpAsyncServer::init (AsyncBuffersPool& pool, Span<HttpConnection> clients, Span<char > headersMemory,
28- Span<AsyncReadableStream::Request> readQueue,
29- Span<AsyncWritableStream::Request> writeQueue)
30- {
31-
32- // TODO: Add some validation of minimum sizes for the queues and the buffers
33- SC_TRY (connections.init (clients, headersMemory));
34- readQueues = readQueue;
35- writeQueues = writeQueue;
36-
37- buffersPool = &pool;
38-
39- memory = true ;
40- return Result (true );
41- }
42-
4337Result HttpAsyncServer::close ()
4438{
4539 SC_TRY (waitForStopToFinish ());
4640 SC_TRY (connections.close ());
47- readQueues = {};
48- writeQueues = {};
4941 buffersPool = nullptr ;
50-
51- memory = false ;
5242 return Result (true );
5343}
5444
@@ -64,7 +54,7 @@ Result HttpAsyncServer::stop()
6454
6555 for (size_t idx = 0 ; idx < connections.getNumTotalConnections (); ++idx)
6656 {
67- HttpConnection & client = connections.getConnectionAt (idx);
57+ HttpAsyncConnectionBase & client = static_cast <HttpAsyncConnectionBase&>( connections.getConnectionAt (idx) );
6858 // Destroy can be safely called in any state (including already destroyed)
6959 client.readableSocketStream .destroy ();
7060 client.writableSocketStream .destroy ();
@@ -89,7 +79,7 @@ Result HttpAsyncServer::waitForStopToFinish()
8979 checkAgainAllClients = false ;
9080 for (size_t idx = 0 ; idx < connections.getNumTotalConnections (); ++idx)
9181 {
92- HttpConnection & client = connections.getConnectionAt (idx);
82+ HttpAsyncConnectionBase & client = static_cast <HttpAsyncConnectionBase&>( connections.getConnectionAt (idx) );
9383 while (not client.readableSocketStream .request .isFree () or not client.writableSocketStream .request .isFree ())
9484 {
9585 SC_TRY (eventLoop->runNoWait ());
@@ -113,23 +103,13 @@ void HttpAsyncServer::onNewClient(AsyncSocketAccept::Result& result)
113103 // Activation always succeeds because we pause asyncAccept when the there are not available clients
114104 SC_ASSERT_RELEASE (connections.activateNew (idx));
115105
116- HttpConnection& client = connections.getConnection (idx);
117- client.socket = move (acceptedClient);
118- const size_t readQueueLen = readQueues.sizeInElements () / connections.getNumTotalConnections ();
119- const size_t writeQueueLen = writeQueues.sizeInElements () / connections.getNumTotalConnections ();
120- SC_TRUST_RESULT (readQueueLen > 0 );
121- SC_TRUST_RESULT (writeQueueLen > 0 );
122- Span<AsyncReadableStream::Request> readQueue;
123- Span<AsyncWritableStream::Request> writeQueue;
124- SC_TRUST_RESULT (readQueues.sliceStartLength (idx.getIndex () * readQueueLen, readQueueLen, readQueue));
125- SC_TRUST_RESULT (writeQueues.sliceStartLength (idx.getIndex () * writeQueueLen, writeQueueLen, writeQueue));
126- client.readableSocketStream .setReadQueue (readQueue);
127- client.writableSocketStream .setWriteQueue (writeQueue);
106+ HttpAsyncConnectionBase& client = static_cast <HttpAsyncConnectionBase&>(connections.getConnection (idx));
107+ client.socket = move (acceptedClient);
128108 SC_TRUST_RESULT (client.readableSocketStream .init (*buffersPool, *eventLoop, client.socket ));
129109 SC_TRUST_RESULT (client.writableSocketStream .init (*buffersPool, *eventLoop, client.socket ));
130110
131111 auto onData = [this , idx](AsyncBufferView::ID bufferID)
132- { onStreamReceive (connections.getConnection (idx), bufferID); };
112+ { onStreamReceive (static_cast <HttpAsyncConnectionBase&>( connections.getConnection (idx) ), bufferID); };
133113 SC_TRUST_RESULT (client.readableSocketStream .eventData .addListener (onData));
134114 SC_TRUST_RESULT (client.readableSocketStream .start ());
135115
@@ -139,7 +119,7 @@ void HttpAsyncServer::onNewClient(AsyncSocketAccept::Result& result)
139119 result.reactivateRequest (connections.getNumActiveConnections () < connections.getNumTotalConnections ());
140120}
141121
142- void HttpAsyncServer::onStreamReceive (HttpConnection & client, AsyncBufferView::ID bufferID)
122+ void HttpAsyncServer::onStreamReceive (HttpAsyncConnectionBase & client, AsyncBufferView::ID bufferID)
143123{
144124 Span<char > readData;
145125 SC_ASSERT_RELEASE (buffersPool->getWritableData (bufferID, readData));
@@ -159,8 +139,8 @@ void HttpAsyncServer::onStreamReceive(HttpConnection& client, AsyncBufferView::I
159139 // Using a struct instead of a lambda so it can unregister itself
160140 struct AfterWrite
161141 {
162- HttpAsyncServer& pself;
163- HttpConnection& client;
142+ HttpAsyncServer& pself;
143+ HttpAsyncConnectionBase& client;
164144
165145 void operator ()()
166146 {
@@ -172,7 +152,7 @@ void HttpAsyncServer::onStreamReceive(HttpConnection& client, AsyncBufferView::I
172152 }
173153}
174154
175- void HttpAsyncServer::closeAsync (HttpConnection & client)
155+ void HttpAsyncServer::closeAsync (HttpAsyncConnectionBase & client)
176156{
177157 if (client.state == HttpConnection::State::Inactive)
178158 {
0 commit comments