Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Server.h"
- Server::Server() {
- port = 0;
- std::cout << "Enter port: " << std::endl;
- std::cin >> port;
- socket.bind(port);
- id = 0;
- socket.setBlocking(false);
- listenThread = std::thread(&Server::run, this);
- listenThread.join();
- }
- void Server::run() {
- sf::Packet packet;
- sf::IpAddress sender;
- unsigned short rport;
- std::cout << "server started!" << std::endl;
- sf::Clock deltaTime;
- while (true) {
- packet = sf::Packet();
- socket.receive(packet, sender, rport);
- unsigned short packetType = 0;
- unsigned short packetugid = 0;
- unsigned short cid = 0;
- packet >> packetugid >> packetType >> cid;
- if (packetugid == ugid) {
- for (auto c : clients) {
- if (c.id == cid) {
- //c.timeSinceLastPacket = 0;
- std::cout << "Reset client tsp of " << c.id << " to 0" << std::endl;
- return;
- }
- }
- if (packetType == pt::handshake) {
- packet >> packetugid >> packetType;
- std::cout << sender << " joined the game!" << std::endl;
- id++;
- Client c;
- c.ip = sender;
- c.port = rport;
- //c.timeSinceLastPacket = 0;
- c.id = id;
- clients.push_back(c);
- packet = sf::Packet();
- packet << ugid << pt::handshake << id << "The server greets you!";
- socket.send(packet, sender, rport);
- }
- }
- for (Client c : clients) {
- c.timeSinceLastPacket++;
- std::cout << c.timeSinceLastPacket << std::endl;
- }
- }
- }
- void Server::removeClient(std::vector<Client> & clientvector, unsigned short id) {
- clientvector.erase(
- std::remove_if(clientvector.begin(), clientvector.end(), [&](Client const & client) {
- return client.id == id;
- }),
- clientvector.end());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement