[2889] | 1 | #include "GGZClient.h" |
---|
| 2 | |
---|
| 3 | #include <cassert> |
---|
[3000] | 4 | #include <boost/bind.hpp> |
---|
[2889] | 5 | |
---|
| 6 | namespace orxonox |
---|
| 7 | { |
---|
| 8 | GGZClient* GGZClient::singletonRef_s = 0; |
---|
| 9 | |
---|
| 10 | GGZClient::GGZClient() |
---|
[2997] | 11 | : ggzSocket(io), gameSocket(io) |
---|
[2889] | 12 | { |
---|
| 13 | assert(singletonRef_s == 0); |
---|
| 14 | singletonRef_s = this; |
---|
| 15 | |
---|
[3000] | 16 | initGGZ(); |
---|
[2889] | 17 | } |
---|
| 18 | |
---|
| 19 | GGZClient::~GGZClient() |
---|
| 20 | { |
---|
[3000] | 21 | deinitGGZ(); |
---|
[2889] | 22 | |
---|
| 23 | assert(singletonRef_s); |
---|
| 24 | singletonRef_s = 0; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | GGZClient& GGZClient::getInstance() |
---|
| 28 | { |
---|
| 29 | assert(singletonRef_s); |
---|
| 30 | return *singletonRef_s; |
---|
| 31 | } |
---|
| 32 | |
---|
[3000] | 33 | bool GGZClient::isActive() |
---|
| 34 | { |
---|
| 35 | return ggzmod_is_ggz_mode(); |
---|
| 36 | } |
---|
| 37 | |
---|
[2997] | 38 | void GGZClient::tick(const float /*dt*/) |
---|
| 39 | { |
---|
| 40 | boost::system::error_code ec; |
---|
| 41 | io.poll(ec); |
---|
| 42 | if (ec) { |
---|
| 43 | /* TODO: Error */ |
---|
| 44 | } |
---|
| 45 | } |
---|
| 46 | |
---|
[2889] | 47 | void GGZClient::initGGZ() |
---|
| 48 | { |
---|
| 49 | ggzmod = ggzmod_new(GGZMOD_GAME); |
---|
| 50 | ggzmod_set_handler(ggzmod, GGZMOD_EVENT_SERVER, |
---|
| 51 | &orxonox::GGZClient::handleGGZModServer); |
---|
| 52 | if (ggzmod_connect(ggzmod) < 0) { |
---|
| 53 | /* TODO: Error */ |
---|
| 54 | } |
---|
[2997] | 55 | int fd = ggzmod_get_fd(ggzmod); |
---|
| 56 | if (fd < 0) { |
---|
[2889] | 57 | /* TODO: Error */ |
---|
| 58 | } |
---|
[2997] | 59 | /* TODO: Error */ |
---|
[3000] | 60 | ggzSocket.assign(boost::asio::local::stream_protocol(), fd); |
---|
| 61 | ggzSocket.async_read_some(boost::asio::null_buffers(), boost::bind(&handleGGZ, boost::asio::placeholders::error)); |
---|
[2889] | 62 | } |
---|
| 63 | |
---|
| 64 | void GGZClient::deinitGGZ() |
---|
| 65 | { |
---|
| 66 | ggzmod_disconnect(ggzmod); |
---|
| 67 | ggzmod_free(ggzmod); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /* Got data from game server */ |
---|
[3000] | 71 | void GGZClient::handleGame(const boost::system::error_code& /*e*/) |
---|
[2889] | 72 | { |
---|
[2997] | 73 | /* TODO: read from gameSocket */ |
---|
[2889] | 74 | } |
---|
| 75 | |
---|
| 76 | /* Got data from GGZ */ |
---|
[2997] | 77 | void GGZClient::handleGGZ(const boost::system::error_code& /*e*/) |
---|
[2889] | 78 | { |
---|
| 79 | ggzmod_dispatch(getInstance().ggzmod); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /* Connection to game server established */ |
---|
| 83 | void GGZClient::handleGGZModServer(GGZMod * ggzmod, GGZModEvent e, |
---|
| 84 | const void *data) |
---|
| 85 | { |
---|
| 86 | ggzmod_set_state(ggzmod, GGZMOD_STATE_PLAYING); |
---|
[3000] | 87 | getInstance().gameSocket.assign(boost::asio::local::stream_protocol(), *(int*)data); |
---|
| 88 | getInstance().gameSocket.async_read_some(boost::asio::null_buffers(), boost::bind(&handleGame, boost::asio::placeholders::error)); |
---|
[2889] | 89 | } |
---|
| 90 | } |
---|