Merge pull request #11089 from sepalani/pcap-share

NetworkCaptureLogger: Allow PCAP shared read access on Windows
This commit is contained in:
Admiral H. Curtiss 2023-01-05 04:06:35 +01:00 committed by GitHub
commit 7b04a6b958
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 8 deletions

View file

@ -35,9 +35,10 @@ IOFile::IOFile(std::FILE* file) : m_file(file), m_good(true)
{
}
IOFile::IOFile(const std::string& filename, const char openmode[]) : m_file(nullptr), m_good(true)
IOFile::IOFile(const std::string& filename, const char openmode[], SharedAccess sh)
: m_file(nullptr), m_good(true)
{
Open(filename, openmode);
Open(filename, openmode, sh);
}
IOFile::~IOFile()
@ -62,12 +63,21 @@ void IOFile::Swap(IOFile& other) noexcept
std::swap(m_good, other.m_good);
}
bool IOFile::Open(const std::string& filename, const char openmode[])
bool IOFile::Open(const std::string& filename, const char openmode[],
[[maybe_unused]] SharedAccess sh)
{
Close();
#ifdef _WIN32
m_good = _tfopen_s(&m_file, UTF8ToTStr(filename).c_str(), UTF8ToTStr(openmode).c_str()) == 0;
if (sh == SharedAccess::Default)
{
m_good = _tfopen_s(&m_file, UTF8ToTStr(filename).c_str(), UTF8ToTStr(openmode).c_str()) == 0;
}
else if (sh == SharedAccess::Read)
{
m_file = _tfsopen(UTF8ToTStr(filename).c_str(), UTF8ToTStr(openmode).c_str(), SH_DENYWR);
m_good = m_file != nullptr;
}
#else
#ifdef ANDROID
if (IsPathAndroidContent(filename))

View file

@ -20,6 +20,12 @@ enum class SeekOrigin
End,
};
enum class SharedAccess
{
Default,
Read,
};
// simple wrapper for cstdlib file functions to
// hopefully will make error checking easier
// and make forgetting an fclose() harder
@ -28,7 +34,8 @@ class IOFile
public:
IOFile();
IOFile(std::FILE* file);
IOFile(const std::string& filename, const char openmode[]);
IOFile(const std::string& filename, const char openmode[],
SharedAccess sh = SharedAccess::Default);
~IOFile();
@ -40,7 +47,8 @@ public:
void Swap(IOFile& other) noexcept;
bool Open(const std::string& filename, const char openmode[]);
bool Open(const std::string& filename, const char openmode[],
SharedAccess sh = SharedAccess::Default);
bool Close();
template <typename T>

View file

@ -83,8 +83,8 @@ PCAPSSLCaptureLogger::PCAPSSLCaptureLogger()
const std::string filepath =
fmt::format("{}{} {:%Y-%m-%d %Hh%Mm%Ss}.pcap", File::GetUserPath(D_DUMPSSL_IDX),
SConfig::GetInstance().GetGameID(), fmt::localtime(std::time(nullptr)));
m_file = std::make_unique<Common::PCAP>(new File::IOFile(filepath, "wb"),
Common::PCAP::LinkType::Ethernet);
m_file = std::make_unique<Common::PCAP>(
new File::IOFile(filepath, "wb", File::SharedAccess::Read), Common::PCAP::LinkType::Ethernet);
}
PCAPSSLCaptureLogger::~PCAPSSLCaptureLogger() = default;