示例#1
0
void LoginManager::run() {
	_running = true;

	while (_running) {
		MultipleTCPSocketsListener multipleSocketsListener;

		vector<TCPSocket*> sockets;
		for (map<std::string, TCPSocket*>::iterator iter = _pendingPeers.begin(); iter != _pendingPeers.end(); iter++) {
			sockets.push_back(iter->second);
		}

		multipleSocketsListener.addSockets(sockets);

		TCPSocket* readySock = multipleSocketsListener.listenToSocket(2);
		if (readySock == NULL) {
			continue;
		}

		string username;
		string password;

		int command = MessengerServer::readCommandFromPeer(readySock);
		switch (command) {
		case LOGIN_REQUEST:
			username = MessengerServer::readDataFromPeer(readySock);
			password = MessengerServer::readDataFromPeer(readySock);
			if (login(username, password, readySock)) {
				MessengerServer::sendCommandToPeer(readySock, LOGIN_REQUEST_APPROVED);
			} else {
				if (_messengerServer->isLoggedIn(username)) { // Already logged in
					MessengerServer::sendCommandToPeer(readySock, LOGIN_REQUEST_ALREADY_LOGGED);
				} else {
					MessengerServer::sendCommandToPeer(readySock, LOGIN_REQUEST_WRONG_DETAILS);
				}
			}
			break;

		case REGISTRATION_REQUEST:
			username = MessengerServer::readDataFromPeer(readySock);
			password = MessengerServer::readDataFromPeer(readySock);
			if (signUp(username, password)) {
				MessengerServer::sendCommandToPeer(readySock, REGISTRATION_REQUEST_APPROVED);
			} else {
				MessengerServer::sendCommandToPeer(readySock, REGISTRATION_REQUEST_DENIED);
			}
			break;

		case EXIT:
			_pendingPeers.erase(readySock->destIpAndPort());
			cout << "Peer disconnected: " + readySock->destIpAndPort() << endl;
			break;
		}
	}
}
void Dispatcher::run()
{
  while(1)
  {
	  TCPSocket* speaker = multiListener->listenToSocket(10);
	  if(speaker!=NULL)
	  {
	  int cmd;
	  speaker->recv((char*)&cmd,4);
	  cmd = ntohl(cmd);
	  switch(cmd)
		  {
	      case SESSION_ESTABLISHED:
		      cout<<"connected";
	    	  break;

		  case OPEN_SESSION_WITH_PEER:
			  {string dest = ReceiveMsg(speaker);
			   ConnectRequest(speaker->destIpAndPort(),dest);}
			   break;

		  case USER_LIST:
			   sendUserList(speaker);
			   break;

		  case ROOM_LIST:
			   sendRoomList(speaker);
			   break;

		  case MEMBERS_LIST:
			  {string room = ReceiveMsg(speaker);
			   sendMembersList(speaker,room);}
			   break;

		  case CREATE_CHAT:
			   SetActiveChat(speaker->destIpAndPort());
			   break;

		  case CLOSE_CHAT:
			   CloseUserChat(speaker->destIpAndPort());
			   break;

		  case CLOSE_SESSION_WITH_PEER:
			   closePeer(speaker->destIpAndPort());
			   break;

		  default:
		  break;
		 }
	   }
   }
}
void MsaTcpServer::run()
{
	_dispatcher->start();

	while(true)
	{
		TCPSocket* newPeer = _serverSock->listenAndAccept();
		if (newPeer!= NULL)
		{
			cout<<"Add Peer"<<newPeer->destIpAndPort()<<endl;
			_dispatcher->addPeer(newPeer);
		}

	}
}
示例#4
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;
}