Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 17th, 2010  |  syntax: None  |  size: 1.29 KB  |  hits: 102  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /* This member function is looped until program termination */
  2. void pdmudServer::ConnectionMonitor()
  3. {
  4.         int nbytes, result;
  5.  
  6.         /* rd, wr, and ex are private members of type fd_set in pdmudServer */
  7.         /* MAX_PLAYERS is a const int in the pdmud namespace; set to 20 for now */
  8.         /* player is a member of class pdmudServer of type vector<pdPlayer> */
  9.  
  10.         result = select(MAX_PLAYERS, &rd, &wr, &ex, NULL);
  11.  
  12.         if (result < 0)
  13.                 throw(pdmudServerError());
  14.  
  15.         if (FD_ISSET(server_fd, &rd)) /* Check for activity on server socket */
  16.         {
  17.                 std::cout << "Player connecting.\n";
  18.                 pdPlayer newplayer;
  19.                 player.push_back(newplayer);
  20.                 (player.back()).ConnectPlayer(server_fd, &rd, &wr, &ex); /* Player accept */
  21.         }
  22.  
  23.         for (int i = 0; i < player.size(); i++)
  24.         {
  25.                 int fd = (player.at(i)).GetFD();
  26.  
  27.                 if (FD_ISSET(fd, &rd)) /* Check for this player activity */
  28.                 {
  29.                         char buf[256] =
  30.                         { "\0" };
  31.                         nbytes = recv(fd, buf, sizeof(char) * 256, 0);
  32.                         if (!nbytes)
  33.                         {
  34.                                 /* No data read; player must be disconnecting */
  35.                                 close(fd);
  36.                                 FD_CLR(fd, &rd);
  37.                                 std::cout << "Player disconnecting.\n";
  38.                         }
  39.                         else
  40.                         {
  41.                                 /* data read from player; print it */
  42.                                 buf[nbytes] = '\0';
  43.                                 std::cout << "Player input: " << buf << "\n";
  44.                         }
  45.                 }
  46.  
  47.         } /* End for */
  48.  
  49. } /* End ConnectionMonitor */