コード例 #1
0
ファイル: rstream.c プロジェクト: VinGorilla/gpunet
static int server_connect(void)
{
	struct pollfd fds;
	int ret = 0;

	set_options(lrs);
	do {
		if (use_async) {
			fds.fd = lrs;
			fds.events = POLLIN;

			ret = do_poll(&fds, poll_timeout);
			if (ret) {
				perror("rpoll");
				return ret;
			}
		}

		rs = rs_accept(lrs, NULL, 0);
	} while (rs < 0 && (errno == EAGAIN || errno == EWOULDBLOCK));
	if (rs < 0) {
		perror("raccept");
		return rs;
	}

	if (use_fork)
		fork_pid = fork();
	if (!fork_pid)
		set_options(rs);
	return ret;
}
コード例 #2
0
Socket *IPv4Socket::accept(bool nonblocking) {
    //fudeb("Socket::accept(s:%d)", sock_);
    LOG_DEBUG((use_rs_ ? "RDMA" : "TCP"));
    int asock = 0;
    while (!stop_) {
        if (nonblocking_) {
            wait_for_read();
        }
        asock = rs_accept(sock_, 0, 0);
        if (asock >= 0)
            break;
        LOG_WARN("accept encountered " << strerror(errno));
        if (asock < 0 && errno != EAGAIN && errno != EINTR
#ifndef _WIN32
                && errno != ECONNABORTED && errno != EPROTO
#endif
           ) {
            throw IOException("accept", "", getErrorNumber());
        }

    }
    if (stop_) {
        return 0;
    }

    uint16_t remote_port = 0;
    char str[INET_ADDRSTRLEN];

    struct sockaddr_in clientaddr;
    socklen_t addrlen = sizeof(clientaddr);
    int res = rs_getpeername(asock, (struct sockaddr *) &clientaddr, &addrlen);
    if (res < 0) {
        throw IOException("Socket::accept() Failed to find peer IP/port", "",
                          res);
    }
    char * buf = inet_ntoa(clientaddr.sin_addr);
    if (buf != 0) {
        str[0] = 0;
#pragma warning( push )
#pragma warning( disable: 4996) // we use strncpy in a safe way
        strncpy(str, buf, INET_ADDRSTRLEN);
#pragma warning( pop )
        LOG_DEBUG("IPv4 Client address is " << str);
        LOG_DEBUG("Client port is " << ntohs(clientaddr.sin_port));
    } else {
        throw IOException(
            "Socket::accept() Failure to resolve remote peer for accepted connection");
    }

    remote_port = ntohs(clientaddr.sin_port);

    Socket *asock2 = new IPv4Socket(this, asock, sock_type_, str, remote_port);
    if (nonblocking) {
        asock2->setNonBlocking();
    }
    // TODO make sendbuffersize
    // asock2->setSendbufferssize(32 * 1024);
    return asock2;
}