diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/sharedmemory/SharedMemory.cpp b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/sharedmemory/SharedMemory.cpp index 5f59fc4..e48107e 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/sharedmemory/SharedMemory.cpp +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/sharedmemory/SharedMemory.cpp @@ -327,4 +327,66 @@ bool SharedMemory::ensureCapacityForInsert(MappingInfo& mapping) return true; } return resizeMapping(mapping, capacity * 2); +} + +/* +Function: ensureLatestMapping +Description: Remaps the file if another process has resized it. +Parameters: + - mapping: MappingInfo&, mapping information and handles +Returns: + - bool: True if the mapping is valid and up to date, otherwise false +*/ +bool SharedMemory::ensureLatestMapping(MappingInfo& mapping) +{ + FileHeader* header = getHeader(mapping); + if (header == nullptr) + { + return false; + } + if (header->capacity == mapping.mappedCapacity) + { + return true; + } + if (!UnmapViewOfFile(mapping.mappedView)) + { + invalidateMapping(mapping); + return false; + } + mapping.mappedView = nullptr; + CloseHandle(mapping.mappingHandle); + mapping.mappingHandle = NULL; + mapping.mappingHandle = + CreateFileMappingA( + mapping.fileHandle, + NULL, + PAGE_READWRITE, + 0, + 0, + NULL); + if (mapping.mappingHandle == NULL) + { + invalidateMapping(mapping); + return false; + } + mapping.mappedView = + MapViewOfFile( + mapping.mappingHandle, + FILE_MAP_ALL_ACCESS, + 0, + 0, + 0); + if (mapping.mappedView == nullptr) + { + invalidateMapping(mapping); + return false; + } + header = getHeader(mapping); + if (header == nullptr) + { + invalidateMapping(mapping); + return false; + } + mapping.mappedCapacity = header->capacity; + return true; } \ No newline at end of file