Created
September 17, 2018 17:29
-
-
Save ChristianHohlfeld/5fac4b7362054d94257896bea6fad9ce to your computer and use it in GitHub Desktop.
yojimbo integration with cocos2d-x win32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GameAdapter.cpp | |
// UDPTEST | |
// | |
// Created by Christian Hohlfeld on 16.09.18. | |
// | |
#include <stdio.h> | |
#include "GameAdapter.h" | |
#include "GameServer.h" | |
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GameAdapter.hpp | |
// UDPTEST | |
// | |
// Created by Christian Hohlfeld on 16.09.18. | |
// | |
#ifndef GameAdapter_h | |
#define GameAdapter_h | |
#include <stdio.h> | |
#include "yojimbo/yojimbo.h" | |
#include "GameMessageFactory.h" | |
class GameServer; | |
class GameAdapter : public yojimbo::Adapter { | |
public: | |
explicit GameAdapter(GameServer* server = NULL) : | |
m_server(server) {} | |
yojimbo::MessageFactory* CreateMessageFactory(yojimbo::Allocator& allocator) override { | |
return YOJIMBO_NEW(allocator, GameMessageFactory, allocator); | |
} | |
void OnServerClientConnected(int clientIndex) override; | |
void OnServerClientDisconnected(int clientIndex) override; | |
private: | |
GameServer* m_server; | |
}; | |
#endif /* GameAdapter_hpp */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GameChannel.h | |
// UDPTEST | |
// | |
// Created by Christian Hohlfeld on 16.09.18. | |
// | |
#ifndef GameChannel_h | |
#define GameChannel_h | |
// two channels, one for each type that Yojimbo supports | |
enum class GameChannel { | |
RELIABLE, | |
UNRELIABLE, | |
COUNT | |
}; | |
#endif /* GameChannel_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GameConnectionConfig.h | |
// UDPTEST | |
// | |
// Created by Christian Hohlfeld on 16.09.18. | |
// | |
#ifndef GameConnectionConfig_h | |
#define GameConnectionConfig_h | |
#include "GameChannel.h" | |
// the client and server config | |
struct GameConnectionConfig : public 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; | |
} | |
}; | |
#endif /* GameConnectionConfig_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GameMessageFactory.h | |
// UDPTEST | |
// | |
// Created by Christian Hohlfeld on 16.09.18. | |
// | |
#ifndef GameMessageFactory_h | |
#define GameMessageFactory_h | |
#include "GameMessageType.h" | |
#include "MyMessage.h" | |
YOJIMBO_MESSAGE_FACTORY_START(GameMessageFactory, (int)GameMessageType::COUNT); | |
YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::TEST, MyMessage); | |
YOJIMBO_MESSAGE_FACTORY_FINISH(); | |
#endif /* GameMessageFactory_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GameMessageType.h | |
// UDPTEST | |
// | |
// Created by Christian Hohlfeld on 16.09.18. | |
// | |
#ifndef GameMessageType_h | |
#define GameMessageType_h | |
enum class GameMessageType { | |
TEST, | |
COUNT | |
}; | |
#endif /* GameMessageType_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GameServer.cpp | |
// UDPTEST | |
// | |
// Created by Christian Hohlfeld on 16.09.18. | |
// | |
#include "GameServer.h" | |
#include <iostream> | |
#include <string> | |
static const uint8_t DEFAULT_PRIVATE_KEY[yojimbo::KeyBytes] = { 0 }; | |
static const int MAX_PLAYERS = 8; | |
GameServer::GameServer(const yojimbo::Address& address) : | |
m_adapter(this), | |
m_server(yojimbo::GetDefaultAllocator(), DEFAULT_PRIVATE_KEY, address, m_connectionConfig, m_adapter, 0.0) | |
{ | |
// 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)); | |
std::cout << "Server address is " << buffer << std::endl; | |
// ... 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GameServer.h | |
// UDPTEST | |
// | |
// Created by Christian Hohlfeld on 16.09.18. | |
// | |
#ifndef GameServer_hpp | |
#define GameServer_hpp | |
#include <stdio.h> | |
#include "yojimbo/yojimbo.h" | |
#include "GameAdapter.h" | |
#include "GameConnectionConfig.h" | |
class GameServer | |
{ | |
public: | |
GameServer(const yojimbo::Address& address); | |
void ClientConnected(int clientIndex); | |
void ClientDisconnected(int clientIndex); | |
private: | |
GameAdapter m_adapter; | |
yojimbo::Server m_server; | |
GameConnectionConfig m_connectionConfig; | |
}; | |
#endif /* GameServer_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TestMessage.hpp | |
// UDPTEST | |
// | |
// Created by Christian Hohlfeld on 16.09.18. | |
// | |
#ifndef TestMessage_hpp | |
#define TestMessage_hpp | |
#include <stdio.h> | |
#include "yojimbo/yojimbo.h" | |
class MyMessage : public yojimbo::Message { | |
public: | |
int m_data; | |
MyMessage() : | |
m_data(0) {} | |
template <typename Stream> | |
bool Serialize(Stream& stream) { | |
serialize_int(stream, m_data, 0, 512); | |
return true; | |
} | |
YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS(); | |
}; | |
#endif /* TestMessage_hpp */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment