-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shaji Khan
committed
Nov 15, 2024
1 parent
804e3d6
commit 234efd4
Showing
43 changed files
with
292 additions
and
14 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,3 +39,4 @@ class Server { | |
int buflen_; | ||
char* buf_; | ||
}; | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
# include "sync.h" | ||
|
||
# ifdef __linux__ | ||
# include "net.cc" | ||
#endif | ||
|
||
void sync_send (Sync * sync) { | ||
IN | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
#define VERSION 155 | ||
#define VERSION 156 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include <boost/asio.hpp> | ||
#include <boost/bind/bind.hpp> | ||
#include <boost/optional.hpp> | ||
#include <iomanip> | ||
#include <iostream> | ||
|
||
namespace asio = boost::asio; | ||
using asio::ip::tcp; | ||
using namespace std::chrono_literals; | ||
using boost::system::error_code; | ||
|
||
void read_session(tcp::socket sock) { | ||
std::cout << "Connection established " << sock.remote_endpoint() << std::endl; | ||
|
||
std::array<char, 1000> buffer; | ||
|
||
for (error_code ec;;) { | ||
size_t n = sock.read_some(asio::buffer(buffer), ec); | ||
std::cout << "Received " << n << " " << quoted(std::string(buffer.data(), n)) << " (" << ec.message() << ")" << std::endl; | ||
|
||
if (ec.failed()) | ||
break; | ||
} | ||
|
||
std::cout << "Connection closed" << std::endl; | ||
} | ||
|
||
class TheServer { | ||
public: | ||
TheServer(asio::any_io_executor ex, uint16_t port) // | ||
: m_Acceptor{ex, tcp::endpoint{tcp::v4(), port}} // | ||
{ | ||
m_Acceptor.set_option(tcp::acceptor::reuse_address(true)); | ||
do_accept(); | ||
} | ||
|
||
private: | ||
void do_accept() { | ||
m_Acceptor.async_accept([this](error_code ec, tcp::socket s) { | ||
if (!ec) { | ||
std::thread(read_session, std::move(s)).detach(); | ||
do_accept(); // and immediately accept new connection(s) | ||
} else { | ||
std::cout << "Connection error (" << ec.message() << ")" << std::endl; | ||
} | ||
}); | ||
} | ||
|
||
tcp::acceptor m_Acceptor; | ||
}; | ||
|
||
constexpr uint16_t s_port = 1900; | ||
|
||
void run_server() { | ||
asio::thread_pool ioc(1); | ||
TheServer server(ioc.get_executor(), s_port); | ||
|
||
std::cout << "Press Enter to quit" << std::endl; | ||
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); | ||
|
||
ioc.stop(); | ||
ioc.join(); | ||
} | ||
|
||
void run_client() { | ||
std::cout << "Starting client" << std::endl; | ||
|
||
asio::io_service ioc(1); | ||
|
||
try { | ||
tcp::socket m_Socket(ioc); | ||
m_Socket.connect({{}, s_port}); | ||
|
||
std::cout << "Client connected" << std::endl; | ||
|
||
for (std::string msg : {"Hello World", "Bye World"}) { | ||
write(m_Socket, asio::buffer(msg)); | ||
std::this_thread::sleep_for(100ms); | ||
} | ||
} catch (std::exception& e) { | ||
std::cerr << "Exception: " << e.what() << "\n"; | ||
} | ||
} | ||
|
||
int main(int argc, char**) { | ||
if (argc>1) | ||
run_server(); | ||
else | ||
run_client(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include "winserver.h" | ||
|
||
asio::thread_pool ioc(1); | ||
|
||
|
||
void read_session(tcp::socket sock) { | ||
std::cout << "Connection established " << sock.remote_endpoint() << std::endl; | ||
|
||
std::array<char, 1000> buffer; | ||
|
||
for (boost::system::error_code ec;;) { | ||
size_t n = sock.read_some(asio::buffer(buffer), ec); | ||
std::cout << "Received " << n << " " << quoted(std::string(buffer.data(), n)) << " (" << ec.message() << ")" << std::endl; | ||
|
||
if (ec.failed()) | ||
break; | ||
} | ||
|
||
std::cout << "Connection closed" << std::endl; | ||
} | ||
|
||
void Client::create () { | ||
IN | ||
|
||
OUT | ||
} | ||
|
||
Client::Client (string host, int port) { | ||
IN | ||
host_ = host ; | ||
port_ = port ; | ||
OUT | ||
} | ||
|
||
std::string Client::send_preset (json j) { | ||
IN | ||
asio::io_service ioc(1); | ||
|
||
try { | ||
tcp::socket m_Socket(ioc); | ||
///| todo enter hostname here | ||
m_Socket.connect({{}, port_}); | ||
|
||
std::cout << "Client connected" << std::endl; | ||
|
||
///| todo: add extra } here | ||
std::string msg = j.dump ().append (string ("}")) ; | ||
write(m_Socket, asio::buffer(msg)); | ||
std::this_thread::sleep_for(100ms); | ||
} catch (std::exception& e) { | ||
std::cerr << "Exception: " << e.what() << "\n"; | ||
} | ||
|
||
return string (""); | ||
OUT | ||
} | ||
|
||
void Server::close_socket () { | ||
ioc.stop(); | ||
ioc.join(); | ||
} | ||
|
||
void Client::close_socket () { | ||
} | ||
|
||
Server::Server () { | ||
|
||
} | ||
|
||
void Server::run () { | ||
TheServer server(ioc.get_executor(), 6906); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# ifndef WINSERVER | ||
# define WINSERVER | ||
# include "presets.h" | ||
#include <winsock2.h> | ||
#include <boost/asio.hpp> | ||
#include <boost/bind/bind.hpp> | ||
#include <boost/optional.hpp> | ||
#include <iomanip> | ||
#include <iostream> | ||
|
||
using namespace std; | ||
using json = nlohmann::json; | ||
|
||
namespace asio = boost::asio; | ||
using asio::ip::tcp; | ||
using namespace std::chrono_literals; | ||
using boost::system::error_code; | ||
|
||
void read_session(tcp::socket sock) ; | ||
|
||
class TheServer { | ||
public: | ||
TheServer(asio::any_io_executor ex, uint16_t port) // | ||
: m_Acceptor{ex, tcp::endpoint{tcp::v4(), port}} // | ||
{ | ||
m_Acceptor.set_option(tcp::acceptor::reuse_address(true)); | ||
do_accept(); | ||
} | ||
|
||
private: | ||
|
||
void do_accept() { | ||
m_Acceptor.async_accept([this](boost::system::error_code ec, tcp::socket s) { | ||
if (!ec) { | ||
std::thread(read_session, std::move(s)).detach(); | ||
do_accept(); // and immediately accept new connection(s) | ||
} else { | ||
std::cout << "Connection error (" << ec.message() << ")" << std::endl; | ||
} | ||
}); | ||
} | ||
|
||
tcp::acceptor m_Acceptor; | ||
}; | ||
|
||
class Client { | ||
public: | ||
Client(string host, int port); | ||
~Client(); | ||
|
||
void run(); | ||
std::string send_preset(json j) ; | ||
virtual void create(); | ||
virtual void close_socket(); | ||
void echo(); | ||
bool get_response(); | ||
|
||
private: | ||
bool send_request(string); | ||
|
||
string host_; | ||
int port_; | ||
int server_; | ||
int buflen_; | ||
char* buf_; | ||
}; | ||
|
||
class Server { | ||
public: | ||
Server(); | ||
~Server(); | ||
|
||
Presets * presets ; | ||
void * sync ; | ||
void run(); | ||
|
||
GMainContext * context ; | ||
void create(); | ||
void close_socket(); | ||
void serve(); | ||
void handle(int); | ||
string get_request(int); | ||
bool send_response(int, string); | ||
|
||
int port_; | ||
int server_; | ||
int buflen_; | ||
char* buf_; | ||
}; | ||
|
||
|
||
class NET { | ||
public: | ||
vector <string> addresses; | ||
} ; | ||
|
||
|
||
# endif |
Binary file not shown.