Update stuff :)

This commit is contained in:
Substitute 2021-02-10 16:32:59 -05:00
parent 9f1f324511
commit dd98502646
15 changed files with 201 additions and 80 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

8
.idea/.gitignore vendored Normal file
View 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
View File

@ -0,0 +1 @@
Project_1

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

View 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
View 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
View 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
View 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>

View File

@ -3,4 +3,4 @@ project(Project_1)
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.

View File

@ -8,89 +8,94 @@
<Option virtualFolders="CMake Files\;"/>
<Build>
<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"/>
<MakeCommands>
<Build command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 all"/>
<CompileFile command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
<Clean command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
<DistClean command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
<Build command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 all"/>
<CompileFile command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
<Clean command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
<DistClean command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
</MakeCommands>
</Target>
<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"/>
<MakeCommands>
<Build command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 rebuild_cache"/>
<CompileFile command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
<Clean command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
<DistClean command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
<Build command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 rebuild_cache"/>
<CompileFile command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
<Clean command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
<DistClean command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
</MakeCommands>
</Target>
<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"/>
<MakeCommands>
<Build command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 edit_cache"/>
<CompileFile command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
<Clean command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
<DistClean command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
<Build command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 edit_cache"/>
<CompileFile command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
<Clean command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
<DistClean command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
</MakeCommands>
</Target>
<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 working_dir="/Users/riley/CLionProjects/UNF/Networks/Project 1/cmake-build-debug"/>
<Option output="/Users/riley/ClientServerProject/cmake-build-debug/Project_1" prefix_auto="0" extension_auto="0"/>
<Option working_dir="/Users/riley/ClientServerProject/cmake-build-debug"/>
<Option object_output="./"/>
<Option type="1"/>
<Option compiler="gcc"/>
<Compiler>
<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/include"/>
<Add directory="/System/Library/Frameworks"/>
<Add directory="/Library/Frameworks"/>
</Compiler>
<MakeCommands>
<Build command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 Project_1"/>
<CompileFile command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
<Clean command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
<DistClean command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
<Build command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 Project_1"/>
<CompileFile command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
<Clean command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
<DistClean command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
</MakeCommands>
</Target>
<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 working_dir="/Users/riley/CLionProjects/UNF/Networks/Project 1/cmake-build-debug"/>
<Option output="/Users/riley/ClientServerProject/cmake-build-debug/Project_1" prefix_auto="0" extension_auto="0"/>
<Option working_dir="/Users/riley/ClientServerProject/cmake-build-debug"/>
<Option object_output="./"/>
<Option type="1"/>
<Option compiler="gcc"/>
<Compiler>
<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/include"/>
<Add directory="/System/Library/Frameworks"/>
<Add directory="/Library/Frameworks"/>
</Compiler>
<MakeCommands>
<Build command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 Project_1/fast"/>
<CompileFile command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
<Clean command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
<DistClean command="/usr/bin/make -j8 -f &quot;/Users/riley/CLionProjects/UNF/Networks/Project\ 1/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
<Build command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 Project_1/fast"/>
<CompileFile command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
<Clean command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
<DistClean command="/usr/bin/make -j8 -f &quot;/Users/riley/ClientServerProject/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
</MakeCommands>
</Target>
</Build>
<Unit filename="/Users/riley/CLionProjects/UNF/Networks/Project 1/main.cpp">
<Unit filename="/Users/riley/ClientServerProject/helpers/terminal.h">
<Option target="Project_1"/>
</Unit>
<Unit filename="/Users/riley/CLionProjects/UNF/Networks/Project 1/sockets.cpp">
<Unit filename="/Users/riley/ClientServerProject/main.cpp">
<Option target="Project_1"/>
</Unit>
<Unit filename="/Users/riley/CLionProjects/UNF/Networks/Project 1/sockets.h">
<Unit filename="/Users/riley/ClientServerProject/sockets.cpp">
<Option target="Project_1"/>
</Unit>
<Unit filename="/Users/riley/CLionProjects/UNF/Networks/Project 1/switch_expression.h">
<Unit filename="/Users/riley/ClientServerProject/sockets.h">
<Option target="Project_1"/>
</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\"/>
</Unit>
</Project>

