Пример #1
0
bool openavbEptClntService(int h, int timeout)
{
	AVB_TRACE_ENTRY(AVB_TRACE_ENDPOINT);
	bool rc = FALSE;

	if (h == AVB_ENDPOINT_HANDLE_INVALID) {
		AVB_LOG_ERROR("Client service: invalid socket");
		AVB_TRACE_EXIT(AVB_TRACE_ENDPOINT);
		return FALSE;
	}
	
	struct pollfd fds[1];
	memset(fds, 0, sizeof(struct pollfd));
	fds[0].fd = h;
	fds[0].events = POLLIN;

	AVB_LOG_VERBOSE("Waiting for event...");
	int pRet = poll(fds, 1, timeout);

	if (pRet == 0) {
		AVB_LOG_VERBOSE("Poll timeout");
		AVB_TRACE_EXIT(AVB_TRACE_ENDPOINT);
		return TRUE;
	}
	else if (pRet < 0) {
		if (errno == EINTR) {
			AVB_LOG_VERBOSE("Poll interrupted");
			AVB_TRACE_EXIT(AVB_TRACE_ENDPOINT);
			return TRUE;
		}
		else {
			AVB_LOGF_ERROR("Poll error: %s", strerror(errno));
		}
	}
	else {
		AVB_LOGF_DEBUG("Poll returned %d events", pRet);
		// only one fd, so it's readable.
		openavbEndpointMessage_t msgBuf;
		memset(&msgBuf, 0, OPENAVB_ENDPOINT_MSG_LEN);
		ssize_t nRead = read(h, &msgBuf, OPENAVB_ENDPOINT_MSG_LEN);
					
		if (nRead < OPENAVB_ENDPOINT_MSG_LEN) {
			// sock closed
			if (nRead == 0) {
				AVB_LOG_ERROR("Socket closed unexpectedly");
			}
			else if (nRead < 0) {
				AVB_LOGF_ERROR("Socket read error: %s", strerror(errno));
			}
			else {
				AVB_LOG_ERROR("Socket read to short");
			}
			socketClose(h);
		}
		else {
			// got a message
			if (openavbEptClntReceiveFromServer(h, &msgBuf)) {
				rc = TRUE;
			}
			else {
				AVB_LOG_ERROR("Invalid message received");
				socketClose(h);
			}
		}
	}
	AVB_TRACE_EXIT(AVB_TRACE_ENDPOINT);
	return rc;
}
void openavbEptSrvrService(void)
{
    AVB_TRACE_ENTRY(AVB_TRACE_ENDPOINT);
    struct sockaddr_un addrClient;
    socklen_t lenAddr;
    int i, j;
    int  csock;

    int nfds = POLL_FD_COUNT;
    int pRet;

    AVB_LOG_VERBOSE("Waiting for event...");
    pRet = poll(fds, nfds, 1000);

    if (pRet == 0) {
        AVB_LOG_VERBOSE("poll timeout");
    }
    else if (pRet < 0) {
        if (errno == EINTR) {
            AVB_LOG_VERBOSE("Poll interrupted");
        }
        else {
            AVB_LOGF_ERROR("Poll error: %s", strerror(errno));
        }
    }
    else {
        AVB_LOGF_VERBOSE("Poll returned %d events", pRet);
        for (i=0; i<nfds; i++) {
            if (fds[i].revents != 0) {
                AVB_LOGF_VERBOSE("%d sock=%d, event=0x%x, revent=0x%x", i, fds[i].fd, fds[i].events, fds[i].revents);

                if (i == AVB_ENDPOINT_LISTEN_FDS) {
                    // listen sock - indicates new connection from client
                    lenAddr = sizeof(addrClient);
                    csock = accept(lsock, (struct sockaddr*)&addrClient, &lenAddr);
                    if (csock < 0) {
                        AVB_LOGF_ERROR("Failed to accept connection: %s", strerror(errno));
                    }
                    else {
                        for (j = 0; j < POLL_FD_COUNT; j++) {
                            if (fds[j].fd == SOCK_INVALID) {
                                fds[j].fd = csock;
                                fds[j].events = POLLIN;
                                break;
                            }
                        }
                        if (j >= POLL_FD_COUNT) {
                            AVB_LOG_ERROR("Too many client connections");
                            close(csock);
                        }
                    }
                }
                else {
                    csock = fds[i].fd;
                    openavbEndpointMessage_t  msgBuf;
                    memset(&msgBuf, 0, OPENAVB_ENDPOINT_MSG_LEN);
                    ssize_t nRead = read(csock, &msgBuf, OPENAVB_ENDPOINT_MSG_LEN);
                    AVB_LOGF_VERBOSE("Socket read h=%d,fd=%d: read=%zu, expect=%zu", i, csock, nRead, OPENAVB_ENDPOINT_MSG_LEN);

                    if (nRead < OPENAVB_ENDPOINT_MSG_LEN) {
                        // sock closed
                        if (nRead == 0) {
                            AVB_LOGF_DEBUG("Socket closed, h=%d", i);
                        }
                        else if (nRead < 0) {
                            AVB_LOGF_ERROR("Socket read, h=%d: %s", i, strerror(errno));
                        }
                        else {
                            AVB_LOGF_ERROR("Short read, h=%d", i);
                        }
                        socketClose(i);
                    }
                    else {
                        // got a message
                        if (!openavbEptSrvrReceiveFromClient(i, &msgBuf)) {
                            AVB_LOG_ERROR("Failed to handle message");
                            socketClose(i);
                        }
                    }
                }
            }
        }
    }
    AVB_TRACE_EXIT(AVB_TRACE_ENDPOINT);
}