makiolo

serializer proof of concept

Dec 13th, 2015
115
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fes/sync.h>
  3. #include "serialize.h"
  4. #include "unserialize.h"
  5. #include "network_server.h"
  6. #include "network_client.h"
  7.  
  8. // - You can send a arbitrary messages using operator()(...) in client or server
  9. // - No stubs or intermediate skeletons, just metaprogramming.
  10. // - Dataflow oriented, you can connect signals (fes) to client or server
  11. // - external libraries: RakNet(net problems) + msgpack(serialization problems)
  12.  
  13. int main()
  14. {
  15.     // open server in port 5555 (udp with reliable capacibilities)
  16.     ser::network_server server(5555);
  17.     ser::network_client client("127.0.0.1", 5555);
  18.     // block here, constructor client is connected in async way
  19.     client.await_connected();
  20.  
  21.     // Example: 1/2
  22.     // subscribe functors in metaport 500
  23.     server.get_requests(500).connect(
  24.         [&](int version, auto pipe)
  25.         {
  26.             // retrocompatibility solution
  27.             if(version == 1)
  28.             {
  29.                 ser::unserialize<bool, int, std::string> unserializer(pipe,
  30.                     [](bool a, int b, const std::string& c) {
  31.                         std::cout << "a = " << a << std::endl;
  32.                         std::cout << "b = " << b << std::endl;
  33.                         std::cout << "c = " << c << std::endl;
  34.                     }
  35.                 );
  36.             }
  37.             else
  38.             {
  39.                 ser::unserialize<bool, float, std::string, std::string> unserializer(pipe,
  40.                     [](bool a, int b, const std::string& c, const std::string& d) {
  41.                         std::cout << "a = " << a << std::endl;
  42.                         std::cout << "b = " << b << std::endl;
  43.                         std::cout << "c = " << c << std::endl;
  44.                         std::cout << "d = " << d << std::endl;
  45.                     }
  46.                 );
  47.             }
  48.         }
  49.     );
  50.     // send client to server
  51.     client(     500,    // port destiny
  52.             1,      // interface version
  53.             true, 589, "version 1, puerto 500");
  54.  
  55.     // Example: 2/2
  56.     // send server to client
  57.     // example fes::sync<> for currify port and version
  58.     fes::sync<std::string> input;
  59.     input.connect([&](auto&& ... args) {
  60.         // perfect forwarding everything but currify two first parameters
  61.         server(300, 1, std::forward<decltype(args)>(args)...);
  62.     });
  63.     input.connect([](const std::string& data) {
  64.         std::cout << "local: " << data << std::endl;
  65.     });
  66.     // subscribe functors in metaport 300 (port real is always 5555, ofc)
  67.     client.get_requests(300).connect(
  68.         [&](int, auto pipe) {
  69.             ser::unserialize<std::string> unserializer(pipe,
  70.                 [](const std::string& data) {
  71.                     std::cout << "remote: " << data << std::endl;
  72.                 }
  73.             );
  74.         }
  75.     );
  76.     input("text propagated to local client and remote server");
  77.  
  78.     // dispathing networking a limited time
  79.     client.update_for(fes::deltatime(16));
  80.     server.update_for(fes::deltatime(16));
  81.  
  82.     return 0;
  83. }
RAW Paste Data