Skip to content

Commit

Permalink
sync on win32 works
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaji Khan committed Nov 19, 2024
1 parent ab2e545 commit 3ceb6a4
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 14 deletions.
Binary file modified amprack.exe
Binary file not shown.
Binary file modified main.o
Binary file not shown.
5 changes: 4 additions & 1 deletion presets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,10 @@ int Presets::import_presets_from_json (json j) {
std::string filename = std::string (presets_dir->c_str ()) .append (basename) ;
wtf ("writing file: %s\n", filename.c_str ());
json_to_filename (preset, filename) ;
add_preset (preset, 1);
LOGD ("[presets] warning: not adding preset to UI\n");
# ifdef __linux__
add_preset (preset, 1);
# endif
}

msg (std::string ("Imported ").append (std::to_string (how_many)).append (" presets successfully."));
Expand Down
Binary file modified presets.o
Binary file not shown.
23 changes: 18 additions & 5 deletions sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ void sync_send (Sync * sync) {
LOGD ("ip: %s, port: %s, key: %s\n", ip.c_str(), port.c_str (), key.c_str ());

/// ayyo, importing what we already have!
//~ int how_many = p -> import_presets_from_json (j);
//~ char * ss = g_strdup_printf ("<span foreground=\"green\" weight=\"bold\" size=\"x-large\">Imported %d presets successfully</span>", how_many);
//~ gtk_label_set_markup (sync -> header, ss);
//~ g_free (ss);

if (response.size () > 0) {
j = json::parse (response);
int how_many = p -> import_presets_from_json (j);
char * ss = g_strdup_printf ("<span foreground=\"green\" weight=\"bold\" size=\"x-large\">Imported %d presets successfully</span>", how_many);
gtk_label_set_markup (sync -> header, ss);
g_free (ss);
} else {
LOGD ("[client] server returned empty response\n");
}
//~ LOGD ("[client] synced %d presets\n", how_many);
client -> close_socket ();
OUT
Expand All @@ -42,6 +46,11 @@ void run_server (Sync * sync) {
IN
sync -> server = new Server();
sync -> server -> sync = sync ;

# ifndef __linux__
sync -> server -> status = sync -> header ;
# endif

sync -> server -> presets = (Presets *) sync -> rack -> presets ;
sync -> server->run();

Expand All @@ -53,6 +62,10 @@ void close_sync (Sync * sync) {
sync -> server -> close_socket ();
sync -> t -> join () ;
gtk_window_destroy (sync -> window);
# ifndef __linux__
Presets * p = (Presets *) sync -> rack -> presets ;
p -> load_user (false);
# endif
OUT
}

Expand Down
Binary file modified sync.o
Binary file not shown.
2 changes: 1 addition & 1 deletion version.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define VERSION 158
#define VERSION 159
30 changes: 26 additions & 4 deletions winserver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
asio::thread_pool ioc(1);


void read_session(Presets * p, tcp::socket sock) {
void read_session(Presets * p, GtkLabel * status, tcp::socket sock) {
IN
LOGD ("Connection established");

Expand All @@ -23,10 +23,16 @@ void read_session(Presets * p, tcp::socket sock) {
str.pop_back () ;
LOGD ("[server] got request: %s", str.c_str ());
json j = json::parse (str);

json our = p -> get_all_user_presets ();
std::string msg = our.dump ().append (string ("}")) ;
LOGD ("[server] send message: %s", msg.c_str ());
write(sock, asio::buffer(msg));

int how_many = p -> import_presets_from_json (j);
char * ss = g_strdup_printf ("<span foreground=\"green\" weight=\"bold\" size=\"x-large\">Imported %d presets successfully</span>", how_many);
LOGD("[server] %s\n", ss);
//~ gtk_label_set_markup (sync -> header, ss);
gtk_label_set_markup (status, ss);
g_free (ss);
LOGD ("Connection closed");
OUT
Expand All @@ -48,6 +54,8 @@ Client::Client (string host, int port) {
std::string Client::send_preset (json j) {
IN
asio::io_service ioc(1);
std::array<char, 1000> buffer;
std::string str ("");

try {
tcp::socket m_Socket(ioc);
Expand All @@ -64,11 +72,25 @@ std::string Client::send_preset (json j) {
LOGD ("[client] send message: %s", msg.c_str ());
write(m_Socket, asio::buffer(msg));
std::this_thread::sleep_for(100ms);

LOGV ("[client] reading server response ...");
for (boost::system::error_code ec;;) {
size_t n = m_Socket.read_some(asio::buffer(buffer), ec);
LOGD( "Received %d bytes: %s (%s)", n, std::string(buffer.data(), n).c_str (), ec.message().c_str ());

str.append (std::string(buffer.data(), n));
if (ec.failed() || str.find("}}}") != string::npos)
break;
}

LOGV (str.c_str ());
str.pop_back () ;

} catch (std::exception& e) {
LOGD("Exception: %s",e.what());
}

return string ("{}}");
return str ;
OUT
}

Expand All @@ -87,7 +109,7 @@ Server::Server () {

void Server::run () {
IN
TheServer server(ioc.get_executor(), 6906);
TheServer server(ioc.get_executor(), 6906, status);
main_loop = g_main_loop_new (null, true);
g_main_loop_run (main_loop);
OUT
Expand Down
10 changes: 7 additions & 3 deletions winserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ using asio::ip::tcp;
using namespace std::chrono_literals;
using boost::system::error_code;

void read_session(Presets * p, tcp::socket sock) ;
void read_session(Presets * p, GtkLabel *, tcp::socket sock) ;

class TheServer {
public:
Presets * presets ;
TheServer(asio::any_io_executor ex, uint16_t port) //
GtkLabel * status ;
TheServer(asio::any_io_executor ex, uint16_t port, GtkLabel * s) //
: m_Acceptor{ex, tcp::endpoint{tcp::v4(), port}} //
{
IN
status = s ;
m_Acceptor.set_option(tcp::acceptor::reuse_address(true));
do_accept();
OUT
Expand All @@ -36,7 +38,7 @@ class TheServer {
IN
m_Acceptor.async_accept([this](boost::system::error_code ec, tcp::socket s) {
if (!ec) {
std::thread(read_session, presets, std::move(s)).detach();
std::thread(read_session, presets, status, std::move(s)).detach();
do_accept(); // and immediately accept new connection(s)
} else {
//~ std::cout << "Connection error (" << ec.message() << ")" << std::endl;
Expand All @@ -51,6 +53,7 @@ class TheServer {

class Client {
public:
Presets * presets;
Client(string host, int port);
~Client();

Expand All @@ -77,6 +80,7 @@ class Server {
~Server();

Presets * presets ;
GtkLabel * status ;
void * sync ;
void run();

Expand Down
Binary file modified winserver.o
Binary file not shown.

0 comments on commit 3ceb6a4

Please sign in to comment.