Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minify the binary #8

Merged
merged 31 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6e83162
Drop boost logger, simplify cmake targets
janekbaraniewski May 1, 2024
544aba9
Client works, remove readme dislaimer
janekbaraniewski May 1, 2024
a681974
Cleanup logs in serial client
janekbaraniewski May 1, 2024
ad33793
Cleanup devel debuug minimal clients
janekbaraniewski May 1, 2024
aea675c
Remove one streamDescriptor usage
janekbaraniewski May 1, 2024
28c1c90
Change another descriptor to plain integer
janekbaraniewski May 1, 2024
e545aff
Cleanup boost lib usage from client
janekbaraniewski May 1, 2024
4980cb5
Cleanup vsp implementation
janekbaraniewski May 1, 2024
a8eaa1a
Drop dependency on boost
janekbaraniewski May 1, 2024
bbaf28d
Add missing import
janekbaraniewski May 1, 2024
2190ed0
Update logs
janekbaraniewski May 1, 2024
26c6c55
Update logos
janekbaraniewski May 2, 2024
b4e55d0
Exit 1
janekbaraniewski May 2, 2024
ced8509
Update readme a bit - check if TOC works on gh
janekbaraniewski May 4, 2024
f7f8dc1
Update readme
janekbaraniewski May 4, 2024
b0cb230
Update readme
janekbaraniewski May 4, 2024
f96944f
Alternative implementation of server
janekbaraniewski May 5, 2024
54bb442
Fix log message
janekbaraniewski May 5, 2024
a67b3ce
Update logging
janekbaraniewski May 5, 2024
9f1b7cc
Update logging
janekbaraniewski May 5, 2024
e35e765
Update logging
janekbaraniewski May 5, 2024
f9fbe93
Update server implementation
janekbaraniewski May 5, 2024
03bc5fa
Add more logging
janekbaraniewski May 5, 2024
6172e95
Add reader thread, add more logging
janekbaraniewski May 5, 2024
df4ac21
More logging
janekbaraniewski May 5, 2024
6014a87
Add missing startReading when client connects
janekbaraniewski May 5, 2024
ea7fb04
Add more logs
janekbaraniewski May 5, 2024
b6b937d
Update serial device flags
janekbaraniewski May 5, 2024
797ac15
Update serial device flags
janekbaraniewski May 5, 2024
10593d1
Update serial device flags
janekbaraniewski May 5, 2024
9b17bb2
Cleanup
janekbaraniewski May 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading