clang-format
This commit is contained in:
89
src/ipc.cpp
89
src/ipc.cpp
@@ -7,18 +7,19 @@
|
||||
|
||||
#include "ipc.hpp"
|
||||
|
||||
namespace ipc
|
||||
{
|
||||
namespace ipc {
|
||||
|
||||
#define set_message(verbose, s, ...) { \
|
||||
m_message = fmt::format(s __VA_OPT__(,) __VA_ARGS__); \
|
||||
if (verbose < 0) { \
|
||||
throw std::runtime_error(m_message); \
|
||||
} else if (verbose <= m_verbose) { \
|
||||
fmt::print(m_message); \
|
||||
fmt::print("\n"); \
|
||||
} \
|
||||
}
|
||||
#define set_message(verbose, s, ...) \
|
||||
{ \
|
||||
m_message = fmt::format(s __VA_OPT__(, ) __VA_ARGS__); \
|
||||
if (verbose < 0) { \
|
||||
throw std::runtime_error(m_message); \
|
||||
} \
|
||||
else if (verbose <= m_verbose) { \
|
||||
fmt::print(m_message); \
|
||||
fmt::print("\n"); \
|
||||
} \
|
||||
}
|
||||
|
||||
std::string
|
||||
default_name();
|
||||
@@ -36,9 +37,13 @@ start_external(const std::string& executable, bool is_server)
|
||||
}
|
||||
|
||||
void
|
||||
start_external(const std::string& executable, const std::string& ipc_name, bool is_server)
|
||||
start_external(
|
||||
const std::string& executable,
|
||||
const std::string& ipc_name,
|
||||
bool is_server)
|
||||
{
|
||||
auto command = fmt::format("{} {} {}", executable, ipc_name, is_server ? "1" : "0");
|
||||
auto command
|
||||
= fmt::format("{} {} {}", executable, ipc_name, is_server ? "1" : "0");
|
||||
fmt::print("execute: {}\n", command);
|
||||
|
||||
auto thread_func = [command] {
|
||||
@@ -49,7 +54,7 @@ start_external(const std::string& executable, const std::string& ipc_name, bool
|
||||
}
|
||||
|
||||
Ipc::Ipc(bool is_server, int verbose)
|
||||
: Ipc(default_name(), is_server, verbose)
|
||||
: Ipc(default_name(), is_server, verbose)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -67,17 +72,29 @@ Ipc::write(const void* buffer, size_t count, size_t size)
|
||||
ssize_t bytes_sent = 0;
|
||||
|
||||
do {
|
||||
bytes_current = _write(&bytes_buffer[bytes_sent], bytes_required - bytes_sent);
|
||||
bytes_current
|
||||
= _write(&bytes_buffer[bytes_sent], bytes_required - bytes_sent);
|
||||
bytes_sent += bytes_current;
|
||||
// set_message(2, "write {} -> {}/{}\n", bytes_current, bytes_sent, bytes_required);
|
||||
// set_message(2, "write {} -> {}/{}\n", bytes_current, bytes_sent,
|
||||
// bytes_required);
|
||||
} while ((bytes_current > 0) && (bytes_sent != bytes_required));
|
||||
|
||||
if (bytes_sent != bytes_required) {
|
||||
set_message(-1, "c : (-) write ({} * {}) = {} bytes error: only {} bytes are sent",
|
||||
count, size, bytes_required, bytes_sent);
|
||||
set_message(
|
||||
-1,
|
||||
"c : (-) write ({} * {}) = {} bytes error: only {} bytes are sent",
|
||||
count,
|
||||
size,
|
||||
bytes_required,
|
||||
bytes_sent);
|
||||
}
|
||||
|
||||
set_message(1, "c : (+) write ({} * {}) = {} bytes", count, size, bytes_required);
|
||||
set_message(
|
||||
1,
|
||||
"c : (+) write ({} * {}) = {} bytes",
|
||||
count,
|
||||
size,
|
||||
bytes_required);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -89,22 +106,40 @@ Ipc::read(void* buffer, size_t count, size_t size)
|
||||
|
||||
const ssize_t bytes_required = size * count;
|
||||
|
||||
char* bytes_buffer = (char*)buffer;
|
||||
ssize_t bytes_current = 0;
|
||||
char* bytes_buffer = (char*)buffer;
|
||||
ssize_t bytes_current = 0;
|
||||
ssize_t bytes_received = 0;
|
||||
|
||||
do {
|
||||
bytes_current = _read(&bytes_buffer[bytes_received], bytes_required - bytes_received);
|
||||
bytes_current = _read(
|
||||
&bytes_buffer[bytes_received],
|
||||
bytes_required - bytes_received);
|
||||
bytes_received += bytes_current;
|
||||
set_message(2, "read {} -> {}/{}\n", bytes_current, bytes_received, bytes_required);
|
||||
set_message(
|
||||
2,
|
||||
"read {} -> {}/{}\n",
|
||||
bytes_current,
|
||||
bytes_received,
|
||||
bytes_required);
|
||||
} while ((bytes_current > 0) && (bytes_received != bytes_required));
|
||||
|
||||
if (bytes_received != bytes_required) {
|
||||
set_message(-1, "c : (-) read ({} * {}) = {} bytes error: only {} bytes are recieved",
|
||||
count, size, bytes_required, bytes_received);
|
||||
set_message(
|
||||
-1,
|
||||
"c : (-) read ({} * {}) = {} bytes error: only {} bytes are "
|
||||
"recieved",
|
||||
count,
|
||||
size,
|
||||
bytes_required,
|
||||
bytes_received);
|
||||
}
|
||||
|
||||
set_message(1, "c : (+) read ({} * {}) = {} bytes", size, count, bytes_required);
|
||||
set_message(
|
||||
1,
|
||||
"c : (+) read ({} * {}) = {} bytes",
|
||||
size,
|
||||
count,
|
||||
bytes_required);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -123,4 +158,4 @@ Ipc::read(char* str)
|
||||
read(str, 1, len);
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace ipc
|
||||
|
||||
26
src/ipc.hpp
26
src/ipc.hpp
@@ -6,11 +6,10 @@
|
||||
#include <string>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
namespace ipc
|
||||
{
|
||||
namespace ipc {
|
||||
|
||||
void
|
||||
init();
|
||||
@@ -22,11 +21,14 @@ void
|
||||
start_external(const std::string& executable, bool is_server);
|
||||
|
||||
void
|
||||
start_external(const std::string& executable, const std::string& ipc_name, bool is_server);
|
||||
start_external(
|
||||
const std::string& executable,
|
||||
const std::string& ipc_name,
|
||||
bool is_server);
|
||||
|
||||
class Ipc
|
||||
{
|
||||
public:
|
||||
public:
|
||||
Ipc(bool is_server, int verbose = 0);
|
||||
|
||||
Ipc(const std::string& name, bool is_server, int verbose = 0);
|
||||
@@ -36,14 +38,14 @@ public:
|
||||
void
|
||||
write(const char* str);
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
void
|
||||
write(const T& data)
|
||||
{
|
||||
write(&data, 1, sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
void
|
||||
write(const T* data, size_t count)
|
||||
{
|
||||
@@ -56,14 +58,14 @@ public:
|
||||
void
|
||||
read(char* str);
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
void
|
||||
read(T& data)
|
||||
{
|
||||
read(&data, 1, sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
void
|
||||
read(T* data, size_t count)
|
||||
{
|
||||
@@ -73,14 +75,14 @@ public:
|
||||
void
|
||||
read(void* buffer, size_t count, size_t size);
|
||||
|
||||
protected:
|
||||
protected:
|
||||
ssize_t
|
||||
_write(const void* buffer, size_t bytes);
|
||||
|
||||
ssize_t
|
||||
_read(void* buffer, size_t bytes);
|
||||
|
||||
protected:
|
||||
protected:
|
||||
int m_verbose;
|
||||
std::string m_message;
|
||||
|
||||
@@ -92,4 +94,4 @@ protected:
|
||||
#endif
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace ipc
|
||||
|
||||
@@ -87,7 +87,7 @@ ipc_read_string(void* ipc, char* str)
|
||||
i->read(str);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
void
|
||||
ipc_write(void* ipc, const T* data, size_t count)
|
||||
{
|
||||
@@ -97,7 +97,7 @@ ipc_write(void* ipc, const T* data, size_t count)
|
||||
i->write(data, count);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
void
|
||||
ipc_read(void* ipc, T* data, size_t count)
|
||||
{
|
||||
@@ -107,8 +107,16 @@ ipc_read(void* ipc, T* data, size_t count)
|
||||
i->read(data, count);
|
||||
}
|
||||
|
||||
#define _IPC_WRITE_FN(data_type) _IPC_WRITE_FN_NAME(data_type) { return ipc_write(ipc, data, count); }
|
||||
#define _IPC_READ_FN(data_type) _IPC_READ_FN_NAME (data_type) { return ipc_read (ipc, data, count); }
|
||||
#define _IPC_WRITE_FN(data_type) \
|
||||
_IPC_WRITE_FN_NAME(data_type) \
|
||||
{ \
|
||||
return ipc_write(ipc, data, count); \
|
||||
}
|
||||
#define _IPC_READ_FN(data_type) \
|
||||
_IPC_READ_FN_NAME(data_type) \
|
||||
{ \
|
||||
return ipc_read(ipc, data, count); \
|
||||
}
|
||||
|
||||
_IPC_WRITE_FN(char);
|
||||
_IPC_WRITE_FN(int);
|
||||
|
||||
@@ -40,8 +40,10 @@ ipc_write_string(void* ipc, const char* str);
|
||||
void
|
||||
ipc_read_string(void* ipc, char* str);
|
||||
|
||||
#define _IPC_WRITE_FN_NAME(type) void ipc_write_ ## type (void* ipc, const type * data, size_t count)
|
||||
#define _IPC_READ_FN_NAME(type) void ipc_read_ ## type (void* ipc, type * data, size_t count)
|
||||
#define _IPC_WRITE_FN_NAME(type) \
|
||||
void ipc_write_##type(void* ipc, const type* data, size_t count)
|
||||
#define _IPC_READ_FN_NAME(type) \
|
||||
void ipc_read_##type(void* ipc, type* data, size_t count)
|
||||
|
||||
_IPC_WRITE_FN_NAME(char);
|
||||
_IPC_WRITE_FN_NAME(int);
|
||||
|
||||
@@ -8,8 +8,7 @@
|
||||
|
||||
#include "ipc.hpp"
|
||||
|
||||
namespace ipc
|
||||
{
|
||||
namespace ipc {
|
||||
|
||||
std::string
|
||||
default_name()
|
||||
@@ -20,8 +19,7 @@ default_name()
|
||||
void
|
||||
make_fifo(const std::string& name, unsigned int mode)
|
||||
{
|
||||
if (mkfifo(name.c_str(), mode) < 0)
|
||||
{
|
||||
if (mkfifo(name.c_str(), mode) < 0) {
|
||||
auto message = fmt::format("mkfifo '{}' : {}", name, strerror(errno));
|
||||
throw std::runtime_error(message);
|
||||
}
|
||||
@@ -35,7 +33,7 @@ init(const std::string& name, unsigned int mode)
|
||||
}
|
||||
|
||||
Ipc::Ipc(const std::string& name, bool is_server, int verbose)
|
||||
: m_verbose(verbose)
|
||||
: m_verbose(verbose)
|
||||
{
|
||||
auto fifo_open = [](std::string name, int flags) -> int {
|
||||
int fd = open(name.c_str(), flags);
|
||||
@@ -50,7 +48,8 @@ Ipc::Ipc(const std::string& name, bool is_server, int verbose)
|
||||
if (is_server) {
|
||||
m_reader_fd = fifo_open(name + "_c", O_RDONLY);
|
||||
m_writer_fd = fifo_open(name + "_s", O_WRONLY);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
m_writer_fd = fifo_open(name + "_c", O_WRONLY);
|
||||
m_reader_fd = fifo_open(name + "_s", O_RDONLY);
|
||||
}
|
||||
@@ -72,4 +71,4 @@ Ipc::_read(void* buffer, size_t bytes)
|
||||
return ::read(m_reader_fd, buffer, bytes);
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace ipc
|
||||
|
||||
@@ -11,8 +11,7 @@
|
||||
|
||||
#define UNUSED(x) (void)(x)
|
||||
|
||||
namespace ipc
|
||||
{
|
||||
namespace ipc {
|
||||
|
||||
// https://stackoverflow.com/a/17387176
|
||||
// Returns the last Win32 error, in string format.
|
||||
@@ -22,29 +21,29 @@ getLastErrorAsString()
|
||||
{
|
||||
// Get the error message ID, if any.
|
||||
DWORD errorMessageID = ::GetLastError();
|
||||
if(errorMessageID == 0) {
|
||||
return std::string(); //No error message has been recorded
|
||||
if (errorMessageID == 0) {
|
||||
return std::string(); // No error message has been recorded
|
||||
}
|
||||
|
||||
LPSTR messageBuffer = nullptr;
|
||||
|
||||
// Ask Win32 to give us the string version of that message ID.
|
||||
// The parameters we pass in, tell Win32 to create the buffer that holds the
|
||||
// message for us (because we don't yet know how long the message string will be).
|
||||
// message for us (because we don't yet know how long the message string
|
||||
// will be).
|
||||
//
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagea
|
||||
//
|
||||
size_t size = FormatMessageA(
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||
FORMAT_MESSAGE_IGNORE_INSERTS, // dwFlags
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
|
||||
| FORMAT_MESSAGE_IGNORE_INSERTS, // dwFlags
|
||||
|
||||
NULL, // lpSource
|
||||
errorMessageID, // dwMessageId
|
||||
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), // dwLanguageId
|
||||
(LPSTR)&messageBuffer, // lpBuffer
|
||||
0, // nSize
|
||||
NULL // *Arguments
|
||||
NULL, // lpSource
|
||||
errorMessageID, // dwMessageId
|
||||
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), // dwLanguageId
|
||||
(LPSTR)&messageBuffer, // lpBuffer
|
||||
0, // nSize
|
||||
NULL // *Arguments
|
||||
);
|
||||
|
||||
// Copy the error message into a std::string.
|
||||
@@ -75,7 +74,7 @@ init(const std::string& name, unsigned int mode)
|
||||
// https://habr.com/ru/post/166155/
|
||||
|
||||
Ipc::Ipc(const std::string& name, bool is_server, int verbose)
|
||||
: m_verbose(verbose)
|
||||
: m_verbose(verbose)
|
||||
{
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
@@ -83,21 +82,22 @@ Ipc::Ipc(const std::string& name, bool is_server, int verbose)
|
||||
// fmt::print("full name = {}\n", full_name);
|
||||
|
||||
auto throw_failed = [](const std::string& name) {
|
||||
auto message = fmt::format("{}() failed: {}", name, getLastErrorAsString());
|
||||
auto message
|
||||
= fmt::format("{}() failed: {}", name, getLastErrorAsString());
|
||||
throw std::runtime_error(message);
|
||||
};
|
||||
|
||||
if (is_server) {
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createnamedpipea
|
||||
m_pipe = CreateNamedPipe(
|
||||
(LPSTR)full_name.c_str(), // lpName
|
||||
PIPE_ACCESS_DUPLEX, // dwOpenMode
|
||||
PIPE_TYPE_BYTE, // dwPipeMode
|
||||
1, // nMaxInstances
|
||||
0, // nOutBufferSize
|
||||
0, // nInBufferSize
|
||||
0, // nDefaultTimeOut
|
||||
NULL // lpSecurityAttributes
|
||||
(LPSTR)full_name.c_str(), // lpName
|
||||
PIPE_ACCESS_DUPLEX, // dwOpenMode
|
||||
PIPE_TYPE_BYTE, // dwPipeMode
|
||||
1, // nMaxInstances
|
||||
0, // nOutBufferSize
|
||||
0, // nInBufferSize
|
||||
0, // nDefaultTimeOut
|
||||
NULL // lpSecurityAttributes
|
||||
);
|
||||
|
||||
if (m_pipe == INVALID_HANDLE_VALUE) {
|
||||
@@ -112,18 +112,18 @@ Ipc::Ipc(const std::string& name, bool is_server, int verbose)
|
||||
return;
|
||||
}
|
||||
|
||||
auto time_step = 500ms;
|
||||
auto time_step = 500ms;
|
||||
auto time_current = 0ms;
|
||||
while (time_current < 10s) {
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
|
||||
m_pipe = CreateFile(
|
||||
(LPSTR)full_name.c_str(), // lpFileName
|
||||
GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
|
||||
0, // dwShareMode
|
||||
NULL, // lpSecurityAttributes
|
||||
OPEN_EXISTING, // dwCreationDisposition
|
||||
FILE_ATTRIBUTE_NORMAL, // dwFlagsAndAttributes
|
||||
NULL // hTemplateFile
|
||||
(LPSTR)full_name.c_str(), // lpFileName
|
||||
GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
|
||||
0, // dwShareMode
|
||||
NULL, // lpSecurityAttributes
|
||||
OPEN_EXISTING, // dwCreationDisposition
|
||||
FILE_ATTRIBUTE_NORMAL, // dwFlagsAndAttributes
|
||||
NULL // hTemplateFile
|
||||
);
|
||||
|
||||
if (m_pipe != INVALID_HANDLE_VALUE) {
|
||||
@@ -150,12 +150,12 @@ Ipc::_write(const void* buffer, size_t bytes)
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile
|
||||
|
||||
DWORD numBytesWritten = 0;
|
||||
auto result = WriteFile(
|
||||
m_pipe, // hFile
|
||||
buffer, // lpBuffer
|
||||
bytes, // nNumberOfBytesToWrite
|
||||
&numBytesWritten, // lpNumberOfBytesWritten
|
||||
NULL // lpOverlapped
|
||||
auto result = WriteFile(
|
||||
m_pipe, // hFile
|
||||
buffer, // lpBuffer
|
||||
bytes, // nNumberOfBytesToWrite
|
||||
&numBytesWritten, // lpNumberOfBytesWritten
|
||||
NULL // lpOverlapped
|
||||
);
|
||||
|
||||
if (!result) {
|
||||
@@ -171,12 +171,12 @@ Ipc::_read(void* buffer, size_t bytes)
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile
|
||||
|
||||
DWORD numBytesRead = 0;
|
||||
auto result = ReadFile(
|
||||
m_pipe, // hFile
|
||||
buffer, // lpBuffer
|
||||
bytes, // nNumberOfBytesToRead
|
||||
&numBytesRead, // lpNumberOfBytesRead
|
||||
NULL // lpOverlapped
|
||||
auto result = ReadFile(
|
||||
m_pipe, // hFile
|
||||
buffer, // lpBuffer
|
||||
bytes, // nNumberOfBytesToRead
|
||||
&numBytesRead, // lpNumberOfBytesRead
|
||||
NULL // lpOverlapped
|
||||
);
|
||||
|
||||
if (!result) {
|
||||
@@ -186,4 +186,4 @@ Ipc::_read(void* buffer, size_t bytes)
|
||||
return numBytesRead;
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace ipc
|
||||
|
||||
Reference in New Issue
Block a user