Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fes/sync.h>
- #include "serialize.h"
- #include "unserialize.h"
- #include "network_server.h"
- #include "network_client.h"
- // - You can send a arbitrary messages using operator()(...) in client or server
- // - No stubs or intermediate skeletons, just metaprogramming.
- // - Dataflow oriented, you can connect signals (fes) to client or server
- // - external libraries: RakNet(net problems) + msgpack(serialization problems)
- int main()
- {
- // open server in port 5555 (udp with reliable capacibilities)
- ser::network_server server(5555);
- ser::network_client client("127.0.0.1", 5555);
- // block here, constructor client is connected in async way
- client.await_connected();
- // Example: 1/2
- // subscribe functors in metaport 500
- server.get_requests(500).connect(
- [&](int version, auto pipe)
- {
- // retrocompatibility solution
- if(version == 1)
- {
- ser::unserialize<bool, int, std::string> unserializer(pipe,
- [](bool a, int b, const std::string& c) {
- std::cout << "a = " << a << std::endl;
- std::cout << "b = " << b << std::endl;
- std::cout << "c = " << c << std::endl;
- }
- );
- }
- else
- {
- ser::unserialize<bool, float, std::string, std::string> unserializer(pipe,
- [](bool a, int b, const std::string& c, const std::string& d) {
- std::cout << "a = " << a << std::endl;
- std::cout << "b = " << b << std::endl;
- std::cout << "c = " << c << std::endl;
- std::cout << "d = " << d << std::endl;
- }
- );
- }
- }
- );
- // send client to server
- client( 500, // port destiny
- 1, // interface version
- true, 589, "version 1, puerto 500");
- // Example: 2/2
- // send server to client
- // example fes::sync<> for currify port and version
- fes::sync<std::string> input;
- input.connect([&](auto&& ... args) {
- // perfect forwarding everything but currify two first parameters
- server(300, 1, std::forward<decltype(args)>(args)...);
- });
- input.connect([](const std::string& data) {
- std::cout << "local: " << data << std::endl;
- });
- // subscribe functors in metaport 300 (port real is always 5555, ofc)
- client.get_requests(300).connect(
- [&](int, auto pipe) {
- ser::unserialize<std::string> unserializer(pipe,
- [](const std::string& data) {
- std::cout << "remote: " << data << std::endl;
- }
- );
- }
- );
- input("text propagated to local client and remote server");
- // dispathing networking a limited time
- client.update_for(fes::deltatime(16));
- server.update_for(fes::deltatime(16));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement