diff options
| author | Per Larsson <[email protected]> | 2022-03-23 14:03:01 +0100 |
|---|---|---|
| committer | Per Larsson <[email protected]> | 2022-03-23 14:03:01 +0100 |
| commit | a1c5a93627fefa2db6dd3ae620e112ba46065f78 (patch) | |
| tree | 90ae9bf52a10c1ecfcdd85c47fc89ceb22795162 /zenhttp | |
| parent | Map derived data references. (diff) | |
| parent | Added route '/prj/list' for retrieving project info. (diff) | |
| download | zen-a1c5a93627fefa2db6dd3ae620e112ba46065f78.tar.xz zen-a1c5a93627fefa2db6dd3ae620e112ba46065f78.zip | |
Merge branch 'main' into ddcref
Diffstat (limited to 'zenhttp')
| -rw-r--r-- | zenhttp/httpserver.cpp | 16 | ||||
| -rw-r--r-- | zenhttp/httpshared.cpp | 4 | ||||
| -rw-r--r-- | zenhttp/httpsys.h | 2 | ||||
| -rw-r--r-- | zenhttp/include/zenhttp/httpserver.h | 1 | ||||
| -rw-r--r-- | zenhttp/workthreadpool.cpp | 77 | ||||
| -rw-r--r-- | zenhttp/workthreadpool.h | 46 |
6 files changed, 18 insertions, 128 deletions
diff --git a/zenhttp/httpserver.cpp b/zenhttp/httpserver.cpp index 710b6f356..df5a0596a 100644 --- a/zenhttp/httpserver.cpp +++ b/zenhttp/httpserver.cpp @@ -375,6 +375,22 @@ HttpServerRequest::WriteResponse(HttpResponseCode ResponseCode, CbObject Data) } void +HttpServerRequest::WriteResponse(HttpResponseCode ResponseCode, CbArray Array) +{ + if (m_AcceptType == HttpContentType::kJSON) + { + ExtendableStringBuilder<1024> Sb; + WriteResponse(ResponseCode, HttpContentType::kJSON, Array.ToJson(Sb).ToView()); + } + else + { + SharedBuffer Buf = Array.GetBuffer(); + std::array<IoBuffer, 1> Buffers{IoBufferBuilder::MakeCloneFromMemory(Buf.GetData(), Buf.GetSize())}; + return WriteResponse(ResponseCode, HttpContentType::kCbObject, Buffers); + } +} + +void HttpServerRequest::WriteResponse(HttpResponseCode ResponseCode, HttpContentType ContentType, std::string_view ResponseString) { return WriteResponse(ResponseCode, ContentType, std::u8string_view{(char8_t*)ResponseString.data(), ResponseString.size()}); diff --git a/zenhttp/httpshared.cpp b/zenhttp/httpshared.cpp index ab1463559..f2ce17e16 100644 --- a/zenhttp/httpshared.cpp +++ b/zenhttp/httpshared.cpp @@ -39,8 +39,6 @@ FormatPackageMessage(const CbPackage& Data) ResponseBuffers.reserve(3 + Attachments.size()); // TODO: may want to use an additional fudge factor here to avoid growing since each // attachment is likely to consist of several buffers - uint64_t TotalAttachmentsSize = 0; - // Fixed size header CbPackageHeader Hdr{.HeaderMagic = kCbPkgMagic, .AttachmentCount = gsl::narrow<uint32_t>(Attachments.size())}; @@ -81,7 +79,6 @@ FormatPackageMessage(const CbPackage& Data) for (const SharedBuffer& Segment : Compressed.GetSegments()) { ResponseBuffers.push_back(Segment.AsIoBuffer()); - TotalAttachmentsSize += Segment.GetSize(); } } else if (CbObject AttachmentObject = Attachment.AsObject()) @@ -100,7 +97,6 @@ FormatPackageMessage(const CbPackage& Data) for (const SharedBuffer& Segment : AttachmentBinary.GetSegments()) { ResponseBuffers.push_back(Segment.AsIoBuffer()); - TotalAttachmentsSize += Segment.GetSize(); } } else diff --git a/zenhttp/httpsys.h b/zenhttp/httpsys.h index 0453b8740..d6bd34890 100644 --- a/zenhttp/httpsys.h +++ b/zenhttp/httpsys.h @@ -15,8 +15,8 @@ #if ZEN_WITH_HTTPSYS # define _WINSOCKAPI_ # include <zencore/windows.h> +# include <zencore/workthreadpool.h> # include "iothreadpool.h" -# include "workthreadpool.h" # include <http.h> diff --git a/zenhttp/include/zenhttp/httpserver.h b/zenhttp/include/zenhttp/httpserver.h index 545b96db2..5bd51740a 100644 --- a/zenhttp/include/zenhttp/httpserver.h +++ b/zenhttp/include/zenhttp/httpserver.h @@ -94,6 +94,7 @@ public: virtual void WriteResponse(HttpResponseCode ResponseCode, HttpContentType ContentType, CompositeBuffer& Payload); void WriteResponse(HttpResponseCode ResponseCode, CbObject Data); + void WriteResponse(HttpResponseCode ResponseCode, CbArray Array); void WriteResponse(HttpResponseCode ResponseCode, CbPackage Package); void WriteResponse(HttpResponseCode ResponseCode, HttpContentType ContentType, std::string_view ResponseString); void WriteResponse(HttpResponseCode ResponseCode, HttpContentType ContentType, IoBuffer Blob); diff --git a/zenhttp/workthreadpool.cpp b/zenhttp/workthreadpool.cpp deleted file mode 100644 index 41eaaae94..000000000 --- a/zenhttp/workthreadpool.cpp +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright Epic Games, Inc. All Rights Reserved. - -#include "workthreadpool.h" - -#include <zencore/logging.h> - -namespace zen { - -namespace detail { - struct LambdaWork : IWork - { - LambdaWork(auto Work) : WorkFunction(Work) {} - virtual void Execute() override { WorkFunction(); } - - std::function<void()> WorkFunction; - }; -} // namespace detail - -WorkerThreadPool::WorkerThreadPool(int InThreadCount) -{ - for (int i = 0; i < InThreadCount; ++i) - { - m_WorkerThreads.emplace_back(&WorkerThreadPool::WorkerThreadFunction, this); - } -} - -WorkerThreadPool::~WorkerThreadPool() -{ - m_WorkQueue.CompleteAdding(); - - for (std::thread& Thread : m_WorkerThreads) - { - Thread.join(); - } - - m_WorkerThreads.clear(); -} - -void -WorkerThreadPool::ScheduleWork(Ref<IWork> Work) -{ - m_WorkQueue.Enqueue(std::move(Work)); -} - -void -WorkerThreadPool::ScheduleWork(std::function<void()>&& Work) -{ - m_WorkQueue.Enqueue(new detail::LambdaWork(Work)); -} - -void -WorkerThreadPool::WorkerThreadFunction() -{ - do - { - Ref<IWork> Work; - if (m_WorkQueue.WaitAndDequeue(Work)) - { - try - { - Work->Execute(); - } - catch (std::exception& e) - { - Work->m_Exception = std::current_exception(); - - ZEN_WARN("Caught exception in worker thread: {}", e.what()); - } - } - else - { - return; - } - } while (true); -} - -} // namespace zen
\ No newline at end of file diff --git a/zenhttp/workthreadpool.h b/zenhttp/workthreadpool.h deleted file mode 100644 index 834339d50..000000000 --- a/zenhttp/workthreadpool.h +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright Epic Games, Inc. All Rights Reserved. - -#pragma once - -#include <zencore/zencore.h> - -#include <zencore/blockingqueue.h> -#include <zencore/refcount.h> - -#include <exception> -#include <functional> -#include <system_error> -#include <thread> -#include <vector> - -namespace zen { - -struct IWork : public RefCounted -{ - virtual void Execute() = 0; - - inline std::exception_ptr GetException() { return m_Exception; } - -private: - std::exception_ptr m_Exception; - - friend class WorkerThreadPool; -}; - -class WorkerThreadPool -{ -public: - WorkerThreadPool(int InThreadCount); - ~WorkerThreadPool(); - - void ScheduleWork(Ref<IWork> Work); - void ScheduleWork(std::function<void()>&& Work); - - void WorkerThreadFunction(); - -private: - std::vector<std::thread> m_WorkerThreads; - BlockingQueue<Ref<IWork>> m_WorkQueue; -}; - -} // namespace zen |