summaryrefslogtreecommitdiff
path: root/Lagom/SimpleIPCServer.cpp
blob: 290c95300c72150c47812cc04e7abe4091cda7b7 (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
40
#include <LibCore/CEventLoop.h>
#include <LibCore/CTimer.h>
#include <LibCore/CoreIPCServer.h>
#include <LibCore/CLocalServer.h>
#include <stdio.h>
#include "SimpleEndpoint.h"

class SimpleIPCServer final :
    public IPC::Server::ConnectionNG<SimpleEndpoint>,
    public SimpleEndpoint {

    C_OBJECT(SimpleIPCServer)
public:
    SimpleIPCServer(CLocalSocket& socket, int client_id)
        : ConnectionNG(*this, socket, client_id)
    {
    }

    virtual OwnPtr<Simple::ComputeSumResponse> handle(const Simple::ComputeSum& message)
    {
        return make<Simple::ComputeSumResponse>(message.a() + message.b() + message.c());
    }
};

int main(int, char**)
{
    CEventLoop event_loop;

    unlink("/tmp/simple-ipc");
    auto server = CLocalServer::construct();
    server->listen("/tmp/simple-ipc");
    server->on_ready_to_accept = [&] {
        auto client_socket = server->accept();
        ASSERT(client_socket);
        static int next_client_id = 0;
        IPC::Server::new_connection_ng_for_client<SimpleIPCServer>(*client_socket, ++next_client_id);
    };

    return event_loop.exec();
}