From e024ef3f0cb6104a8352915fde2a1052779f841e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Nie=C3=9Fner?= Date: Wed, 24 Apr 2019 09:21:17 +0200 Subject: [PATCH] Update Dependencies & Yojimbo hello world & Bone server --- .gitmodules | 3 - CMakeLists.txt | 59 ++++++++---- conanfile.txt | 13 +++ externals | 1 - oldsrc/Constants.h | 9 ++ oldsrc/Protocoll.h | 22 +++++ oldsrc/Server.cpp | 164 ++++++++++++++++++++++++++++++++ oldsrc/Server.h | 36 +++++++ src/Constants.h | 9 -- src/DarkRP2DDedicatedServer.cpp | 98 +++++-------------- src/GameServer.cpp | 137 ++++++++++++++++++++++++++ src/GameServer.h | 98 +++++++++++++++++++ src/Protocoll.h | 22 ----- src/Server.cpp | 164 -------------------------------- src/Server.h | 36 ------- 15 files changed, 541 insertions(+), 330 deletions(-) delete mode 100644 .gitmodules create mode 100644 conanfile.txt delete mode 160000 externals create mode 100644 oldsrc/Constants.h create mode 100644 oldsrc/Protocoll.h create mode 100644 oldsrc/Server.cpp create mode 100644 oldsrc/Server.h delete mode 100644 src/Constants.h create mode 100644 src/GameServer.cpp create mode 100644 src/GameServer.h delete mode 100644 src/Protocoll.h delete mode 100644 src/Server.cpp delete mode 100644 src/Server.h diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 1f88c3b..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "externals"] - path = externals - url = http://192.168.178.103/DarkRP/Externals diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b194a1..fd2287d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,38 +4,55 @@ set (CMAKE_CXX_STANDARD 11) include(ExternalProject) if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE RELEASE) + set(CMAKE_BUILD_TYPE Release) add_definitions(-DNDEBUG) endif(NOT CMAKE_BUILD_TYPE) + +if( CMAKE_SIZEOF_VOID_P EQUAL 8 ) + MESSAGE( "64-Bit Compiler erkannt" ) + SET( EX_PLATFORM 64 ) + SET( EX_PLATFORM_NAME "x64" ) +else( CMAKE_SIZEOF_VOID_P EQUAL 8 ) + MESSAGE( "32-Bit Compiler erkannt" ) + SET( EX_PLATFORM 32 ) + SET( EX_PLATFORM_NAME "x86" ) +endif( CMAKE_SIZEOF_VOID_P EQUAL 8 ) + + +# Download automatically, you can also just copy the conan.cmake file +if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake") + message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan") + file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/v0.13/conan.cmake" + "${CMAKE_BINARY_DIR}/conan.cmake") +endif() +include(${CMAKE_BINARY_DIR}/conan.cmake) + +# Launch Conan Install +conan_cmake_run(CONANFILE conanfile.txt + BASIC_SETUP CMAKE_TARGETS + BUILD missing) + MESSAGE( STATUS "PROJECT_SOURCE_DIR: " ${PROJECT_SOURCE_DIR} ) MESSAGE( STATUS "DarkRP2D_DEDICATED_SERVER_SOURCE_DIR: " ${DarkRP2D_DEDICATED_SERVER_SOURCE_DIR} ) -set(DarkRP2D_EXTERNAL_DIR ${DarkRP2D_DEDICATED_SERVER_SOURCE_DIR}/../external) -set(SDL2_net_DIR ${DarkRP2D_EXTERNAL_DIR}/SDL2_net-2.0.1) -set(SDL2_DIR ${DarkRP2D_EXTERNAL_DIR}/SDL2-2.0.8) - #Libs - -find_package(SDL2 REQUIRED CONFIG) -include_directories(${SDL2_INCLUDE_DIRS}) - -find_package(SDL2_net REQUIRED CONFIG) -include_directories(${SDL2_NET_INCLUDE_DIRS}) - -MESSAGE( STATUS "SDL2 Lib: " ${SDL2_LIBRARIES} ) -MESSAGE( STATUS "SDL2_NET Lib: " ${SDL2_NET_LIBRARIES} ) +include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) +conan_basic_setup() file(GLOB_RECURSE DarkRP2D_DEDICATED_SERVER_SOURCE_FILES ${DarkRP2D_DEDICATED_SERVER_SOURCE_DIR}/src/*.cpp) add_executable(DarkRP2DDServer ${DarkRP2D_DEDICATED_SERVER_SOURCE_FILES}) -target_link_libraries(DarkRP2DDServer ${SDL2_LIBRARIES} - ${SDL2_NET_LIBRARIES} ) +MESSAGE( STATUS "Conan search Dirs: " "${CONAN_LIB_DIRS}") +MESSAGE( STATUS "Conan Libs: " "${CONAN_LIBS}") +MESSAGE( STATUS "Conan Includes: " "${CONAN_INCLUDE_DIRS}") -add_custom_target(CopyBinaries - COMMAND ${CMAKE_COMMAND} -E copy ${SDL2_DLL} ${CMAKE_BINARY_DIR} - COMMAND ${CMAKE_COMMAND} -E copy ${SDL2_NET_DLL} ${CMAKE_BINARY_DIR} -) +target_link_libraries(DarkRP2DDServer ${CONAN_LIBS}) -add_dependencies(DarkRP2DDServer CopyBinaries) \ No newline at end of file +#add_custom_target(CopyBinaries +#COMMAND ${CMAKE_COMMAND} -E copy ${SDL2_DLL} ${CMAKE_BINARY_DIR} +# COMMAND ${CMAKE_COMMAND} -E copy ${SDL2_NET_DLL} ${CMAKE_BINARY_DIR} +#) + +#add_dependencies(DarkRP2DDServer CopyBinaries) \ No newline at end of file diff --git a/conanfile.txt b/conanfile.txt new file mode 100644 index 0000000..c996fed --- /dev/null +++ b/conanfile.txt @@ -0,0 +1,13 @@ +[requires] +box2d/2.3.1@conan/stable +glm/0.9.9.1@g-truc/stable +EntityX/1.1.2@gulum/stable +Yojimbo/1.2.1@gulum/stable +vscodepropertiesgen/0.1@mkovalchik/stable + + +[generators] +cmake + +[imports] +bin, *.dll -> ./bin # Copies all dll files from packages bin folder to my "./" folder \ No newline at end of file diff --git a/externals b/externals deleted file mode 160000 index eed3fae..0000000 --- a/externals +++ /dev/null @@ -1 +0,0 @@ -Subproject commit eed3fae856c51f33f34e92d6a8b759c7c1d85f3a diff --git a/oldsrc/Constants.h b/oldsrc/Constants.h new file mode 100644 index 0000000..f2df971 --- /dev/null +++ b/oldsrc/Constants.h @@ -0,0 +1,9 @@ +// #pragma once + +// namespace constants { + +// const int CLIENT_PORT = 9999; +// const int MAX_CLIENTS = 128; + +// } + diff --git a/oldsrc/Protocoll.h b/oldsrc/Protocoll.h new file mode 100644 index 0000000..2455fd6 --- /dev/null +++ b/oldsrc/Protocoll.h @@ -0,0 +1,22 @@ +// #pragma once + +// #define CONNECTION_REQUEST 0x01 +// #define CONNECTION_ACCEPTED 0x02 +// #define CONNECTION_DENIED 0x03 + +// struct ConnectionRequest { +// ConnectionRequest() : type(CONNECTION_REQUEST) {} +// char type = CONNECTION_REQUEST; +// }; + +// struct ConnectionAccepted { +// ConnectionAccepted() : type(CONNECTION_ACCEPTED) {} +// char type; +// int index; +// }; + +// struct ConnectionDenied { +// ConnectionDenied() : type(CONNECTION_DENIED) {} +// char type; +// }; + diff --git a/oldsrc/Server.cpp b/oldsrc/Server.cpp new file mode 100644 index 0000000..fbed280 --- /dev/null +++ b/oldsrc/Server.cpp @@ -0,0 +1,164 @@ +// #include "Server.h" +// #include "Protocoll.h" + +// #include +// #include +// #include + +// Server::Server(int port) : m_maxClients(constants::MAX_CLIENTS), m_clientConnected{0} +// { +// udpSocket = SDLNet_UDP_Open(port); + +// if (udpSocket == nullptr) +// { +// std::cout << "\tSDLNet_UDP_Open failed : " << SDLNet_GetError() << std::endl; +// } +// std::cout << "Server was started and is listening on port: " << port << std::endl; + +// //Creating ConnectionAccepted packet! +// // Allocate udp packet with 512 byte size +// connectionAccepted = SDLNet_AllocPacket(512); + +// if (connectionAccepted == nullptr) +// { +// std::cout << "\tSDLNet_AllocPacket failed : " << SDLNet_GetError() << std::endl; +// } +// ConnectionAccepted conAcc = ConnectionAccepted(); +// memcpy(connectionAccepted->data, &conAcc, sizeof(ConnectionAccepted)); +// connectionAccepted->len = sizeof(ConnectionAccepted); + +// connectionDenied = SDLNet_AllocPacket(512); + +// if (connectionDenied == nullptr) +// { +// std::cout << "\tSDLNet_AllocPacket failed : " << SDLNet_GetError() << std::endl; +// } +// ConnectionDenied conDen = ConnectionDenied(); +// memcpy(connectionDenied->data, &conDen, sizeof(ConnectionDenied)); +// connectionDenied->len = sizeof(ConnectionDenied); + +// //Creating receiver packet! +// receivedPacket = SDLNet_AllocPacket(512); + +// if (receivedPacket == nullptr) +// { +// std::cout << "\tSDLNet_AllocPacket failed : " << SDLNet_GetError() << std::endl; +// } +// } + +// int Server::FindFreeClientIndex() const +// { +// for (int i = 0; i < m_maxClients; ++i) +// { +// if (!m_clientConnected[i]) +// return i; +// } +// return -1; +// } + +// int Server::FindExistingClientIndex(const IPaddress &ip) const +// { +// for (int i = 0; i < m_maxClients; ++i) +// { +// if (m_clientConnected[i] && m_clients[i].host == ip.host && m_clients[i].port == ip.port) +// return i; +// } +// return -1; +// } + +// bool Server::IsClientConnected(int clientIndex) const +// { +// return m_clientConnected[clientIndex]; +// } + +// const IPaddress &Server::GetClientAddress(int clientIndex) const +// { +// return m_clients[clientIndex]; +// } + +// void Server::update(unsigned int delta) +// { +// //Receive packages +// if (SDLNet_UDP_Recv(udpSocket, receivedPacket)) +// { +// switch (*(receivedPacket->data)) +// { +// case CONNECTION_REQUEST: +// std::cout << "Connection request package received\n"; +// int freeIndex; +// if ((freeIndex = this->FindFreeClientIndex()) == -1) +// { +// std::cout << "Connection denied because server full" << std::endl; +// //Send Connection denied +// this->sendConnectionDenied(receivedPacket->address); +// break; +// } +// std::cout << "Free index found: " << freeIndex << std::endl; +// int indexOfExistingClient; +// if ((indexOfExistingClient = FindExistingClientIndex(receivedPacket->address)) == -1) +// { +// std::cout << "Client doesent exist create new" << std::endl; +// m_clients[freeIndex] = receivedPacket->address; +// m_clientConnected[freeIndex] = true; +// //Send connection accepted with index +// this->sendConnectionAccepted(receivedPacket->address, freeIndex); +// m_numConnectedClients++; +// break; +// } +// else +// { +// std::cout << "Client exist sending accepted" << std::endl; +// //Send connection accepted with index +// this->sendConnectionAccepted(receivedPacket->address, indexOfExistingClient); +// } +// break; +// default: +// break; +// } +// } + +// //Wait 10 sec +// using namespace std::chrono_literals; +// std::this_thread::sleep_for(1s); + +// //Send data +// /*for(auto &value: clients) { +// value.send(this->udpSocket,*helloWorldPacket); +// } +// std::cout << "Data was send!!" << std::endl;*/ +// } + +// bool Server::sendPacket(UDPpacket &packet) +// { +// // Send +// // SDLNet_UDP_Send returns number of packets sent. 0 means error +// if (SDLNet_UDP_Send(this->udpSocket, -1, &packet) == 0) +// { +// std::cout << "\tSDLNet_UDP_Send failed : " << SDLNet_GetError() << "\n" +// << "==========================================================================================================\n"; +// return false; +// } +// return true; +// } + +// bool Server::sendConnectionDenied(IPaddress dest) +// { +// this->connectionDenied->address.host = dest.host; +// this->connectionDenied->address.port = dest.port; +// return this->sendPacket(*connectionDenied); +// } + +// bool Server::sendConnectionAccepted(IPaddress dest, int index) +// { +// this->connectionAccepted->address.host = dest.host; +// this->connectionAccepted->address.port = dest.port; +// ((ConnectionAccepted *) connectionAccepted->data)->index = index; +// return this->sendPacket(*connectionAccepted); +// } + +// Server::~Server() +// { +// SDLNet_FreePacket(receivedPacket); +// SDLNet_FreePacket(connectionAccepted); +// SDLNet_UDP_Close(udpSocket); +// } \ No newline at end of file diff --git a/oldsrc/Server.h b/oldsrc/Server.h new file mode 100644 index 0000000..9f84adf --- /dev/null +++ b/oldsrc/Server.h @@ -0,0 +1,36 @@ +// #pragma once + +// #include +// #include "Constants.h" + +// class Server { +// public: +// Server(int port); +// ~Server(); + +// int FindFreeClientIndex() const; +// int FindExistingClientIndex( const IPaddress &ip ) const; +// bool IsClientConnected( int clientIndex ) const; +// const IPaddress & GetClientAddress( int clientIndex ) const; + +// void update(unsigned int delta); + +// void gameTick(); + +// private: +// UDPsocket udpSocket; + +// UDPpacket *connectionDenied; +// UDPpacket *connectionAccepted; +// UDPpacket *receivedPacket; + +// int m_maxClients; +// int m_numConnectedClients; +// bool m_clientConnected[constants::MAX_CLIENTS]; +// IPaddress m_clients[constants::MAX_CLIENTS]; + +// bool sendPacket(UDPpacket &packet); + +// bool sendConnectionDenied(IPaddress dest); +// bool sendConnectionAccepted(IPaddress dest, int index); +// }; \ No newline at end of file diff --git a/src/Constants.h b/src/Constants.h deleted file mode 100644 index 129704e..0000000 --- a/src/Constants.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -namespace constants { - - const int CLIENT_PORT = 9999; - const int MAX_CLIENTS = 128; - -} - diff --git a/src/DarkRP2DDedicatedServer.cpp b/src/DarkRP2DDedicatedServer.cpp index 9fa1483..c6bcc8e 100644 --- a/src/DarkRP2DDedicatedServer.cpp +++ b/src/DarkRP2DDedicatedServer.cpp @@ -1,85 +1,35 @@ -#include -#include - -#include +#include #include -#include +#include +#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; -} \ No newline at end of file +} diff --git a/src/GameServer.cpp b/src/GameServer.cpp new file mode 100644 index 0000000..4d7422b --- /dev/null +++ b/src/GameServer.cpp @@ -0,0 +1,137 @@ +#include "Gameserver.h" + +#include + +using namespace yojimbo; + +yojimbo::MessageFactory *GameAdapter::CreateMessageFactory(yojimbo::Allocator &allocator) +{ + return YOJIMBO_NEW(allocator, GameMessageFactory, allocator); +} + +void GameAdapter::OnServerClientConnected(int clientIndex) +{ + if (m_server != NULL) + { + m_server->ClientConnected(clientIndex); + } +} + +void GameAdapter::OnServerClientDisconnected(int clientIndex) +{ + if (m_server != NULL) + { + m_server->ClientDisconnected(clientIndex); + } +} + +GameServer::GameServer(const yojimbo::Address &address) : m_adapter(this), + m_server(yojimbo::GetDefaultAllocator(), + DEFAULT_PRIVATE_KEY, + address, + m_connectionConfig, + m_adapter, 0.0), + m_running(false) +{ + // start the server + m_server.Start(MAX_PLAYERS); + if (!m_server.IsRunning()) + { + throw std::runtime_error("Could not start server at port " + std::to_string(address.GetPort())); + } + + // print the port we got in case we used port 0 + char buffer[256]; + m_server.GetAddress().ToString(buffer, sizeof(buffer)); + + // ... load game ... +} + +void GameServer::ClientConnected(int clientIndex) +{ + std::cout << "client " << clientIndex << " connected" << std::endl; +} + +void GameServer::ClientDisconnected(int clientIndex) +{ + std::cout << "client " << clientIndex << " disconnected" << std::endl; +} + +void GameServer::Run() +{ + float fixedDt = 1.0f / 60.0f; + m_running = true; + while (m_running) + { + double currentTime = yojimbo_time(); + if (m_time <= currentTime) + { + Update(fixedDt); + m_time += fixedDt; + } + else + { + yojimbo_sleep(m_time - currentTime); + } + } + if (m_server.IsRunning()) + m_server.Stop(); +} + +void GameServer::Update(float dt) +{ + // stop if server is not running + if (!m_server.IsRunning()) + { + m_running = false; + return; + } + + // update server and process messages + m_server.AdvanceTime(m_time); + m_server.ReceivePackets(); + ProcessMessages(); + + // ... process client inputs ... + // ... update game ... + // ... send game state to clients ... + + m_server.SendPackets(); +} + +void GameServer::ProcessMessages() +{ + for (int i = 0; i < MAX_PLAYERS; i++) + { + if (m_server.IsClientConnected(i)) + { + for (int j = 0; j < m_connectionConfig.numChannels; j++) + { + yojimbo::Message *message = m_server.ReceiveMessage(i, j); + while (message != NULL) + { + ProcessMessage(i, message); + m_server.ReleaseMessage(i, message); + message = m_server.ReceiveMessage(i, j); + } + } + } + } +} + +void GameServer::ProcessMessage(int clientIndex, yojimbo::Message *message) +{ + switch (message->GetType()) + { + case (int)GameMessageType::TEST: + ProcessTestMessage(clientIndex, (TestMessage *)message); + break; + default: + break; + } +} + +void GameServer::ProcessTestMessage(int clientIndex, TestMessage *message) +{ + // ... process test message ... +} \ No newline at end of file diff --git a/src/GameServer.h b/src/GameServer.h new file mode 100644 index 0000000..7f5b268 --- /dev/null +++ b/src/GameServer.h @@ -0,0 +1,98 @@ +#pragma once + +#include +#include + +class GameServer; + +// a simple test message +enum class GameMessageType +{ + TEST, + COUNT +}; + +// two channels, one for each type that Yojimbo supports +enum class GameChannel +{ + RELIABLE, + UNRELIABLE, + COUNT +}; + +// the client and server config +struct GameConnectionConfig : yojimbo::ClientServerConfig +{ + GameConnectionConfig() + { + numChannels = 2; + channel[(int)GameChannel::RELIABLE].type = yojimbo::CHANNEL_TYPE_RELIABLE_ORDERED; + channel[(int)GameChannel::UNRELIABLE].type = yojimbo::CHANNEL_TYPE_UNRELIABLE_UNORDERED; + } +}; + +class TestMessage : public yojimbo::Message +{ + public: + int m_data; + + TestMessage() : m_data(0) {} + + template + bool Serialize(Stream &stream) + { + serialize_int(stream, m_data, 0, 512); + return true; + } + + YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS(); +}; + +// the adapter +class GameAdapter : public yojimbo::Adapter +{ + public: + explicit GameAdapter(GameServer *server = NULL) : m_server(server) {} + + void OnServerClientConnected(int clientIndex) override; + + void OnServerClientDisconnected(int clientIndex) override; + + yojimbo::MessageFactory* CreateMessageFactory(yojimbo::Allocator& allocator) override; + + private: + GameServer *m_server; +}; + +// the message factory +YOJIMBO_MESSAGE_FACTORY_START(GameMessageFactory, (int)GameMessageType::COUNT); +YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::TEST, TestMessage); +YOJIMBO_MESSAGE_FACTORY_FINISH(); + +static const uint8_t DEFAULT_PRIVATE_KEY[yojimbo::KeyBytes] = {0}; +static const int MAX_PLAYERS = 64; + +class GameServer +{ + public: + GameServer(const yojimbo::Address &address); + + void ClientConnected(int clientIndex); + void ClientDisconnected(int clientIndex); + + void ProcessMessages(); + void ProcessMessage(int clientIndex, yojimbo::Message *message); + void ProcessTestMessage(int clientIndex, TestMessage *message); + + void Run(); + void Update(float dt); + + bool m_running; + + private: + GameConnectionConfig m_connectionConfig; + yojimbo::Server m_server; + GameAdapter m_adapter; + + double m_time; +}; diff --git a/src/Protocoll.h b/src/Protocoll.h deleted file mode 100644 index dc4befe..0000000 --- a/src/Protocoll.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#define CONNECTION_REQUEST 0x01 -#define CONNECTION_ACCEPTED 0x02 -#define CONNECTION_DENIED 0x03 - -struct ConnectionRequest { - ConnectionRequest() : type(CONNECTION_REQUEST) {} - char type = CONNECTION_REQUEST; -}; - -struct ConnectionAccepted { - ConnectionAccepted() : type(CONNECTION_ACCEPTED) {} - char type; - int index; -}; - -struct ConnectionDenied { - ConnectionDenied() : type(CONNECTION_DENIED) {} - char type; -}; - diff --git a/src/Server.cpp b/src/Server.cpp deleted file mode 100644 index 7b29ebb..0000000 --- a/src/Server.cpp +++ /dev/null @@ -1,164 +0,0 @@ -#include "Server.h" -#include "Protocoll.h" - -#include -#include -#include - -Server::Server(int port) : m_maxClients(constants::MAX_CLIENTS), m_clientConnected{0} -{ - udpSocket = SDLNet_UDP_Open(port); - - if (udpSocket == nullptr) - { - std::cout << "\tSDLNet_UDP_Open failed : " << SDLNet_GetError() << std::endl; - } - std::cout << "Server was started and is listening on port: " << port << std::endl; - - //Creating ConnectionAccepted packet! - // Allocate udp packet with 512 byte size - connectionAccepted = SDLNet_AllocPacket(512); - - if (connectionAccepted == nullptr) - { - std::cout << "\tSDLNet_AllocPacket failed : " << SDLNet_GetError() << std::endl; - } - ConnectionAccepted conAcc = ConnectionAccepted(); - memcpy(connectionAccepted->data, &conAcc, sizeof(ConnectionAccepted)); - connectionAccepted->len = sizeof(ConnectionAccepted); - - connectionDenied = SDLNet_AllocPacket(512); - - if (connectionDenied == nullptr) - { - std::cout << "\tSDLNet_AllocPacket failed : " << SDLNet_GetError() << std::endl; - } - ConnectionDenied conDen = ConnectionDenied(); - memcpy(connectionDenied->data, &conDen, sizeof(ConnectionDenied)); - connectionDenied->len = sizeof(ConnectionDenied); - - //Creating receiver packet! - receivedPacket = SDLNet_AllocPacket(512); - - if (receivedPacket == nullptr) - { - std::cout << "\tSDLNet_AllocPacket failed : " << SDLNet_GetError() << std::endl; - } -} - -int Server::FindFreeClientIndex() const -{ - for (int i = 0; i < m_maxClients; ++i) - { - if (!m_clientConnected[i]) - return i; - } - return -1; -} - -int Server::FindExistingClientIndex(const IPaddress &ip) const -{ - for (int i = 0; i < m_maxClients; ++i) - { - if (m_clientConnected[i] && m_clients[i].host == ip.host && m_clients[i].port == ip.port) - return i; - } - return -1; -} - -bool Server::IsClientConnected(int clientIndex) const -{ - return m_clientConnected[clientIndex]; -} - -const IPaddress &Server::GetClientAddress(int clientIndex) const -{ - return m_clients[clientIndex]; -} - -void Server::update(unsigned int delta) -{ - //Receive packages - if (SDLNet_UDP_Recv(udpSocket, receivedPacket)) - { - switch (*(receivedPacket->data)) - { - case CONNECTION_REQUEST: - std::cout << "Connection request package received\n"; - int freeIndex; - if ((freeIndex = this->FindFreeClientIndex()) == -1) - { - std::cout << "Connection denied because server full" << std::endl; - //Send Connection denied - this->sendConnectionDenied(receivedPacket->address); - break; - } - std::cout << "Free index found: " << freeIndex << std::endl; - int indexOfExistingClient; - if ((indexOfExistingClient = FindExistingClientIndex(receivedPacket->address)) == -1) - { - std::cout << "Client doesent exist create new" << std::endl; - m_clients[freeIndex] = receivedPacket->address; - m_clientConnected[freeIndex] = true; - //Send connection accepted with index - this->sendConnectionAccepted(receivedPacket->address, freeIndex); - m_numConnectedClients++; - break; - } - else - { - std::cout << "Client exist sending accepted" << std::endl; - //Send connection accepted with index - this->sendConnectionAccepted(receivedPacket->address, indexOfExistingClient); - } - break; - default: - break; - } - } - - //Wait 10 sec - using namespace std::chrono_literals; - std::this_thread::sleep_for(1s); - - //Send data - /*for(auto &value: clients) { - value.send(this->udpSocket,*helloWorldPacket); - } - std::cout << "Data was send!!" << std::endl;*/ -} - -bool Server::sendPacket(UDPpacket &packet) -{ - // Send - // SDLNet_UDP_Send returns number of packets sent. 0 means error - if (SDLNet_UDP_Send(this->udpSocket, -1, &packet) == 0) - { - std::cout << "\tSDLNet_UDP_Send failed : " << SDLNet_GetError() << "\n" - << "==========================================================================================================\n"; - return false; - } - return true; -} - -bool Server::sendConnectionDenied(IPaddress dest) -{ - this->connectionDenied->address.host = dest.host; - this->connectionDenied->address.port = dest.port; - return this->sendPacket(*connectionDenied); -} - -bool Server::sendConnectionAccepted(IPaddress dest, int index) -{ - this->connectionAccepted->address.host = dest.host; - this->connectionAccepted->address.port = dest.port; - ((ConnectionAccepted *) connectionAccepted->data)->index = index; - return this->sendPacket(*connectionAccepted); -} - -Server::~Server() -{ - SDLNet_FreePacket(receivedPacket); - SDLNet_FreePacket(connectionAccepted); - SDLNet_UDP_Close(udpSocket); -} \ No newline at end of file diff --git a/src/Server.h b/src/Server.h deleted file mode 100644 index fc51500..0000000 --- a/src/Server.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include -#include "Constants.h" - -class Server { - public: - Server(int port); - ~Server(); - - int FindFreeClientIndex() const; - int FindExistingClientIndex( const IPaddress &ip ) const; - bool IsClientConnected( int clientIndex ) const; - const IPaddress & GetClientAddress( int clientIndex ) const; - - void update(unsigned int delta); - - void gameTick(); - - private: - UDPsocket udpSocket; - - UDPpacket *connectionDenied; - UDPpacket *connectionAccepted; - UDPpacket *receivedPacket; - - int m_maxClients; - int m_numConnectedClients; - bool m_clientConnected[constants::MAX_CLIENTS]; - IPaddress m_clients[constants::MAX_CLIENTS]; - - bool sendPacket(UDPpacket &packet); - - bool sendConnectionDenied(IPaddress dest); - bool sendConnectionAccepted(IPaddress dest, int index); -}; \ No newline at end of file