Update stuff :)
This commit is contained in:
parent
9f1f324511
commit
dd98502646
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
1
.idea/.name
Normal file
1
.idea/.name
Normal file
@ -0,0 +1 @@
|
|||||||
|
Project_1
|
2
.idea/ClientServerProject.iml
Normal file
2
.idea/ClientServerProject.iml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
6
.idea/inspectionProfiles/Project_Default.xml
Normal file
6
.idea/inspectionProfiles/Project_Default.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="EndlessLoop" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||||
|
</profile>
|
||||||
|
</component>
|
4
.idea/misc.xml
Normal file
4
.idea/misc.xml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||||
|
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/ClientServerProject.iml" filepath="$PROJECT_DIR$/.idea/ClientServerProject.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
@ -3,4 +3,4 @@ project(Project_1)
|
|||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
add_executable(Project_1 main.cpp sockets.cpp switch_expression.h)
|
add_executable(Project_1 main.cpp sockets.cpp switch_expression.h helpers/terminal.h)
|
Binary file not shown.
@ -8,89 +8,94 @@
|
|||||||
<Option virtualFolders="CMake Files\;"/>
|
<Option virtualFolders="CMake Files\;"/>
|
||||||
<Build>
|
<Build>
|
||||||
<Target title="all">
|
<Target title="all">
|
||||||
<Option working_dir="/Users/riley/CLionProjects/UNF/Networks/Project 1/cmake-build-debug"/>
|
<Option working_dir="/Users/riley/ClientServerProject/cmake-build-debug"/>
|
||||||
<Option type="4"/>
|
<Option type="4"/>
|
||||||
<MakeCommands>
|
<MakeCommands>
|
||||||
<Build command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 all"/>
|
<Build command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 all"/>
|
||||||
<CompileFile command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 "$file""/>
|
<CompileFile command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 "$file""/>
|
||||||
<Clean command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
<Clean command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
||||||
<DistClean command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
<DistClean command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
||||||
</MakeCommands>
|
</MakeCommands>
|
||||||
</Target>
|
</Target>
|
||||||
<Target title="rebuild_cache">
|
<Target title="rebuild_cache">
|
||||||
<Option working_dir="/Users/riley/CLionProjects/UNF/Networks/Project 1/cmake-build-debug"/>
|
<Option working_dir="/Users/riley/ClientServerProject/cmake-build-debug"/>
|
||||||
<Option type="4"/>
|
<Option type="4"/>
|
||||||
<MakeCommands>
|
<MakeCommands>
|
||||||
<Build command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 rebuild_cache"/>
|
<Build command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 rebuild_cache"/>
|
||||||
<CompileFile command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 "$file""/>
|
<CompileFile command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 "$file""/>
|
||||||
<Clean command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
<Clean command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
||||||
<DistClean command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
<DistClean command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
||||||
</MakeCommands>
|
</MakeCommands>
|
||||||
</Target>
|
</Target>
|
||||||
<Target title="edit_cache">
|
<Target title="edit_cache">
|
||||||
<Option working_dir="/Users/riley/CLionProjects/UNF/Networks/Project 1/cmake-build-debug"/>
|
<Option working_dir="/Users/riley/ClientServerProject/cmake-build-debug"/>
|
||||||
<Option type="4"/>
|
<Option type="4"/>
|
||||||
<MakeCommands>
|
<MakeCommands>
|
||||||
<Build command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 edit_cache"/>
|
<Build command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 edit_cache"/>
|
||||||
<CompileFile command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 "$file""/>
|
<CompileFile command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 "$file""/>
|
||||||
<Clean command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
<Clean command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
||||||
<DistClean command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
<DistClean command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
||||||
</MakeCommands>
|
</MakeCommands>
|
||||||
</Target>
|
</Target>
|
||||||
<Target title="Project_1">
|
<Target title="Project_1">
|
||||||
<Option output="/Users/riley/CLionProjects/UNF/Networks/Project 1/cmake-build-debug/Project_1" prefix_auto="0" extension_auto="0"/>
|
<Option output="/Users/riley/ClientServerProject/cmake-build-debug/Project_1" prefix_auto="0" extension_auto="0"/>
|
||||||
<Option working_dir="/Users/riley/CLionProjects/UNF/Networks/Project 1/cmake-build-debug"/>
|
<Option working_dir="/Users/riley/ClientServerProject/cmake-build-debug"/>
|
||||||
<Option object_output="./"/>
|
<Option object_output="./"/>
|
||||||
<Option type="1"/>
|
<Option type="1"/>
|
||||||
<Option compiler="gcc"/>
|
<Option compiler="gcc"/>
|
||||||
<Compiler>
|
<Compiler>
|
||||||
<Add directory="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1"/>
|
<Add directory="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1"/>
|
||||||
|
<Add directory="/usr/local/include"/>
|
||||||
<Add directory="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/include"/>
|
<Add directory="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/include"/>
|
||||||
<Add directory="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include"/>
|
<Add directory="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include"/>
|
||||||
<Add directory="/System/Library/Frameworks"/>
|
<Add directory="/System/Library/Frameworks"/>
|
||||||
<Add directory="/Library/Frameworks"/>
|
<Add directory="/Library/Frameworks"/>
|
||||||
</Compiler>
|
</Compiler>
|
||||||
<MakeCommands>
|
<MakeCommands>
|
||||||
<Build command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 Project_1"/>
|
<Build command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 Project_1"/>
|
||||||
<CompileFile command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 "$file""/>
|
<CompileFile command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 "$file""/>
|
||||||
<Clean command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
<Clean command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
||||||
<DistClean command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
<DistClean command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
||||||
</MakeCommands>
|
</MakeCommands>
|
||||||
</Target>
|
</Target>
|
||||||
<Target title="Project_1/fast">
|
<Target title="Project_1/fast">
|
||||||
<Option output="/Users/riley/CLionProjects/UNF/Networks/Project 1/cmake-build-debug/Project_1" prefix_auto="0" extension_auto="0"/>
|
<Option output="/Users/riley/ClientServerProject/cmake-build-debug/Project_1" prefix_auto="0" extension_auto="0"/>
|
||||||
<Option working_dir="/Users/riley/CLionProjects/UNF/Networks/Project 1/cmake-build-debug"/>
|
<Option working_dir="/Users/riley/ClientServerProject/cmake-build-debug"/>
|
||||||
<Option object_output="./"/>
|
<Option object_output="./"/>
|
||||||
<Option type="1"/>
|
<Option type="1"/>
|
||||||
<Option compiler="gcc"/>
|
<Option compiler="gcc"/>
|
||||||
<Compiler>
|
<Compiler>
|
||||||
<Add directory="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1"/>
|
<Add directory="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1"/>
|
||||||
|
<Add directory="/usr/local/include"/>
|
||||||
<Add directory="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/include"/>
|
<Add directory="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/include"/>
|
||||||
<Add directory="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include"/>
|
<Add directory="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include"/>
|
||||||
<Add directory="/System/Library/Frameworks"/>
|
<Add directory="/System/Library/Frameworks"/>
|
||||||
<Add directory="/Library/Frameworks"/>
|
<Add directory="/Library/Frameworks"/>
|
||||||
</Compiler>
|
</Compiler>
|
||||||
<MakeCommands>
|
<MakeCommands>
|
||||||
<Build command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 Project_1/fast"/>
|
<Build command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 Project_1/fast"/>
|
||||||
<CompileFile command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 "$file""/>
|
<CompileFile command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 "$file""/>
|
||||||
<Clean command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
<Clean command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
||||||
<DistClean command="/usr/bin/make -j8 -f "/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
<DistClean command="/usr/bin/make -j8 -f "/Users/riley/ClientServerProject/cmake-build-debug/Makefile" VERBOSE=1 clean"/>
|
||||||
</MakeCommands>
|
</MakeCommands>
|
||||||
</Target>
|
</Target>
|
||||||
</Build>
|
</Build>
|
||||||
<Unit filename="/Users/riley/CLionProjects/UNF/Networks/Project 1/main.cpp">
|
<Unit filename="/Users/riley/ClientServerProject/helpers/terminal.h">
|
||||||
<Option target="Project_1"/>
|
<Option target="Project_1"/>
|
||||||
</Unit>
|
</Unit>
|
||||||
<Unit filename="/Users/riley/CLionProjects/UNF/Networks/Project 1/sockets.cpp">
|
<Unit filename="/Users/riley/ClientServerProject/main.cpp">
|
||||||
<Option target="Project_1"/>
|
<Option target="Project_1"/>
|
||||||
</Unit>
|
</Unit>
|
||||||
<Unit filename="/Users/riley/CLionProjects/UNF/Networks/Project 1/sockets.h">
|
<Unit filename="/Users/riley/ClientServerProject/sockets.cpp">
|
||||||
<Option target="Project_1"/>
|
<Option target="Project_1"/>
|
||||||
</Unit>
|
</Unit>
|
||||||
<Unit filename="/Users/riley/CLionProjects/UNF/Networks/Project 1/switch_expression.h">
|
<Unit filename="/Users/riley/ClientServerProject/sockets.h">
|
||||||
<Option target="Project_1"/>
|
<Option target="Project_1"/>
|
||||||
</Unit>
|
</Unit>
|
||||||
<Unit filename="/Users/riley/CLionProjects/UNF/Networks/Project 1/CMakeLists.txt">
|
<Unit filename="/Users/riley/ClientServerProject/switch_expression.h">
|
||||||
|
<Option target="Project_1"/>
|
||||||
|
</Unit>
|
||||||
|
<Unit filename="/Users/riley/ClientServerProject/CMakeLists.txt">
|
||||||
<Option virtualFolder="CMake Files\"/>
|
<Option virtualFolder="CMake Files\"/>
|
||||||
</Unit>
|
</Unit>
|
||||||
</Project>
|
</Project>
|
||||||
|
16
helpers/terminal.h
Normal file
16
helpers/terminal.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
inline auto terminal_size()
|
||||||
|
{
|
||||||
|
struct winsize result{};
|
||||||
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &result);
|
||||||
|
return std::make_tuple(result.ws_col, result.ws_row);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class CharT, class Traits>
|
||||||
|
std::basic_ostream<CharT,Traits>& hl( std::basic_ostream<CharT, Traits>& os)
|
||||||
|
{
|
||||||
|
auto [width, height] = terminal_size();
|
||||||
|
os << std::setfill('-') << std::setw(width) << "\n" << std::setfill(' ') << std::setw(0);
|
||||||
|
return os;
|
||||||
|
}
|
108
main.cpp
108
main.cpp
@ -4,10 +4,12 @@
|
|||||||
#include "sockets.h"
|
#include "sockets.h"
|
||||||
#include "user_input.h"
|
#include "user_input.h"
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <iomanip>
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
#include "osyncstream.h"
|
#include "osyncstream.h"
|
||||||
#include "switch_expression.h"
|
#include "switch_expression.h"
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include "helpers/terminal.h"
|
||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
auto server(const addrinfo_up& address) noexcept -> void
|
auto server(const addrinfo_up& address) noexcept -> void
|
||||||
@ -92,7 +94,7 @@ auto client(const addrinfo_up& address) noexcept -> void
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
bool onClientConnected(networked& client)
|
auto onClientConnected(networked& client)
|
||||||
{
|
{
|
||||||
sscout << "Accepted Client on Server.\n";
|
sscout << "Accepted Client on Server.\n";
|
||||||
|
|
||||||
@ -104,27 +106,20 @@ bool onClientConnected(networked& client)
|
|||||||
}
|
}
|
||||||
auto onClientMessaged(networked& client, std::string& message)
|
auto onClientMessaged(networked& client, std::string& message)
|
||||||
{
|
{
|
||||||
sscout << "message was " << message << "\n";
|
try
|
||||||
|
{
|
||||||
|
int operation = std::stoi(message);
|
||||||
|
sscout << "OPCode is " << operation << ".\n";
|
||||||
client.send_message("thanks for the message!");
|
client.send_message("thanks for the message!");
|
||||||
|
}catch(std::invalid_argument& invalid_number)
|
||||||
|
{
|
||||||
|
sscout << "Invalid OPCode, Ignored.\n";
|
||||||
|
}
|
||||||
client.disconnect();
|
client.disconnect();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline auto resolve_selection(const int selection) -> std::string
|
auto get_ip_port(int mode)
|
||||||
{
|
|
||||||
if (selection == 1)
|
|
||||||
return "Server";
|
|
||||||
else if (selection == 2)
|
|
||||||
return "Client";
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
net::prologue();
|
|
||||||
const auto mode = get_input<int>("Select Mode\n\t1. Server\n\t2. Client\n>:","Invalid Choice!",math::is_between<0,3>);
|
|
||||||
while (true)
|
|
||||||
{
|
{
|
||||||
std::string ipaddress{};
|
std::string ipaddress{};
|
||||||
std::uint16_t port{};
|
std::uint16_t port{};
|
||||||
@ -133,28 +128,83 @@ int main()
|
|||||||
else
|
else
|
||||||
ipaddress = get_input<std::string>("Enter IP: ");
|
ipaddress = get_input<std::string>("Enter IP: ");
|
||||||
port = get_input<std::uint16_t>("Enter Port:");
|
port = get_input<std::uint16_t>("Enter Port:");
|
||||||
sscout << resolve_selection(mode) << " selected, ip/port is " << ipaddress << "/" << port << ".\n";
|
return std::make_tuple(mode == 1 ? "Server" : mode == 2 ? "Client" : "Unknown", ipaddress, port);
|
||||||
if (mode == 2)
|
}
|
||||||
|
|
||||||
|
auto execute_as_client(std::string ipaddress, std::uint16_t port)
|
||||||
{
|
{
|
||||||
|
auto num_clients = get_input<int>("Enter number of clients. (1-25)", "Invalid Amount", math::is_between<0, 26>);
|
||||||
|
auto operation = get_input<int>("Pick Command\n\t1. Date", "Invalid Selection", math::is_between<0, 2>);
|
||||||
|
auto internal_tracker = 0;
|
||||||
|
std::vector<std::thread> threads{};
|
||||||
|
threads.reserve(num_clients);
|
||||||
|
auto [width, _] = terminal_size();
|
||||||
|
for(int i = 0; i < num_clients; ++i)
|
||||||
|
{
|
||||||
|
threads.emplace_back(std::thread([&, i]()
|
||||||
|
{
|
||||||
|
client net_client{};
|
||||||
|
auto start = std::chrono::high_resolution_clock::now();
|
||||||
|
net_client.message_received += [=, &internal_tracker](std::string& message)
|
||||||
|
{
|
||||||
|
auto end = std::chrono::high_resolution_clock::now();
|
||||||
|
std::chrono::duration<double, std::milli> elapsed = end-start;
|
||||||
|
sscout << "message for " << internal_tracker << math::numerical_suffix(internal_tracker++) << " of which is " << message << ".\n"
|
||||||
|
<< "Round Trip Was " << elapsed.count() << " ms\n"
|
||||||
|
<< hl;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
auto res = net_client.connect(ipaddress, port);
|
||||||
|
if (res.joinable())
|
||||||
|
{
|
||||||
|
net_client.send_message(std::to_string(operation));
|
||||||
|
res.join();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
sscout << "All clients connected.\n";
|
||||||
|
for(auto&& thread : threads)
|
||||||
|
thread.join();
|
||||||
|
sscout << "Finished!\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
net::prologue();
|
||||||
|
const auto mode = get_input<int>("Select Mode\n\t1. Server\n\t2. Client\n>:","Invalid Choice!",math::is_between<0,3>);
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
auto [prefix, ipaddress, port] = get_ip_port(mode);
|
||||||
|
sscout << prefix << " selected, ip/port is " << ipaddress << "/" << port << ".\n";
|
||||||
|
if (mode == 2) {
|
||||||
|
execute_as_client(ipaddress, port);
|
||||||
|
continue;
|
||||||
|
for (int i = 0; i < 10; ++i) {
|
||||||
client net_client{};
|
client net_client{};
|
||||||
net_client.message_received += [](std::string &what) {
|
net_client.message_received += [](std::string &what) {
|
||||||
sscout << "GOT " << what << ".\n";
|
sscout << "GOT " << what << ".\n";
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
if(net_client.connect(ipaddress, port))
|
auto res = net_client.connect(ipaddress, port);
|
||||||
{
|
if (res.joinable()) {
|
||||||
net_client.send_message("!Hello!\n");
|
net_client.send_message(std::string("!Hello! from") + std::to_string(i) + "\n");
|
||||||
net_client.send_message("Second Message\n");
|
res.join();
|
||||||
net_client.send_message("Final Message!\n");
|
} else {
|
||||||
while(net_client.is_valid()){}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
sscout << "NO NET\n";
|
sscout << "NO NET\n";
|
||||||
}
|
}
|
||||||
net_client.disconnect();
|
net_client.disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
server net_server{};
|
||||||
|
|
||||||
|
net_server.client_connected += onClientConnected;
|
||||||
|
net_server.message_received += onClientMessaged;
|
||||||
|
net_server.listen(ipaddress, port).join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
auto listen_server = []()
|
auto listen_server = []()
|
||||||
|
15
server.h
15
server.h
@ -33,9 +33,8 @@ class client : public networked
|
|||||||
while(is_valid()) /* Accept all messages */
|
while(is_valid()) /* Accept all messages */
|
||||||
{
|
{
|
||||||
auto message = receive_message();
|
auto message = receive_message();
|
||||||
if (message.empty() && is_valid())
|
if (message.empty() && is_valid()) //connection is closed or broken.
|
||||||
{
|
{
|
||||||
sscout << "Server Connection Lost\n";
|
|
||||||
disconnect();
|
disconnect();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -45,14 +44,14 @@ class client : public networked
|
|||||||
public:
|
public:
|
||||||
event<std::string&> message_received {};
|
event<std::string&> message_received {};
|
||||||
explicit client() : networked(net::socket(AF_INET, SOCK_STREAM, 0)){}
|
explicit client() : networked(net::socket(AF_INET, SOCK_STREAM, 0)){}
|
||||||
auto connect(const std::string& ip, const port port) -> bool
|
auto connect(const std::string& ip, const port port) -> std::thread
|
||||||
{
|
{
|
||||||
const auto address = net::getaddrinfo(ip, std::to_string(port));
|
const auto address = net::getaddrinfo(ip, std::to_string(port));
|
||||||
auto remote_address = net::getaddrinfo(ip, std::to_string(port));
|
auto remote_address = net::getaddrinfo(ip, std::to_string(port));
|
||||||
if (net::connect(socket, remote_address) < 0)
|
if (net::connect(socket, remote_address) < 0)
|
||||||
return false;
|
return std::thread();
|
||||||
std::thread(&client::accept_message, this).detach();
|
auto thread = std::thread(&client::accept_message, this);
|
||||||
return true;
|
return thread;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -74,8 +73,8 @@ class server : networked
|
|||||||
|
|
||||||
auto accept_client_message(networked client) -> void //add const later
|
auto accept_client_message(networked client) -> void //add const later
|
||||||
{
|
{
|
||||||
using namespace std::chrono_literals;
|
//using namespace std::chrono_literals;
|
||||||
std::this_thread::sleep_for(2000ms);
|
//std::this_thread::sleep_for(2000ms);
|
||||||
while(client.is_valid()) /* Accept all messages */
|
while(client.is_valid()) /* Accept all messages */
|
||||||
{
|
{
|
||||||
auto message = client.receive_message();
|
auto message = client.receive_message();
|
||||||
|
20
user_input.h
20
user_input.h
@ -12,6 +12,22 @@ namespace math
|
|||||||
{
|
{
|
||||||
template<auto lb, typeof(lb) ub, std::enable_if_t<std::is_integral<typeof(lb)>::value, bool> = true>
|
template<auto lb, typeof(lb) ub, std::enable_if_t<std::is_integral<typeof(lb)>::value, bool> = true>
|
||||||
constexpr auto is_between(typeof(lb) x) -> bool { return (ub > x) && (x > lb); }
|
constexpr auto is_between(typeof(lb) x) -> bool { return (ub > x) && (x > lb); }
|
||||||
|
template<auto lb, std::enable_if_t<std::is_integral<typeof(lb)>::value, bool> = true>
|
||||||
|
constexpr auto is_larger(typeof(lb) x) -> bool { return (x > lb); }
|
||||||
|
template<auto ub, std::enable_if_t<std::is_integral<typeof(ub)>::value, bool> = true>
|
||||||
|
constexpr auto is_smaller(typeof(ub) x) -> bool { return (ub > x); }
|
||||||
|
template<typename T, std::enable_if_t<std::is_integral<typeof(T)>::value, bool> = true>
|
||||||
|
auto numerical_suffix(T value) -> std::string
|
||||||
|
{
|
||||||
|
std::string suffixes[4] = {"th", "st", "nd", "rd"};
|
||||||
|
value %= 100;
|
||||||
|
if (math::is_between<11,13>(value))
|
||||||
|
return "th";
|
||||||
|
value %= 10;
|
||||||
|
if (math::is_larger<3>(value))
|
||||||
|
return "th";
|
||||||
|
return suffixes[value % 4];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -37,6 +53,6 @@ T get_input(const std::string& prompt, const std::string& on_error, std::functio
|
|||||||
}
|
}
|
||||||
catch (std::exception&) { return user_input; } /* This should not happen ..... cin/cout are not configured to throw...*/
|
catch (std::exception&) { return user_input; } /* This should not happen ..... cin/cout are not configured to throw...*/
|
||||||
}
|
}
|
||||||
|
struct always_true_validator { template<typename T> bool operator()(T) const { return true; }};
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T get_input(const std::string& prompt) { return get_input<T>(prompt, "", [](auto) {return true; }); }
|
T get_input(const std::string& prompt) { return get_input<T>(prompt, "", always_true_validator()); }
|
Loading…
Reference in New Issue
Block a user