Welcome to mirror list, hosted at ThFree Co, Russian Federation.

IPCConnection.cpp - github.com/neutrinolabs/ulalaca-xrdp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fd50dd10459f660671fab8c5709dae96f7553890 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// Created by Gyuhwan Park on 2022/05/24.
//

#if defined(HAVE_CONFIG_H)
#include <config_ac.h>
#endif

#include <cassert>

#include <fcntl.h>
#include <poll.h>

extern "C" {
#include "parse.h"
#include "defines.h"
}

#include "IPCConnection.hpp"

IPCConnection::IPCConnection(std::string socketPath):
    _socket(socketPath),
    _isWorkerTerminated(false),
    
    _messageId(0),
    _ackId(0)
{

}

void IPCConnection::connect() {
    _socket.connect();

    // enable non-blocking io
    auto flags = fcntl(_socket.descriptor(), F_GETFL, 0);
    if (fcntl(_socket.descriptor(), F_SETFL, flags | O_NONBLOCK)) {
        throw SystemCallException(errno, "fcntl");
    }


    _workerThread = std::thread(&IPCConnection::workerLoop, this);
}

void IPCConnection::disconnect() {
    _isWorkerTerminated = true;
    
    if (_workerThread.joinable()) {
        _workerThread.join();
    }
    _socket.close();
}

std::unique_ptr<ULIPCHeader, IPCConnection::MallocFreeDeleter> IPCConnection::nextHeader() {
    return std::move(read<ULIPCHeader>(sizeof(ULIPCHeader)));
}

void IPCConnection::write(const void *pointer, size_t size) {
    assert(pointer != nullptr);
    assert(size > 0);
    
    std::scoped_lock<std::mutex> scopedWriteTasksLock(_writeTasksLock);
    
    std::unique_ptr<uint8_t, MallocFreeDeleter> data(
        (uint8_t *) malloc(size),
        free
    );
    
    std::memcpy(data.get(), pointer, size);
    
    _writeTasks.emplace(size, std::move(data));
}

void IPCConnection::workerLoop() {
    const size_t MAX_READ_SIZE = 8192;

    size_t readPos = 0;
    std::unique_ptr<uint8_t> readBuffer;

    pollfd pollFd {
        _socket.descriptor(),
        POLLIN | POLLOUT,
        0
    };

    while (!_isWorkerTerminated) {
        if (poll(&pollFd, 1, 1) < 0) {
            throw SystemCallException(errno, "poll");
        }

        bool canRead = (pollFd.revents & POLLIN) > 0;
        bool canWrite = (pollFd.revents & POLLOUT) > 0;

        if (canWrite && !_writeTasks.empty()) {
            auto &writeTask = _writeTasks.front();
            
            if (_socket.write(writeTask.second.get(), writeTask.first) < 0) {
                if (errno == EAGAIN) {
                    continue;
                }
                
                LOG(LOG_LEVEL_ERROR, "write() failed (errno=%d)", errno);
                continue;
            }
    
            {
                std::scoped_lock<std::mutex> scopedWriteTasksLock(_writeTasksLock);
                _writeTasks.pop();
            }
        }

        if (canRead && !_readTasks.empty()) {
            auto &readTask = _readTasks.front();

            auto &contentLength = readTask.first;
            auto &promise = readTask.second;

            if (readBuffer == nullptr) {
                readPos = 0;
                readBuffer = std::unique_ptr<uint8_t>(new uint8_t[contentLength]);
            }

            int readForBytes = std::min(
                (size_t) MAX_READ_SIZE,
                contentLength - readPos
            );

            size_t retval = _socket.read(readBuffer.get() + readPos, readForBytes);

            if (retval < 0) {
                if (errno == EAGAIN) {
                    continue;
                } else {
                    throw SystemCallException(errno, "read");
                }
            }

            readPos += retval;

            if (readPos >= contentLength) {
                promise.set_value(std::move(readBuffer));
                {
                    std::scoped_lock<std::mutex> scopedReadTasksLock(_readTasksLock);
                    _readTasks.pop();
                }

                readBuffer = nullptr;
                readPos = 0;
            }
        }
        
        if (pollFd.revents & POLLHUP) {
            LOG(LOG_LEVEL_WARNING, "POLLHUP bit set");
        }
    
        if (pollFd.revents & POLLERR) {
            LOG(LOG_LEVEL_ERROR, "POLLERR bit set; closing connection");
            break;
        }
    }
}