cdvdgigaherz: Improve IOCtlSrc::ReadSectors2048

Don't use a RAW_READ_INFO struct when only the LARGE_INTEGER member is
used. Use SetFilePointerEx which is slightly simpler and doesn't require
checking GetLastError() in some circumstances to check whether the read
has actually failed.

Also use a mutex to prevent simultaneous access from both the read
thread and the keepalive thread to prevent overlapping SetFilePointerEx
calls from causing the wrong data to be read.

And print error messages should a failure occur.
This commit is contained in:
Jonathan Li 2016-10-14 18:12:03 +01:00
parent fdd6ea3651
commit fc085c1758
2 changed files with 19 additions and 17 deletions

View File

@ -22,6 +22,7 @@
#include <cstdio>
#include <cstring>
#include <mutex>
#include <string>
#include <vector>
@ -61,6 +62,7 @@ class IOCtlSrc
u32 m_sectors = 0;
u32 m_layer_break = 0;
std::vector<toc_entry> m_toc;
mutable std::mutex m_lock;
bool ReadDVDInfo();
bool ReadCDInfo();

View File

@ -113,31 +113,31 @@ const std::vector<toc_entry> &IOCtlSrc::ReadTOC() const
s32 IOCtlSrc::ReadSectors2048(u32 sector, u32 count, char *buffer)
{
RAW_READ_INFO rri;
std::lock_guard<std::mutex> guard(m_lock);
LARGE_INTEGER offset;
offset.QuadPart = sector * 2048ULL;
DWORD size = 0;
rri.DiskOffset.QuadPart = sector * (u64)2048;
rri.SectorCount = count;
//fall back to standard reading
if (SetFilePointer(m_device, rri.DiskOffset.LowPart, &rri.DiskOffset.HighPart, FILE_BEGIN) == -1) {
if (GetLastError() != 0)
return -1;
}
if (ReadFile(m_device, buffer, 2048 * count, &size, NULL) == 0) {
if (!SetFilePointerEx(m_device, offset, nullptr, FILE_BEGIN)) {
fprintf(stderr, " * CDVD SetFilePointerEx failed: sector %u: error %u\n",
sector, GetLastError());
return -1;
}
if (size != (2048 * count)) {
return -1;
const DWORD bytes_to_read = 2048 * count;
DWORD bytes_read;
if (ReadFile(m_device, buffer, bytes_to_read, &bytes_read, nullptr)) {
if (bytes_read == bytes_to_read)
return 0;
fprintf(stderr, " * CDVD ReadFile: sectors %u-%u: %u bytes read, %u bytes expected\n",
sector, sector + count - 1, bytes_read, bytes_to_read);
} else {
fprintf(stderr, " * CDVD ReadFile failed: sectors %u-%u: error %u\n",
sector, sector + count - 1, GetLastError());
}
return 0;
return -1;
}
s32 IOCtlSrc::ReadSectors2352(u32 sector, u32 count, char *buffer)
{
struct sptdinfo