16
helpers/terminal.h Normal file
View 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;
}

124
main.cpp
View File

@ -4,10 +4,12 @@
#include "sockets.h"
#include "user_input.h"
#include <chrono>
#include <iomanip>
#include "server.h"
#include "osyncstream.h"
#include "switch_expression.h"
#include <sys/ioctl.h>
#include "helpers/terminal.h"
/*
*
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";
@ -104,55 +106,103 @@ bool onClientConnected(networked& client)
}
auto onClientMessaged(networked& client, std::string& message)
{
sscout << "message was " << message << "\n";
client.send_message("thanks for the message!");
try
{
int operation = std::stoi(message);
sscout << "OPCode is " << operation << ".\n";
client.send_message("thanks for the message!");
}catch(std::invalid_argument& invalid_number)
{
sscout << "Invalid OPCode, Ignored.\n";
}
client.disconnect();
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 "";
std::string ipaddress{};
std::uint16_t port{};
if (mode == 1)
ipaddress = "0.0.0.0";
else
ipaddress = get_input<std::string>("Enter IP: ");
port = get_input<std::uint16_t>("Enter Port:");
return std::make_tuple(mode == 1 ? "Server" : mode == 2 ? "Client" : "Unknown", ipaddress, port);
}
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)
{
std::string ipaddress{};
std::uint16_t port{};
if (mode == 1)
ipaddress = "0.0.0.0";
else
ipaddress = get_input<std::string>("Enter IP: ");
port = get_input<std::uint16_t>("Enter Port:");
sscout << resolve_selection(mode) << " selected, ip/port is " << ipaddress << "/" << port << ".\n";
if (mode == 2)
{
client net_client{};
net_client.message_received += [](std::string& what){
sscout << "GOT " << what << ".\n";
return true;
};
if(net_client.connect(ipaddress, port))
{
net_client.send_message("!Hello!\n");
net_client.send_message("Second Message\n");
net_client.send_message("Final Message!\n");
while(net_client.is_valid()){}
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{};
net_client.message_received += [](std::string &what) {
sscout << "GOT " << what << ".\n";
return true;
};
auto res = net_client.connect(ipaddress, port);
if (res.joinable()) {
net_client.send_message(std::string("!Hello! from") + std::to_string(i) + "\n");
res.join();
} else {
sscout << "NO NET\n";
}
net_client.disconnect();
}
else
{
sscout << "NO NET\n";
}
net_client.disconnect();
}
else{
server net_server{};
net_server.client_connected += onClientConnected;
net_server.message_received += onClientMessaged;
net_server.listen(ipaddress, port).join();
}
}

View File

@ -33,9 +33,8 @@ class client : public networked
while(is_valid()) /* Accept all messages */
{
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();
break;
}
@ -45,14 +44,14 @@ class client : public networked
public:
event<std::string&> message_received {};
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));
auto remote_address = net::getaddrinfo(ip, std::to_string(port));
if (net::connect(socket, remote_address) < 0)
return false;
std::thread(&client::accept_message, this).detach();
return true;
return std::thread();
auto thread = std::thread(&client::accept_message, this);
return thread;
}
};
@ -74,8 +73,8 @@ class server : networked
auto accept_client_message(networked client) -> void //add const later
{
using namespace std::chrono_literals;
std::this_thread::sleep_for(2000ms);
//using namespace std::chrono_literals;
//std::this_thread::sleep_for(2000ms);
while(client.is_valid()) /* Accept all messages */
{
auto message = client.receive_message();

View File

@ -12,6 +12,22 @@ namespace math
{
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); }
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>
@ -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...*/
}
struct always_true_validator { template<typename T> bool operator()(T) const { return true; }};
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()); }