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 f0cb3a548a
3 changed files with 31 additions and 2 deletions

View File

@ -1,5 +1,5 @@
//
// Copyright (c) 2017 Wind River Systems, Inc.
// Copyright (c) 2017,2023 Wind River Systems, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
@ -106,6 +106,18 @@ 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
// try to connect in the following while loop.
if (!m_client.fd_valid()) {
FM_WARNING_LOG("Invalid fd. Try to reconnect...");
m_connected = false;
}
}
while (!m_connected) {
struct addrinfo hints;
struct addrinfo *result = NULL, *rp;

View File

@ -1,5 +1,5 @@
//
// Copyright (c) 2017 Wind River Systems, Inc.
// Copyright (c) 2017,2023 Wind River Systems, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
@ -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,21 @@ 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, 0)) < 0) {
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);