gremlin/src/common/Network.cpp

474 lines
13 KiB
C++
Raw Normal View History

2011-01-16 23:02:40 +01:00
#include "Network.h"
2011-01-12 22:05:22 +01:00
2011-01-19 23:24:58 +01:00
#include "btBulletDynamicsCommon.h"
2011-01-16 23:02:40 +01:00
#include <iostream>
#include <cmath>
2011-01-24 23:52:12 +01:00
#include <typeinfo>
2011-01-25 18:43:49 +01:00
#include <istream>
2011-01-12 23:41:33 +01:00
2018-11-26 13:15:41 +01:00
namespace gln {
2011-01-18 21:52:05 +01:00
Network::Network(Game *game) :
2018-11-26 13:15:41 +01:00
game(game), client_peer(0) {
2011-01-16 23:02:40 +01:00
}
2011-01-12 22:05:22 +01:00
2011-01-16 23:02:40 +01:00
void Network::initialize(const std::string &hostname) {
2011-01-12 22:05:22 +01:00
if (enet_initialize() != 0) {
2011-01-14 14:59:18 +01:00
throw "failed to initialize enet";
2011-01-12 22:05:22 +01:00
}
2011-01-16 23:02:40 +01:00
if (hostname.empty()) {
std::cout << "[Network] Start server." << std::endl;
2011-01-12 22:05:22 +01:00
ENetAddress address;
address.host = ENET_HOST_ANY;
address.port = 1234;
host = enet_host_create(&address, 32, 2, 0, 0);
if (host == NULL) {
2011-01-16 23:02:40 +01:00
throw "An error occurred while trying to create an ENet server host.";
2011-01-12 22:05:22 +01:00
}
} else {
2011-01-16 23:02:40 +01:00
std::cout << "[Network] Start client." << std::endl;
2011-01-12 22:05:22 +01:00
ENetAddress address;
ENetEvent event;
host = enet_host_create(NULL, 1, 2, 57600 / 8, 14400 / 8);
if (host == NULL) {
2011-01-16 23:02:40 +01:00
throw "An error occurred while trying to create an ENet client host.";
2011-01-12 22:05:22 +01:00
}
2011-01-25 23:21:03 +01:00
std::cout << "[Network] connect to " << hostname << std::endl;
2011-01-16 23:02:40 +01:00
enet_address_set_host(&address, hostname.c_str());
2011-01-12 22:05:22 +01:00
address.port = 1234;
/* Initiate the connection, allocating the two channels 0 and 1. */
client_peer = enet_host_connect(host, &address, 2, 0);
if (client_peer == NULL) {
2011-01-16 23:02:40 +01:00
throw "No available peers for initiating an ENet connection.";
2011-01-12 22:05:22 +01:00
}
/* Wait up to 5 seconds for the connection attempt to succeed. */
2018-11-26 13:15:41 +01:00
if (enet_host_service(host, &event, 2000) > 0
&& event.type == ENET_EVENT_TYPE_CONNECT) {
2011-01-25 23:21:03 +01:00
std::cout << "[Network] connected" << std::endl;
2011-01-12 22:05:22 +01:00
} else {
/* Either the 5 seconds are up or a disconnect event was */
/* received. Reset the peer in the event the 5 seconds */
/* had run out without any significant event. */
enet_peer_reset(client_peer);
2011-01-16 23:02:40 +01:00
throw "[Network] connection failed.";
2011-01-12 22:05:22 +01:00
}
}
}
2011-01-16 23:02:40 +01:00
void Network::shutdown() {
2011-01-12 22:05:22 +01:00
enet_host_destroy(host);
enet_deinitialize();
}
2011-01-16 23:02:40 +01:00
void Network::sendGameUpdates() {
2011-01-12 22:05:22 +01:00
size_t i;
for (i = 0; i < GAME_PLAYER_COUNT; i++) {
if (game->player[i].status == 0)
continue;
player_update_message_t msg;
2011-01-24 23:52:12 +01:00
//msg.msg_id = MESSAGE_PLAYER_UPDATE;
2011-01-12 22:05:22 +01:00
msg.player_id = game->player[i].id;
btVector3 v = game->player[i].body->getLinearVelocity();
btVector3 p = game->player[i].body->getWorldTransform().getOrigin();
msg.x = p.x();
msg.y = p.y();
msg.z = p.z();
msg.vx = v.x();
msg.vy = v.y();
msg.vz = v.z();
2011-01-12 22:05:22 +01:00
msg.points = game->player[i].points;
2011-01-25 18:43:49 +01:00
YAML::Emitter em;
em << msg;
ENetPacket * packet = enet_packet_create((uint8_t *) em.c_str(),
em.size(), 0);
2011-01-12 22:05:22 +01:00
enet_host_broadcast(host, 0, packet);
}
2011-01-18 22:50:24 +01:00
for (i = 0; i < game->teams.size(); i++) {
2011-01-12 22:05:22 +01:00
team_update_message_t msg;
2011-01-18 22:50:24 +01:00
msg.team_id = game->teams[i].id;
msg.points = game->teams[i].points;
msg.wins = game->teams[i].wins;
2011-01-25 18:43:49 +01:00
YAML::Emitter em;
em << msg;
ENetPacket * packet = enet_packet_create((uint8_t *) em.c_str(),
em.size(), 0);
2011-01-12 22:05:22 +01:00
enet_host_broadcast(host, 0, packet);
}
2011-01-12 23:41:33 +01:00
enet_host_flush(host);
2011-01-12 22:05:22 +01:00
}
2011-01-24 23:52:12 +01:00
template<class T>
T *_message_cast(void *p, size_t l) {
if (sizeof(T) != l) {
std::cout << sizeof(T) << std::endl;
std::cout << l << std::endl;
throw(typeid(T).name());
}
return (T *) p;
}
2011-01-25 18:43:49 +01:00
#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
2018-11-26 13:15:41 +01:00
EMIT_START(player_spawn_message_t) EMIT_FIELD(team_id)EMIT_FIELD(player_id)
2011-01-25 18:43:49 +01:00
EMIT_END()
PARSE_START(player_spawn_message_t) PARSE_FIELD(team_id)
PARSE_FIELD(player_id)PARSE_END()
// player_kill_message_t
2018-11-26 13:15:41 +01:00
EMIT_START(player_kill_message_t) EMIT_FIELD(player_id)
2011-01-25 18:43:49 +01:00
EMIT_END()
PARSE_START(player_kill_message_t) PARSE_FIELD(player_id)PARSE_END()
// accept_message_t
2018-11-26 13:15:41 +01:00
EMIT_START(accept_message_t) EMIT_FIELD(player_id)
2011-01-25 18:43:49 +01:00
EMIT_END()
PARSE_START(accept_message_t) PARSE_FIELD(player_id)PARSE_END()
// player_update_message_t
2018-11-26 13:15:41 +01:00
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)
2011-01-25 18:43:49 +01:00
EMIT_END()
2018-11-26 13:15:41 +01:00
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)
2011-01-25 18:43:49 +01:00
PARSE_FIELD(points)PARSE_END()
// player_accelerate_message_t
2018-11-26 13:15:41 +01:00
EMIT_START(player_accelerate_message_t) EMIT_FIELD(player_id)EMIT_FIELD(x)EMIT_FIELD(
y)EMIT_FIELD(z)EMIT_FIELD(rx)EMIT_FIELD( ry)EMIT_FIELD(rz)
2011-01-25 18:43:49 +01:00
EMIT_END()
2018-11-26 13:15:41 +01:00
PARSE_START(player_accelerate_message_t) PARSE_FIELD(player_id)PARSE_FIELD(x)PARSE_FIELD(
y)PARSE_FIELD(rx)PARSE_FIELD( ry)
PARSE_FIELD(rz)PARSE_END()
2011-01-25 18:43:49 +01:00
// bomb_drop_meesage_t
2018-11-26 13:15:41 +01:00
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)
2011-01-25 18:43:49 +01:00
EMIT_END()
2018-11-26 13:15:41 +01:00
PARSE_START(bomb_drop_meesage_t) PARSE_FIELD(x)PARSE_FIELD(y)PARSE_FIELD(z)PARSE_FIELD(
vx)PARSE_FIELD(vy)PARSE_FIELD(vz)
2011-01-25 18:43:49 +01:00
PARSE_FIELD(ttl)PARSE_END()
// bomb_update_meesage_t
2018-11-26 13:15:41 +01:00
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)
2011-01-25 18:43:49 +01:00
EMIT_END()
2018-11-26 13:15:41 +01:00
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)
2011-01-25 18:43:49 +01:00
PARSE_FIELD(ttl)PARSE_END()
// point_update_mesage_t
2018-11-26 13:15:41 +01:00
EMIT_START(point_update_mesage_t) EMIT_FIELD(point_index)EMIT_FIELD(x)EMIT_FIELD(
y)EMIT_FIELD(z)EMIT_FIELD(status)
2011-01-25 18:43:49 +01:00
EMIT_END()
2018-11-26 13:15:41 +01:00
PARSE_START(point_update_mesage_t) PARSE_FIELD(point_index)PARSE_FIELD(x)PARSE_FIELD(
y)PARSE_FIELD(z)
2011-01-25 18:43:49 +01:00
PARSE_FIELD(status)PARSE_END()
// team_update_message_t
2018-11-26 13:15:41 +01:00
EMIT_START(team_update_message_t) EMIT_FIELD(team_id)EMIT_FIELD(points)EMIT_FIELD(
wins)
2011-01-25 18:43:49 +01:00
EMIT_END()
2018-11-26 13:15:41 +01:00
PARSE_START(team_update_message_t) PARSE_FIELD(team_id)PARSE_FIELD(points)
2011-01-25 18:43:49 +01:00
PARSE_FIELD(wins)PARSE_END()
2011-01-16 23:02:40 +01:00
void Network::dispatch(enet_uint8 *data, size_t length) {
2011-01-25 18:43:49 +01:00
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 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 m;
doc >> m;
player_t *player = game->getPlayer(m.player_id);
player->status = 0;
player->team = 0;
break;
}
case MESSAGE_ACCEPT: {
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 m;
doc >> m;
player_t *player = game->getPlayer(m.player_id);
#if 1
2011-01-25 18:43:49 +01:00
btVector3 v = player->body->getLinearVelocity();
btVector3 p = player->body->getWorldTransform().getOrigin();
btVector3 v1(m.vx, m.vy, m.vz);
btVector3 p1(m.x, m.y, m.z);
player->body->activate(true);
player->body->setLinearVelocity(v1);
2011-01-25 22:46:36 +01:00
btVector3 dp = p1 - p;
if (dp.length2() > 5.)
player->body->getWorldTransform().setOrigin(p1);
else
2011-01-25 22:53:57 +01:00
player->body->getWorldTransform().setOrigin(p + dp / 10.);
2011-01-25 18:43:49 +01:00
//player->body->applyCentralForce((v1 -v)/10.0);
#endif
#if 0
2011-01-25 18:43:49 +01:00
std::cout << " v:" << um->vx << " " << um->vy << " " << um -> vz
<< std::endl;
std::cout << " p:" << um->x << " " << um->y << " " << um -> z
<< std::endl;
double threshold = 0.1;
double dx = um->x - player->x;
double dy = um->y - player->y;
double dz = um->z - player->z;
std::cout << " d:" << dx << " " << dy << " " << dz << std::endl;
if (fabs(dx) < threshold) {
player->x += 0.1 * dx;
} else {
player->x = um->x;
2011-01-12 22:05:22 +01:00
}
2011-01-25 18:43:49 +01:00
if (fabs(dy) < threshold) {
player->y += 0.1 * dy;
} else {
player->y = um->y;
}
2011-01-25 18:43:49 +01:00
if (fabs(dz) < threshold) {
player->z += 0.1 * dz;
} else {
player->z = um->z;
}
2011-01-25 18:43:49 +01:00
player->vx = um->vx;
player->vy = um->vy;
player->vz = um->vz;
#endif
player->points = m.points;
break;
}
case MESSAGE_POINT_UPDATE: {
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 m;
doc >> m;
player_t *player = game->getPlayer(m.player_id);
2011-02-08 23:06:43 +01:00
btVector3 v = player->body->getLinearVelocity();
v += btVector3(m.x, m.y, m.z);
player->body->setLinearVelocity(v);
2011-01-25 18:43:49 +01:00
player->body->activate(true);
2011-02-08 23:06:43 +01:00
//player->body->applyCentralImpulse(btVector3(m.x, m.y, m.z));
2011-01-25 18:43:49 +01:00
break;
}
case MESSAGE_BOMB_DROP: {
bomb_drop_meesage_t m;
doc >> m;
bomb_t *bomb = game->spawn_bomb();
if (bomb == NULL)
2011-01-12 22:05:22 +01:00
return;
2011-01-25 18:43:49 +01:00
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);
enet_host_broadcast(host, 0, packet);
2011-01-12 22:05:22 +01:00
}
2011-01-25 18:43:49 +01:00
break;
}
case MESSAGE_TEAM_UPDATE: {
team_update_message_t m;
doc >> m;
Team *team = game->getTeam(m.team_id);
if (team == NULL)
2011-01-12 22:05:22 +01:00
return;
2011-01-25 18:43:49 +01:00
team->points = m.points;
team->wins = m.wins;
break;
}
2011-01-12 22:05:22 +01:00
};
}
2011-01-16 23:02:40 +01:00
void Network::service(uint32_t timeout) {
2011-01-12 22:05:22 +01:00
ENetEvent event;
2011-01-16 23:02:40 +01:00
if (host == 0)
throw "not connected";
2011-01-12 22:05:22 +01:00
/* Wait up to 1000 milliseconds for an event. */
2011-01-16 23:02:40 +01:00
while (enet_host_service(host, &event, timeout) > 0) {
2011-01-12 22:05:22 +01:00
switch (event.type) {
2011-01-16 23:02:40 +01:00
case ENET_EVENT_TYPE_CONNECT: {
2011-01-25 23:21:03 +01:00
char hostname[256];
enet_address_get_host(&event.peer->address, hostname,
sizeof(hostname));
std::cout << "[Network] A new client connected from " << hostname
<< std::endl;
2011-01-16 23:02:40 +01:00
// bring new client up to date
size_t i;
for (i = 0; i < GAME_PLAYER_COUNT; i++) {
if (game->player[i].status == 0)
2011-01-25 18:43:49 +01:00
continue;
2011-01-12 22:05:22 +01:00
// send player spawn message
player_spawn_message_t spwan_msg;
2011-01-16 23:02:40 +01:00
spwan_msg.team_id = game->player[i].team->id;
spwan_msg.player_id = game->player[i].id;
2011-01-25 18:43:49 +01:00
YAML::Emitter em;
em << spwan_msg;
2018-11-26 13:15:41 +01:00
ENetPacket * packet = enet_packet_create((uint8_t *) em.c_str(),
em.size(), ENET_PACKET_FLAG_RELIABLE);
2011-01-16 23:02:40 +01:00
enet_peer_send(event.peer, 0, packet);
}
2011-01-12 22:05:22 +01:00
2011-01-16 23:02:40 +01:00
for (i = 0; i < GAME_POINT_COUNT; i++) {
if (game->point[i].status == 0)
2011-01-25 18:43:49 +01:00
continue;
2011-01-16 23:02:40 +01:00
point_update_mesage_t msg;
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;
2011-01-25 18:43:49 +01:00
YAML::Emitter em;
em << msg;
2018-11-26 13:15:41 +01:00
ENetPacket * packet = enet_packet_create((uint8_t *) em.c_str(),
em.size(), ENET_PACKET_FLAG_RELIABLE);
2011-01-12 22:05:22 +01:00
enet_peer_send(event.peer, 0, packet);
}
2011-01-16 23:02:40 +01:00
2011-01-18 22:50:24 +01:00
Team *team = game->team_with_least_players();
2011-01-23 20:11:24 +01:00
player_t *player = game->spawnPlayer(team);
2011-01-16 23:02:40 +01:00
event.peer->data = player;
2011-01-25 18:43:49 +01:00
{
// 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;
2011-01-16 23:02:40 +01:00
2018-11-26 13:15:41 +01:00
ENetPacket * packet = enet_packet_create((uint8_t *) em.c_str(),
em.size(), ENET_PACKET_FLAG_RELIABLE);
2011-01-25 18:43:49 +01:00
enet_host_broadcast(host, 0, packet);
}
{
// send team and player
accept_message_t msg;
msg.player_id = player->id;
YAML::Emitter em;
em << msg;
2018-11-26 13:15:41 +01:00
ENetPacket * packet = enet_packet_create((uint8_t *) em.c_str(),
em.size(), ENET_PACKET_FLAG_RELIABLE);
2011-01-25 18:43:49 +01:00
enet_peer_send(event.peer, 0, packet);
}
2011-01-16 23:02:40 +01:00
// send state
}
2011-01-12 22:05:22 +01:00
break;
case ENET_EVENT_TYPE_RECEIVE: {
2011-01-16 23:02:40 +01:00
dispatch(event.packet->data, event.packet->dataLength);
2011-01-12 22:05:22 +01:00
enet_packet_destroy(event.packet);
break;
}
2011-01-24 23:52:12 +01:00
case ENET_EVENT_TYPE_DISCONNECT: {
2011-01-16 23:02:40 +01:00
/* Reset the peer's client information. */
player_t *player = (player_t *) event.peer->data;
player->status = 0;
player->team = 0;
2011-01-24 23:52:12 +01:00
#if 0
2011-01-16 23:02:40 +01:00
// send player spawn message
player_kill_message_t msg;
msg.msg_id = MESSAGE_PLAYER_KILL;
msg.player_id = player->id;
ENetPacket * packet = enet_packet_create(&msg, sizeof(msg),
ENET_PACKET_FLAG_RELIABLE);
enet_host_broadcast(host, 0, packet);
2011-01-24 23:52:12 +01:00
#endif
2011-01-16 23:02:40 +01:00
}
2011-01-12 22:05:22 +01:00
event.peer->data = NULL;
break;
}
}
}
2011-01-24 23:52:12 +01:00
//void Network::sendMessage(message_t &m) {
// if (client_peer) {
// msgpack::sbuffer b;
// msgpack::packer<msgpack::sbuffer> packer(b);
// m.pack(packer);
// ENetPacket * packet = enet_packet_create(b.data(), b.size(),
// ENET_PACKET_FLAG_RELIABLE);
// enet_peer_send(client_peer, 0, packet);
// } else {
// // dispatch
// }
//}
void Network::send(const YAML::Emitter &em) {
sendMessage((uint8_t *) em.c_str(), em.size());
}
2011-01-16 23:02:40 +01:00
void Network::sendMessage(uint8_t *data, size_t length) {
2011-01-12 22:05:22 +01:00
if (client_peer) {
ENetPacket * packet = enet_packet_create(data, length,
ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(client_peer, 0, packet);
} else {
2011-01-16 23:02:40 +01:00
dispatch(data, length);
2011-01-12 22:05:22 +01:00
}
}
2018-11-26 13:15:41 +01:00
} // namespace grln