Refactored os.cpp to os_unix.cpp and os_windows.cpp

This commit is contained in:
gabime
2024-01-14 12:09:22 +02:00
parent 3954caccc1
commit 885b796447
9 changed files with 391 additions and 291 deletions

View File

@@ -4,7 +4,7 @@
#pragma once
#ifdef _WIN32
#error include tcp_client-windows.h instead
#error include tcp_client_windows.h instead
#endif
// tcp client helper
@@ -23,7 +23,7 @@
namespace spdlog {
namespace details {
class tcp_client {
class tcp_client_unix {
int socket_ = -1;
public:
@@ -38,7 +38,7 @@ public:
int fd() const { return socket_; }
~tcp_client() { close(); }
~tcp_client_unix() { close(); }
// try to connect or throw on failure
void connect(const std::string &host, int port) {

View File

@@ -22,7 +22,7 @@
namespace spdlog {
namespace details {
class tcp_client {
class tcp_client_unix {
SOCKET socket_ = INVALID_SOCKET;
static void init_winsock_() {
@@ -42,9 +42,9 @@ class tcp_client {
}
public:
tcp_client() { init_winsock_(); }
tcp_client_unix() { init_winsock_(); }
~tcp_client() {
~tcp_client_unix() {
close();
::WSACleanup();
}

View File

@@ -10,7 +10,7 @@
#include "./os.h"
#ifdef _WIN32
#error "include udp_client-windows.h instead"
#error "include udp_client_windows.h instead"
#endif
#include <arpa/inet.h>
@@ -26,7 +26,7 @@
namespace spdlog {
namespace details {
class udp_client {
class udp_client_unix {
static constexpr int TX_BUFFER_SIZE = 1024 * 10;
int socket_ = -1;
struct sockaddr_in sockAddr_;
@@ -39,7 +39,7 @@ class udp_client {
}
public:
udp_client(const std::string &host, uint16_t port) {
udp_client_unix(const std::string &host, uint16_t port) {
socket_ = ::socket(PF_INET, SOCK_DGRAM, 0);
if (socket_ < 0) {
throw_spdlog_ex("error: Create Socket Failed!");
@@ -63,7 +63,7 @@ public:
::memset(sockAddr_.sin_zero, 0x00, sizeof(sockAddr_.sin_zero));
}
~udp_client() { cleanup_(); }
~udp_client_unix() { cleanup_(); }
int fd() const { return socket_; }

View File

@@ -25,7 +25,7 @@
namespace spdlog {
namespace details {
class udp_client {
class udp_client_unix {
static constexpr int TX_BUFFER_SIZE = 1024 * 10;
SOCKET socket_ = INVALID_SOCKET;
sockaddr_in addr_ = {};
@@ -55,7 +55,7 @@ class udp_client {
}
public:
udp_client(const std::string &host, uint16_t port) {
udp_client_unix(const std::string &host, uint16_t port) {
init_winsock_();
addr_.sin_family = PF_INET;
@@ -83,7 +83,7 @@ public:
}
}
~udp_client() { cleanup_(); }
~udp_client_unix() { cleanup_(); }
SOCKET fd() const { return socket_; }

View File

@@ -9,7 +9,7 @@
#ifdef _WIN32
#include "../details/tcp_client-windows.h"
#else
#include "../details/tcp_client.h"
#include "../details/tcp_client_unix.h"
#endif
#include <chrono>
@@ -65,7 +65,7 @@ protected:
void flush_() override {}
tcp_sink_config config_;
details::tcp_client client_;
details::tcp_client_unix client_;
};
using tcp_sink_mt = tcp_sink<std::mutex>;

View File

@@ -9,7 +9,7 @@
#ifdef _WIN32
#include "../details/udp_client-windows.h"
#else
#include "../details/udp_client.h"
#include "../details/udp_client_unix.h"
#endif
#include <chrono>
@@ -49,7 +49,7 @@ protected:
}
void flush_() override {}
details::udp_client client_;
details::udp_client_unix client_;
};
using udp_sink_mt = udp_sink<std::mutex>;