mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
Refactor handle closing in Windows platform code
This commit is contained in:
parent
1fe8d93612
commit
7421b95939
38
src/libipc/platform/win/close_handle.h
Normal file
38
src/libipc/platform/win/close_handle.h
Normal file
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* \file libipc/platform/win/close_handle.h
|
||||
* \author mutouyun (orz@orzz.org)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
#include "libimp/log.h"
|
||||
#include "libimp/system.h"
|
||||
|
||||
#include "libipc/def.h"
|
||||
|
||||
LIBIPC_NAMESPACE_BEG_
|
||||
using namespace ::LIBIMP;
|
||||
|
||||
namespace winapi {
|
||||
|
||||
/**
|
||||
* \brief Closes an open object handle.
|
||||
* \see https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
|
||||
*/
|
||||
inline result<void> close_handle(HANDLE h) noexcept {
|
||||
LIBIMP_LOG_();
|
||||
if (h == NULL) {
|
||||
log.error("handle is null.");
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
if (!::CloseHandle(h)) {
|
||||
auto err = sys::error();
|
||||
log.error("failed: CloseHandle(", h, "). error = ", err);
|
||||
return err;
|
||||
}
|
||||
return std::error_code{};
|
||||
}
|
||||
|
||||
} // namespace winapi
|
||||
LIBIPC_NAMESPACE_END_
|
||||
@ -12,13 +12,17 @@
|
||||
#include <Windows.h>
|
||||
|
||||
#include "libimp/log.h"
|
||||
#include "libpmr/new.h"
|
||||
#include "libipc/event.h"
|
||||
|
||||
#include "get_sa.h"
|
||||
#include "to_tchar.h"
|
||||
#include "close_handle.h"
|
||||
|
||||
LIBIPC_NAMESPACE_BEG_
|
||||
|
||||
using namespace ::LIBIMP;
|
||||
using namespace ::LIBPMR;
|
||||
|
||||
struct evt_handle {
|
||||
std::string name;
|
||||
@ -51,26 +55,20 @@ result<evt_t> evt_open(std::string name) noexcept {
|
||||
log.error("failed: CreateEvent(FALSE, FALSE, ", name, "). error = ", err);
|
||||
return err;
|
||||
}
|
||||
return new evt_handle{std::move(name), h};
|
||||
return $new<evt_handle>(std::move(name), h);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Closes an open object handle.
|
||||
* \see https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
|
||||
* \brief Closes an open event object handle.
|
||||
*/
|
||||
result<void> evt_close(evt_t evt) noexcept {
|
||||
LIBIMP_LOG_();
|
||||
LIBIMP_UNUSED auto guard = std::unique_ptr<evt_handle>(evt);
|
||||
LIBIMP_UNUSED auto guard = std::unique_ptr<evt_handle, deleter>(evt);
|
||||
if (!is_valid(evt)) {
|
||||
log.error("handle is null.");
|
||||
return {};
|
||||
}
|
||||
if (!::CloseHandle(evt->h_event)) {
|
||||
auto err = sys::error();
|
||||
log.error("failed: CloseHandle(", evt->h_event, "). error = ", err);
|
||||
return err;
|
||||
}
|
||||
return std::error_code{};
|
||||
return winapi::close_handle(evt->h_event);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -16,8 +16,10 @@
|
||||
|
||||
#include "get_sa.h"
|
||||
#include "to_tchar.h"
|
||||
#include "close_handle.h"
|
||||
|
||||
LIBIPC_NAMESPACE_BEG_
|
||||
|
||||
using namespace ::LIBIMP;
|
||||
|
||||
struct shm_handle {
|
||||
@ -29,24 +31,6 @@ struct shm_handle {
|
||||
|
||||
namespace {
|
||||
|
||||
/**
|
||||
* \brief Closes an open object handle.
|
||||
* \see https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
|
||||
*/
|
||||
result<void> mmap_close(HANDLE h) {
|
||||
LIBIMP_LOG_();
|
||||
if (h == NULL) {
|
||||
log.error("handle is null.");
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
if (!::CloseHandle(h)) {
|
||||
auto err = sys::error();
|
||||
log.error("failed: CloseHandle(", h, "). error = ", err);
|
||||
return err;
|
||||
}
|
||||
return std::error_code{};
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Creates or opens a file mapping object for a specified file.
|
||||
*
|
||||
@ -113,7 +97,7 @@ result<HANDLE> mmap_open(std::string const &file, std::size_t size, mode::type t
|
||||
/// (with its current size, not the specified size), and GetLastError returns ERROR_ALREADY_EXISTS.
|
||||
if ((type == mode::create) && (::GetLastError() == ERROR_ALREADY_EXISTS)) {
|
||||
log.info("the file being created already exists. file = ", file, ", type = ", type);
|
||||
mmap_close(*h);
|
||||
winapi::close_handle(*h);
|
||||
return sys::error();
|
||||
}
|
||||
return h;
|
||||
@ -174,7 +158,7 @@ result<void> mmap_release(HANDLE h, LPCVOID mem) {
|
||||
if (!::UnmapViewOfFile(mem)) {
|
||||
log.warning("failed: UnmapViewOfFile. error = ", sys::error());
|
||||
}
|
||||
return mmap_close(h);
|
||||
return winapi::close_handle(h);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@ -9,13 +9,16 @@
|
||||
|
||||
#include "libimp/log.h"
|
||||
#include "libimp/system.h"
|
||||
|
||||
#include "libpmr/new.h"
|
||||
#include "libipc/shm.h"
|
||||
|
||||
#include "mmap_impl.h"
|
||||
#include "close_handle.h"
|
||||
|
||||
LIBIPC_NAMESPACE_BEG_
|
||||
|
||||
using namespace ::LIBIMP;
|
||||
using namespace ::LIBPMR;
|
||||
|
||||
result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noexcept {
|
||||
LIBIMP_LOG_();
|
||||
@ -27,16 +30,16 @@ result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noex
|
||||
auto mem = mmap_memof(*h);
|
||||
if (*mem == NULL) {
|
||||
log.error("failed: mmap_memof(", *h, ").");
|
||||
mmap_close(*h);
|
||||
winapi::close_handle(*h);
|
||||
return mem.error();
|
||||
}
|
||||
auto sz = mmap_sizeof(*mem);
|
||||
if (!sz) {
|
||||
log.error("failed: mmap_sizeof(", *mem, ").");
|
||||
mmap_close(*h);
|
||||
winapi::close_handle(*h);
|
||||
return sz.error();
|
||||
}
|
||||
return new shm_handle{std::move(name), *sz, *mem, *h};
|
||||
return $new<shm_handle>(std::move(name), *sz, *mem, *h);
|
||||
}
|
||||
|
||||
result<void> shm_close(shm_t h) noexcept {
|
||||
@ -47,7 +50,7 @@ result<void> shm_close(shm_t h) noexcept {
|
||||
}
|
||||
auto shm = static_cast<shm_handle *>(h);
|
||||
auto ret = mmap_release(shm->h_fmap, shm->memp);
|
||||
delete shm;
|
||||
$delete(shm);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user