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

View File

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

View File

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

View File

@@ -1,55 +1,46 @@
#include <iostream> #include <iostream>
#include "client.hpp"
#include "protocol.hpp" #include "protocol.hpp"
#include "server.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[]) void run_client() {
{
try
{
boost::asio::io_context io_context; boost::asio::io_context io_context;
if(argc == 1) spdlog::info("Running client...");
{
std::cout << "Running client\n";
client s(io_context, boost::asio::ip::make_address("0.0.0.0"), 9000, 0); client s(io_context, boost::asio::ip::make_address("0.0.0.0"), 9000, 0);
s.init(); s.init();
io_context.run(); io_context.run();
} }
if(argc == 2) void run_server() {
{ boost::asio::io_context io_context;
std::cout << "Running server\n"; spdlog::info("Running client...");
server s(io_context, 9000); server s(io_context, 9000);
io_context.run(); 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"; std::cerr << "Exception: " << e.what() << "\n";
} }
return 0; 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;
} }