forked from linuxdeepin/dde-session
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifo.cpp
More file actions
86 lines (77 loc) · 1.73 KB
/
fifo.cpp
File metadata and controls
86 lines (77 loc) · 1.73 KB
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
// SPDX-FileCopyrightText: 2021 - 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "utils/fifo.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <QDebug>
Fifo::Fifo(QObject *parent)
: QObject(parent)
, m_fd(-1)
{
m_fifoPath = QString(getenv("HOME")) + "/.cache/dde-session-fifo";
}
Fifo::~Fifo()
{
if (m_fd > 0) {
close(m_fd);
}
}
int Fifo::OpenRead()
{
if(mkfifo(m_fifoPath.toStdString().c_str(), 0666) < 0 && errno != EEXIST) {
qDebug() << "mkfifo error";
return -1;
}
m_fd = open(m_fifoPath.toStdString().c_str(), O_RDONLY);
if (m_fd < 0) {
qDebug() << "open fifo error";
return -1;
}
return 0;
}
int Fifo::Write(QString data)
{
if (m_fd < 0) {
qDebug() << "write fifo error";
return -1;
}
write(m_fd, data.toStdString().c_str(), size_t(data.length()));
return 0;
}
int Fifo::Read(QString &data)
{
if (m_fd < 0) {
qDebug() << "read fifo error";
return -1;
}
int len = 0;
char buf[1024] = { 0 };
len = read(m_fd, buf, sizeof(buf));
if (len > 0) {
data = QString::fromStdString(buf);
}
return len;
}
int Fifo::OpenWrite()
{
if(mkfifo(m_fifoPath.toStdString().c_str(), 0666) < 0 && errno != EEXIST) {
qDebug() << "mkfifo error";
return -1;
}
// signal(SIGPIPE, [](int ret){
// qDebug() << "SIGQUIT catched!";
// });
m_fd = open(m_fifoPath.toStdString().c_str(), O_WRONLY);
if (m_fd < 0) {
qDebug() << "open fifo error";
return -1;
}
return 0;
}