aboutsummaryrefslogtreecommitdiff
path: root/src/Rpc.cpp
blob: 206b1cfc9fdff61c46ffa2bd4d4a4ce23b9b13cc (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "../include/Rpc.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/fcntl.h>

namespace gsr {
    static void get_runtime_filepath(char *buffer, size_t buffer_size, const char *filename) {
        char dir[PATH_MAX];

        const char *runtime_dir = getenv("XDG_RUNTIME_DIR");
        if(runtime_dir)
            snprintf(dir, sizeof(dir), "%s", runtime_dir);
        else
            snprintf(dir, sizeof(dir), "/run/user/%d", geteuid());

        if(access(dir, F_OK) != 0)
            snprintf(dir, sizeof(dir), "/tmp");

        snprintf(buffer, buffer_size, "%s/%s", dir, filename);
    }

    Rpc::~Rpc() {
        if(fd > 0)
            close(fd);

        if(file)
            fclose(file);

        if(!fifo_filepath.empty())
            remove(fifo_filepath.c_str());
    }

    bool Rpc::create(const char *name) {
        if(file) {
            fprintf(stderr, "Error: Rpc::create: already created/opened\n");
            return false;
        }

        char fifo_filepath_tmp[PATH_MAX];
        get_runtime_filepath(fifo_filepath_tmp, sizeof(fifo_filepath_tmp), name);
        fifo_filepath = fifo_filepath_tmp;
        remove(fifo_filepath.c_str());

        if(mkfifo(fifo_filepath.c_str(), 0600) != 0) {
            fprintf(stderr, "Error: mkfifo failed, error: %s, %s\n", strerror(errno), fifo_filepath.c_str());
            return false;
        }

        if(!open_filepath(fifo_filepath.c_str())) {
            remove(fifo_filepath.c_str());
            fifo_filepath.clear();
            return false;
        }

        return true;
    }

    bool Rpc::open(const char *name) {
        if(file) {
            fprintf(stderr, "Error: Rpc::open: already created/opened\n");
            return false;
        }

        char fifo_filepath_tmp[PATH_MAX];
        get_runtime_filepath(fifo_filepath_tmp, sizeof(fifo_filepath_tmp), name);
        return open_filepath(fifo_filepath_tmp);
    }

    bool Rpc::open_filepath(const char *filepath) {
        fd = ::open(filepath, O_RDWR | O_NONBLOCK);
        if(fd <= 0)
            return false;

        file = fdopen(fd, "r+");
        if(!file) {
            close(fd);
            fd = 0;
            return false;
        }
        fd = 0;
        return true;
    }

    bool Rpc::write(const char *str, size_t size) {
        if(!file) {
            fprintf(stderr, "Error: Rpc::write: fifo not created/opened yet\n");
            return false;
        }

        ssize_t offset = 0;
        while(offset < (ssize_t)size) {
            const ssize_t bytes_written = fwrite(str + offset, 1, size - offset, file);
            fflush(file);
            if(bytes_written > 0)
                offset += bytes_written;
        }
        return true;
    }

    void Rpc::poll() {
        if(!file) {
            //fprintf(stderr, "Error: Rpc::poll: fifo not created/opened yet\n");
            return;
        }

        std::string name;
        char line[1024];
        while(fgets(line, sizeof(line), file)) {
            int line_len = strlen(line);
            if(line_len == 0)
                continue;

            if(line[line_len - 1] == '\n') {
                line[line_len - 1] = '\0';
                --line_len;
            }

            name = line;
            auto it = handlers_by_name.find(name);
            if(it != handlers_by_name.end())
                it->second(name);
        }
    }

    bool Rpc::add_handler(const std::string &name, RpcCallback callback) {
        return handlers_by_name.insert(std::make_pair(name, std::move(callback))).second;
    }
}