I have a loop while(1)that uses recvfromto receive data that was sent to a domain socket from another process (P2).
The while loop should do 2 things, first listen for incoming data from P2, and secondly, run another function checkVoltage().
So this is a bit like this:
while(true)
{
listenOnSocket() /*listens for 100 u seconds*/
checkVoltage();
}
My problem is this: a function listenOnSocket()uses a function recvfromto check input from another process. He conducts listening for 100 seconds, then turns off and proceeds to perform the function checkVoltage(). Thus, it spends 99% of the time in function listenOnSocket(). My problem is that if P2 sends the information to the socket during a function checkVoltage(), then it will lead to error, stating: sending datagram message: No such file or directory.
Is there a way to check this loop for any data that was sent to the socket earlier? Thus, if P2 sends data during a function checkVoltage(), this will not result in an error.
Thank.
EDIT:
, listenOnSocket() FireControl, P1 (, P2), FireControl , . P2 P1 , , .
, , recvfrom, , , - .
, !
EDIT2: listenOnSocket():
command listenOnSocket(int timeout, float utimeout)
{
command payload;
int sock;
socklen_t* length;
struct sockaddr_un name;
char buf[1024];
struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = utimeout;
sock = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sock < 0)
{
perror("opening datagram socket");
payload = nullPayload;
}
name.sun_family = AF_UNIX;
strcpy(name.sun_path, NAME);
unlink(name.sun_path);
if (bind(sock, (struct sockaddr *) &name, sizeof(struct sockaddr_un)))
{
perror("binding name to datagram socket\n");
payload = nullPayload;
}
if (timeout != 0 || utimeout != 0)
{
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));
}
else
{
tv.tv_sec = 0;
tv.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));
}
if (recvfrom(sock, &payload, sizeof(command), 0, (struct sockaddr *)&name, &length) < 0)
{
payload = nullPayload;
}
unlink(NAME);
return payload;
}
, :
while (1)
{
buffer = getADCValue();
checkVoltage();
temp = listenOnSocket(0, 100); /*Look for a new command*/
doStuffWithTempIfItHasChanged();
}
}