Clean up part 1.

This commit is contained in:
Substitute 2021-02-10 23:25:56 -05:00
parent dd98502646
commit 360d8325f4
18 changed files with 25498 additions and 131 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -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 helpers/terminal.h) add_executable(Project_1 main.cpp modernize/sockets.cpp)

Binary file not shown.

View File

@ -80,19 +80,13 @@
</MakeCommands> </MakeCommands>
</Target> </Target>
</Build> </Build>
<Unit filename="/Users/riley/ClientServerProject/helpers/terminal.h">
<Option target="Project_1"/>
</Unit>
<Unit filename="/Users/riley/ClientServerProject/main.cpp"> <Unit filename="/Users/riley/ClientServerProject/main.cpp">
<Option target="Project_1"/> <Option target="Project_1"/>
</Unit> </Unit>
<Unit filename="/Users/riley/ClientServerProject/sockets.cpp"> <Unit filename="/Users/riley/ClientServerProject/modernize/sockets.cpp">
<Option target="Project_1"/> <Option target="Project_1"/>
</Unit> </Unit>
<Unit filename="/Users/riley/ClientServerProject/sockets.h"> <Unit filename="/Users/riley/ClientServerProject/modernize/sockets.h">
<Option target="Project_1"/>
</Unit>
<Unit filename="/Users/riley/ClientServerProject/switch_expression.h">
<Option target="Project_1"/> <Option target="Project_1"/>
</Unit> </Unit>
<Unit filename="/Users/riley/ClientServerProject/CMakeLists.txt"> <Unit filename="/Users/riley/ClientServerProject/CMakeLists.txt">

BIN
libraries/.DS_Store vendored Normal file

Binary file not shown.

