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

Remove redundant code, update logging with spdlog #2

Merged
merged 8 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 7 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ name: ci

on:
push:
branches: [ main ]
paths-ignore:
- '**.md'
branches: [main]
paths-ignore: ['**.md']
pull_request:
branches: [ main ]
paths-ignore:
- '**.md'
branches: [main]
paths-ignore: ['**.md']
workflow_dispatch:

jobs:
ci:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: true

- name: Install Dependencies
run: |
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,3 @@ build
*.exe
*.out
*.app

# IDEs
.vscode
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "spdlog"]
path = spdlog
url = git@github.com:gabime/spdlog.git
15 changes: 15 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"configurations": [
{
"name": "Linux",
"includePath": ["${workspaceFolder}/**"],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "linux-clang-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
79 changes: 79 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"cSpell.words": ["spdlog"],
"files.associations": {
"*.json": "json",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"any": "cpp",
"array": "cpp",
"atomic": "cpp",
"strstream": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"coroutine": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"regex": "cpp",
"source_location": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"ostream": "cpp",
"ranges": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cfenv": "cpp",
"cinttypes": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"variant": "cpp",
"*.ipp": "cpp"
}
}
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ cmake_minimum_required(VERSION 3.10)

project(TcpClientServerApp VERSION 0.1.0)

add_subdirectory(server)
add_subdirectory(client)
add_subdirectory(server)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ sequenceDiagram
## Build

```shell
git clone git@github.com:iamazeem/TcpClientServerApp.git
git clone --recursive git@github.com:iamazeem/TcpClientServerApp.git
cd TcpClientServerApp
cmake -S . -B build
cmake --build build
Expand Down
5 changes: 3 additions & 2 deletions client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ project(client VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS "-Wall -O3")

include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../spdlog/include
${CMAKE_CURRENT_SOURCE_DIR}/../common
)

set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/../common/utilities.cpp
${CMAKE_CURRENT_SOURCE_DIR}/packet.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../common/utils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/client.cpp
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)
Expand Down
87 changes: 59 additions & 28 deletions client/client.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "spdlog/spdlog.h"

#include "client.hpp"
#include "packet.hpp"
#include "message.hpp"
#include "utils.hpp"

using boost::asio::ip::address;
using boost::asio::placeholders::error;
Expand All @@ -9,61 +12,89 @@ client::client(const std::string ip, const unsigned short port) noexcept
: m_endpoint{address::from_string(ip), port},
m_socket{m_io_service}
{
LOG_INF() << "Initiating client... [" << m_endpoint << "]" << std::endl;
}

client::~client() noexcept
{
disconnect();
LOG_INF() << "Client exited successfully!" << std::endl;
}

void client::start() noexcept
{
if (connect())
{
m_packet.process(m_socket);
process();
}
}

bool client::connect() noexcept
{
bool is_connected = true;

LOG_INF() << "Connecting to server..." << std::endl;
spdlog::info("connecting [{}:{}]", m_endpoint.address().to_string(), m_endpoint.port());

try
error_code ec;
m_socket.connect(m_endpoint, ec);
if (ec)
{
error_code ec;
m_socket.connect(m_endpoint, ec);
if (ec)
{
LOG_ERR() << "Error: " << ec.message() << std::endl;
is_connected = false;
}
spdlog::error("failed to connect, error: {}", ec.message());
return false;
}
catch (const std::exception &e)

spdlog::info("connected");
return true;
}

void client::disconnect() noexcept
{
if (m_socket.is_open())
{
LOG_ERR() << "Exception: " << e.what() << std::endl;
is_connected = false;
m_socket.close();
spdlog::info("disconnected");
}
}

if (is_connected)
void client::process() noexcept
{
// Receive welcome message
spdlog::info("receiving welcome message from server");
const auto welcome_msg = recv(m_socket);
if (welcome_msg.get_header().get_type() != message_t::type_t::welcome)
{
LOG_INF() << "Connected successfully!" << std::endl;
spdlog::info("invalid welcome message type, {}", welcome_msg.dump());
return;
}
else
spdlog::info("welcome message received, {}", welcome_msg.dump());

// Send command request message
message_t cmd_request_msg;
cmd_request_msg.set(message_t::type_t::command_request, "ls test");
spdlog::info("sending command request [{}]", cmd_request_msg.dump());
if (!send(m_socket, cmd_request_msg))
{
LOG_ERR() << "Failed to connect to the server!" << std::endl;
spdlog::error("error while sending command request");
return;
}
spdlog::info("command request sent");

return is_connected;
}
// Receive command response message
spdlog::info("receiving command response from server");
const auto cmd_response_msg = recv(m_socket);
if (cmd_response_msg.get_header().get_type() != message_t::type_t::command_response)
{
spdlog::info("invalid command response type, {}", cmd_response_msg.dump());
return;
}
spdlog::info("command response received: {}", cmd_response_msg.dump());

void client::disconnect() noexcept
{
if (m_socket.is_open())
// Send EXIT message
message_t exit_msg;
exit_msg.set(message_t::type_t::exit, "EXIT");
spdlog::info("sending exit message to server [{}]", exit_msg.dump());
if (!send(m_socket, exit_msg))
{
m_socket.close();
spdlog::error("error while sending command request");
return;
}
spdlog::info("exit message sent");

spdlog::info("session completed");
}
5 changes: 1 addition & 4 deletions client/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

#include <boost/asio.hpp>

#include "common.hpp"
#include "packet.hpp"

using boost::asio::io_service;
using boost::asio::ip::tcp;
using boost::system::error_code;
Expand All @@ -20,9 +17,9 @@ class client final
private:
bool connect() noexcept;
void disconnect() noexcept;
void process() noexcept;

io_service m_io_service;
tcp::endpoint m_endpoint;
tcp::socket m_socket;
packet m_packet;
};
9 changes: 7 additions & 2 deletions client/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#include "common.hpp"
#include <cstdlib>
#include "spdlog/spdlog.h"
#include "defaults.hpp"
#include "client.hpp"

int main()
{
client client{SERVER_IP, SERVER_PORT};
spdlog::default_logger()->set_pattern("%+");
spdlog::default_logger()->set_level(spdlog::level::debug);

client client{defaults::server::ip, defaults::server::port};
client.start();
return EXIT_SUCCESS;
}
Loading