Skip to content

Commit

Permalink
Merge pull request #8 from janekbaraniewski/make-binary-as-minimal-as…
Browse files Browse the repository at this point in the history
…-possible

minify the binary
  • Loading branch information
janekbaraniewski authored May 5, 2024
2 parents 6d5a4ff + 9b17bb2 commit 8f6e548
Show file tree
Hide file tree
Showing 29 changed files with 561 additions and 728 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ jobs:
with:
python-version: '3.x'

- name: Install dependencies (Ubuntu)
if: startsWith(matrix.config.os, 'ubuntu')
run: |
sudo apt-get update
sudo apt-get install -y libboost-all-dev
- name: Install dependencies (macOS)
if: startsWith(matrix.config.os, 'macos')
run: |
brew install boost
# - name: Install dependencies (Ubuntu)
# if: startsWith(matrix.config.os, 'ubuntu')
# run: |
# sudo apt-get update
# sudo apt-get install -y libboost-all-dev

# - name: Install dependencies (macOS)
# if: startsWith(matrix.config.os, 'macos')
# run: |
# brew install boost

- name: Install dependencies (Windows)
if: startsWith(matrix.config.os, 'windows')
Expand Down
26 changes: 13 additions & 13 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ jobs:
with:
python-version: '3.x'

- name: Install dependencies (Ubuntu)
if: startsWith(matrix.config.os, 'ubuntu')
run: |
sudo apt-get update
sudo apt-get install -y libboost-all-dev
# - name: Install dependencies (Ubuntu)
# if: startsWith(matrix.config.os, 'ubuntu')
# run: |
# sudo apt-get update
# sudo apt-get install -y libboost-all-dev

- name: Install dependencies (macOS)
if: startsWith(matrix.config.os, 'macos')
run: |
brew install boost
# - name: Install dependencies (macOS)
# if: startsWith(matrix.config.os, 'macos')
# run: |
# brew install boost

- name: Install dependencies (Windows)
if: startsWith(matrix.config.os, 'windows')
run: |
choco install boost-msvc-14.2
# - name: Install dependencies (Windows)
# if: startsWith(matrix.config.os, 'windows')
# run: |
# choco install boost-msvc-14.2