25447
libraries/json.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,6 @@
#pragma once #pragma once
#include <sys/ioctl.h>
#include <iomanip>
inline auto terminal_size() inline auto terminal_size()
{ {
@ -8,7 +10,7 @@ inline auto terminal_size()
} }
template<class CharT, class Traits> template<class CharT, class Traits>
std::basic_ostream<CharT,Traits>& hl( std::basic_ostream<CharT, Traits>& os) std::basic_ostream<CharT,Traits>& hr(std::basic_ostream<CharT, Traits>& os)
{ {
auto [width, height] = terminal_size(); auto [width, height] = terminal_size();
os << std::setfill('-') << std::setw(width) << "\n" << std::setfill(' ') << std::setw(0); os << std::setfill('-') << std::setw(width) << "\n" << std::setfill(' ') << std::setw(0);

122
main.cpp
View File

@ -1,15 +1,16 @@
#if WIN32 #if WIN32
#define NOMINMAX #define NOMINMAX
#endif #endif
#include "sockets.h" #include "modernize/sockets.h"
#include "user_input.h" #include "libraries/user_input.h"
#include "libraries/osyncstream.h"
#include "libraries/terminal.h"
#include <chrono> #include <chrono>
#include <iomanip> #include <iomanip>
#include "server.h" #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 auto server(const addrinfo_up& address) noexcept -> void
@ -110,7 +111,17 @@ auto onClientMessaged(networked& client, std::string& message)
{ {
int operation = std::stoi(message); int operation = std::stoi(message);
sscout << "OPCode is " << operation << ".\n"; sscout << "OPCode is " << operation << ".\n";
client.send_message("thanks for the message!"); switch(operation)
{
case 1:
client.send_message(net::system("date"));
break;
case 4:
client.send_message(net::system("netstat -ant"));
break;
default:
break;
}
}catch(std::invalid_argument& invalid_number) }catch(std::invalid_argument& invalid_number)
{ {
sscout << "Invalid OPCode, Ignored.\n"; sscout << "Invalid OPCode, Ignored.\n";
@ -133,25 +144,25 @@ auto get_ip_port(int mode)
auto execute_as_client(std::string ipaddress, std::uint16_t 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 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 operation = get_input<int>("Pick Command\n\t1. Date\n\t2. \n\t3. \n\t4. Netstat\n:>", "Invalid Selection", math::is_between<0, 5>);
auto internal_tracker = 0; auto internal_tracker = 0;
std::vector<std::thread> threads{}; std::vector<std::thread> threads{};
threads.reserve(num_clients); threads.reserve(num_clients);
auto [width, _] = terminal_size();
for(int i = 0; i < num_clients; ++i) for(int i = 0; i < num_clients; ++i)
{ {
threads.emplace_back(std::thread([&, i]() threads.emplace_back(std::thread([&]()
{ {
client net_client{}; client net_client{};
auto start = std::chrono::high_resolution_clock::now(); auto start = std::chrono::high_resolution_clock::now();
net_client.message_received += [=, &internal_tracker](std::string& message) net_client.message_received += [start, &internal_tracker](std::string& message)
{ {
auto end = std::chrono::high_resolution_clock::now(); auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end-start; std::chrono::duration<double, std::milli> elapsed = end-start;
sscout << "message for " << internal_tracker << math::numerical_suffix(internal_tracker++) << " of which is " << message << ".\n" sscout << internal_tracker << math::numerical_suffix(internal_tracker++) << " client response\n"
<< "Round Trip Was " << elapsed.count() << " ms\n" << message
<< hl; << "Round Trip Was " << elapsed.count() << " ms\n"
<< hr;
return true; return true;
}; };
@ -163,41 +174,23 @@ auto execute_as_client(std::string ipaddress, std::uint16_t port)
} }
})); }));
} }
sscout << "All clients connected.\n"; sscout << "All clients connected.\n" << hr;
for(auto&& thread : threads) for(auto&& thread : threads)
thread.join(); thread.join();
sscout << "Finished!\n"; sscout << "Finished!\n";
} }
int main() int main() {
{
net::prologue(); net::prologue();
const auto mode = get_input<int>("Select Mode\n\t1. Server\n\t2. Client\n>:","Invalid Choice!",math::is_between<0,3>); const auto mode = get_input<int>("Select Mode\n\t1. Server\n\t2. Client\n>:", "Invalid Choice!",
while (true) math::is_between<0, 3>);
{ while (true) {
auto [prefix, ipaddress, port] = get_ip_port(mode); auto[prefix, ipaddress, port] = get_ip_port(mode);
sscout << prefix << " selected, ip/port is " << ipaddress << "/" << port << ".\n"; sscout << prefix << " selected, ip/port is " << ipaddress << "/" << port << ".\n";
if (mode == 2) { if (mode == 2) {
execute_as_client(ipaddress, port); execute_as_client(ipaddress, port);
continue; } else {
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{
server net_server{}; server net_server{};
net_server.client_connected += onClientConnected; net_server.client_connected += onClientConnected;
@ -205,53 +198,4 @@ int main()
net_server.listen(ipaddress, port).join(); net_server.listen(ipaddress, port).join();
} }
} }
auto listen_server = []()
{
server srv{};
srv.client_connected += onClientConnected;
srv.message_received += onClientMessaged;
auto thread = srv.listen("0.0.0.0", 80);
if(thread.joinable())
{
thread.join();
}else{
sscout << "Thread is bad!\n";
}
};
net::epilogue();
return 0;
/*
*
const auto selection = get_input<int>("Select Mode\n\t1. Server\n\t2. Client\n>:", "Invalid Choice!", [](const auto x) {return x > 0 && x < 3; });
auto address = std::string("0.0.0.0");
auto port = 0_p;
addrinfo_up address_binding{};
while(!address_binding.get())
{
if (selection == 2)
address = get_input<std::string>("Enter Remote IP (aaa.bbb.ccc.ddd): ", "");
port = get_input<unsigned short>("Enter Port: ", "");
address_binding = net::getaddrinfo(address, std::to_string(port));
}
try
{
std::cout << "You have selected " << (selection == 1 ? "Server" : "Client") << std::endl;
std::cout << "Address: " << address << ":" << port << std::endl;
}catch(std::exception&){}
if (selection == 2)
client(address_binding);
else
listen_server(address_binding);
*
*/
net::epilogue();
} }

View File

@ -1,7 +1,5 @@
#include "sockets.h" #include "sockets.h"
#include "osyncstream.h"
#include <array> #include <array>
#include <iostream>
#include <memory> #include <memory>
#include <mutex> #include <mutex>

View File

@ -1,9 +1,5 @@
#pragma once #pragma once
#if WIN32 #include "../os/auto.h"
#include "windows.h"
#else
#include "linux.h"
#endif
#include <cstring> #include <cstring>
#include <functional> #include <functional>
#include <iostream> #include <iostream>

6
os/auto.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#if WIN32
#include "../os/windows.h"
#else
#include "../os/linux.h"
#endif

View File

View File

@ -1,9 +1,10 @@
#pragma once #pragma once
#include "sockets.h" #include "modernize/sockets.h"
#include "event.h" #include "libraries/event.h"
#include "libraries/osyncstream.h"
#include <thread> #include <thread>
#include "osyncstream.h"
class networked class networked
{ {

View File

@ -1,21 +0,0 @@
#pragma once
#include <functional>
template<typename T>
void on(T what, std::tuple<T, std::function<void()>> match)
{
auto [a,b] = match;
if (what == a)
b;
}
template<typename T>
void on(T what, std::tuple<T, std::function<void()>> match, std::tuple<T, std::function<void()>> others)
{
auto [a,b] = match;
if (what == a)
b;
else
on(what, others);
}