Skip to content

Instantly share code, notes, and snippets.

@ChristianHohlfeld
Created October 4, 2018 20:32
Show Gist options
  • Save ChristianHohlfeld/dea566f451c3f720a76ca7527f60fd74 to your computer and use it in GitHub Desktop.
Save ChristianHohlfeld/dea566f451c3f720a76ca7527f60fd74 to your computer and use it in GitHub Desktop.
GameServer.cpp
//
// GameServer.cpp
// UDPTEST
//
// Created by Christian Hohlfeld on 16.09.18.
//
#include "GameServer.h"
#include <iostream>
#include <string>
#include "GameMessageType.h"
#include "cocos2d.h"
#include <time.h>
static const uint8_t DEFAULT_PRIVATE_KEY[yojimbo::KeyBytes] = { 0 };
static const int MAX_PLAYERS = 2;
GameServer::GameServer(yojimbo::Allocator& allocator, const yojimbo::Address* address, yojimbo::Adapter& myAdapter, yojimbo::ClientServerConfig& clientServerConfig, double time)
{
// myAdapter.setGameServer(this);
// create Server
m_serverRef = new yojimbo::Server(allocator, DEFAULT_PRIVATE_KEY, *address, clientServerConfig, myAdapter, time);
numChannels = clientServerConfig.numChannels;
}
yojimbo::Address GameServer::GetAddress() {
return m_serverRef->GetAddress();
}
void GameServer::Start() {
// start the server
m_serverRef->Start(MAX_PLAYERS);
if (!m_serverRef->IsRunning()) {
throw std::runtime_error("Could not start server at that port ");
}
m_running = m_serverRef->IsRunning();
// print the port we got in case we used port 0
char buffer[256];
m_serverRef->GetAddress().ToString(buffer, sizeof(buffer));
std::cout << "Server address is " << buffer << std::endl;
m_time = time(nullptr);
}
int GameServer::GetNumConnectedClients() {
return m_serverRef->GetNumConnectedClients();
}
GameServer::~GameServer() {
//delete m_adapter;
delete m_serverRef;
}
void GameServer::ClientConnected(int clientIndex) {
cocos2d::log("client connected ");
std::cout << "client " << clientIndex << " connected" << std::endl;
}
void GameServer::ClientDisconnected(int clientIndex) {
cocos2d::log("client disconnected ");
std::cout << "client " << clientIndex << " disconnected" << std::endl;
}
void GameServer::ProcessMessages() {
if (!m_serverRef->IsRunning()) {
m_running = false;
return;
}
for (int i = 0; i < MAX_PLAYERS; i++) {
if (m_serverRef->IsClientConnected(i)) {
for (int j = 0; j < numChannels; j++) {
yojimbo::Message* message = m_serverRef->ReceiveMessage(i, j);
while (message != NULL) {
ProcessMessage(i, message);
m_serverRef->ReleaseMessage(i, message);
message = m_serverRef->ReceiveMessage(i, j);
}
}
}
}
}
void GameServer::ProcessMessage(int clientIndex, yojimbo::Message* message) {
switch (message->GetType()) {
case (int)GameMessageType::TEST:
ProcessMyMessage(clientIndex, (MyMessage*)message);
break;
default:
break;
}
}
void GameServer::ProcessMyMessage(int clientIndex, MyMessage* message) {
setReceivedDataFromMessage((MyMessage*)message);
}
void GameServer::SendPackets() {
m_serverRef->SendPackets();
}
void GameServer::ReceivePackets() {
m_serverRef->ReceivePackets();
}
void GameServer::AdvanceTime(double time) {
m_serverRef->AdvanceTime(time);
}
void GameServer::setReceivedDataFromMessage(MyMessage* message) {
if (!message) return;
xPosReceived = message->data.float_valueX;
yPosReceived = message->data.float_valueY;
m_receivedRotation = message->data.float_valueRotation;
}
float GameServer::getReceivedXPos() {
return xPosReceived;
}
float GameServer::getReceivedYPos() {
return yPosReceived;
}
float GameServer::getReceivedRotation() {
return m_receivedRotation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment