Skip to content
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ CppProperties.json
Userfile.csv
Userinfo.txt

#ignore file with personal information
c_cpp_properties.json

# User-specific files
*.rsuser
*.suo
Expand Down
28 changes: 26 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,31 @@
"thread": "cpp",
"unordered_map": "cpp",
"xhash": "cpp",
"*.rh": "cpp"
"*.rh": "cpp",
"*.ipp": "cpp",
"any": "cpp",
"array": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"compare": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"coroutine": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"map": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"source_location": "cpp",
"future": "cpp",
"ranges": "cpp",
"stop_token": "cpp"
},
"C_Cpp.default.cppStandard": "c++17"
"C_Cpp.default.cppStandard": "c++17",
"C_Cpp.dimInactiveRegions": false
}
65 changes: 50 additions & 15 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,70 @@
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "cl.exe",
"type": "cppbuild",
"label": "C/C++: g++.exe Create Server",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"/std:c++17",
"/property:GenerateFullPaths=true",
"/t:build",
"/consoleloggerparameters:NoSummary"
"--std",
"c++20",
"-g",
"${workspaceFolder}\\common\\*.cpp",
"${workspaceFolder}\\src\\*.cpp",
"-static",
"-static-libgcc",
"-static-libstdc++",
"-o",
"${workspaceFolder}\\LSMSServer.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"presentation": {
"reveal": "silent"
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$msCompile"
"detail": "Compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe Aktive Datei kompilieren",
"label": "C/C++: g++.exe Create Client",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"--std",
"c++17",
"c++20",
"-g",
"${workspaceFolder}\\src\\*.cpp",
"${workspaceFolder}\\common\\*.cpp",
"${workspaceFolder}\\Client\\src\\*.cpp",
"-static",
"-static-libgcc",
"-static-libstdc++",
"-o",
"${workspaceFolder}\\LSMS.exe"
"${workspaceFolder}\\Client\\LSMSClient.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe Compile Common Files",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"--std",
"c++20",
"-g",
"${workspaceFolder}\\common\\*.cpp"
],
"options": {
"cwd": "${fileDirname}"
Expand Down
Binary file added Client/LSMSClient.exe
Binary file not shown.
1 change: 1 addition & 0 deletions Client/src/Client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "Client.h"
17 changes: 17 additions & 0 deletions Client/src/Client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once
#include <common/LibNet.h>

namespace lsms {

class Client : public libnet::Client_Interface<libnet::libmsg::_DefaultMessageType> {
private:
bool m_running = false;
bool m_connected = false;

bool mf_startup();
public:
void run_client();

};

}
Binary file added LSMSServer.exe
Binary file not shown.
21 changes: 21 additions & 0 deletions common/CommonNetInclude.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <memory>
#include <thread>
#include <mutex>
#include <deque>
#include <optional>
#include <vector>
#include <iostream>
#include <algorithm>
#include <chrono>
#include <cstdint>

#ifdef _WIN32
#define _WIN32_WINNT 0x0A00
#endif

#define ASIO_STANDALONE
#include <asio.hpp>
#include <asio/ts/buffer.hpp>
#include <asio/ts/internet.hpp>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
204 changes: 204 additions & 0 deletions common/LibConnection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
#pragma once
#include "CommonNetInclude.h"
#include "LibMsg.h"
#include "LibTSQueue.h"

namespace lsms
{
namespace libnet
{
template <typename T>
class Connection : public std::enable_shared_from_this<Connection<T>> // enables us to create a shared pointer to this -> similar to this just shared_ptr
{
public:
enum class owner
{
server,
client
};

Connection(owner parent, asio::io_context &asioContext, asio::ip::tcp::socket socket, TSQueue<Owned_Message<T>> &qIn)
: m_asioContext(asioContext);
m_socket(socket);
m_qMessagesIn(qIn)
{
m_nOwnerType = parent;
}
virtual ~Connection(){

};

public:
bool connect_to_server(const asio::ip::tcp::resolver::results_type& endpoints)
{
if(m_nOwnerType == owner::client)
{
asio::async_connect(m_socket, endpoints,
[this](std::error_code ec, asio::ip::tcp::endpoint endpoint)
{
if(!ec)
{
mf_read_header();
}
});
}
}
void connect_to_client(uint32_t uid = 0)
{
if (m_nOwnerType == owner::server)
{
if (m_socket.is_open())
{
id = uid;
mf_read_header();
}
}
}

void disconnect()
{
if (is_connected())
asio::post(m_asioContext, [this]()
{ m_socket.close(); });
}
bool is_connected() const
{
return m_socket.is_open();
}
uint32_t get_id() const
{
return m_id;
}

public:
bool send(const libmsg::Message<T> &msg)
{
asio::post(m_asioContext,
[this, msg]()
{
bool b_is_writing_messages = !m_qMessagesOut.empty();
m_qMessagesOut.push_back(msg);
if (!b_is_writing_messages)
{
mf_write_header();
}
});
}

private:
void mf_read_header()
{
asio::async_read(m_socket, asio::buffer(&m_TempMsgIn.header, sizeof(libmsg::MessageHeader<T>)),
[this](std::error_code ec, std::size_t length)
{
if (!ec)
{
if (m_TempMsgIn.header.size > 0)
{
m_TempMsgIn.body.resize(m_TempMsgIn.header.size);
mf_read_body();
}
else
{
mf_add_to_incoming_message_queue();
}
}
else
{
// inform server here
m_socket.close();
}
});
}

void mf_read_body()
{
asio::async_read(m_socket, asio::buffer(m_TempMsgIn.body, m_TempMsgIn.body.size()),
[this](std::error_code ec, std::size_t length)
{
if (!ec)
{
mf_add_to_incoming_message_queue();
}
else
{
// failed to read and add to body
m_socket.close();
}
});
}

void mf_write_header()
{
asio::async_write(m_socket, asio::buffer(&m_qMessagesOut.front().header, sizeof(libmsg::MessageHeader<T>)),
[this](std::error_code ec, std::size_t lenght)
{
if (!ec)
{
if (m_qMessagesOut.front().body.size() > 0)
{
mf_write_body();
}
else
{
m_qMessagesOut.pop_front();
if (!m_qMessagesOut.empty())
{
mf_write_header();
}
}
}
else
{
// inform that something went wrong
m_socket.close();
}
});
}

void mf_write_body()
{
asio::async_write(m_socket, asio::buffer(&m_qMessagesOut.front().body.data(), m_qMessagesOut.front().body.size()),
[this](std::error_code, std::size_t length)
{
if (!ec)
{
m_qMessagesOut.pop_front();

if (!m_qMessagesOut.empty())
{
mf_write_header();
}
}
else
{
// inform that sending failed
m_socket.close();
}
});
}
void mf_add_to_incoming_message_queue()
{
if (m_nOwnerType == owner::server)
m_qMessagesIn.push_back({this->shared_from_this(), m_TempMsgIn});
else
m_qMessagesIn.push_back({nullptr, m_TempMsgIn});

mf_read_header();
}

protected:
asio::ip::tcp::socket m_socket;

asio::io_context &m_asioContext;

TSQueue<libmsg::Message<T>> m_qMessagesOut;
TSQueue<libmsg::Owned_Message<T>> &m_qMessagesIn;
libmsg::Message<T> m_TempMsgIn;

owner m_nOwnerType = owner::server;
uint32_t m_id = 0;
};

}
}
Loading