Server.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <utility>
  2. #include "network/Server.h"
  3. Server::Client::Client(ENetPeer* peer, int id) : peer(peer), id(id) {
  4. }
  5. Server::Client::~Client() {
  6. if(peer != nullptr) {
  7. enet_peer_disconnect(peer, 0);
  8. }
  9. }
  10. Server::Client::Client(Client&& other) : Client(nullptr, -1) {
  11. std::swap(peer, other.peer);
  12. std::swap(id, other.id);
  13. }
  14. Server::Client& Server::Client::operator=(Client&& other) {
  15. std::swap(peer, other.peer);
  16. std::swap(id, other.id);
  17. return *this;
  18. }
  19. int Server::Client::getId() const {
  20. return id;
  21. }
  22. Server::Server(Port port, int maxClients) : server(nullptr), idCounter(1) {
  23. ENetAddress address;
  24. address.host = ENET_HOST_ANY;
  25. address.port = port;
  26. server = enet_host_create(&address, maxClients, 2, 0, 0);
  27. if(server == nullptr) {
  28. error.clear().append("cannot crate ENet server host");
  29. }
  30. }
  31. Server::~Server() {
  32. enet_host_destroy(server);
  33. }
  34. bool Server::hasError() const {
  35. return error.getLength() > 0;
  36. }
  37. const Server::Error& Server::getError() const {
  38. return error;
  39. }