MCPcopy Create free account
hub / github.com/URLab-Sim/UnrealRoboticsLab / Open

Method Open

Source/URLab/Private/Transport/ShmRegion.cpp:43–171  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

41}
42
43bool FMjShmRegion::Open(const FString& Path, uint32 InBufferStride, uint32 InNBuffers)
44{
45 if (MappedAddr != nullptr)
46 {
47 UE_LOG(LogURLabNet, Warning, TEXT("FMjShmRegion::Open: already open at %s"), *FilePath);
48 return false;
49 }
50 if (InBufferStride < sizeof(uint32))
51 {
52 UE_LOG(LogURLabNet, Error, TEXT("FMjShmRegion::Open: stride must be >= 4"));
53 return false;
54 }
55
56 // Make sure parent directory exists.
57 const FString ParentDir = FPaths::GetPath(Path);
58 if (!ParentDir.IsEmpty())
59 {
60 IFileManager::Get().MakeDirectory(*ParentDir, /*Tree=*/true);
61 }
62
63 const uint64 TotalSize = sizeof(FMjShmHeader) + uint64(InBufferStride) * uint64(InNBuffers);
64
65#if PLATFORM_WINDOWS
66 // Create / truncate the backing file.
67 HANDLE hFile = ::CreateFileW(*Path,
68 GENERIC_READ | GENERIC_WRITE,
69 FILE_SHARE_READ | FILE_SHARE_WRITE,
70 nullptr,
71 CREATE_ALWAYS,
72 FILE_ATTRIBUTE_NORMAL,
73 nullptr);
74 if (hFile == INVALID_HANDLE_VALUE)
75 {
76 UE_LOG(LogURLabNet, Error, TEXT("FMjShmRegion::Open: CreateFileW failed for %s (err=%lu)"),
77 *Path, ::GetLastError());
78 return false;
79 }
80
81 // Set file size to TotalSize via SetFilePointerEx + SetEndOfFile.
82 LARGE_INTEGER NewPos;
83 NewPos.QuadPart = static_cast<LONGLONG>(TotalSize);
84 if (!::SetFilePointerEx(hFile, NewPos, nullptr, FILE_BEGIN) || !::SetEndOfFile(hFile))
85 {
86 UE_LOG(LogURLabNet, Error, TEXT("FMjShmRegion::Open: SetEndOfFile failed (err=%lu)"),
87 ::GetLastError());
88 ::CloseHandle(hFile);
89 return false;
90 }
91
92 HANDLE hMap = ::CreateFileMappingW(hFile, nullptr, PAGE_READWRITE,
93 static_cast<DWORD>(TotalSize >> 32), static_cast<DWORD>(TotalSize & 0xFFFFFFFFu),
94 nullptr);
95 if (!hMap)
96 {
97 UE_LOG(LogURLabNet, Error, TEXT("FMjShmRegion::Open: CreateFileMappingW failed (err=%lu)"),
98 ::GetLastError());
99 ::CloseHandle(hFile);
100 return false;

Callers 2

TransportInitMethod · 0.45
TransportInitMethod · 0.45

Calls 2

IsEmptyMethod · 0.80
GetPathFunction · 0.50

Tested by

no test coverage detected