mirror of
https://github.com/radio95-rnt/rds95.git
synced 2026-07-30 16:59:15 +02:00
try to optimize control_pipe.c
This commit is contained in:
+14
-23
@@ -15,7 +15,6 @@ int open_control_pipe(char *filename) {
|
|||||||
/* setup the poller */
|
/* setup the poller */
|
||||||
poller.fd = fd;
|
poller.fd = fd;
|
||||||
poller.events = POLLIN;
|
poller.events = POLLIN;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,41 +25,33 @@ int open_control_pipe(char *filename) {
|
|||||||
void poll_control_pipe() {
|
void poll_control_pipe() {
|
||||||
static unsigned char pipe_buf[CTL_BUFFER_SIZE];
|
static unsigned char pipe_buf[CTL_BUFFER_SIZE];
|
||||||
static unsigned char cmd_buf[CMD_BUFFER_SIZE];
|
static unsigned char cmd_buf[CMD_BUFFER_SIZE];
|
||||||
struct timeval timeout;
|
int bytes_read;
|
||||||
int ret;
|
|
||||||
fd_set set;
|
|
||||||
char *token;
|
char *token;
|
||||||
|
|
||||||
FD_ZERO(&set);
|
/* check for new commands - return early if none */
|
||||||
FD_SET(fd, &set);
|
|
||||||
timeout.tv_sec = 0;
|
|
||||||
timeout.tv_usec = READ_TIMEOUT_MS * 1000;
|
|
||||||
|
|
||||||
/* check for new commands */
|
|
||||||
if (poll(&poller, 1, READ_TIMEOUT_MS) <= 0) return;
|
if (poll(&poller, 1, READ_TIMEOUT_MS) <= 0) return;
|
||||||
|
if (!(poller.revents & POLLIN)) return;
|
||||||
|
|
||||||
/* return early if there are no new commands */
|
/* read data from pipe */
|
||||||
if (poller.revents == 0) return;
|
|
||||||
|
|
||||||
memset(pipe_buf, 0, CTL_BUFFER_SIZE);
|
memset(pipe_buf, 0, CTL_BUFFER_SIZE);
|
||||||
|
bytes_read = read(fd, pipe_buf, CTL_BUFFER_SIZE - 1);
|
||||||
|
|
||||||
ret = select(fd + 1, &set, NULL, NULL, &timeout);
|
if (bytes_read <= 0) return;
|
||||||
if (ret == -1 || ret == 0) {
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
read(fd, pipe_buf, CTL_BUFFER_SIZE - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* handle commands per line this is really good because if were sending text commands very quick after eachother then we can get a rt of for example 'Now its 12:00RT Now its 12:01' */
|
/* process each command line */
|
||||||
token = strtok((char *)pipe_buf, "\n");
|
token = strtok((char *)pipe_buf, "\n");
|
||||||
while (token != NULL) {
|
while (token != NULL) {
|
||||||
|
size_t cmd_len = strlen(token);
|
||||||
|
if (cmd_len > 0 && cmd_len < CMD_BUFFER_SIZE) {
|
||||||
memset(cmd_buf, 0, CMD_BUFFER_SIZE);
|
memset(cmd_buf, 0, CMD_BUFFER_SIZE);
|
||||||
memcpy(cmd_buf, token, CMD_BUFFER_SIZE - 1);
|
strncpy((char *)cmd_buf, token, CMD_BUFFER_SIZE - 1);
|
||||||
token = strtok(NULL, "\n");
|
|
||||||
|
|
||||||
process_ascii_cmd(cmd_buf);
|
process_ascii_cmd(cmd_buf);
|
||||||
}
|
}
|
||||||
|
token = strtok(NULL, "\n");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void close_control_pipe() {
|
void close_control_pipe() {
|
||||||
if (fd > 0) close(fd);
|
if (fd > 0) close(fd);
|
||||||
|
fd = -1;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user