TCPSocket* MultipleTCPSocketsListener::listenToSocket(int timeout){

	struct timeval tv = {timeout, 0};
	tSocketsContainer::iterator iter = sockets.begin();
	tSocketsContainer::iterator endIter = sockets.end();
	fd_set fdset;
	FD_ZERO(&fdset);
	int highfd = 0;

	// Fill the set with file descriptors
	for (;iter != endIter;iter++) {
		highfd++;
		FD_SET((*iter)->getSocketFid(), &fdset);
	}

	// Perform the select
	int returned;
	if (timeout>0){
		returned = select(sizeof(fdset)*8, &fdset, NULL, NULL, &tv);
	}else{
		returned = select(sizeof(fdset)*8, &fdset, NULL, NULL, NULL);
	}
	if (returned) {
		for (int i = 0; i < highfd; i++) {
			TCPSocket* tmpSocket = sockets[i];
			if (FD_ISSET(tmpSocket->getSocketFid(), &fdset)) {
				return tmpSocket;
			}
		}
	}
	return NULL;
}
Esempio n. 2
0
void Listener::run() {
	_running = true;

	while (_running) {
		TCPSocket* sock = _listenSock->listenAndAccept();
		if (sock == NULL) {
			break;
		}

		struct timeval timeout;
		timeout.tv_sec = 720;
		timeout.tv_usec = 0;

		setsockopt(sock->getSocketFid(), SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof timeout);

		_loginManager->addPendingPeer(sock);
		cout << "New peer connected: " << sock->destIpAndPort() << endl;
	}

	cout << "Listener has stopped" << endl;
}