completed switch to yaml for messages

This commit is contained in:
gmueller 2011-01-25 18:43:49 +01:00
parent 1dbdb25f30
commit d4dc19d358
2 changed files with 307 additions and 182 deletions

View File

@ -12,6 +12,7 @@
#include <iostream>
#include <cmath>
#include <typeinfo>
#include <istream>
Network::Network(Game *game) :
game(game), client_peer(0) {
@ -93,16 +94,21 @@ void Network::sendGameUpdates() {
msg.vy = v.y();
msg.vz = v.z();
msg.points = game->player[i].points;
ENetPacket * packet = enet_packet_create(&msg, sizeof(msg), 0);
YAML::Emitter em;
em << msg;
ENetPacket * packet = enet_packet_create((uint8_t *) em.c_str(),
em.size(), 0);
enet_host_broadcast(host, 0, packet);
}
for (i = 0; i < game->teams.size(); i++) {
team_update_message_t msg;
//msg.msg_id = MESSAGE_TEAM_UPDATE;
msg.team_id = game->teams[i].id;
msg.points = game->teams[i].points;
msg.wins = game->teams[i].wins;
ENetPacket * packet = enet_packet_create(&msg, sizeof(msg), 0);
YAML::Emitter em;
em << msg;
ENetPacket * packet = enet_packet_create((uint8_t *) em.c_str(),
em.size(), 0);
enet_host_broadcast(host, 0, packet);
}
@ -119,40 +125,178 @@ T *_message_cast(void *p, size_t l) {
return (T *) p;
}
#define PARSE_START(msg) void operator >>(const YAML::Node& node, msg& m) {
#define PARSE_FIELD(name) node[#name] >> m.name;
#define PARSE_END() }
#define EMIT_START(msg) YAML::Emitter& operator <<(YAML::Emitter& out, const msg& m) { out << YAML::BeginMap; out << YAML::Key << "id" << YAML::Value << msg::id;
#define EMIT_FIELD(name) out << YAML::Key << #name << YAML::Value << m.name;
#define EMIT_END() out << YAML::EndMap;return out;}
// player_spawn_message_t
EMIT_START(player_spawn_message_t)
EMIT_FIELD(team_id)
EMIT_FIELD(player_id)
EMIT_END()
PARSE_START(player_spawn_message_t) PARSE_FIELD(team_id)
PARSE_FIELD(player_id)PARSE_END()
// player_kill_message_t
EMIT_START(player_kill_message_t)
EMIT_FIELD(player_id)
EMIT_END()
PARSE_START(player_kill_message_t) PARSE_FIELD(player_id)PARSE_END()
// accept_message_t
EMIT_START(accept_message_t)
EMIT_FIELD(player_id)
EMIT_END()
PARSE_START(accept_message_t) PARSE_FIELD(player_id)PARSE_END()
// player_update_message_t
EMIT_START(player_update_message_t)
EMIT_FIELD(player_id)
EMIT_FIELD(x)
EMIT_FIELD(y)
EMIT_FIELD(z)
EMIT_FIELD(vx)
EMIT_FIELD(vy)
EMIT_FIELD(vz)
EMIT_FIELD(points)
EMIT_END()
PARSE_START(player_update_message_t) PARSE_FIELD(player_id)
PARSE_FIELD(x)
PARSE_FIELD(y)
PARSE_FIELD(z)
PARSE_FIELD(vx)
PARSE_FIELD(vy)
PARSE_FIELD(vz)
PARSE_FIELD(points)PARSE_END()
// player_accelerate_message_t
EMIT_START(player_accelerate_message_t)
EMIT_FIELD(player_id)
EMIT_FIELD(x)
EMIT_FIELD(y)
EMIT_FIELD(z)
EMIT_END()
PARSE_START(player_accelerate_message_t) PARSE_FIELD(player_id)
PARSE_FIELD(x)
PARSE_FIELD(y)
PARSE_FIELD(z)PARSE_END()
// bomb_drop_meesage_t
EMIT_START(bomb_drop_meesage_t)
EMIT_FIELD(x)
EMIT_FIELD(y)
EMIT_FIELD(z)
EMIT_FIELD(vx)
EMIT_FIELD(vy)
EMIT_FIELD(vz)
EMIT_FIELD(ttl)
EMIT_END()
PARSE_START(bomb_drop_meesage_t) PARSE_FIELD(x)
PARSE_FIELD(y)
PARSE_FIELD(z)
PARSE_FIELD(vx)
PARSE_FIELD(vy)
PARSE_FIELD(vz)
PARSE_FIELD(ttl)PARSE_END()
// bomb_update_meesage_t
EMIT_START(bomb_update_meesage_t)
EMIT_FIELD(bomb_index)
EMIT_FIELD(x)
EMIT_FIELD(y)
EMIT_FIELD(z)
EMIT_FIELD(vx)
EMIT_FIELD(vy)
EMIT_FIELD(vz)
EMIT_FIELD(ttl)
EMIT_END()
PARSE_START(bomb_update_meesage_t) PARSE_FIELD(bomb_index)
PARSE_FIELD(x)
PARSE_FIELD(y)
PARSE_FIELD(z)
PARSE_FIELD(vx)
PARSE_FIELD(vy)
PARSE_FIELD(vz)
PARSE_FIELD(ttl)PARSE_END()
// point_update_mesage_t
EMIT_START(point_update_mesage_t)
EMIT_FIELD(point_index)
EMIT_FIELD(x)
EMIT_FIELD(y)
EMIT_FIELD(z)
EMIT_FIELD(status)
EMIT_END()
PARSE_START(point_update_mesage_t) PARSE_FIELD(point_index)
PARSE_FIELD(x)
PARSE_FIELD(y)
PARSE_FIELD(z)
PARSE_FIELD(status)PARSE_END()
// team_update_message_t
EMIT_START(team_update_message_t)
EMIT_FIELD(team_id)
EMIT_FIELD(points)
EMIT_FIELD(wins)
EMIT_END()
PARSE_START(team_update_message_t) PARSE_FIELD(team_id)
PARSE_FIELD(points)
PARSE_FIELD(wins)PARSE_END()
void Network::dispatch(enet_uint8 *data, size_t length) {
#if 0
message_t *msg = (message_t *) data;
switch (msg->msg_id) {
std::istringstream istr;
istr.rdbuf()->pubsetbuf((char *) data, length);
YAML::Parser parser;
parser.Load(istr);
YAML::Node doc;
parser.GetNextDocument(doc);
int msd_id;
doc["id"] >> msd_id;
switch (msd_id) {
case MESSAGE_PLAYER_SPAWN: {
player_spawn_message_t *sm = _message_cast<player_spawn_message_t> (
data, length);
Team *team = game->getTeam(sm->team_id);
player_t *player = game->spawnPlayerWithId(team, sm->player_id);
player_spawn_message_t m;
doc >> m;
Team *team = game->getTeam(m.team_id);
player_t *player = game->spawnPlayerWithId(team, m.player_id);
break;
}
case MESSAGE_PLAYER_KILL: {
player_kill_message_t *sm = _message_cast<player_kill_message_t> (data,
length);
player_t *player = game->getPlayer(sm->player_id);
player_kill_message_t m;
doc >> m;
player_t *player = game->getPlayer(m.player_id);
player->status = 0;
player->team = 0;
break;
}
case MESSAGE_ACCEPT: {
accept_message_t *am = _message_cast<accept_message_t> (data, length);
game->local_player = game->getPlayer(am->player_id);
std::cout << "[Network] accpeted player " << am->player_id << std::endl;
accept_message_t m;
doc >> m;
game->local_player = game->getPlayer(m.player_id);
std::cout << "[Network] accpeted player " << m.player_id << std::endl;
break;
}
case MESSAGE_PLAYER_UPDATE: {
player_update_message_t *um = _message_cast<player_update_message_t> (
data, length);
player_t *player = game->getPlayer(um->player_id);
player_update_message_t m;
doc >> m;
player_t *player = game->getPlayer(m.player_id);
#if 1
btVector3 v = player->body->getLinearVelocity();
btVector3 p = player->body->getWorldTransform().getOrigin();
btVector3 v1(um->vx, um->vy, um->vz);
btVector3 p1(um->x, um->y, um->z);
btVector3 v1(m.vx, m.vy, m.vz);
btVector3 p1(m.x, m.y, m.z);
player->body->activate(true);
player->body->setLinearVelocity(v1);
player->body->getWorldTransform().setOrigin((2 * p + p1) / 3);
@ -187,44 +331,43 @@ void Network::dispatch(enet_uint8 *data, size_t length) {
player->vy = um->vy;
player->vz = um->vz;
#endif
std::cout << um->z << std::endl;
player->points = um->points;
player->points = m.points;
break;
}
case MESSAGE_POINT_UPDATE: {
point_update_mesage_t *msg = _message_cast<point_update_mesage_t> (
data, length);
point_t *p = &game->point[msg->point_index];
p->status = msg->status;
p->x = msg->x;
p->y = msg->y;
p->z = msg->z;
point_update_mesage_t m;
doc >> m;
point_t *p = &game->point[m.point_index];
p->status = m.status;
p->x = m.x;
p->y = m.y;
p->z = m.z;
break;
}
case MESSAGE_PLAYER_ACCELERATE: {
player_accelerate_message_t *um = _message_cast<
player_accelerate_message_t> (data, length);
player_t *player = game->getPlayer(um->player_id);
player->vx += um->x;
player->vy += um->y;
player->vz += um->z;
player_accelerate_message_t m;
doc >> m;
player_t *player = game->getPlayer(m.player_id);
player->vx += m.x;
player->vy += m.y;
player->vz += m.z;
player->body->activate(true);
player->body->applyCentralImpulse(btVector3(um->x, um->y, um->z));
player->body->applyCentralImpulse(btVector3(m.x, m.y, m.z));
break;
}
case MESSAGE_BOMB_DROP: {
bomb_drop_meesage_t *m = _message_cast<bomb_drop_meesage_t> (data,
length);
bomb_drop_meesage_t m;
doc >> m;
bomb_t *bomb = game->spawn_bomb();
if (bomb == NULL)
return;
bomb->x = m->x + m->vx * 0.0001;
bomb->y = m->y + m->vy * 0.0001;
bomb->z = m->z + m->vz * 0.0001;
bomb->vx = m->vx;
bomb->vy = m->vy;
bomb->vz = m->vz;
bomb->ttl = m->ttl;
bomb->x = m.x + m.vx * 0.0001;
bomb->y = m.y + m.vy * 0.0001;
bomb->z = m.z + m.vz * 0.0001;
bomb->vx = m.vx;
bomb->vy = m.vy;
bomb->vz = m.vz;
bomb->ttl = m.ttl;
if (client_peer == NULL) {
ENetPacket * packet = enet_packet_create(data, length,
ENET_PACKET_FLAG_RELIABLE);
@ -233,17 +376,16 @@ void Network::dispatch(enet_uint8 *data, size_t length) {
break;
}
case MESSAGE_TEAM_UPDATE: {
team_update_message_t *m = _message_cast<team_update_message_t> (data,
length);
Team *team = game->getTeam(m->team_id);
team_update_message_t m;
doc >> m;
Team *team = game->getTeam(m.team_id);
if (team == NULL)
return;
team->points = m->points;
team->wins = m->wins;
team->points = m.points;
team->wins = m.wins;
break;
}
};
#endif
}
void Network::service(uint32_t timeout) {
@ -259,7 +401,6 @@ void Network::service(uint32_t timeout) {
std::cout << "A new client connected from "
<< event.peer->address.host << " "
<< event.peer->address.port << std::endl;
#if 0
// bring new client up to date
size_t i;
for (i = 0; i < GAME_PLAYER_COUNT; i++) {
@ -267,11 +408,13 @@ void Network::service(uint32_t timeout) {
continue;
// send player spawn message
player_spawn_message_t spwan_msg;
spwan_msg.msg_id = MESSAGE_PLAYER_SPAWN;
spwan_msg.team_id = game->player[i].team->id;
spwan_msg.player_id = game->player[i].id;
ENetPacket * packet = enet_packet_create(&spwan_msg,
sizeof(spwan_msg), ENET_PACKET_FLAG_RELIABLE);
YAML::Emitter em;
em << spwan_msg;
ENetPacket * packet = enet_packet_create(
(uint8_t *) em.c_str(), em.size(),
ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(event.peer, 0, packet);
}
@ -279,13 +422,16 @@ void Network::service(uint32_t timeout) {
if (game->point[i].status == 0)
continue;
point_update_mesage_t msg;
msg.msg_id = MESSAGE_POINT_UPDATE;
msg.point_index = i;
msg.status = game->point[i].status;
msg.x = game->point[i].x;
msg.y = game->point[i].y;
msg.z = game->point[i].z;
ENetPacket * packet = enet_packet_create(&msg, sizeof(msg),
YAML::Emitter em;
em << msg;
ENetPacket * packet = enet_packet_create(
(uint8_t *) em.c_str(), em.size(),
ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(event.peer, 0, packet);
}
@ -293,24 +439,31 @@ void Network::service(uint32_t timeout) {
Team *team = game->team_with_least_players();
player_t *player = game->spawnPlayer(team);
event.peer->data = player;
{
// send player spawn message
player_spawn_message_t spwan_msg;
spwan_msg.msg_id = MESSAGE_PLAYER_SPAWN;
spwan_msg.team_id = team->id;
spwan_msg.player_id = player->id;
ENetPacket * packet = enet_packet_create(&spwan_msg,
sizeof(spwan_msg), ENET_PACKET_FLAG_RELIABLE);
enet_host_broadcast(host, 0, packet);
YAML::Emitter em;
em << spwan_msg;
ENetPacket * packet = enet_packet_create(
(uint8_t *) em.c_str(), em.size(),
ENET_PACKET_FLAG_RELIABLE);
enet_host_broadcast(host, 0, packet);
}
{
// send team and player
accept_message_t msg;
msg.msg_id = MESSAGE_ACCEPT;
msg.player_id = player->id;
packet = enet_packet_create(&msg, sizeof(msg),
YAML::Emitter em;
em << msg;
ENetPacket * packet = enet_packet_create(
(uint8_t *) em.c_str(), em.size(),
ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(event.peer, 0, packet);
#endif
}
// send state
}
break;
@ -342,39 +495,6 @@ void Network::service(uint32_t timeout) {
}
#define EMIT_START(msg) YAML::Emitter& operator <<(YAML::Emitter& out, const msg& m) { out << YAML::Key << "id" << YAML::Value << msg::id;
#define EMIT_FIELD(name) out << YAML::Key << #name << YAML::Value << m.name;
#define EMIT_END() out << YAML::EndMap;return out;}
EMIT_START(player_spawn_message_t)
EMIT_FIELD(team_id)
EMIT_FIELD(player_id)
EMIT_END()
EMIT_START(player_accelerate_message_t)
EMIT_FIELD(player_id)
EMIT_FIELD(x)
EMIT_FIELD(y)
EMIT_FIELD(z)
EMIT_END()
EMIT_START(bomb_drop_meesage_t)
EMIT_FIELD(x)
EMIT_FIELD(y)
EMIT_FIELD(z)
EMIT_FIELD(vx)
EMIT_FIELD(vy)
EMIT_FIELD(vz)
EMIT_FIELD(ttl)
EMIT_END()
#define PARSE_START(msg) void operator >>(const YAML::Node& node, msg& m) {
#define PARSE_FIELD(name) node[#name] >> m.name;
#define PARSE_END() }
PARSE_START(player_spawn_message_t) PARSE_FIELD(team_id)
PARSE_FIELD(player_id)PARSE_END()
//void Network::sendMessage(message_t &m) {
// if (client_peer) {
// msgpack::sbuffer b;

View File

@ -16,6 +16,8 @@
#include <string>
#define EMIT_DEFINE(msg) YAML::Emitter& operator <<(YAML::Emitter& out, const msg& m);
#define MESSAGE_PLAYER_SPAWN 0
#define MESSAGE_PLAYER_KILL 1
#define MESSAGE_ACCEPT 2
@ -33,6 +35,7 @@ struct player_spawn_message_t {
size_t team_id;
size_t player_id;
};
EMIT_DEFINE(player_spawn_message_t)
struct player_kill_message_t {
enum {
@ -40,6 +43,7 @@ struct player_kill_message_t {
};
uint16_t player_id;
};
EMIT_DEFINE(player_kill_message_t)
struct accept_message_t {
enum {
@ -47,6 +51,7 @@ struct accept_message_t {
};
uint16_t player_id;
};
EMIT_DEFINE(accept_message_t)
struct player_update_message_t {
enum {
@ -58,6 +63,7 @@ struct player_update_message_t {
double vx, vy, vz;
uint16_t points;
};
EMIT_DEFINE(player_update_message_t)
struct player_accelerate_message_t {
enum {
@ -66,6 +72,7 @@ struct player_accelerate_message_t {
uint16_t player_id;
double x, y, z;
};
EMIT_DEFINE(player_accelerate_message_t)
struct bomb_drop_meesage_t {
enum {
@ -75,6 +82,7 @@ struct bomb_drop_meesage_t {
double vx, vy, vz;
double ttl;
};
EMIT_DEFINE(bomb_drop_meesage_t)
struct bomb_update_meesage_t {
enum {
@ -85,6 +93,7 @@ struct bomb_update_meesage_t {
double vx, vy, vz;
double ttl;
};
EMIT_DEFINE(bomb_update_meesage_t)
struct point_update_mesage_t {
enum {
@ -94,6 +103,7 @@ struct point_update_mesage_t {
uint8_t status;
double x, y, z;
};
EMIT_DEFINE(point_update_mesage_t)
struct team_update_message_t {
enum {
@ -103,12 +113,7 @@ struct team_update_message_t {
uint16_t points;
uint16_t wins;
};
#define EMIT_DEFINE(msg) YAML::Emitter& operator <<(YAML::Emitter& out, const msg& m);
EMIT_DEFINE(player_spawn_message_t)
EMIT_DEFINE(player_accelerate_message_t)
EMIT_DEFINE(bomb_drop_meesage_t)
EMIT_DEFINE(team_update_message_t)
class Network {
public: