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
+22 -24
View File
@@ -70,7 +70,7 @@ void ServerApplication::Init() {
}
//initiate the remaining singletons
netUtil->Open(configUtil->Int("server.port"), sizeof(Packet));
netUtil->Open(configUtil->Int("server.port"), sizeof(Packet::Packet));
//create the threads
beginQueueThread();
@@ -115,42 +115,40 @@ void ServerApplication::Quit() {
//-------------------------
void ServerApplication::UpdateWorld(double delta) {
for (auto it : players) {
it.second.Update(delta);
}
//
}
//-------------------------
//Network loop
//-------------------------
int ServerApplication::HandlePacket(Packet p) {
int ServerApplication::HandlePacket(Packet::Packet p) {
switch(p.meta.type) {
case PacketType::NONE:
case Packet::Type::NONE:
//DO NOTHING
return 0;
break;
case PacketType::PING:
case Packet::Type::PING:
//quick pong
p.meta.type = PacketType::PONG;
netUtil->Send(&p.meta.address, &p, sizeof(Packet));
p.meta.type = Packet::Type::PONG;
netUtil->Send(&p.meta.address, &p, sizeof(Packet::Packet));
break;
case PacketType::PONG:
case Packet::Type::PONG:
//
break;
case PacketType::BROADCAST_REQUEST:
case Packet::Type::BROADCAST_REQUEST:
HandleBroadcast(p.broadcastRequest);
break;
// case PacketType::BROADCAST_RESPONSE:
// //
// break;
case PacketType::JOIN_REQUEST:
case Packet::Type::JOIN_REQUEST:
HandleConnection(p.joinRequest);
break;
// case PacketType::JOIN_RESPONSE:
// //
// break;
case PacketType::DISCONNECT:
case Packet::Type::DISCONNECT:
HandleDisconnection(p.disconnect);
break;
// case PacketType::SYNCHRONIZE:
@@ -171,22 +169,22 @@ int ServerApplication::HandlePacket(Packet p) {
return 1;
}
void ServerApplication::HandleBroadcast(BroadcastRequest& bcast) {
void ServerApplication::HandleBroadcast(Packet::BroadcastRequest& bcast) {
//respond to a broadcast request with the server's data
Packet p;
p.meta.type = PacketType::BROADCAST_RESPONSE;
Packet::Packet p;
p.meta.type = Packet::Type::BROADCAST_RESPONSE;
snprintf(p.broadcastResponse.name, PACKET_STRING_SIZE, "%s", configUtil->CString("server.name"));
//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) {
//ignore the new connection if there's too many clients connected
return;
}
//create the containers
ClientData client = { uniqueIndex++ };
ClientEntry client = { uniqueIndex++ };
//bind the address
client.channel = netUtil->Bind(&request.meta.address);
@@ -195,20 +193,20 @@ void ServerApplication::HandleConnection(JoinRequest& request) {
clients[client.index] = client;
//send the player their information
Packet p;
p.meta.type = PacketType::JOIN_RESPONSE;
Packet::Packet p;
p.meta.type = Packet::Type::JOIN_RESPONSE;
p.joinResponse.clientIndex = client.index;
//TODO: resource list
netUtil->Send(client.channel, &p, sizeof(Packet));
netUtil->Send(client.channel, &p, sizeof(Packet::Packet));
//pretty
cout << "New connection: index " << client.index << endl;
cout << "number of clients: " << clients.size() << endl;
}
void ServerApplication::HandleDisconnection(Disconnect& disconnect) {
void ServerApplication::HandleDisconnection(Packet::Disconnect& disconnect) {
//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);
clients.erase(disconnect.clientIndex);
+10 -26
View File
@@ -23,10 +23,13 @@
#define SERVERAPPLICATION_HPP_
#include "utilities.hpp"
#include "packet_type.hpp"
#include "packet.hpp"
#include "singleton.hpp"
#include "network_queue.hpp"
#include "client_entry.hpp"
#include "player_entry.hpp"
#include "config_Utility.hpp"
#include "udp_network_utility.hpp"
#include "vector2.hpp"
@@ -40,25 +43,6 @@
//lazy
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 {
public:
ServerApplication();
@@ -74,10 +58,10 @@ private:
void UpdateWorld(double delta);
//network loop
int HandlePacket(Packet p);
void HandleBroadcast(BroadcastRequest&);
void HandleConnection(JoinRequest&);
void HandleDisconnection(Disconnect&);
int HandlePacket(Packet::Packet p);
void HandleBroadcast(Packet::BroadcastRequest&);
void HandleConnection(Packet::JoinRequest&);
void HandleDisconnection(Packet::Disconnect&);
//services
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
@@ -86,8 +70,8 @@ private:
//members
Clock::time_point lastTick = Clock::now();
std::map<int, ClientData> clients;
std::map<int, PlayerData> players;
std::map<int, ClientEntry> clients;
std::map<int, PlayerEntry> players;
bool running = false;