-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifo.cpp
More file actions
145 lines (123 loc) · 2.97 KB
/
fifo.cpp
File metadata and controls
145 lines (123 loc) · 2.97 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
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
/***************************************************************************
fifo.cpp - code to allow interprocess communication via a fifo, or "names pipe"
*
* copyright : (C) 2009 by Jim Skon
*
* This code permits the creation and use of FIFOs for communication
* between processes.
*
* the named piped is created and used in /tmp
*
***************************************************************************/
#include "fifo.h"
using namespace std;
Fifo::Fifo(){
fd = 0;
}
Fifo::Fifo(string name){
// create a named pipe (FIFO)
// build the name string
pipename = PATH + SIG + name;
umask(0);
// Create (or open) the fifo
int result = mknod(pipename.c_str(),MODE | S_IFIFO, 0);
if ((result == -1) && (errno != EEXIST)) {
cout << "Error creating pipe: " << name << endl;
return;
}
//cout << "Success creating pipe: " << name << endl;
fd = 0;
return;
}
void Fifo::openwrite() {
if (fd !=0) {
cout << "Fifo already opened: " << pipename << endl;
return;
}
// Open the pipe
fd = open(pipename.c_str(),O_WRONLY);
// Check if open succeeded
if (fd ==-1) {
cout << "Error - bad input pipe: " << pipename << endl;
return;
}
}
void Fifo::openread() {
if (fd !=0) {
cout << "Fifo already opened: " << pipename << endl;
return;
}
// Open the pipe
fd = open(pipename.c_str(),O_RDONLY);
// Check if open succeeded
if (fd ==-1) {
cout << "Error - bad input pipe: " << pipename << endl;
return;
}
}
void Fifo::fifoclose() {
close(fd);
fd = 0;
}
// Receive a message from a FIFO (named pipe)
string Fifo::recv() {
if (fd ==0) {
cout << "Fifo not open for read: " << pipename << endl;
return ("");
}
int length, i;
string message;
bool done;
int bytes;
char inbuff;
// clear message buffer
message = "";
// read until we see an end of message line
done = false;
i = 0;
while (i<MaxMess && !done) {
// Read the next character in the fifo
bytes = read(fd, &inbuff,1);
// -1 means something isn't working
if (bytes ==-1) {
cout << "Error - bad read on input pipe: " << pipename << endl;
return("");
}
// check if nothing was read
if (bytes > 0) {
// Check if end of message
if (inbuff == MESSTERM && (i > 0)) {
done = true;
} else {
i++;
message += inbuff;
}
} else {
// Nothing to read, try to open
fifoclose();
openread();
}
}
return(message);
}
// Send a message to a FIFO (named pipe)
// Return 0 if fails, 1 if succeeds
void Fifo::send(string message) {
if (fd ==0) {
cout << "Fifo not open for send: " << pipename << endl;
return;
}
int bytes;
// Append end of message terminator
message = message + MESSTERM;
bytes = write(fd, message.c_str(),message.length());
if (bytes ==-1) {
cout << "Error - bad write on output pipe: " << pipename << endl;
return;
}
if (bytes == 0) {
cout << "Error - nothing written: " << pipename << endl;
return;
}
return;
}