aboutsummaryrefslogtreecommitdiff
path: root/src/Rpc.cpp
blob: 2bf54fb815c89ff4b0ad8227bab57b1d1fc8f4bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "../include/Rpc.hpp"
#include <string>
#include <cassert>

namespace dchat
{
    Rpc::Rpc(u16 port) : 
        context(1), 
        socket(context, ZMQ_PAIR)
    {
        std::string addr = "tcp://*:";
        addr += std::to_string(port);
        socket.bind(addr);
    }

    void Rpc::recv(RpcRecvCallbackFunc recvCallbackFunc)
    {
        assert(recvCallbackFunc);
        zmq::message_t request;
        if(socket.recv(&request, ZMQ_NOBLOCK))
        {
            recvCallbackFunc(&request);
        }
    }

    bool Rpc::send(const void *data, const usize size)
    {
        if(size == 0) return false;
        try
        {
            return socket.send(data, size, ZMQ_NOBLOCK) > 0;
        }
        catch(zmq::error_t &e)
        {
            fprintf(stderr, "Rpc::send failed, reason: %s\n", e.what());
            return false;
        }
    }
}