-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor_message_reader.cpp
169 lines (155 loc) · 3.81 KB
/
sensor_message_reader.cpp
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
161
162
163
164
165
166
167
168
169
#include <functional>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <thread>
#include "sensor_message_reader.h"
SensorMessageReader::SensorMessageReader(int file_desc, const char *restart_path, size_t message_size, std::function<void(void *)> read_func)
{
producer_pid = 0;
this->restart_path = restart_path;
this->read_func = read_func;
this->sfd = file_desc;
this->running = true;
this->message_size = message_size;
}
void SensorMessageReader::start()
{
_thread = std::thread(&SensorMessageReader::read_unix_socket, this);
}
void SensorMessageReader::stop()
{
running = false;
_thread.join();
}
int SensorMessageReader::write_restart_message()
{
int fd = open(restart_path, O_WRONLY | O_CREAT, 0600);
if (!fd)
{
perror("restart");
return 0;
}
close(fd);
return 1;
}
// Keep waiting for pending connections, return a descriptor when one is established
int SensorMessageReader::wait_accept_socket()
{
fd_set input;
// NOTE: blocks until a connection request arrives.
FD_ZERO(&input);
FD_SET(sfd, &input);
int cfd = accept4(sfd, NULL, NULL, SOCK_NONBLOCK);
if (cfd == -1)
{
if (errno == EWOULDBLOCK)
{ // no pending connections
return 0;
}
else
{
perror("error when accepting connection");
return 0;
}
}
else
{
return cfd;
}
}
pid_t SensorMessageReader::read_producer_PID(int cfd)
{
fd_set input;
struct timeval timeout;
FD_ZERO(&input);
FD_SET(cfd, &input);
timeout.tv_sec = 0;
timeout.tv_usec = 100e3;
int ret = select(cfd + 1, &input, NULL, NULL, &timeout);
if (!ret)
{
printf("PID read timed out\n");
return 0;
}
else if (ret == -1)
{
perror("Select");
return 0;
}
int PID;
pid_t numRead = read(cfd, &PID, sizeof(pid_t));
if (numRead != sizeof(pid_t))
{
perror("Read failed");
return 0;
}
return PID;
}
// Keep waiting for connections then reading messages in a loop using wait_accept_socket
int SensorMessageReader::read_unix_socket()
{
size_t numRead;
fd_set input;
struct timeval timeout;
char message_buf[message_size];
while (running)
{
// sfd remains open and can be used to accept further connections. */
printf("Waiting to accept a connection...\n");
int cfd = wait_accept_socket();
if (!cfd)
{
sleep(1);
continue;
}
printf("Accepted socket fd = %d\n", cfd);
this->producer_pid = read_producer_PID(cfd);
if (!producer_pid)
{
close(cfd);
continue;
}
while (running)
{
FD_ZERO(&input);
FD_SET(cfd, &input);
timeout.tv_sec = 0;
timeout.tv_usec = 100e3;
int ret = select(cfd + 1, &input, NULL, NULL, &timeout);
if (!ret)
{
printf("socket read timed out\n");
continue;
}
else if (ret == -1)
{
perror("Select");
return 1;
}
numRead = read(cfd, &message_buf, message_size);
if (numRead != message_size)
{
perror("read failed");
printf("numRead: %u \n", numRead);
if (!this->write_restart_message())
{
exit(EXIT_FAILURE);
}
break;
}
else
{
read_func(message_buf);
}
}
if (close(cfd) == -1)
{
perror("close");
}
}
return 0;
}