use spdlog and start using google coding style

This commit is contained in:
2022-04-26 16:11:49 +02:00
parent 15657ae475
commit 32fdc8f891
4 changed files with 91 additions and 118 deletions

View File

@@ -1,19 +1,18 @@
#pragma once
#include <boost/asio.hpp>
#include "boost/bind.hpp"
#include <functional>
#include <sstream>
#include <string>
#include <vector>
class client
{
#include "boost/bind.hpp"
class client {
public:
client(boost::asio::io_service& io_service,
const boost::asio::ip::address& multicast_address,
short multicast_port,
unsigned short tcp_port);
short multicast_port, unsigned short tcp_port);
void init();
void set_outbound_interface(const boost::asio::ip::address_v4& address);

View File

@@ -1,60 +1,52 @@
#pragma once
#include <boost/asio.hpp>
#include <cstdlib>
#include <iostream>
#include <boost/asio.hpp>
#include "protocol.hpp"
#include "spdlog/spdlog.h"
using boost::asio::ip::udp;
class server
{
class server {
public:
server(boost::asio::io_context& io_context, short port)
: socket_(io_context, udp::endpoint(udp::v4(), port))
{
: socket_(io_context, udp::endpoint(udp::v4(), port)) {
do_receive();
}
void do_receive()
{
std::cout << "do_receive\n";
void do_receive() {
spdlog::debug("do receive");
socket_.async_receive_from(
boost::asio::buffer(data_, max_length), sender_endpoint_,
[this](boost::system::error_code ec, std::size_t bytes_recvd)
{
if (!ec && bytes_recvd > 0)
{
[this](boost::system::error_code ec, std::size_t bytes_recvd) {
if (!ec && bytes_recvd > 0) {
std::cout << data_ << std::endl;
auto* msg_obj = reinterpret_cast<commons::protocol::generic_message_base*>(data_);
auto* msg_obj =
reinterpret_cast<commons::protocol::generic_message_base*>(
data_);
spdlog::debug("Object Type: {}",
static_cast<int>(msg_obj->get_type()));
spdlog::debug("Object Size: {}", msg_obj->get_size());
std::cout << "Received Type: " << static_cast<int>(msg_obj->get_type()) << "\n";
std::cout << "Received Size: " << msg_obj->get_size() << "\n";
auto* casted_obj = reinterpret_cast<commons::protocol::draw_rectangle*>(msg_obj);
std::cout << "Positon: X = " << casted_obj->position.x
<< " Y = " << casted_obj->position.y << std::endl;
auto* casted_obj =
reinterpret_cast<commons::protocol::draw_rectangle*>(msg_obj);
spdlog::debug("Position: X = {}, Y = {}", casted_obj->position.x,
casted_obj->position.y);
do_send(bytes_recvd);
}
else
{
} else {
do_receive();
}
});
}
void do_send(std::size_t length)
{
socket_.async_send_to(
boost::asio::buffer(data_, length), sender_endpoint_,
[this](boost::system::error_code /*ec*/, std::size_t /*bytes_sent*/)
{
do_receive();
});
void do_send(std::size_t length) {
socket_.async_send_to(boost::asio::buffer(data_, length), sender_endpoint_,
[this](boost::system::error_code /*ec*/,
std::size_t /*bytes_sent*/) { do_receive(); });
}
private:

View File

@@ -1,62 +1,53 @@
#include "client.hpp"
#include "protocol.hpp"
#include <iostream>
#include "protocol.hpp"
#include "spdlog/spdlog.h"
client::client(boost::asio::io_service& io_service,
const boost::asio::ip::address& multicast_address,
short multicast_port,
unsigned short tcp_port)
: endpoint_(multicast_address, multicast_port)
, socket_(io_service, endpoint_.protocol())
, timer_(io_service)
, port_(tcp_port)
{}
short multicast_port, unsigned short tcp_port)
: endpoint_(multicast_address, multicast_port),
socket_(io_service, endpoint_.protocol()),
timer_(io_service),
port_(tcp_port) {}
void client::init()
{
std::cout << "Send test msg\n";
void client::init() {
spdlog::debug("Send test msg");
auto msg = std::make_unique<commons::protocol::draw_rectangle>();
msg->position = commons::protocol::vec2{3, 2};
void* void_ptr = reinterpret_cast<void*>(msg.get());
auto size = sizeof(*msg.get());
std::cout << "Object Type: " << static_cast<int>(msg->get_type()) << "\n";
std::cout << "Object size: " << size << "\n";
std::cout << "Positon: X = " << msg->position.x
<< " Y = " << msg->position.y << std::endl;
spdlog::debug("Object Type: {}", static_cast<int>(msg->get_type()));
spdlog::debug("Object Size: {}", size);
spdlog::debug("Position: X = {}, Y = {}", msg->position.x, msg->position.y);
socket_.async_send_to(
boost::asio::buffer(void_ptr, size), endpoint_,
std::bind(&client::handle_send_to, this,
std::placeholders::_1));
std::bind(&client::handle_send_to, this, std::placeholders::_1));
}
void client::set_outbound_interface(const boost::asio::ip::address_v4& address)
{
void client::set_outbound_interface(
const boost::asio::ip::address_v4& address) {
boost::asio::ip::multicast::outbound_interface option(address);
socket_.set_option(option);
}
void client::handle_send_to(const boost::system::error_code& error)
{
if(!error)
{
void client::handle_send_to(const boost::system::error_code& error) {
if (!error) {
timer_.expires_from_now(boost::posix_time::seconds(1));
timer_.async_wait( std::bind(&client::handle_timeout, this,
std::placeholders::_1));
timer_.async_wait(
std::bind(&client::handle_timeout, this, std::placeholders::_1));
}
}
void client::handle_timeout(const boost::system::error_code& error)
{
if (!error)
{
void client::handle_timeout(const boost::system::error_code& error) {
if (!error) {
// spdlog::trace("Resending Message: '{}'", "TestMessage");
std::cout << "done\n";
spdlog::debug("done");
// socket_.async_send_to(
// boost::asio::buffer("TestMessage"), endpoint_,
// std::bind(&client::handle_send_to, this,

View File

@@ -1,55 +1,46 @@
#include <iostream>
#include "client.hpp"
#include "protocol.hpp"
#include "server.hpp"
#include "client.hpp"
#include "spdlog/spdlog.h"
void init_spdlog() {
spdlog::set_level(spdlog::level::debug); // Set global log level to debug
// spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v");
spdlog::debug("Setting Loglevel to debug...");
}
int main(int argc, char* argv[])
{
try
{
void run_client() {
boost::asio::io_context io_context;
if(argc == 1)
{
std::cout << "Running client\n";
spdlog::info("Running client...");
client s(io_context, boost::asio::ip::make_address("0.0.0.0"), 9000, 0);
s.init();
io_context.run();
}
if(argc == 2)
{
std::cout << "Running server\n";
void run_server() {
boost::asio::io_context io_context;
spdlog::info("Running client...");
server s(io_context, 9000);
io_context.run();
}
int main(int argc, char *argv[]) {
init_spdlog();
try {
if (argc == 1) {
run_client();
}
catch (std::exception& e)
{
if (argc == 2) {
run_server();
}
} catch (std::exception &e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
// using namespace commons::protocol;
// auto draw_msg = draw_rectangle{};
//
// for(int i = 0; i < 3; ++i)
// {
// std::cout << "Size of " << i << " is " << get_size(static_cast<Type>(i)) << "\n";
// }
//
// auto rectangle = std::make_unique<draw_pixel>();
// void* void_ptr = reinterpret_cast<void*>(rectangle.get());
//
// generic_message_base* rectangle_ptr = reinterpret_cast<generic_message_base*>(void_ptr);
//
// std::cout << "Type: " << static_cast<int>(rectangle_ptr->get_type()) << "\n";
// std::cout << "Size: " << static_cast<int>(rectangle_ptr->get_size()) << "\n";
//
// auto* final_msg = get_object(rectangle_ptr->get_type(), rectangle_ptr);
//
// return 0;
}