diff --git a/server/console.hpp b/server/console.hpp index a9e8db8..c0d7184 100644 --- a/server/console.hpp +++ b/server/console.hpp @@ -5,9 +5,7 @@ #include #include #include - - -bool console_is_running = false; +#include "gameserver.hpp" class ServerConsole { @@ -26,13 +24,13 @@ void * console_logic(void *) std::string input_string; - while(console_is_running) + while(game_is_running) { if(std::getline(std::cin, input_string)) { if(input_string == "stop") { - console_is_running = false; + game_is_running = false; } else { @@ -46,7 +44,7 @@ void * console_logic(void *) ServerConsole::ServerConsole(void) { //Intialized values - console_is_running = true; + game_is_running = true; thread = pthread_t(); @@ -64,7 +62,7 @@ void ServerConsole::stop(void) bool ServerConsole::is_running(void) { - return console_is_running; + return game_is_running; } #endif \ No newline at end of file diff --git a/server/gameserver.hpp b/server/gameserver.hpp index 4c4f273..a9abfa3 100644 --- a/server/gameserver.hpp +++ b/server/gameserver.hpp @@ -8,6 +8,9 @@ #include #include "gamemap.hpp" +using namespace std; + +bool game_is_running = false; const int SERVER_PORT = 7777; const int MAX_PLAYERS = 32; @@ -21,86 +24,155 @@ static std::string usernames[MAX_PLAYERS]; static ENetHost* server; //=================== -// Network Processes +// Game Server Logic //=================== -void PlayerMove(ENetEvent* event) +namespace gameserver { - int peer_id = event->peer -> incomingPeerID; - - int move_x = 0; - int move_y = 1; - - - std::string data_input((char*)event->packet->data); - data_input = data_input.substr(2); - - //Parse input string - std::stringstream ss(data_input); - std::string tempString; - std::getline(ss, tempString, ','); - move_x = std::stoi(tempString); - std::getline(ss, tempString, '\n'); - move_y = std::stoi(tempString); - - //Update player position - std::string resp = "2|" + gamemap.move_entity(usernames[peer_id], "player", move_x, move_y); - const char* data = resp.c_str(); - ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); - enet_host_broadcast(server, 0, packet); -} - -void DataRequest(ENetEvent* event) -{ - //Create world data dump - std::string map_dump_resp("2|"); - map_dump_resp += gamemap.get_tile_dump() + gamemap.get_entity_dump(); - const char* data = map_dump_resp.c_str(); - - //Build and send reliable packet to requester - ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); - enet_peer_send(event->peer, 0, packet); -} - -void Authenticate(ENetEvent* event) -{ - //Grab username from packet data - std::string username((char*)event->packet->data); - username = username.substr(2); - - //Determine if peer already has username - int peer_id = event -> peer -> incomingPeerID; - const char* data; - if(usernames[peer_id] == "") + void Intialize() { - //Update username array - usernames[peer_id] = username; + printf("Starting DT game server...\n"); - //Spawn entity - std::string spawn_data = gamemap.spawn_entity(usernames[peer_id], "player", 128, 129); + for(int i = 0; i < MAX_PLAYERS; i++) + { + usernames[i] = ""; + } + } - //Tell peers about new player - data = ("2|" + spawn_data).c_str(); + void PlayerMove(ENetEvent* event) + { + int peer_id = event->peer -> incomingPeerID; + + int move_x = 0; + int move_y = 0; + + //Remove id marker from packet data + std::string data_input((char*)event->packet->data); + data_input = data_input.substr(2); + + //Parse input string + std::stringstream ss(data_input); + std::string tempString; + std::getline(ss, tempString, ','); + move_x = std::stoi(tempString); + std::getline(ss, tempString, '\n'); + move_y = std::stoi(tempString); + + //Update player position + std::string resp = "2|" + gamemap.move_entity(usernames[peer_id], "player", move_x, move_y); + const char* data = resp.c_str(); ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); enet_host_broadcast(server, 0, packet); - - std::cout << usernames[peer_id] << " spawned!" << std::endl; - std::cout << peer_id << " | " << username << std::endl; - - std::string okay_response("1|OK"); - data = (okay_response + username).c_str(); - - //Return success - packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); - enet_peer_send(event -> peer, 0, packet); } - else + + void DataRequest(ENetEvent* event) { - std::string resp("1|Already logged in!"); - data = resp.c_str(); - - //Return reliable error message + //Create world data dump + std::string map_dump_resp("2|"); + map_dump_resp += gamemap.get_tile_dump() + gamemap.get_entity_dump(); + const char* data = map_dump_resp.c_str(); + + //Build and send reliable packet to requester ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); - enet_peer_send(event -> peer, 0, packet); + enet_peer_send(event->peer, 0, packet); + } + + void Authenticate(ENetEvent* event) + { + //Grab username from packet data + std::string username((char*)event->packet->data); + username = username.substr(2); + + //Determine if peer already has username + int peer_id = event -> peer -> incomingPeerID; + const char* data; + if(usernames[peer_id] == "") + { + //Update username array + usernames[peer_id] = username; + + //Spawn entity + std::string spawn_data = gamemap.spawn_entity(usernames[peer_id], "player", 128, 129); + + //Tell peers about new player + data = ("2|" + spawn_data).c_str(); + ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); + enet_host_broadcast(server, 0, packet); + + std::cout << "'" << usernames[peer_id] << "' spawned!" << std::endl; + + std::string okay_response("1|OK"); + data = (okay_response + username).c_str(); + + //Return success + packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); + enet_peer_send(event -> peer, 0, packet); + } + else + { + std::string resp("1|Already logged in!"); + data = resp.c_str(); + + //Return reliable error message + ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); + enet_peer_send(event -> peer, 0, packet); + } + } + + void ProcessChatMessage(ENetEvent* event) + { + int peer_id = event->peer -> incomingPeerID; + if(usernames[peer_id] != "") + { + //Parse input string + std::stringstream ss((char*)event->packet->data); + std::string chat_message; + std::getline(ss, chat_message, '\n'); + std::string resp = "<" + usernames[peer_id] + "> " + chat_message; + const char* data = resp.c_str(); + + std::cout << data << std::endl; + + ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); + enet_host_broadcast(server, 1, packet); + } + } + + void HandlePlayerDisconnect(ENetEvent* event) + { + //Clear username data and remove entity + std::string username = usernames[event -> peer -> incomingPeerID]; + if(username != "") + { + std::cout << "'" << username << "' disconnected" << std::endl; + gamemap.remove_entity(username,"player"); + usernames[event -> peer -> incomingPeerID] = ""; + + std::string resp = "2|delete,player:" + username; + const char* data = resp.c_str(); + ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); + enet_host_broadcast(server, 0, packet); + } + } + + void ProcessGeneralInput(ENetEvent* event) + { + char c [1]; + c[0] = event->packet->data[0]; + switch (c[0]) + { + case '1': + gameserver::Authenticate(event); + break; + case '2': + gameserver::DataRequest(event); + break; + case '3': + gameserver::PlayerMove(event); + break; + default: + std::cout << "Invalid packet recieved!" < 0) { - printf("Parsing event!\n"); switch (event.type) { case ENET_EVENT_TYPE_CONNECT: @@ -64,73 +57,24 @@ int main (int argc, char ** argv) break; case ENET_EVENT_TYPE_RECEIVE: - std::cout << "A packet of length " << event.packet -> dataLength << " containing " - << event.packet -> data << " was received from " << event.peer -> data << " on channel" - << event.channelID << std::endl; + //Channel #0 processes authentication, world updates, + // and player movement if (event.channelID == 0) { - char c [1]; - c[0] = event.packet->data[0]; - - /* Determine which function to call based on - the value of the first character of packet */ - std::cout << "Packet Type: " << c << std::endl; - switch (c[0]) - { - case '1': - Authenticate(&event); - break; - case '2': - DataRequest(&event); - break; - case '3': - PlayerMove(&event); - break; - default: - std::cout << "Invalid packet recieved!" < incomingPeerID; - if(usernames[peer_id] != "") - { - //Parse input string - std::stringstream ss((char*)event.packet->data); - std::string chat_message; - std::getline(ss, chat_message, '\n'); - std::string resp = "<" + usernames[peer_id] + "> " + chat_message; - const char* data = resp.c_str(); - - std::cout << data << std::endl; - - ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); - enet_host_broadcast(server, 1, packet); - } - enet_packet_destroy (event.packet); + gameserver::ProcessChatMessage(&event); + } - + enet_packet_destroy (event.packet); break; case ENET_EVENT_TYPE_DISCONNECT: - std::cout << event.peer -> data << " disconnected." << std::endl; - - //Clear username data and remove entity - std::string username = usernames[event.peer -> incomingPeerID]; - if(username != "") - { - std::cout << "Removing '" << username << "'s player data!" << std::endl; - gamemap.remove_entity(username,"player"); - usernames[event.peer -> incomingPeerID] = ""; - - std::string resp = "2|delete,player:" + username; - const char* data = resp.c_str(); - ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); - enet_host_broadcast(server, 0, packet); - } + gameserver::HandlePlayerDisconnect(&event); //Open peer for new connection event.peer -> data = NULL;