This repository has been archived on 2023-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
defend-together/server/main.cpp

93 lines
2.4 KiB
C++
Raw Permalink Normal View History

2020-05-01 01:16:46 +02:00
/* ./server/main.cpp */
#include <stdio.h>
#include <string>
#include <sstream>
#include <iostream>
#include <enet/enet.h>
#include "console.hpp"
#include "gameserver.hpp"
2020-05-04 10:10:19 +02:00
using namespace std;
2020-05-01 01:16:46 +02:00
int main (int argc, char ** argv)
{
2020-05-04 10:10:19 +02:00
//Intialize Enet
2020-05-01 01:16:46 +02:00
if (enet_initialize () != 0)
{
fprintf (stderr, "ENet server failed to initialize!\n");
return EXIT_FAILURE;
}
atexit (enet_deinitialize);
2020-05-04 10:10:19 +02:00
//Intialize game data
gameserver::Intialize();
2020-05-01 01:16:46 +02:00
2020-05-04 10:10:19 +02:00
//Create console
2020-05-01 01:16:46 +02:00
ServerConsole console;
//Create ENet objects
ENetEvent event;
ENetAddress address;
2020-05-04 10:10:19 +02:00
//Build Enet Host
2020-05-01 01:16:46 +02:00
address.host = ENET_HOST_ANY;
address.port = SERVER_PORT;
2020-05-02 23:25:31 +02:00
const int CHANNEL_COUNT = 2;
2020-05-01 01:16:46 +02:00
server = enet_host_create (&address, MAX_PLAYERS, CHANNEL_COUNT, 0, 0);
if (server == NULL)
{
std::cout << "Failed to create ENet server host!" << std::endl;
return 1;
}
2020-05-04 12:38:58 +02:00
//Start ticker
gameserver::StartTicker();
2020-05-04 10:10:19 +02:00
//Run main game server loop
2020-05-01 01:16:46 +02:00
std::cout << "Awaiting connections..." << std::endl;
2020-05-04 10:10:19 +02:00
while(game_is_running)
2020-05-01 01:16:46 +02:00
{
ENetEvent event;
while (enet_host_service (server, & event, EVENT_RATE) > 0)
{
switch (event.type)
{
case ENET_EVENT_TYPE_CONNECT:
std::cout << "A new client connected from " << event.peer -> address.host
<< ":" << event.peer -> address.port << std::endl;
break;
case ENET_EVENT_TYPE_RECEIVE:
2020-05-04 10:10:19 +02:00
//Channel #0 processes authentication, world updates,
// and player movement
2020-05-02 23:25:31 +02:00
if (event.channelID == 0)
{
2020-05-04 10:10:19 +02:00
gameserver::ProcessGeneralInput(&event);
2020-05-02 23:25:31 +02:00
}
2020-05-04 10:10:19 +02:00
//Channel #1 proccesses chat messages
2020-05-02 23:25:31 +02:00
else if(event.channelID == 1)
{
2020-05-04 10:10:19 +02:00
gameserver::ProcessChatMessage(&event);
2020-05-02 23:25:31 +02:00
}
2020-05-04 10:10:19 +02:00
enet_packet_destroy (event.packet);
2020-05-04 01:34:07 +02:00
2020-05-01 01:16:46 +02:00
break;
case ENET_EVENT_TYPE_DISCONNECT:
2020-05-04 10:10:19 +02:00
gameserver::HandlePlayerDisconnect(&event);
//Open peer for new connection
2020-05-01 01:16:46 +02:00
event.peer -> data = NULL;
}
}
}
// Exit process
console.stop();
enet_host_destroy(server);
return 0;
}