-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
47f1565
commit 244da03
Showing
4 changed files
with
78 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
#ifndef SERVER_H | ||
#define SERVER_H | ||
#ifndef SERIALSERVER_H | ||
#define SERIALSERVER_H | ||
|
||
#include "common.hpp" | ||
#include "ISerialPort.h" | ||
#include "RealSerialPort.h" | ||
|
||
using namespace boost::asio; | ||
using ip::tcp; | ||
using std::string; | ||
#include <boost/asio.hpp> | ||
#include <array> | ||
|
||
class SerialServer { | ||
public: | ||
SerialServer(boost::asio::io_service& io_service, const std::string& device, unsigned int baud_rate); | ||
|
||
void run(); | ||
|
||
private: | ||
void start_accept(); | ||
void handle_session(); | ||
void async_read_socket(); | ||
void async_read_serial(); | ||
|
||
boost::asio::io_service& io_service_; | ||
ISerialPort& serial_; | ||
boost::asio::ip::tcp::acceptor& acceptor_; | ||
boost::asio::ip::tcp::acceptor acceptor_; | ||
boost::asio::ip::tcp::socket socket_; | ||
std::array<char, 1024> buf; // Buffer for data | ||
|
||
void start_accept(); | ||
void do_read_write(); | ||
public: | ||
SerialServer(boost::asio::io_service& io, ISerialPort& serial, boost::asio::ip::tcp::acceptor& acceptor); | ||
void run(); | ||
RealSerialPort serial_port_; | ||
std::array<char, 1024> buffer_; | ||
}; | ||
|
||
#endif // SERVER_H | ||
#endif // SERIALSERVER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,63 @@ | ||
#include "SerialServer.h" | ||
#include <boost/log/trivial.hpp> | ||
|
||
using namespace boost::asio; | ||
using namespace boost::program_options; | ||
using ip::tcp; | ||
using std::string; | ||
using std::endl; | ||
|
||
SerialServer::SerialServer(io_service& io, ISerialPort& serial, tcp::acceptor& acceptor) | ||
: io_service_(io), serial_(serial), acceptor_(acceptor), socket_(io) { | ||
BOOST_LOG_TRIVIAL(info) << "Starting server and waiting for connection..."; | ||
start_accept(); | ||
} | ||
SerialServer::SerialServer(boost::asio::io_service& io_service, const std::string& device, unsigned int baud_rate) | ||
: io_service_(io_service), | ||
acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 3333)), | ||
socket_(io_service), | ||
serial_port_(io_service) { | ||
serial_port_.open(device, boost::asio::serial_port_base::baud_rate(baud_rate)); | ||
} | ||
|
||
void SerialServer::run() { | ||
BOOST_LOG_TRIVIAL(info) << "Server is running."; | ||
start_accept(); | ||
io_service_.run(); | ||
BOOST_LOG_TRIVIAL(info) << "Server stopped."; | ||
} | ||
|
||
void SerialServer::start_accept() { | ||
acceptor_.async_accept(socket_, [this](boost::system::error_code ec) { | ||
if (!ec) { | ||
BOOST_LOG_TRIVIAL(info) << "Client connected. Starting to handle read/write operations."; | ||
do_read_write(); | ||
} else { | ||
BOOST_LOG_TRIVIAL(error) << "Error accepting connection: " << ec.message(); | ||
start_accept(); | ||
handle_session(); | ||
} | ||
start_accept(); | ||
}); | ||
} | ||
|
||
void SerialServer::do_read_write() { | ||
// Asynchronous read from serial port | ||
serial_.async_read_some(boost::asio::buffer(buf), [this](boost::system::error_code ec, std::size_t length) { | ||
if (!ec && length > 0) { | ||
// Data is available | ||
async_write(socket_, boost::asio::buffer(buf, length), [this](boost::system::error_code ec, std::size_t) { | ||
void SerialServer::handle_session() { | ||
async_read_socket(); | ||
async_read_serial(); | ||
} | ||
|
||
void SerialServer::async_read_socket() { | ||
socket_.async_read_some(boost::asio::buffer(buffer_), [this](boost::system::error_code ec, std::size_t length) { | ||
if (!ec) { | ||
serial_port_.async_write(boost::asio::buffer(buffer_, length), [this](boost::system::error_code ec, std::size_t) { | ||
if (!ec) { | ||
BOOST_LOG_TRIVIAL(info) << "Data successfully written to client."; | ||
async_read_socket(); | ||
} else { | ||
BOOST_LOG_TRIVIAL(error) << "Error writing to client: " << ec.message(); | ||
} | ||
}); | ||
} else if (!ec) { | ||
BOOST_LOG_TRIVIAL(info) << "No data received from serial. Waiting..."; | ||
} else { | ||
BOOST_LOG_TRIVIAL(error) << "Error reading from serial port: " << ec.message(); | ||
BOOST_LOG_TRIVIAL(error) << "Read error on socket: " << ec.message(); | ||
socket_.close(); | ||
} | ||
}); | ||
} | ||
|
||
void SerialServer::async_read_serial() { | ||
serial_port_.async_read_some(boost::asio::buffer(buffer_), [this](boost::system::error_code ec, std::size_t length) { | ||
if (!ec) { | ||
boost::asio::async_write(socket_, boost::asio::buffer(buffer_, length), [this](boost::system::error_code ec, std::size_t) { | ||
if (!ec) { | ||
async_read_serial(); | ||
} else { | ||
BOOST_LOG_TRIVIAL(error) << "Error sending to socket: " << ec.message(); | ||
} | ||
}); | ||
} else { | ||
BOOST_LOG_TRIVIAL(error) << "Read error on serial port: " << ec.message(); | ||
socket_.close(); | ||
} | ||
do_read_write(); // Continue the loop | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters