Guest User

Untitled

a guest
Feb 20th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include "Server.h"
  2.  
  3.  
  4.  
  5. Server::Server() {
  6.     port = 0;
  7.     std::cout << "Enter port: " << std::endl;
  8.     std::cin >> port;
  9.     socket.bind(port);
  10.  
  11.     id = 0;
  12.  
  13.     socket.setBlocking(false);
  14.  
  15.     listenThread = std::thread(&Server::run, this);
  16.     listenThread.join();
  17. }
  18.  
  19. void Server::run() {
  20.     sf::Packet packet;
  21.     sf::IpAddress sender;
  22.     unsigned short rport;
  23.  
  24.     std::cout << "server started!" << std::endl;
  25.     sf::Clock deltaTime;
  26.     while (true) {
  27.         packet = sf::Packet();
  28.         socket.receive(packet, sender, rport);
  29.  
  30.  
  31.         unsigned short packetType = 0;
  32.         unsigned short packetugid = 0;
  33.         unsigned short cid = 0;
  34.         packet >> packetugid >> packetType >> cid;
  35.         if (packetugid == ugid) {
  36.             for (auto &c : clients) {
  37.                 if (c.id == cid) {
  38.                     c.timeSinceLastPacket = sf::Time::Zero;
  39.                     std::cout << "Reset client tsp of " << c.id << " to 0" << std::endl;
  40.                     return;
  41.                 }
  42.             }
  43.             if (packetType == pt::handshake) {
  44.                 packet >> packetugid >> packetType;
  45.                 std::cout << sender << " joined the game!" << std::endl;
  46.                 id++;
  47.  
  48.                 Client c;
  49.                 c.ip = sender;
  50.                 c.port = rport;
  51.                 c.id = id;
  52.                
  53.                 clients.push_back(c);
  54.                 packet = sf::Packet();
  55.                 std::cout << id << std::endl;
  56.                 packet << ugid << pt::handshake << id;
  57.                 socket.send(packet, sender, rport);
  58.             }
  59.         }
  60.  
  61.         sf::Time dt = deltaTime.restart();
  62.         for (Client &c : clients) {
  63.             c.timeSinceLastPacket+= dt;
  64.             if (c.timeSinceLastPacket.asSeconds() > 10) {
  65.                 std::cout << c.id << " has timed out!" << std::endl;
  66.                 removeClient(c.id);
  67.             }
  68.         }
  69.     }
  70. }
  71.  
  72. void Server::removeClient(unsigned short value) {
  73.     /*std::list<Client>::iterator itri = clients.begin();
  74.     while (itri != clients.end()) {
  75.         if (itri->id == value) {
  76.             itri = clients.erase(itri);
  77.         } else {
  78.             std::cout << "hey" << std::endl;
  79.             ++itri;
  80.         }
  81.     }*/
  82.  
  83.     std::list<Client>::iterator i = clients.begin();
  84.     while (i != clients.end())
  85.     {
  86.         if (i->id == value)
  87.         {
  88.             clients.erase(i++);  
  89.         }
  90.         else
  91.         {
  92.             ++i;
  93.         }
  94.     }
  95. }
Add Comment
Please, Sign In to add comment