diff options
| author | Stefan Boberg <[email protected]> | 2021-10-14 19:07:14 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-10-14 19:07:14 +0200 |
| commit | 2b71d6a8d57c773bc7734b253a1ffd1e47162184 (patch) | |
| tree | c0c70f9f2f8b9dc895080aac9f7de1140c56ebf0 /zencore/iobuffer.cpp | |
| parent | Merge branch 'main' of https://github.com/EpicGames/zen (diff) | |
| download | zen-2b71d6a8d57c773bc7734b253a1ffd1e47162184.tar.xz zen-2b71d6a8d57c773bc7734b253a1ffd1e47162184.zip | |
asio HTTP implementation (#23)
asio-based HTTP implementation
Diffstat (limited to 'zencore/iobuffer.cpp')
| -rw-r--r-- | zencore/iobuffer.cpp | 83 |
1 files changed, 59 insertions, 24 deletions
diff --git a/zencore/iobuffer.cpp b/zencore/iobuffer.cpp index 922c14f83..119e37d0b 100644 --- a/zencore/iobuffer.cpp +++ b/zencore/iobuffer.cpp @@ -144,7 +144,7 @@ IoBufferCore::MakeOwned(bool Immutable) { if (!IsOwned()) { - const void* OldDataPtr = m_DataPtr; + const void* OldDataPtr = m_DataPtr; AllocateBuffer(m_DataBytes, sizeof(void*)); memcpy(const_cast<void*>(m_DataPtr), OldDataPtr, m_DataBytes); SetIsOwnedByThis(true); @@ -188,29 +188,29 @@ IoBufferExtendedCore::~IoBufferExtendedCore() { if (m_MappedPointer) { -# if ZEN_PLATFORM_WINDOWS +#if ZEN_PLATFORM_WINDOWS UnmapViewOfFile(m_MappedPointer); -# else +#else uint64_t MapSize = ~uint64_t(uintptr_t(m_MmapHandle)); munmap(m_MappedPointer, MapSize); -# endif +#endif } -# if ZEN_PLATFORM_WINDOWS +#if ZEN_PLATFORM_WINDOWS if (m_Flags & kOwnsMmap) { CloseHandle(m_MmapHandle); } -# endif +#endif if (m_Flags & kOwnsFile) { -# if ZEN_PLATFORM_WINDOWS +#if ZEN_PLATFORM_WINDOWS BOOL Success = CloseHandle(m_FileHandle); -# else +#else int Fd = int(uintptr_t(m_FileHandle)); bool Success = (close(Fd) == 0); -# endif +#endif if (!Success) { @@ -244,7 +244,7 @@ IoBufferExtendedCore::Materialize() const const uint64_t MappedOffsetDisplacement = m_FileOffset - MapOffset; const uint64_t MapSize = m_DataBytes + MappedOffsetDisplacement; -# if ZEN_PLATFORM_WINDOWS +#if ZEN_PLATFORM_WINDOWS m_MmapHandle = CreateFileMapping(m_FileHandle, /* lpFileMappingAttributes */ nullptr, /* flProtect */ PAGE_READONLY, @@ -265,7 +265,7 @@ IoBufferExtendedCore::Materialize() const /* FileOffsetHigh */ uint32_t(MapOffset >> 32), /* FileOffsetLow */ uint32_t(MapOffset & 0xffFFffFFu), /* dwNumberOfBytesToMap */ MapSize); -# else +#else m_MmapHandle = (void*)uintptr_t(~MapSize); // ~ so it's never null (assuming MapSize >= 0) m_Flags |= kOwnsMmap; @@ -276,7 +276,7 @@ IoBufferExtendedCore::Materialize() const /* flags */ MAP_SHARED | MAP_NORESERVE, /* fd */ int(uintptr_t(m_FileHandle)), /* offset */ MapOffset); -# endif // ZEN_PLATFORM_WINDOWS +#endif // ZEN_PLATFORM_WINDOWS if (MappedBase == nullptr) { @@ -371,6 +371,41 @@ IoBuffer::GetFileReference(IoBufferFileReference& OutRef) const ////////////////////////////////////////////////////////////////////////// IoBuffer +IoBufferBuilder::ReadFromFileMaybe(IoBuffer& InBuffer) +{ + IoBufferFileReference FileRef; + if (InBuffer.GetFileReference(/* out */ FileRef)) + { + IoBuffer OutBuffer(FileRef.FileChunkSize); + +#if ZEN_PLATFORM_WINDOWS + OVERLAPPED Ovl{}; + + const uint64_t NumberOfBytesToRead = FileRef.FileChunkSize; + const uint64_t& FileOffset = FileRef.FileChunkOffset; + + Ovl.Offset = DWORD(FileOffset & 0xffff'ffffu); + Ovl.OffsetHigh = DWORD(FileOffset >> 32); + + DWORD dwNumberOfBytesRead = 0; + BOOL Success = ::ReadFile(FileRef.FileHandle, OutBuffer.MutableData(), DWORD(NumberOfBytesToRead), &dwNumberOfBytesRead, &Ovl); + + ZEN_ASSERT(dwNumberOfBytesRead == NumberOfBytesToRead); + + // TODO: error handling + + return OutBuffer; +#else +# error Needs implementation +#endif + } + else + { + return InBuffer; + } +} + +IoBuffer IoBufferBuilder::MakeFromFileHandle(void* FileHandle, uint64_t Offset, uint64_t Size) { return IoBuffer(IoBuffer::BorrowedFile, FileHandle, Offset, Size); @@ -381,7 +416,7 @@ IoBufferBuilder::MakeFromFile(const path_char_t* FileName, uint64_t Offset, uint { uint64_t FileSize; -# if ZEN_PLATFORM_WINDOWS +#if ZEN_PLATFORM_WINDOWS CAtlFile DataFile; HRESULT hRes = DataFile.Create(FileName, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING); @@ -392,7 +427,7 @@ IoBufferBuilder::MakeFromFile(const path_char_t* FileName, uint64_t Offset, uint } DataFile.GetSize((ULONGLONG&)FileSize); -# else +#else int Fd = open(FileName, O_RDONLY); if (Fd < 0) { @@ -403,7 +438,7 @@ IoBufferBuilder::MakeFromFile(const path_char_t* FileName, uint64_t Offset, uint struct stat Stat; fstat(Fd, &Stat); FileSize = Stat.st_size; -# endif // ZEN_PLATFORM_WINDOWS +#endif // ZEN_PLATFORM_WINDOWS // TODO: should validate that offset is in range @@ -422,15 +457,15 @@ IoBufferBuilder::MakeFromFile(const path_char_t* FileName, uint64_t Offset, uint if (Size) { -# if ZEN_PLATFORM_WINDOWS +#if ZEN_PLATFORM_WINDOWS void* Fd = DataFile.Detach(); -# endif +#endif return IoBuffer(IoBuffer::File, (void*)uintptr_t(Fd), Offset, Size); } -# if !ZEN_PLATFORM_WINDOWS +#if !ZEN_PLATFORM_WINDOWS close(Fd); -# endif +#endif // For an empty file, we may as well just return an empty memory IoBuffer return IoBuffer(IoBuffer::Wrap, "", 0); @@ -442,7 +477,7 @@ IoBufferBuilder::MakeFromTemporaryFile(const std::filesystem::path& FileName) uint64_t FileSize; void* Handle; -# if ZEN_PLATFORM_WINDOWS +#if ZEN_PLATFORM_WINDOWS CAtlFile DataFile; // We need to open with DELETE since this is used for the case @@ -459,7 +494,7 @@ IoBufferBuilder::MakeFromTemporaryFile(const std::filesystem::path& FileName) DataFile.GetSize((ULONGLONG&)FileSize); Handle = DataFile.Detach(); -# else +#else int Fd = open(FileName.native().c_str(), O_RDONLY); if (Fd < 0) { @@ -472,7 +507,7 @@ IoBufferBuilder::MakeFromTemporaryFile(const std::filesystem::path& FileName) FileSize = Stat.st_size; Handle = (void*)uintptr_t(Fd); -# endif // ZEN_PLATFORM_WINDOWS +#endif // ZEN_PLATFORM_WINDOWS IoBuffer Iob(IoBuffer::File, Handle, 0, FileSize); Iob.m_Core->SetIsWholeFile(true); @@ -489,7 +524,7 @@ HashBuffer(IoBuffer& Buffer) ////////////////////////////////////////////////////////////////////////// -# if ZEN_WITH_TESTS +#if ZEN_WITH_TESTS void iobuffer_forcelink() @@ -503,6 +538,6 @@ TEST_CASE("IoBuffer") zen::IoBuffer buffer3(buffer2, 0, buffer2.Size()); } -# endif +#endif } // namespace zen |