123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- #include "GameServer.h"
- #include "../network/server/Server.h"
- GameServer::GameServer()
- {
- }
- GameServer::~GameServer()
- {
- }
- void GameServer::start(unsigned short port, unsigned short maxClients)
- {
- std::cout << port << std::endl;
- Server server(port, maxClients);
- server.start(this);
-
- while(true)
- {
- string in;
- cin >> in;
- if(onServerCommand(in))
- {
- break;
- }
- }
-
- server.stop();
- }
- bool GameServer::onServerCommand(string& command)
- {
- return command == "q";
- }
- void GameServer::onFullServerClientConnect(int socket)
- {
- string s = "sorry, the server is full";
- Stream answer;
- answer.write(s.data(), s.length());
- answer.sendToSocket(socket);
- }
- void GameServer::onClientConnect(int socket)
- {
- cout << socket << " has connected" << endl;
-
- string s = "Welcome to the server.";
- Stream answer;
- answer.write(s.data(), s.length());
- answer.sendToSocket(socket);
- }
- void GameServer::onClientPackage(int socket, Stream& in)
- {
- string s = "";
- while(in.hasData())
- {
- s = in.readChar() + s;
- }
- Stream answer;
- answer.write(s.data(), s.length());
- answer.sendToSocket(socket);
- }
- void GameServer::onClientDisconnect(int socket)
- {
- cout << socket << " has disconnected" << endl;
-
- string s = "Bye.";
- Stream answer;
- answer.write(s.data(), s.length());
- answer.sendToSocket(socket);
- }
|