Check fd in reconnect

Change-Id: Ibc0f4d96b5c0a385d8fedbc1acd23898f1cbea46
Signed-off-by: Takamasa Takenaka <takamasa.takenaka@windriver.com>
This commit is contained in:
Takamasa Takenaka 2023-10-12 19:21:44 -03:00
parent 1fb9ffdc54
commit b0632a1fc8
3 changed files with 33 additions and 0 deletions

View File

@ -106,6 +106,20 @@ static bool dequeue(fm_buff_t &req) {
static bool fm_lib_reconnect() {
char addr[INET6_ADDRSTRLEN];
if (m_connected) {
// Check fd is valid
// When fm manager is restarted, it is possible to
// have broken pipe. This checks fd is valid or not.
// If not, set m_connected false, so that it will
// connect in the following while loop.
if (!m_client.fd_valid()) {
FM_WARNING_LOG("Invalid fd. Try to reconnect...");
m_connected = false;
} else {
FM_INFO_LOG("Valid fd. Continue...");
}
}
while (!m_connected) {
struct addrinfo hints;
struct addrinfo *result = NULL, *rp;

View File

@ -10,6 +10,7 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/poll.h>
#include <sys/select.h>
#include <errno.h>
#include <unistd.h>
@ -259,6 +260,23 @@ int CFmSocket::select_read(int fd,int timeout, bool &timedout){
return select(&fd,1,NULL,0,timeout,0,timedout);
}
bool CFmSocket::fd_valid(){
struct pollfd pfd = {.fd = m_fd, .events = POLLRDHUP};
if ((poll(&pfd, 1, 1000)) < 0) {
// error to poll
FM_ERROR_LOG("Error to poll to check socket\n");
return false;
} else {
if (pfd.revents & POLLRDHUP) {
// broken pipe
FM_ERROR_LOG("It is broken pipe\n");
return false;
} else {
return true;
}
}
}
bool CFmSocket::recvfrom(void *data, long &len, CFmSockAddr &addr) {
socklen_t addr_len = sizeof(addr.address);
int l = ::recvfrom(m_fd,data,len,0,addr.get_sockaddr(),&addr_len);

View File

@ -67,6 +67,7 @@ public:
int get_fd() { return m_fd; }
bool fd_valid();
bool recvfrom(void *data, long &len, CFmSockAddr &addr );
static int select(int *rfd, int rlen, int *wfds, int wlen,int timeout,int timeoutusec, bool &timedout);