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 <iostream>
#include <cmath> #include <cmath>
#include <typeinfo> #include <typeinfo>
#include <istream>
Network::Network(Game *game) : Network::Network(Game *game) :
game(game), client_peer(0) { game(game), client_peer(0) {
@ -93,16 +94,21 @@ void Network::sendGameUpdates() {
msg.vy = v.y(); msg.vy = v.y();
msg.vz = v.z(); msg.vz = v.z();
msg.points = game->player[i].points; 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); enet_host_broadcast(host, 0, packet);
} }
for (i = 0; i < game->teams.size(); i++) { for (i = 0; i < game->teams.size(); i++) {
team_update_message_t msg; team_update_message_t msg;
//msg.msg_id = MESSAGE_TEAM_UPDATE;
msg.team_id = game->teams[i].id; msg.team_id = game->teams[i].id;
msg.points = game->teams[i].points; msg.points = game->teams[i].points;
msg.wins = game->teams[i].wins; 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); enet_host_broadcast(host, 0, packet);
} }
@ -119,131 +125,267 @@ T *_message_cast(void *p, size_t l) {
return (T *) p; 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) { void Network::dispatch(enet_uint8 *data, size_t length) {
#if 0 std::istringstream istr;
message_t *msg = (message_t *) data; istr.rdbuf()->pubsetbuf((char *) data, length);
switch (msg->msg_id) { YAML::Parser parser;
case MESSAGE_PLAYER_SPAWN: { parser.Load(istr);
player_spawn_message_t *sm = _message_cast<player_spawn_message_t> ( YAML::Node doc;
data, length); parser.GetNextDocument(doc);
Team *team = game->getTeam(sm->team_id); int msd_id;
player_t *player = game->spawnPlayerWithId(team, sm->player_id); doc["id"] >> msd_id;
break; switch (msd_id) {
} case MESSAGE_PLAYER_SPAWN: {
case MESSAGE_PLAYER_KILL: { player_spawn_message_t m;
player_kill_message_t *sm = _message_cast<player_kill_message_t> (data, doc >> m;
length); Team *team = game->getTeam(m.team_id);
player_t *player = game->getPlayer(sm->player_id); player_t *player = game->spawnPlayerWithId(team, m.player_id);
player->status = 0; break;
player->team = 0; }
break; case MESSAGE_PLAYER_KILL: {
} player_kill_message_t m;
case MESSAGE_ACCEPT: { doc >> m;
accept_message_t *am = _message_cast<accept_message_t> (data, length); player_t *player = game->getPlayer(m.player_id);
game->local_player = game->getPlayer(am->player_id); player->status = 0;
std::cout << "[Network] accpeted player " << am->player_id << std::endl; player->team = 0;
break; break;
} }
case MESSAGE_PLAYER_UPDATE: { case MESSAGE_ACCEPT: {
player_update_message_t *um = _message_cast<player_update_message_t> ( accept_message_t m;
data, length); doc >> m;
player_t *player = game->getPlayer(um->player_id); 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 m;
doc >> m;
player_t *player = game->getPlayer(m.player_id);
#if 1 #if 1
btVector3 v = player->body->getLinearVelocity(); btVector3 v = player->body->getLinearVelocity();
btVector3 p = player->body->getWorldTransform().getOrigin(); btVector3 p = player->body->getWorldTransform().getOrigin();
btVector3 v1(um->vx, um->vy, um->vz); btVector3 v1(m.vx, m.vy, m.vz);
btVector3 p1(um->x, um->y, um->z); btVector3 p1(m.x, m.y, m.z);
player->body->activate(true); player->body->activate(true);
player->body->setLinearVelocity(v1); player->body->setLinearVelocity(v1);
player->body->getWorldTransform().setOrigin((2 * p + p1) / 3); player->body->getWorldTransform().setOrigin((2 * p + p1) / 3);
//player->body->applyCentralForce((v1 -v)/10.0); //player->body->applyCentralForce((v1 -v)/10.0);
#endif #endif
#if 0 #if 0
std::cout << " v:" << um->vx << " " << um->vy << " " << um -> vz std::cout << " v:" << um->vx << " " << um->vy << " " << um -> vz
<< std::endl; << std::endl;
std::cout << " p:" << um->x << " " << um->y << " " << um -> z std::cout << " p:" << um->x << " " << um->y << " " << um -> z
<< std::endl; << std::endl;
double threshold = 0.1; double threshold = 0.1;
double dx = um->x - player->x; double dx = um->x - player->x;
double dy = um->y - player->y; double dy = um->y - player->y;
double dz = um->z - player->z; double dz = um->z - player->z;
std::cout << " d:" << dx << " " << dy << " " << dz << std::endl; std::cout << " d:" << dx << " " << dy << " " << dz << std::endl;
if (fabs(dx) < threshold) { if (fabs(dx) < threshold) {
player->x += 0.1 * dx; player->x += 0.1 * dx;
} else { } else {
player->x = um->x; player->x = um->x;
} }
if (fabs(dy) < threshold) { if (fabs(dy) < threshold) {
player->y += 0.1 * dy; player->y += 0.1 * dy;
} else { } else {
player->y = um->y; player->y = um->y;
} }
if (fabs(dz) < threshold) { if (fabs(dz) < threshold) {
player->z += 0.1 * dz; player->z += 0.1 * dz;
} else { } else {
player->z = um->z; player->z = um->z;
} }
player->vx = um->vx; player->vx = um->vx;
player->vy = um->vy; player->vy = um->vy;
player->vz = um->vz; player->vz = um->vz;
#endif #endif
std::cout << um->z << std::endl; player->points = m.points;
player->points = um->points; break;
break; }
} case MESSAGE_POINT_UPDATE: {
case MESSAGE_POINT_UPDATE: { point_update_mesage_t m;
point_update_mesage_t *msg = _message_cast<point_update_mesage_t> ( doc >> m;
data, length); point_t *p = &game->point[m.point_index];
point_t *p = &game->point[msg->point_index]; p->status = m.status;
p->status = msg->status; p->x = m.x;
p->x = msg->x; p->y = m.y;
p->y = msg->y; p->z = m.z;
p->z = msg->z; break;
break; }
} case MESSAGE_PLAYER_ACCELERATE: {
case MESSAGE_PLAYER_ACCELERATE: { player_accelerate_message_t m;
player_accelerate_message_t *um = _message_cast< doc >> m;
player_accelerate_message_t> (data, length); player_t *player = game->getPlayer(m.player_id);
player_t *player = game->getPlayer(um->player_id); player->vx += m.x;
player->vx += um->x; player->vy += m.y;
player->vy += um->y; player->vz += m.z;
player->vz += um->z; player->body->activate(true);
player->body->activate(true); player->body->applyCentralImpulse(btVector3(m.x, m.y, m.z));
player->body->applyCentralImpulse(btVector3(um->x, um->y, um->z)); break;
break; }
} case MESSAGE_BOMB_DROP: {
case MESSAGE_BOMB_DROP: { bomb_drop_meesage_t m;
bomb_drop_meesage_t *m = _message_cast<bomb_drop_meesage_t> (data, doc >> m;
length); bomb_t *bomb = game->spawn_bomb();
bomb_t *bomb = game->spawn_bomb(); if (bomb == NULL)
if (bomb == NULL)
return; return;
bomb->x = m->x + m->vx * 0.0001; bomb->x = m.x + m.vx * 0.0001;
bomb->y = m->y + m->vy * 0.0001; bomb->y = m.y + m.vy * 0.0001;
bomb->z = m->z + m->vz * 0.0001; bomb->z = m.z + m.vz * 0.0001;
bomb->vx = m->vx; bomb->vx = m.vx;
bomb->vy = m->vy; bomb->vy = m.vy;
bomb->vz = m->vz; bomb->vz = m.vz;
bomb->ttl = m->ttl; bomb->ttl = m.ttl;
if (client_peer == NULL) { if (client_peer == NULL) {
ENetPacket * packet = enet_packet_create(data, length, ENetPacket * packet = enet_packet_create(data, length,
ENET_PACKET_FLAG_RELIABLE); ENET_PACKET_FLAG_RELIABLE);
enet_host_broadcast(host, 0, packet); enet_host_broadcast(host, 0, packet);
}
break;
} }
case MESSAGE_TEAM_UPDATE: { break;
team_update_message_t *m = _message_cast<team_update_message_t> (data, }
length); case MESSAGE_TEAM_UPDATE: {
Team *team = game->getTeam(m->team_id); team_update_message_t m;
if (team == NULL) doc >> m;
Team *team = game->getTeam(m.team_id);
if (team == NULL)
return; return;
team->points = m->points; team->points = m.points;
team->wins = m->wins; team->wins = m.wins;
break; break;
} }
}; };
#endif
} }
void Network::service(uint32_t timeout) { void Network::service(uint32_t timeout) {
@ -259,33 +401,37 @@ void Network::service(uint32_t timeout) {
std::cout << "A new client connected from " std::cout << "A new client connected from "
<< event.peer->address.host << " " << event.peer->address.host << " "
<< event.peer->address.port << std::endl; << event.peer->address.port << std::endl;
#if 0
// bring new client up to date // bring new client up to date
size_t i; size_t i;
for (i = 0; i < GAME_PLAYER_COUNT; i++) { for (i = 0; i < GAME_PLAYER_COUNT; i++) {
if (game->player[i].status == 0) if (game->player[i].status == 0)
continue; continue;
// send player spawn message // send player spawn message
player_spawn_message_t spwan_msg; player_spawn_message_t spwan_msg;
spwan_msg.msg_id = MESSAGE_PLAYER_SPAWN;
spwan_msg.team_id = game->player[i].team->id; spwan_msg.team_id = game->player[i].team->id;
spwan_msg.player_id = game->player[i].id; spwan_msg.player_id = game->player[i].id;
ENetPacket * packet = enet_packet_create(&spwan_msg, YAML::Emitter em;
sizeof(spwan_msg), ENET_PACKET_FLAG_RELIABLE); 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); enet_peer_send(event.peer, 0, packet);
} }
for (i = 0; i < GAME_POINT_COUNT; i++) { for (i = 0; i < GAME_POINT_COUNT; i++) {
if (game->point[i].status == 0) if (game->point[i].status == 0)
continue; continue;
point_update_mesage_t msg; point_update_mesage_t msg;
msg.msg_id = MESSAGE_POINT_UPDATE;
msg.point_index = i; msg.point_index = i;
msg.status = game->point[i].status; msg.status = game->point[i].status;
msg.x = game->point[i].x; msg.x = game->point[i].x;
msg.y = game->point[i].y; msg.y = game->point[i].y;
msg.z = game->point[i].z; 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_PACKET_FLAG_RELIABLE);
enet_peer_send(event.peer, 0, packet); enet_peer_send(event.peer, 0, packet);
} }
@ -293,24 +439,31 @@ void Network::service(uint32_t timeout) {
Team *team = game->team_with_least_players(); Team *team = game->team_with_least_players();
player_t *player = game->spawnPlayer(team); player_t *player = game->spawnPlayer(team);
event.peer->data = player; event.peer->data = player;
{
// send player spawn message
player_spawn_message_t spwan_msg;
spwan_msg.team_id = team->id;
spwan_msg.player_id = player->id;
YAML::Emitter em;
em << spwan_msg;
// send player spawn message ENetPacket * packet = enet_packet_create(
player_spawn_message_t spwan_msg; (uint8_t *) em.c_str(), em.size(),
spwan_msg.msg_id = MESSAGE_PLAYER_SPAWN; ENET_PACKET_FLAG_RELIABLE);
spwan_msg.team_id = team->id; enet_host_broadcast(host, 0, packet);
spwan_msg.player_id = player->id; }
ENetPacket * packet = enet_packet_create(&spwan_msg, {
sizeof(spwan_msg), ENET_PACKET_FLAG_RELIABLE); // send team and player
enet_host_broadcast(host, 0, packet); accept_message_t msg;
msg.player_id = player->id;
YAML::Emitter em;
em << msg;
// send team and player ENetPacket * packet = enet_packet_create(
accept_message_t msg; (uint8_t *) em.c_str(), em.size(),
msg.msg_id = MESSAGE_ACCEPT; ENET_PACKET_FLAG_RELIABLE);
msg.player_id = player->id; enet_peer_send(event.peer, 0, packet);
packet = enet_packet_create(&msg, sizeof(msg), }
ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(event.peer, 0, packet);
#endif
// send state // send state
} }
break; 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) { //void Network::sendMessage(message_t &m) {
// if (client_peer) { // if (client_peer) {
// msgpack::sbuffer b; // msgpack::sbuffer b;

View File

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