always check error even if something can be read, flush stdout
This commit is contained in:
+19
-18
@@ -132,7 +132,7 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
string fetchData(ebusd::TCPSocket* socket, bool &listening, uint16_t timeout) {
|
string fetchData(ebusd::TCPSocket* socket, bool &listening, uint16_t timeout, bool &errored) {
|
||||||
char data[1024];
|
char data[1024];
|
||||||
ssize_t datalen;
|
ssize_t datalen;
|
||||||
ostringstream ostream;
|
ostringstream ostream;
|
||||||
@@ -173,25 +173,20 @@ string fetchData(ebusd::TCPSocket* socket, bool &listening, uint16_t timeout) {
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
while (time(&now) && now < endTime) {
|
while (!errored && time(&now) && now < endTime) {
|
||||||
#ifdef HAVE_PPOLL
|
#ifdef HAVE_PPOLL
|
||||||
// check previous hangup
|
|
||||||
if (ret > 0 && ((fds[0].revents & (POLLHUP | POLLRDHUP)) || (fds[1].revents & (POLLHUP | POLLRDHUP)))) {
|
|
||||||
listening = false;
|
|
||||||
break; // remote hung up
|
|
||||||
}
|
|
||||||
// check previous error
|
|
||||||
if (ret > 0 && ((fds[0].revents & POLLERR) || (fds[1].revents & POLLERR))) {
|
|
||||||
listening = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// wait for new fd event
|
// wait for new fd event
|
||||||
ret = ppoll(fds, nfds, &tdiff, nullptr);
|
ret = ppoll(fds, nfds, &tdiff, nullptr);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
perror("ebusctl poll");
|
perror("ebusctl poll");
|
||||||
listening = false;
|
errored = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (ret > 0 && ((fds[0].revents & POLLERR) || (fds[1].revents & POLLERR))) {
|
||||||
|
errored = true;
|
||||||
|
} else if (ret > 0 && ((fds[0].revents & (POLLHUP | POLLRDHUP)) || (fds[1].revents & (POLLHUP | POLLRDHUP)))) {
|
||||||
|
errored = true;
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
#ifdef HAVE_PSELECT
|
#ifdef HAVE_PSELECT
|
||||||
// set readfds to inital checkfds
|
// set readfds to inital checkfds
|
||||||
@@ -226,6 +221,7 @@ string fetchData(ebusd::TCPSocket* socket, bool &listening, uint16_t timeout) {
|
|||||||
|
|
||||||
if (datalen < 0) {
|
if (datalen < 0) {
|
||||||
perror("ebusctl recv");
|
perror("ebusctl recv");
|
||||||
|
errored = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,7 +243,7 @@ string fetchData(ebusd::TCPSocket* socket, bool &listening, uint16_t timeout) {
|
|||||||
sendmessage = message+'\n';
|
sendmessage = message+'\n';
|
||||||
if (socket->send(sendmessage.c_str(), sendmessage.size()) < 0) {
|
if (socket->send(sendmessage.c_str(), sendmessage.size()) < 0) {
|
||||||
perror("ebusctl send in fetch");
|
perror("ebusctl send in fetch");
|
||||||
listening = false;
|
errored = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,6 +269,7 @@ bool connect(const char* host, uint16_t port, uint16_t timeout, char* const *arg
|
|||||||
ret = socket != nullptr;
|
ret = socket != nullptr;
|
||||||
if (ret) {
|
if (ret) {
|
||||||
string message, sendmessage;
|
string message, sendmessage;
|
||||||
|
bool errored = false;
|
||||||
do {
|
do {
|
||||||
bool listening = false;
|
bool listening = false;
|
||||||
|
|
||||||
@@ -298,6 +295,8 @@ bool connect(const char* host, uint16_t port, uint16_t timeout, char* const *arg
|
|||||||
sendmessage = message+'\n';
|
sendmessage = message+'\n';
|
||||||
if (socket->send(sendmessage.c_str(), sendmessage.size()) < 0) {
|
if (socket->send(sendmessage.c_str(), sendmessage.size()) < 0) {
|
||||||
perror("ebusctl send");
|
perror("ebusctl send");
|
||||||
|
ret = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcasecmp(message.c_str(), "Q") == 0
|
if (strcasecmp(message.c_str(), "Q") == 0
|
||||||
@@ -309,18 +308,20 @@ bool connect(const char* host, uint16_t port, uint16_t timeout, char* const *arg
|
|||||||
if (strcasecmp(message.c_str(), "L") == 0
|
if (strcasecmp(message.c_str(), "L") == 0
|
||||||
|| strcasecmp(message.c_str(), "LISTEN") == 0) {
|
|| strcasecmp(message.c_str(), "LISTEN") == 0) {
|
||||||
listening = true;
|
listening = true;
|
||||||
while (listening && !cin.eof()) {
|
while (!errored && listening && !cin.eof()) {
|
||||||
string result(fetchData(socket, listening, timeout));
|
string result(fetchData(socket, listening, timeout, errored));
|
||||||
cout << result;
|
cout << result;
|
||||||
|
cout.flush();
|
||||||
if (strcasecmp(result.c_str(), "LISTEN STOPPED") == 0) {
|
if (strcasecmp(result.c_str(), "LISTEN STOPPED") == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
cout << fetchData(socket, listening, timeout);
|
cout << fetchData(socket, listening, timeout, errored);
|
||||||
|
cout.flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (!once && !cin.eof());
|
} while (!errored && !once && !cin.eof());
|
||||||
delete socket;
|
delete socket;
|
||||||
} else {
|
} else {
|
||||||
cout << "error connecting to " << host << ":" << port << endl;
|
cout << "error connecting to " << host << ":" << port << endl;
|
||||||
|
|||||||
Reference in New Issue
Block a user