- name: Set up CMake (All platforms)
uses: lukka/get-cmake@latest
Expand Down
49 changes: 10 additions & 39 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ project(SerialNetworkBridge LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find Boost libraries
find_package(Boost 1.65 REQUIRED COMPONENTS system program_options log_setup log date_time)

include(FetchContent)
FetchContent_Declare(
googletest
Expand All @@ -23,48 +20,22 @@ endif()

include_directories(${CMAKE_SOURCE_DIR}/include)

add_library(logging src/logging.cpp)
target_include_directories(logging PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(logging PUBLIC
Boost::log
Boost::log_setup
Boost::date_time
# Create core library without Logger.cpp, which is now in the logging library
add_library(core_lib
src/Logger.cpp
src/SerialServer.cpp
src/SerialClient.cpp
src/VirtualSerialPort.cpp
src/SerialPort.cpp
src/TCPServer.cpp
)

add_library(core_lib src/logging.cpp src/SerialServer.cpp src/SerialClient.cpp src/VirtualSerialPort.cpp)
target_include_directories(core_lib PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(core_lib PRIVATE
Boost::system
Boost::program_options
Boost::log
Boost::log_setup
Boost::date_time
logging
target_link_libraries(core_lib PUBLIC
pthread
)

# Create the executable
add_executable(ser2net2ser src/main.cpp)
target_link_libraries(ser2net2ser PRIVATE
core_lib
logging
Boost::system
Boost::program_options
Boost::log
Boost::log_setup
Boost::date_time
)

# # TODO: REENABLE TESTS
# enable_testing()
# add_executable(test_app tests/test_server.cpp)
# target_compile_definitions(test_app PRIVATE UNIT_TEST)
# target_link_libraries(test_app PRIVATE
# serial_server_lib # this includes server.cpp
# Boost::system
# GTest::gtest_main
# GTest::gtest
# GTest::gmock
# )

# include(GoogleTest)
# gtest_discover_tests(test_app EXTRA_ARGS --gtest_color=yes)
14 changes: 1 addition & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ WORKDIR /app
# Install build dependencies
RUN apt-get update && apt-get install -y \
g++ \
cmake \
libboost-system-dev \
libboost-program-options-dev \
libboost-log-dev \
libboost-date-time-dev
cmake

# Copy source code
COPY . /app
Expand All @@ -23,14 +19,6 @@ FROM ubuntu:20.04

WORKDIR /app

# Install runtime dependencies only
RUN apt-get update && apt-get install -y \
libboost-system1.71.0 \
libboost-program-options1.71.0 \
libboost-log1.71.0 \
libboost-date-time1.71.0 && \
rm -rf /var/lib/apt/lists/*

# Copy binaries from the builder stage
COPY --from=builder /app/ser2net2ser /usr/local/bin/ser2net2ser

Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# ser2net2ser

> WIP: server seems to work fine, client still has some issues
## Motivation

I've been using `socat` + `ser2net` for sharing serial devices over network for some time, mainly in my other project -> [kubeserial]. I've run into few limitations so I created this project to recreate the behaviour of `socat` + `ser2net` and then solve connection issues I've been having.

## Requirements

- Linux or macOS operating system
- Boost libraries (system, program_options, log_setup, log, date_time)
- CMake for building the project
- Docker (optional) for containerization

Expand Down Expand Up @@ -66,3 +67,7 @@ Dockerfiles for both the server and client are included. Build and run the conta
```bash
make build-images
```

<!-- Links -->

[kubeserial]: https://github.com/janekbaraniewski/kubeserial "KubeSerial"
10 changes: 4 additions & 6 deletions include/ISerialPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
#define ISERIALPORT_H

#include <string>
#include <boost/asio.hpp>
#include <functional>

using namespace boost::asio;
using std::string;

class ISerialPort {
public:
virtual void open(const string& device, boost::asio::serial_port_base::baud_rate baudRate) = 0;
// virtual void set_option(const serial_port_base::baud_rate& option) = 0;
virtual void async_read_some(const boost::asio::mutable_buffer& buffer, std::function<void(const boost::system::error_code&, std::size_t)> handler) = 0;
virtual void async_write(const boost::asio::const_buffer& buffer, std::function<void(const boost::system::error_code&, std::size_t)> handler) = 0;
virtual void open(const string& device, unsigned int baudRate) = 0;
virtual ssize_t async_read_some(char* buffer, size_t size) = 0;
virtual ssize_t async_write(const char* buffer, size_t size) = 0;
virtual ~ISerialPort() = default;
};

Expand Down
37 changes: 37 additions & 0 deletions include/Logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef LOGGER_H
#define LOGGER_H

#include <sstream>
#include <mutex>
#include <string>

class Logger {
public:
enum class Level {
Info,
Warning,
Error
};

private:
std::ostringstream stream;
Level logLevel;
static std::mutex mtx;

public:
Logger(Level level = Level::Info);
~Logger();

// Delete copy constructor and assignment operator
Logger(const Logger&) = delete;
Logger& operator=(const Logger&) = delete;
Logger& operator<<(std::ostream& (*pf)(std::ostream&));
static std::string levelToString(Level level);

template <typename T>
Logger& operator<<(const T& msg);
};

#include "Logger.inl" // Include template implementation

#endif // LOGGER_H
11 changes: 11 additions & 0 deletions include/Logger.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
template <typename T>
inline Logger& Logger::operator<<(const T& msg) {
stream << msg;
return *this;
}

// Handle ostream manipulators like std::endl
inline Logger& Logger::operator<<(std::ostream& (*pf)(std::ostream&)) {
stream << pf;
return *this;
}
55 changes: 0 additions & 55 deletions include/RealSerialPort.h

This file was deleted.

4 changes: 1 addition & 3 deletions include/SerialClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "VirtualSerialPort.h"
#include "SocketClient.h"

using namespace boost::asio;
using ip::tcp;
using std::string;

class SerialClient {
Expand All @@ -15,7 +13,7 @@ class SerialClient {
VirtualSerialPort vsp_;

public:
SerialClient(boost::asio::io_service& io_service, const std::string& server_ip, unsigned short server_port, const std::string& vsp_name);
SerialClient(const std::string& server_ip, unsigned short server_port, const std::string& vsp_name);
SocketClient socketClient_;
void run();
};
Expand Down
33 changes: 33 additions & 0 deletions include/SerialPort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef SERIALPORT_H
#define SERIALPORT_H

#include <thread>
#include <atomic>
#include <queue>
#include <mutex>
#include <condition_variable>

#include "Logger.h"

class SerialPort {
private:
int serial_fd;
std::thread read_thread;
std::atomic<bool> keep_reading;
std::queue<std::string> read_buffer;
std::mutex mtx;
std::condition_variable cv;

void readLoop();
void configurePort(int baud_rate);

public:
SerialPort(const std::string& device, int baud_rate);
~SerialPort();
void startReading();
void stopReading();
void writeData(const std::string& data);
std::string readData();
};

#endif // SERIALPORT_H
Loading

0 comments on commit 8f6e548

Please sign in to comment.