Update Dependencies & Yojimbo hello world & Bone server

This commit is contained in:
Julian Nießner
2019-04-24 09:21:17 +02:00
parent fc2e3e801c
commit e024ef3f0c
15 changed files with 541 additions and 330 deletions

View File

@@ -1,85 +1,35 @@
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <signal.h>
#include <iostream>
#include <SDL_net.h>
#include <yojimbo.h>
#include "GameServer.h"
using namespace yojimbo;
static GameServer *serv;
void interrupt_handler( int /*dummy*/ )
{
serv->m_running = false;
}
int main(int argc, char *argv[])
{
TCPsocket sd, csd; /* Socket descriptor, Client socket descriptor */
IPaddress ip, *remoteIP;
int quit, quit2;
char buffer[512];
std::cout << "Starting DarkRP2D Server..." << std::endl;
if (SDLNet_Init() < 0)
if ( !InitializeYojimbo() )
{
fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError());
exit(EXIT_FAILURE);
printf( "error: failed to initialize Yojimbo!\n" );
return 1;
}
yojimbo_log_level( YOJIMBO_LOG_LEVEL_INFO );
/* Resolving the host using NULL make network interface to listen */
if (SDLNet_ResolveHost(&ip, NULL, 9999) < 0)
{
fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError());
exit(EXIT_FAILURE);
}
std::cout << "Yojimbo lives!" << std::endl;
signal( SIGINT, interrupt_handler );
/* Open a connection with the IP provided (listen on the host's port) */
if (!(sd = SDLNet_TCP_Open(&ip)))
{
fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError());
exit(EXIT_FAILURE);
}
std::cout << "DarkRP2D Server started." << std::endl;
std::cout << "Waiting for a Connection!" << std::endl;
/* Wait for a connection, send data and term */ quit = 0;
while (!quit)
{ /* This check the sd if there is a pending connection. * If there is one, accept that, and open a new socket for communicating */
if ((csd = SDLNet_TCP_Accept(sd)))
{ /* Now we can communicate with the client using csd socket * sd will remain opened waiting other connections */
/* Get the remote address */
if ((remoteIP = SDLNet_TCP_GetPeerAddress(csd))) /* Print the address, converting in the host format */
printf("Host connected: %x %d\n", SDLNet_Read32(&remoteIP->host), SDLNet_Read16(&remoteIP->port));
else
fprintf(stderr, "SDLNet_TCP_GetPeerAddress: %s\n", SDLNet_GetError());
quit2 = 0;
while (!quit2)
{
if (SDLNet_TCP_Recv(csd, buffer, 512) > 0)
{
printf("Client say: %s\n", buffer);
if (strcmp(buffer, "exit") == 0) /* Terminate this connection */
{
quit2 = 1;
printf("Terminate connection\n");
}
if (strcmp(buffer, "quit") == 0) /* Quit the program */
{
quit2 = 1;
quit = 1;
printf("Quit program\n");
}
}
}
/* Close the client socket */ SDLNet_TCP_Close(csd);
}
}
std::cout << "Server is closing..." << std::endl;
SDLNet_TCP_Close(sd);
SDLNet_Quit();
std::cout << "Server closed!" << std::endl;
yojimbo::Address addr(127,0,0,1,9999);
GameServer server(addr);
serv = &server; //For interrupt
server.Run();
ShutdownYojimbo();
return EXIT_SUCCESS;
return 0;
}
}