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