mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
Adjust the parameter error of the existing api implementation.
This commit is contained in:
parent
d0af950071
commit
a56a0cd643
@ -38,11 +38,11 @@ result<std::int64_t> conf(info r) noexcept {
|
||||
}
|
||||
default:
|
||||
log.error("invalid info = ", underlyof(r));
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
auto err = sys::error();
|
||||
log.error("info = ", underlyof(r), ", error = ", err);
|
||||
return {false, err};
|
||||
return err;
|
||||
}
|
||||
|
||||
} // namespace sys
|
||||
|
||||
@ -19,7 +19,6 @@
|
||||
|
||||
LIBIMP_NAMESPACE_BEG_
|
||||
namespace sys {
|
||||
namespace {
|
||||
|
||||
/**
|
||||
* \brief Gets a text description of the system error
|
||||
@ -56,8 +55,6 @@ std::string error_string(DWORD code) noexcept {
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
/**
|
||||
* \brief Get the system error number.
|
||||
* \see https://en.cppreference.com/w/cpp/error/system_category
|
||||
@ -82,7 +79,7 @@ result<std::int64_t> conf(info r) noexcept {
|
||||
}
|
||||
default:
|
||||
log.error("invalid info = ", underlyof(r));
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -51,7 +51,7 @@ result<int> shm_open_fd(std::string const &name, mode::type type) noexcept {
|
||||
LIBIMP_LOG_();
|
||||
if (name.empty()) {
|
||||
log.error("name is empty.");
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
|
||||
/// \brief Open the object for read-write access.
|
||||
@ -70,7 +70,7 @@ result<int> shm_open_fd(std::string const &name, mode::type type) noexcept {
|
||||
break;
|
||||
default:
|
||||
log.error("mode type is invalid. type = ", type);
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
|
||||
/// \brief Create/Open POSIX shared memory bject
|
||||
@ -153,7 +153,9 @@ result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noex
|
||||
result<void> shm_close(shm_t h) noexcept {
|
||||
LIBIMP_LOG_();
|
||||
auto shm = valid(h);
|
||||
if (shm == nullptr) return {};
|
||||
if (shm == nullptr) {
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
if (::munmap(shm->memp, shm->f_sz) == posix::failed) {
|
||||
auto err = sys::error();
|
||||
log.error("failed: munmap(", shm->memp, ", ", shm->f_sz, "). error = ", err);
|
||||
|
||||
@ -49,7 +49,7 @@ result<evt_t> evt_open(std::string name) noexcept {
|
||||
if (h == NULL) {
|
||||
auto err = sys::error();
|
||||
log.error("failed: CreateEvent(FALSE, FALSE, ", name, "). error = ", err);
|
||||
return {nullptr, err};
|
||||
return err;
|
||||
}
|
||||
return new evt_handle{std::move(name), h};
|
||||
}
|
||||
@ -99,7 +99,7 @@ result<bool> evt_wait(evt_t evt, std::int64_t ms) noexcept {
|
||||
LIBIMP_LOG_();
|
||||
if (!is_valid(evt)) {
|
||||
log.error("handle is null.");
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
DWORD dwMilliseconds = (ms < 0) ? INFINITE : static_cast<DWORD>(ms);
|
||||
auto r = ::WaitForSingleObject(evt->h_event, dwMilliseconds);
|
||||
@ -115,7 +115,7 @@ result<bool> evt_wait(evt_t evt, std::int64_t ms) noexcept {
|
||||
}
|
||||
auto err = sys::error();
|
||||
log.error("failed: WaitForSingleObject(", evt->h_event, ", ", dwMilliseconds, "). error = ", err);
|
||||
return {false, err};
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -126,14 +126,14 @@ result<bool> evt_wait(::LIBIMP::span<evt_t const> evts, std::int64_t ms) noexcep
|
||||
LIBIMP_LOG_();
|
||||
if (evts.empty()) {
|
||||
log.error("evts handle is empty.");
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
// Conversion of the event handle array to the windows handle array.
|
||||
std::vector<HANDLE> handles(evts.size());
|
||||
for (std::size_t i = 0; i < evts.size(); ++i) {
|
||||
if (!is_valid(evts[i])) {
|
||||
log.error("handle is null.");
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
handles[i] = evts[i]->h_event;
|
||||
}
|
||||
@ -152,7 +152,7 @@ result<bool> evt_wait(::LIBIMP::span<evt_t const> evts, std::int64_t ms) noexcep
|
||||
}
|
||||
auto err = sys::error();
|
||||
log.error("failed: WaitForMultipleObjects(", handles.size(), ", ", dwMilliseconds, "). error = ", err);
|
||||
return {false, err};
|
||||
return err;
|
||||
}
|
||||
|
||||
LIBIPC_NAMESPACE_END_
|
||||
|
||||
@ -37,7 +37,7 @@ result<void> mmap_close(HANDLE h) {
|
||||
LIBIMP_LOG_();
|
||||
if (h == NULL) {
|
||||
log.error("handle is null.");
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
if (!::CloseHandle(h)) {
|
||||
auto err = sys::error();
|
||||
@ -64,12 +64,12 @@ result<HANDLE> mmap_open(std::string const &file, std::size_t size, mode::type t
|
||||
LIBIMP_LOG_();
|
||||
if (file.empty()) {
|
||||
log.error("file name is empty.");
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
auto t_name = detail::to_tstring(file);
|
||||
if (t_name.empty()) {
|
||||
log.error("file name is empty. (TCHAR conversion failed)");
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
|
||||
/// \brief Opens a named file mapping object.
|
||||
@ -78,7 +78,7 @@ result<HANDLE> mmap_open(std::string const &file, std::size_t size, mode::type t
|
||||
if (h == NULL) {
|
||||
auto err = sys::error();
|
||||
log.error("failed: OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, ", file, "). error = ", err);
|
||||
return {nullptr, err};
|
||||
return err;
|
||||
}
|
||||
return h;
|
||||
};
|
||||
@ -91,7 +91,7 @@ result<HANDLE> mmap_open(std::string const &file, std::size_t size, mode::type t
|
||||
if (h == NULL) {
|
||||
auto err = sys::error();
|
||||
log.error("failed: CreateFileMapping(PAGE_READWRITE | SEC_COMMIT, ", size, ", ", file, "). error = ", err);
|
||||
return {nullptr, err};
|
||||
return err;
|
||||
}
|
||||
return h;
|
||||
};
|
||||
@ -105,7 +105,7 @@ result<HANDLE> mmap_open(std::string const &file, std::size_t size, mode::type t
|
||||
return try_open();
|
||||
} else if (!(type & mode::create)) {
|
||||
log.error("mode type is invalid. type = ", type);
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
auto h = try_create();
|
||||
if (!h) return h;
|
||||
@ -114,7 +114,7 @@ result<HANDLE> mmap_open(std::string const &file, std::size_t size, mode::type t
|
||||
if ((type == mode::create) && (::GetLastError() == ERROR_ALREADY_EXISTS)) {
|
||||
log.info("the file being created already exists. file = ", file, ", type = ", type);
|
||||
mmap_close(*h);
|
||||
return {};
|
||||
return sys::error();
|
||||
}
|
||||
return h;
|
||||
}
|
||||
@ -127,13 +127,13 @@ result<LPVOID> mmap_memof(HANDLE h) {
|
||||
LIBIMP_LOG_();
|
||||
if (h == NULL) {
|
||||
log.error("handle is null.");
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
LPVOID mem = ::MapViewOfFile(h, FILE_MAP_ALL_ACCESS, 0, 0, 0);
|
||||
if (mem == NULL) {
|
||||
auto err = sys::error();
|
||||
log.error("failed: MapViewOfFile(", h, ", FILE_MAP_ALL_ACCESS). error = ", err);
|
||||
return {nullptr, err};
|
||||
return err;
|
||||
}
|
||||
return mem;
|
||||
}
|
||||
@ -146,13 +146,13 @@ result<SIZE_T> mmap_sizeof(LPCVOID mem) {
|
||||
LIBIMP_LOG_();
|
||||
if (mem == NULL) {
|
||||
log.error("memory pointer is null.");
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
MEMORY_BASIC_INFORMATION mem_info {};
|
||||
if (::VirtualQuery(mem, &mem_info, sizeof(mem_info)) == 0) {
|
||||
auto err = sys::error();
|
||||
log.error("failed: VirtualQuery(", mem, "). error = ", err);
|
||||
return {false, err};
|
||||
return err;
|
||||
}
|
||||
return mem_info.RegionSize;
|
||||
}
|
||||
@ -165,11 +165,11 @@ result<void> mmap_release(HANDLE h, LPCVOID mem) {
|
||||
LIBIMP_LOG_();
|
||||
if (h == NULL) {
|
||||
log.error("handle is null.");
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
if (mem == NULL) {
|
||||
log.error("memory pointer is null.");
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
if (!::UnmapViewOfFile(mem)) {
|
||||
log.warning("failed: UnmapViewOfFile. error = ", sys::error());
|
||||
|
||||
@ -22,19 +22,19 @@ result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noex
|
||||
auto h = mmap_open(name, size, type);
|
||||
if (*h == NULL) {
|
||||
log.error("failed: mmap_open(name = ", name, ", size = ", size, ", type = ", type, ").");
|
||||
return {nullptr, h.error()};
|
||||
return h.error();
|
||||
}
|
||||
auto mem = mmap_memof(*h);
|
||||
if (*mem == NULL) {
|
||||
log.error("failed: mmap_memof(", *h, ").");
|
||||
mmap_close(*h);
|
||||
return {nullptr, mem.error()};
|
||||
return mem.error();
|
||||
}
|
||||
auto sz = mmap_sizeof(*mem);
|
||||
if (!sz) {
|
||||
log.error("failed: mmap_sizeof(", *mem, ").");
|
||||
mmap_close(*h);
|
||||
return {nullptr, sz.error()};
|
||||
return sz.error();
|
||||
}
|
||||
return new shm_handle{std::move(name), *sz, *mem, *h};
|
||||
}
|
||||
@ -43,7 +43,7 @@ result<void> shm_close(shm_t h) noexcept {
|
||||
LIBIMP_LOG_();
|
||||
if (h == nullptr) {
|
||||
log.error("shm handle is null.");
|
||||
return {};
|
||||
return std::make_error_code(std::errc::invalid_argument);
|
||||
}
|
||||
auto shm = static_cast<shm_handle *>(h);
|
||||
auto ret = mmap_release(shm->h_fmap, shm->memp);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user