Branch 'dev-char' is stable, merging into 'dev'

This commit is contained in:
Kayne Ruse
2013-06-24 10:43:39 +10:00
17 changed files with 363 additions and 136 deletions
+1 -1
View File
@@ -89,7 +89,7 @@ void ClientApplication::Init() {
flags); flags);
//initiate the remaining singletons //initiate the remaining singletons
netUtil->Open(0, sizeof(Packet)); netUtil->Open(0, sizeof(Packet::Packet));
} }
void ClientApplication::Proc() { void ClientApplication::Proc() {
+1 -1
View File
@@ -25,7 +25,7 @@
#include "scene_list.hpp" #include "scene_list.hpp"
#include "base_scene.hpp" #include "base_scene.hpp"
#include "singleton.hpp" #include "singleton.hpp"
#include "packet_type.hpp" #include "packet.hpp"
#include "information_manager.hpp" #include "information_manager.hpp"
#include "config_utility.hpp" #include "config_utility.hpp"
+42 -12
View File
@@ -35,6 +35,7 @@ InWorld::InWorld() {
#endif #endif
cout << "Client Index: " << infoMgr->GetClientIndex() << endl; cout << "Client Index: " << infoMgr->GetClientIndex() << endl;
font.SetSurface(surfaceMgr->Get("font")); font.SetSurface(surfaceMgr->Get("font"));
pc.GetSprite()->SetSurface(surfaceMgr->Get("elliot"), 32, 48);
} }
InWorld::~InWorld() { InWorld::~InWorld() {
@@ -53,6 +54,7 @@ void InWorld::FrameStart() {
void InWorld::Update(double delta) { void InWorld::Update(double delta) {
while(HandlePacket(popNetworkPacket())); while(HandlePacket(popNetworkPacket()));
pc.Update(delta);
} }
void InWorld::FrameEnd() { void InWorld::FrameEnd() {
@@ -62,6 +64,8 @@ void InWorld::FrameEnd() {
void InWorld::Render(SDL_Surface* const screen) { void InWorld::Render(SDL_Surface* const screen) {
ClockFrameRate(); ClockFrameRate();
pc.DrawTo(screen);
//since we're using this twice, make a tmp var //since we're using this twice, make a tmp var
string fps = itos(GetFrameRate()); string fps = itos(GetFrameRate());
font.DrawStringTo(fps, screen, screen->w - fps.size() * font.GetCharW(), 0); font.DrawStringTo(fps, screen, screen->w - fps.size() * font.GetCharW(), 0);
@@ -94,29 +98,55 @@ void InWorld::KeyDown(SDL_KeyboardEvent const& key) {
case SDLK_ESCAPE: case SDLK_ESCAPE:
ExitGame(); ExitGame();
break; break;
case SDLK_w:
pc.MoveDirection(CardinalDirection::NORTH);
break;
case SDLK_s:
pc.MoveDirection(CardinalDirection::SOUTH);
break;
case SDLK_a:
pc.MoveDirection(CardinalDirection::EAST);
break;
case SDLK_d:
pc.MoveDirection(CardinalDirection::WEST);
break;
} }
} }
void InWorld::KeyUp(SDL_KeyboardEvent const& key) { void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
// //reversed
switch(key.keysym.sym) {
case SDLK_w:
pc.MoveDirection(CardinalDirection::SOUTH);
break;
case SDLK_s:
pc.MoveDirection(CardinalDirection::NORTH);
break;
case SDLK_a:
pc.MoveDirection(CardinalDirection::WEST);
break;
case SDLK_d:
pc.MoveDirection(CardinalDirection::EAST);
break;
}
} }
//------------------------- //-------------------------
//Utilities //Utilities
//------------------------- //-------------------------
int InWorld::HandlePacket(Packet p) { int InWorld::HandlePacket(Packet::Packet p) {
switch(p.meta.type) { switch(p.meta.type) {
case PacketType::NONE: case Packet::Type::NONE:
//DO NOTHING //DO NOTHING
return 0; return 0;
break; break;
case PacketType::PING: case Packet::Type::PING:
//quick pong //quick pong
p.meta.type = PacketType::PONG; p.meta.type = Packet::Type::PONG;
netUtil->Send(&p.meta.address, &p, sizeof(Packet)); netUtil->Send(&p.meta.address, &p, sizeof(Packet::Packet));
break; break;
case PacketType::PONG: case Packet::Type::PONG:
// //
break; break;
// case PacketType::BROADCAST_REQUEST: // case PacketType::BROADCAST_REQUEST:
@@ -131,7 +161,7 @@ int InWorld::HandlePacket(Packet p) {
// case PacketType::JOIN_RESPONSE: // case PacketType::JOIN_RESPONSE:
// // // //
// break; // break;
case PacketType::DISCONNECT: case Packet::Type::DISCONNECT:
HandleDisconnection(p.disconnect); HandleDisconnection(p.disconnect);
break; break;
// case PacketType::SYNCHRONIZE: // case PacketType::SYNCHRONIZE:
@@ -154,10 +184,10 @@ int InWorld::HandlePacket(Packet p) {
void InWorld::Disconnect() { void InWorld::Disconnect() {
//disconnect //disconnect
Packet p; Packet::Packet p;
p.meta.type = PacketType::DISCONNECT; p.meta.type = Packet::Type::DISCONNECT;
p.disconnect.clientIndex = infoMgr->GetClientIndex(); p.disconnect.clientIndex = infoMgr->GetClientIndex();
netUtil->Send(GAME_CHANNEL, reinterpret_cast<void*>(&p), sizeof(Packet)); netUtil->Send(GAME_CHANNEL, reinterpret_cast<void*>(&p), sizeof(Packet::Packet));
netUtil->Unbind(GAME_CHANNEL); netUtil->Unbind(GAME_CHANNEL);
endQueueThread(); endQueueThread();
@@ -171,7 +201,7 @@ void InWorld::ExitGame() {
cout << "The game session has ended" << endl; cout << "The game session has ended" << endl;
} }
void InWorld::HandleDisconnection(::Disconnect& disconnect) { void InWorld::HandleDisconnection(Packet::Disconnect& disconnect) {
Disconnect(); Disconnect();
SetNextScene(SceneList::MAINMENU); SetNextScene(SceneList::MAINMENU);
cout << "You have been disconnected" << endl; cout << "You have been disconnected" << endl;
+7 -3
View File
@@ -26,7 +26,7 @@
#include "utilities.hpp" #include "utilities.hpp"
#include "defines.hpp" #include "defines.hpp"
#include "singleton.hpp" #include "singleton.hpp"
#include "packet_type.hpp" #include "packet.hpp"
#include "network_queue.hpp" #include "network_queue.hpp"
#include "information_manager.hpp" #include "information_manager.hpp"
@@ -37,6 +37,9 @@
#include "raster_font.hpp" #include "raster_font.hpp"
#include "frame_rate.hpp" #include "frame_rate.hpp"
//debugging
#include "player_character.hpp"
class InWorld : public BaseScene { class InWorld : public BaseScene {
public: public:
//Public access members //Public access members
@@ -59,10 +62,10 @@ protected:
void KeyUp(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&);
//Utilities //Utilities
int HandlePacket(Packet p); int HandlePacket(Packet::Packet p);
void Disconnect(); void Disconnect();
void ExitGame(); void ExitGame();
void HandleDisconnection(::Disconnect&); void HandleDisconnection(Packet::Disconnect&);
//services //services
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get(); ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
@@ -72,6 +75,7 @@ protected:
//members //members
RasterFont font; RasterFont font;
PlayerCharacter pc;
}; };
#endif #endif
+16 -16
View File
@@ -155,30 +155,30 @@ void Lobby::KeyUp(SDL_KeyboardEvent const& key) {
//Utilities //Utilities
//------------------------- //-------------------------
int Lobby::HandlePacket(Packet p) { int Lobby::HandlePacket(Packet::Packet p) {
switch(p.meta.type) { switch(p.meta.type) {
case PacketType::NONE: case Packet::Type::NONE:
//DO NOTHING //DO NOTHING
return 0; return 0;
break; break;
case PacketType::PING: case Packet::Type::PING:
//quick pong //quick pong
p.meta.type = PacketType::PONG; p.meta.type = Packet::Type::PONG;
netUtil->Send(&p.meta.address, &p, sizeof(Packet)); netUtil->Send(&p.meta.address, &p, sizeof(Packet::Packet));
break; break;
case PacketType::PONG: case Packet::Type::PONG:
// //
break; break;
// case PacketType::BROADCAST_REQUEST: // case PacketType::BROADCAST_REQUEST:
// // // //
// break; // break;
case PacketType::BROADCAST_RESPONSE: case Packet::Type::BROADCAST_RESPONSE:
PushServer(p.broadcastResponse); PushServer(p.broadcastResponse);
break; break;
// case PacketType::JOIN_REQUEST: // case PacketType::JOIN_REQUEST:
// // // //
// break; // break;
case PacketType::JOIN_RESPONSE: case Packet::Type::JOIN_RESPONSE:
BeginGame(p.joinResponse); BeginGame(p.joinResponse);
break; break;
// case PacketType::DISCONNECT: // case PacketType::DISCONNECT:
@@ -203,13 +203,13 @@ int Lobby::HandlePacket(Packet p) {
} }
void Lobby::BroadcastNetwork() { void Lobby::BroadcastNetwork() {
Packet p; Packet::Packet p;
p.meta.type = PacketType::BROADCAST_REQUEST; p.meta.type = Packet::Type::BROADCAST_REQUEST;
netUtil->Send("255.255.255.255", configUtil->Int("server.port"), &p, sizeof(Packet)); netUtil->Send("255.255.255.255", configUtil->Int("server.port"), &p, sizeof(Packet::Packet));
serverList.clear(); serverList.clear();
} }
void Lobby::PushServer(BroadcastResponse& bcast) { void Lobby::PushServer(Packet::BroadcastResponse& bcast) {
ServerEntry entry; ServerEntry entry;
entry.name = bcast.name; entry.name = bcast.name;
entry.address = bcast.meta.address; entry.address = bcast.meta.address;
@@ -221,12 +221,12 @@ void Lobby::ConnectToServer(ServerEntry* server) {
if (!server) { if (!server) {
throw(runtime_error("No server received")); throw(runtime_error("No server received"));
} }
Packet p; Packet::Packet p;
p.meta.type = PacketType::JOIN_REQUEST; p.meta.type = Packet::Type::JOIN_REQUEST;
netUtil->Send(&server->address, reinterpret_cast<void*>(&p), sizeof(Packet)); netUtil->Send(&server->address, reinterpret_cast<void*>(&p), sizeof(Packet::Packet));
} }
void Lobby::BeginGame(JoinResponse& response) { void Lobby::BeginGame(Packet::JoinResponse& response) {
//should be downloading the resources here as well //should be downloading the resources here as well
infoMgr->SetClientIndex(response.clientIndex); infoMgr->SetClientIndex(response.clientIndex);
netUtil->Bind(&response.meta.address, GAME_CHANNEL); netUtil->Bind(&response.meta.address, GAME_CHANNEL);
+7 -9
View File
@@ -23,10 +23,13 @@
#define LOBBY_HPP_ #define LOBBY_HPP_
#include "base_scene.hpp" #include "base_scene.hpp"
#include "utilities.hpp" #include "utilities.hpp"
#include "defines.hpp" #include "defines.hpp"
#include "singleton.hpp" #include "singleton.hpp"
#include "packet_type.hpp"
#include "server_entry.hpp"
#include "packet.hpp"
#include "network_queue.hpp" #include "network_queue.hpp"
#include "information_manager.hpp" #include "information_manager.hpp"
@@ -39,11 +42,6 @@
#include <vector> #include <vector>
#include <string> #include <string>
struct ServerEntry {
std::string name;
IPaddress address;
};
class Lobby : public BaseScene { class Lobby : public BaseScene {
public: public:
//Public access members //Public access members
@@ -65,11 +63,11 @@ protected:
void KeyUp(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&);
//utilities //utilities
int HandlePacket(Packet p); int HandlePacket(Packet::Packet p);
void BroadcastNetwork(); void BroadcastNetwork();
void PushServer(BroadcastResponse&); void PushServer(Packet::BroadcastResponse&);
void ConnectToServer(ServerEntry*); void ConnectToServer(ServerEntry*);
void BeginGame(JoinResponse&); void BeginGame(Packet::JoinResponse&);
//services //services
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get(); ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
+108
View File
@@ -0,0 +1,108 @@
/* Copyright: (c) Kayne Ruse 2013
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "player_character.hpp"
void PlayerCharacter::Update(double delta) {
if (limitSpeed) {
constexpr double d = 1.0/sqrt(2);
position += motion * delta * d;
}
else {
position += motion * delta;
}
sprite.Update(delta);
}
void PlayerCharacter::MoveDirection(CardinalDirection cd) {
//shift the movement in this direction
switch(cd) {
case CardinalDirection::NORTH:
if (motion.y >= 0) {
motion.y -= WALKING_SPEED;
}
break;
case CardinalDirection::SOUTH:
if (motion.y <= 0) {
motion.y += WALKING_SPEED;
}
break;
case CardinalDirection::EAST:
if (motion.x >= 0) {
motion.x -= WALKING_SPEED;
}
break;
case CardinalDirection::WEST:
if (motion.x <= 0) {
motion.x += WALKING_SPEED;
}
break;
}
//short cut
if (motion.x != 0 && motion.y != 0) {
sprite.SetInterval(0.1);
limitSpeed = true;
}
else if (motion.x != 0 || motion.y != 0) {
sprite.SetInterval(0.1);
limitSpeed = false;
}
else {
sprite.SetInterval(0);
sprite.SetCurrentFrame(0);
limitSpeed = false;
}
//face the correct direction
FaceDirection();
}
void PlayerCharacter::FaceDirection(CardinalDirection cd) {
switch(cd) {
case CardinalDirection::NORTH:
sprite.SetCurrentStrip(1);
break;
case CardinalDirection::SOUTH:
sprite.SetCurrentStrip(0);
break;
case CardinalDirection::EAST:
sprite.SetCurrentStrip(2);
break;
case CardinalDirection::WEST:
sprite.SetCurrentStrip(3);
break;
}
}
void PlayerCharacter::FaceDirection() {
//base the direction on the character's movement
if (motion.y > 0) {
FaceDirection(CardinalDirection::SOUTH);
}
else if (motion.y < 0) {
FaceDirection(CardinalDirection::NORTH);
}
else if (motion.x < 0) {
FaceDirection(CardinalDirection::EAST);
}
else if (motion.x > 0) {
FaceDirection(CardinalDirection::WEST);
}
}
+62
View File
@@ -0,0 +1,62 @@
/* Copyright: (c) Kayne Ruse 2013
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef PLAYERCHARACTER_HPP_
#define PLAYERCHARACTER_HPP_
#include "vector2.hpp"
#include "sprite_sheet.hpp"
#include "defines.hpp"
class PlayerCharacter {
public:
PlayerCharacter() = default;
~PlayerCharacter() = default;
void Update(double delta);
void MoveDirection(CardinalDirection);
void FaceDirection(CardinalDirection);
void DrawTo(SDL_Surface* const dest) { sprite.DrawTo(dest, position.x, position.y); }
//accessors and mutators
Vector2 SetPosition(Vector2 v) { return position = v; }
Vector2 ShiftPosition(Vector2 v) { return position += v; }
Vector2 GetPosition() { return position; }
Vector2 SetMotion(Vector2 v) { return motion = v; }
Vector2 ShiftMotion(Vector2 v) { return motion += v; }
Vector2 GetMotion() { return motion; }
SpriteSheet* GetSprite() { return &sprite; }
private:
void FaceDirection();
Vector2 position;
Vector2 motion;
SpriteSheet sprite;
//for moving diagonal
bool limitSpeed = false;
};
#endif
@@ -19,14 +19,13 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#ifndef CHARACTER_HPP_ #ifndef CLIENTENTRY_HPP_
#define CHARACTER_HPP_ #define CLIENTENTRY_HPP_
class Character { struct ClientEntry {
public: int index;
Character(); int channel;
~Character(); int playerIndex;
private:
}; };
#endif #endif
+6
View File
@@ -25,4 +25,10 @@
#define GAME_CHANNEL 0 #define GAME_CHANNEL 0
#define CHAT_CHANNEL 1 #define CHAT_CHANNEL 1
#define WALKING_SPEED 140
enum class CardinalDirection {
NORTH, SOUTH, EAST, WEST
};
#endif #endif
+8 -8
View File
@@ -32,7 +32,7 @@
static SDL_sem* lock = SDL_CreateSemaphore(1); static SDL_sem* lock = SDL_CreateSemaphore(1);
static SDL_Thread* queueThread = nullptr; static SDL_Thread* queueThread = nullptr;
static std::deque<Packet> queue; static std::deque<Packet::Packet> queue;
static bool running = false; static bool running = false;
@@ -41,8 +41,8 @@ static int networkQueue(void*) {
while(running) { while(running) {
SDL_SemWait(lock); SDL_SemWait(lock);
while(netUtil->Receive()) { while(netUtil->Receive()) {
Packet p; Packet::Packet p;
memcpy(&p, netUtil->GetInData(), sizeof(Packet)); memcpy(&p, netUtil->GetInData(), sizeof(Packet::Packet));
p.meta.address = netUtil->GetInPacket()->address; p.meta.address = netUtil->GetInPacket()->address;
queue.push_back(p); queue.push_back(p);
} }
@@ -80,19 +80,19 @@ void killQueueThread() {
queueThread = nullptr; queueThread = nullptr;
} }
Packet peekNetworkPacket() { Packet::Packet peekNetworkPacket() {
SDL_SemWait(lock); SDL_SemWait(lock);
Packet p; Packet::Packet p;
if (queue.size() > 0) { if (queue.size() > 0) {
Packet p = queue[0]; Packet::Packet p = queue[0];
} }
SDL_SemPost(lock); SDL_SemPost(lock);
return p; return p;
} }
Packet popNetworkPacket() { Packet::Packet popNetworkPacket() {
SDL_SemWait(lock); SDL_SemWait(lock);
Packet p; Packet::Packet p;
if (queue.size() > 0) { if (queue.size() > 0) {
p = queue[0]; p = queue[0];
queue.pop_front(); queue.pop_front();
+3 -3
View File
@@ -22,13 +22,13 @@
#ifndef NETWORKQUEUE_HPP_ #ifndef NETWORKQUEUE_HPP_
#define NETWORKQUEUE_HPP_ #define NETWORKQUEUE_HPP_
#include "packet_type.hpp" #include "packet.hpp"
void beginQueueThread(); void beginQueueThread();
void endQueueThread(); void endQueueThread();
void killQueueThread(); void killQueueThread();
Packet peekNetworkPacket(); Packet::Packet peekNetworkPacket();
Packet popNetworkPacket(); Packet::Packet popNetworkPacket();
void flushNetworkQueue(); void flushNetworkQueue();
#endif #endif
@@ -30,7 +30,9 @@
#pragma pack(push, 0) #pragma pack(push, 0)
enum class PacketType { namespace Packet {
enum class Type {
NONE = 0, NONE = 0,
PING = 1, PING = 1,
@@ -43,13 +45,11 @@ enum class PacketType {
SYNCHRONIZE = 8, SYNCHRONIZE = 8,
PLAYER_NEW = 9, PLAYER = 9,
PLAYER_DELETE = 10,
PLAYER_MOVE = 11,
}; };
struct Metadata { struct Metadata {
PacketType type; Type type;
IPaddress address; IPaddress address;
}; };
@@ -90,27 +90,15 @@ struct Synchronize {
Metadata meta; Metadata meta;
}; };
struct PlayerNew { struct Player {
Metadata meta; Metadata meta;
int playerIndex; //player data
//TODO Playerdata
}; };
struct PlayerDelete {
Metadata meta;
int playerIndex;
};
struct PlayerMove {
Metadata meta;
int playerIndex;
Vector2 position;
Vector2 motion;
};
union Packet { union Packet {
Packet() { Packet() {
meta.type = PacketType::NONE; meta.type = Type::NONE;
meta.address.host = 0; meta.address.host = 0;
meta.address.port = 0; meta.address.port = 0;
}; };
@@ -124,15 +112,17 @@ union Packet {
JoinResponse joinResponse; JoinResponse joinResponse;
Disconnect disconnect; Disconnect disconnect;
PlayerNew playerNew; Synchronize sync;
PlayerDelete playerDelete;
PlayerMove playerMove; Player player;
#ifdef DEBUG #ifdef DEBUG
char buffer[1024]; char buffer[1024];
#endif #endif
}; };
} //namespace Packet
#pragma pack(pop) #pragma pack(pop)
#endif #endif
+38
View File
@@ -0,0 +1,38 @@
/* Copyright: (c) Kayne Ruse 2013
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef PLAYERENTRY_HPP_
#define PLAYERENTRY_HPP_
#include "vector2.hpp"
#include <string>
struct PlayerEntry {
int index;
int clientIndex;
std::string handle;
std::string avatar;
Vector2 position;
Vector2 motion;
};
#endif
@@ -19,6 +19,16 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#include "character.hpp" #ifndef SERVERENTRY_HPP_
#define SERVERENTRY_HPP_
#include "SDL_net/SDL_net.h"
#include <string>
struct ServerEntry {
std::string name;
IPaddress address;
};
#endif
+22 -24
View File
@@ -70,7 +70,7 @@ void ServerApplication::Init() {
} }
//initiate the remaining singletons //initiate the remaining singletons
netUtil->Open(configUtil->Int("server.port"), sizeof(Packet)); netUtil->Open(configUtil->Int("server.port"), sizeof(Packet::Packet));
//create the threads //create the threads
beginQueueThread(); beginQueueThread();
@@ -115,42 +115,40 @@ void ServerApplication::Quit() {
//------------------------- //-------------------------
void ServerApplication::UpdateWorld(double delta) { void ServerApplication::UpdateWorld(double delta) {
for (auto it : players) { //
it.second.Update(delta);
}
} }
//------------------------- //-------------------------
//Network loop //Network loop
//------------------------- //-------------------------
int ServerApplication::HandlePacket(Packet p) { int ServerApplication::HandlePacket(Packet::Packet p) {
switch(p.meta.type) { switch(p.meta.type) {
case PacketType::NONE: case Packet::Type::NONE:
//DO NOTHING //DO NOTHING
return 0; return 0;
break; break;
case PacketType::PING: case Packet::Type::PING:
//quick pong //quick pong
p.meta.type = PacketType::PONG; p.meta.type = Packet::Type::PONG;
netUtil->Send(&p.meta.address, &p, sizeof(Packet)); netUtil->Send(&p.meta.address, &p, sizeof(Packet::Packet));
break; break;
case PacketType::PONG: case Packet::Type::PONG:
// //
break; break;
case PacketType::BROADCAST_REQUEST: case Packet::Type::BROADCAST_REQUEST:
HandleBroadcast(p.broadcastRequest); HandleBroadcast(p.broadcastRequest);
break; break;
// case PacketType::BROADCAST_RESPONSE: // case PacketType::BROADCAST_RESPONSE:
// // // //
// break; // break;
case PacketType::JOIN_REQUEST: case Packet::Type::JOIN_REQUEST:
HandleConnection(p.joinRequest); HandleConnection(p.joinRequest);
break; break;
// case PacketType::JOIN_RESPONSE: // case PacketType::JOIN_RESPONSE:
// // // //
// break; // break;
case PacketType::DISCONNECT: case Packet::Type::DISCONNECT:
HandleDisconnection(p.disconnect); HandleDisconnection(p.disconnect);
break; break;
// case PacketType::SYNCHRONIZE: // case PacketType::SYNCHRONIZE:
@@ -171,22 +169,22 @@ int ServerApplication::HandlePacket(Packet p) {
return 1; return 1;
} }
void ServerApplication::HandleBroadcast(BroadcastRequest& bcast) { void ServerApplication::HandleBroadcast(Packet::BroadcastRequest& bcast) {
//respond to a broadcast request with the server's data //respond to a broadcast request with the server's data
Packet p; Packet::Packet p;
p.meta.type = PacketType::BROADCAST_RESPONSE; p.meta.type = Packet::Type::BROADCAST_RESPONSE;
snprintf(p.broadcastResponse.name, PACKET_STRING_SIZE, "%s", configUtil->CString("server.name")); snprintf(p.broadcastResponse.name, PACKET_STRING_SIZE, "%s", configUtil->CString("server.name"));
//TODO version information //TODO version information
netUtil->Send(&bcast.meta.address, &p, sizeof(Packet)); netUtil->Send(&bcast.meta.address, &p, sizeof(Packet::Packet));
} }
void ServerApplication::HandleConnection(JoinRequest& request) { void ServerApplication::HandleConnection(Packet::JoinRequest& request) {
if (clients.size() >= SDLNET_MAX_UDPCHANNELS) { if (clients.size() >= SDLNET_MAX_UDPCHANNELS) {
//ignore the new connection if there's too many clients connected //ignore the new connection if there's too many clients connected
return; return;
} }
//create the containers //create the containers
ClientData client = { uniqueIndex++ }; ClientEntry client = { uniqueIndex++ };
//bind the address //bind the address
client.channel = netUtil->Bind(&request.meta.address); client.channel = netUtil->Bind(&request.meta.address);
@@ -195,20 +193,20 @@ void ServerApplication::HandleConnection(JoinRequest& request) {
clients[client.index] = client; clients[client.index] = client;
//send the player their information //send the player their information
Packet p; Packet::Packet p;
p.meta.type = PacketType::JOIN_RESPONSE; p.meta.type = Packet::Type::JOIN_RESPONSE;
p.joinResponse.clientIndex = client.index; p.joinResponse.clientIndex = client.index;
//TODO: resource list //TODO: resource list
netUtil->Send(client.channel, &p, sizeof(Packet)); netUtil->Send(client.channel, &p, sizeof(Packet::Packet));
//pretty //pretty
cout << "New connection: index " << client.index << endl; cout << "New connection: index " << client.index << endl;
cout << "number of clients: " << clients.size() << endl; cout << "number of clients: " << clients.size() << endl;
} }
void ServerApplication::HandleDisconnection(Disconnect& disconnect) { void ServerApplication::HandleDisconnection(Packet::Disconnect& disconnect) {
//disconnect a client (redundant message) //disconnect a client (redundant message)
netUtil->Send(clients[disconnect.clientIndex].channel, &disconnect, sizeof(Packet)); netUtil->Send(clients[disconnect.clientIndex].channel, &disconnect, sizeof(Packet::Packet));
netUtil->Unbind(clients[disconnect.clientIndex].channel); netUtil->Unbind(clients[disconnect.clientIndex].channel);
clients.erase(disconnect.clientIndex); clients.erase(disconnect.clientIndex);
+10 -26
View File
@@ -23,10 +23,13 @@
#define SERVERAPPLICATION_HPP_ #define SERVERAPPLICATION_HPP_
#include "utilities.hpp" #include "utilities.hpp"
#include "packet_type.hpp" #include "packet.hpp"
#include "singleton.hpp" #include "singleton.hpp"
#include "network_queue.hpp" #include "network_queue.hpp"
#include "client_entry.hpp"
#include "player_entry.hpp"
#include "config_Utility.hpp" #include "config_Utility.hpp"
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "vector2.hpp" #include "vector2.hpp"
@@ -40,25 +43,6 @@
//lazy //lazy
typedef std::chrono::high_resolution_clock Clock; typedef std::chrono::high_resolution_clock Clock;
struct ClientData {
int index;
int channel;
int playerIndex;
};
struct PlayerData {
int index;
int clientIndex;
std::string handle;
std::string avatar;
Vector2 position;
Vector2 motion;
void Update(double delta) {
position += motion * delta;
}
};
class ServerApplication { class ServerApplication {
public: public:
ServerApplication(); ServerApplication();
@@ -74,10 +58,10 @@ private:
void UpdateWorld(double delta); void UpdateWorld(double delta);
//network loop //network loop
int HandlePacket(Packet p); int HandlePacket(Packet::Packet p);
void HandleBroadcast(BroadcastRequest&); void HandleBroadcast(Packet::BroadcastRequest&);
void HandleConnection(JoinRequest&); void HandleConnection(Packet::JoinRequest&);
void HandleDisconnection(Disconnect&); void HandleDisconnection(Packet::Disconnect&);
//services //services
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get(); ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
@@ -86,8 +70,8 @@ private:
//members //members
Clock::time_point lastTick = Clock::now(); Clock::time_point lastTick = Clock::now();
std::map<int, ClientData> clients; std::map<int, ClientEntry> clients;
std::map<int, PlayerData> players; std::map<int, PlayerEntry> players;
bool running = false; bool running = false;