| 1 | #define GI_INLINE 1 |
| 2 | #include <gio/gio.hpp> |
| 3 | |
| 4 | #include <iostream> |
| 5 | |
| 6 | namespace GLib = gi::repository::GLib; |
| 7 | namespace Gio = gi::repository::Gio; |
| 8 | |
| 9 | const std::string localhost("127.0.0.1"); |
| 10 | static GLib::MainLoop loop; |
| 11 | |
| 12 | // many calls here support GError |
| 13 | // so typically will throw here instead |
| 14 | // (unless GError output is explicitly requested in call signature) |
| 15 | |
| 16 | // NOTE abundancy of gi::expect not generally needed; |
| 17 | // only needed when using --dl and --expected |
| 18 | |
| 19 | static bool |
| 20 | receive(Gio::Socket s, GLib::IOCondition /*cond*/) |
| 21 | { |
| 22 | guint8 buffer[1024] = { |
| 23 | 0, |
| 24 | }; |
| 25 | Gio::SocketAddress a; |
| 26 | int count = gi::expect(s.receive_from(&a, buffer, sizeof(buffer))); |
| 27 | if (count > 0) { |
| 28 | // let's see where it came from |
| 29 | std::string origin("someone"); |
| 30 | auto ia = gi::object_cast<Gio::InetSocketAddress>(a); |
| 31 | if (ia) { |
| 32 | origin = gi::expect(gi::expect(ia.get_address()).to_string()); |
| 33 | origin += ":"; |
| 34 | origin += std::to_string(gi::expect(ia.get_port())); |
| 35 | } |
| 36 | std::cout << origin << " said " << (char *)buffer << std::endl; |
| 37 | // quit when idle |
| 38 | GLib::idle_add([]() { |
| 39 | loop.quit(); |
| 40 | return GLib::SOURCE_REMOVE_; |
| 41 | }); |
| 42 | } |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | Gio::Socket |
| 47 | open(bool listen) |
| 48 | { |
| 49 | auto socket = gi::expect(Gio::Socket::new_(Gio::SocketFamily::IPV4_, |
| 50 | Gio::SocketType::DATAGRAM_, Gio::SocketProtocol::DEFAULT_)); |
| 51 | |
| 52 | auto address = |
| 53 | gi::expect(Gio::InetSocketAddress::new_from_string(localhost, 0)); |
| 54 | socket.bind(address, false); |
| 55 | socket.set_blocking(false); |
| 56 | |
| 57 | if (listen) { |
| 58 | // runtime introspection has a hard time here, |
| 59 | // but with a bit of extra information, we can keep going |
| 60 | #if defined(GI_CALL_ARGS) && CALL_ARGS <= 1 |
| 61 | // so we should have this signature for a function with 1 non-required |
| 62 | GLib::Source source = |
| 63 | gi::expect(socket.create_source({.condition = GLib::IOCondition::IN_})); |
| 64 | #else |
| 65 | GLib::Source source = |
| 66 | gi::expect(socket.create_source(GLib::IOCondition::IN_, nullptr)); |
| 67 | #endif |
| 68 | source.set_callback<Gio::SocketSourceFunc>(receive); |
| 69 | source.attach(); |
| 70 | } |
| 71 | |
| 72 | return socket; |
| 73 | } |
| 74 | |
| 75 | static void |
| 76 | die(const std::string &why) |
| 77 | { |
| 78 | std::cerr << why << std::endl; |
| 79 | exit(2); |
| 80 | } |
| 81 | |
| 82 | int |
| 83 | main(int argc, char **argv) |
| 84 | { |
| 85 | if (argc < 2) |
| 86 | die("missing argument"); |
| 87 | |
| 88 | std::string msg = argv[1]; |
| 89 | std::cout << "will send message " << msg << std::endl; |
| 90 | |
| 91 | auto recv = open(true); |
| 92 | auto local = gi::expect(recv.get_local_address()); |
| 93 | auto send = open(false); |
| 94 | send.send_to(local, (guint8 *)msg.data(), msg.size(), nullptr); |
| 95 | |
| 96 | loop = gi::expect(GLib::MainLoop::new_()); |
| 97 | loop.run(); |
| 98 | } |