-
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.
Move network code into self-contained class
- Loading branch information
Showing
6 changed files
with
171 additions
and
57 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
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @file network.hpp | ||
* @brief Header file for blink1_control::network | ||
* | ||
* Including this file will give access to all classes within blink1_control::network | ||
*/ | ||
#pragma once | ||
|
||
#include "network/NetworkManager.hpp" | ||
|
||
namespace blink1_control { | ||
|
||
/** | ||
* Classes related to network connections | ||
*/ | ||
namespace network { | ||
|
||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* @file NetworkManager.hpp | ||
* @brief Header file for blink1_control::network::NetworkManager. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#pragma GCC diagnostic push | ||
#pragma GCC diagnostic ignored "-Wsign-conversion" | ||
#pragma GCC diagnostic ignored "-Wold-style-cast" | ||
#pragma GCC diagnostic ignored "-Wshorten-64-to-32" | ||
#pragma GCC diagnostic ignored "-Wshadow" | ||
#include <boost/asio.hpp> | ||
#include <boost/thread/thread.hpp> | ||
#pragma GCC diagnostic pop | ||
|
||
namespace blink1_control::network { | ||
|
||
/** | ||
* This class creates and maintains network connections. | ||
*/ | ||
class NetworkManager { | ||
|
||
std::shared_ptr<boost::asio::io_context> ioc; | ||
boost::asio::local::stream_protocol::endpoint endpoint; | ||
boost::asio::local::stream_protocol::acceptor acceptor; | ||
std::unique_ptr<boost::thread> ioThread; | ||
|
||
void acceptHandler(const boost::system::error_code& error, boost::asio::local::stream_protocol::socket peer); | ||
|
||
void startAccept(); | ||
|
||
public: | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param socketPath The path to bind the socket to. | ||
*/ | ||
NetworkManager(std::string_view socketPath); | ||
|
||
/** | ||
* Destructor. | ||
*/ | ||
~NetworkManager(); | ||
|
||
NetworkManager(const NetworkManager& other) = delete; | ||
NetworkManager(NetworkManager&& other) = delete; | ||
NetworkManager& operator=(const NetworkManager& other) = delete; | ||
NetworkManager& operator=(NetworkManager&& other) = delete; | ||
|
||
/** | ||
* Starts the network I/O service. | ||
*/ | ||
void start(); | ||
|
||
/** | ||
* Stops the network I/O service. | ||
*/ | ||
void stop(); | ||
|
||
}; | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "network/NetworkManager.hpp" | ||
|
||
#include <iostream> | ||
|
||
namespace b = boost; | ||
namespace ba = boost::asio; | ||
namespace bal = boost::asio::local; | ||
|
||
static void runIo(std::shared_ptr<ba::io_context> ioContext) { | ||
ba::io_service::work work(*ioContext); | ||
std::cout << "Starting I/O service\n"; | ||
ioContext->run(); | ||
std::cout << "I/O service finished\n"; | ||
} | ||
|
||
namespace blink1_control::network { | ||
|
||
NetworkManager::NetworkManager(std::string_view socketPath) : | ||
ioc(std::make_shared<ba::io_context>()), | ||
endpoint(socketPath), | ||
acceptor(*ioc, endpoint), | ||
ioThread(nullptr) | ||
{} | ||
|
||
NetworkManager::~NetworkManager() { | ||
stop(); | ||
} | ||
|
||
void NetworkManager::start() { | ||
ioc->restart(); | ||
acceptor.listen(); | ||
startAccept(); | ||
ioThread = std::make_unique<boost::thread>(std::bind_front(&runIo, this->ioc)); | ||
} | ||
|
||
void NetworkManager::stop() { | ||
acceptor.cancel(); | ||
ioc->stop(); | ||
} | ||
|
||
void NetworkManager::startAccept() { | ||
acceptor.async_accept(std::bind_front(&NetworkManager::acceptHandler, this)); | ||
} | ||
|
||
void NetworkManager::acceptHandler(const b::system::error_code& error, bal::stream_protocol::socket peer) { | ||
if (error) { | ||
|
||
if (error.value() != b::system::errc::operation_canceled) { | ||
std::cout << "Error accepting connection: " << error.what() << "\n"; | ||
} | ||
|
||
return; | ||
} | ||
|
||
std::cout << "Got connection\n"; | ||
bal::stream_protocol::iostream clientIos(std::move(peer)); | ||
std::string data; | ||
std::cout << "Outbound: \"Hello, World!\"\n"; | ||
clientIos << "Hello, World!\n"; | ||
std::getline(clientIos, data); | ||
std::cout << "Inbound: " << data << "\n"; | ||
std::cout << "Closing connection\n"; | ||
|
||
startAccept(); | ||
} | ||
|
||
} |