небольшая чистка и улучшение кода, попытка исправить picloud

This commit is contained in:
Бычков Андрей
2022-11-07 17:16:27 +03:00
parent 8a5e72c723
commit f08a07cab0
11 changed files with 54 additions and 79 deletions

View File

@@ -45,7 +45,7 @@ void PIWaitEvent::create() {
piCout << "Error with CreateEventA:" << errorString();
}
#else
for (int i = 0; i < 3; ++i) memset(&(fds[i]), 0, sizeof(fds[i]));
for (int i = 0; i < sizeof(fds); ++i) memset(&(fds[i]), 0, sizeof(fds[i]));
if (::pipe(pipe_fd) < 0) {
piCout << "Error with pipe:" << errorString();
} else {
@@ -62,7 +62,7 @@ void PIWaitEvent::destroy() {
event = NULL;
}
#else
for (int i = 0; i < 2; ++i) {
for (int i = 0; i < sizeof(pipe_fd); ++i) {
if (pipe_fd[i] != 0) {
::close(pipe_fd[i]);
pipe_fd[i] = 0;
@@ -72,35 +72,24 @@ void PIWaitEvent::destroy() {
}
#ifdef WINDOWS
bool PIWaitEvent::wait() {
#else
bool PIWaitEvent::wait(int fd, PIWaitEvent::SelectRole role) {
#endif
bool PIWaitEvent::wait(int fd, CheckRole role) {
if (!isCreate()) return false;
#ifdef WINDOWS
DWORD ret = WaitForSingleObjectEx(event, INFINITE, TRUE);
ResetEvent(event);
if (ret == WAIT_IO_COMPLETION || ret == WAIT_FAILED) return false;
#else
if (fd == -1) return false;
int nfds = piMaxi(pipe_fd[ReadEnd], fd) + 1;
int fd_index = 0;
switch (role) {
case CheckRead:
fd_index = 0;
break;
case CheckWrite:
fd_index = 1;
break;
}
for (int i = 0; i < 3; ++i) FD_ZERO(&(fds[i]));
FD_SET(pipe_fd[ReadEnd], &(fds[0]));
FD_SET(fd, &(fds[2]));
FD_SET(fd, &(fds[fd_index]));
::select(nfds, &(fds[0]), &(fds[1]), &(fds[2]), nullptr);
int fd_index = role;
for (int i = 0; i < sizeof(fds); ++i) FD_ZERO(&(fds[i]));
FD_SET(pipe_fd[ReadEnd], &(fds[CheckRead]));
FD_SET(fd, &(fds[CheckExeption]));
if (fd_index != CheckExeption) FD_SET(fd, &(fds[fd_index]));
::select(nfds, &(fds[CheckRead]), &(fds[CheckWrite]), &(fds[CheckExeption]), nullptr);
int buf = 0;
while (::read(pipe_fd[ReadEnd], &buf, sizeof(buf)) > 0);
if (FD_ISSET(fd, &(fds[2]))) return false;
if (FD_ISSET(fd, &(fds[CheckExeption]))) return false;
return FD_ISSET(fd, &(fds[fd_index]));
#endif
return true;
@@ -124,3 +113,8 @@ bool PIWaitEvent::isCreate() const {
return pipe_fd[ReadEnd] != 0;
#endif
}
void * PIWaitEvent::getEvent() const {
return event;
}