| [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 |     { | 
|---|
 | 41 |         boost::system::error_code ec; | 
|---|
 | 42 |         io.poll(ec); | 
|---|
| [3107] | 43 |         if (ec) | 
|---|
 | 44 |         { | 
|---|
| [2997] | 45 |             /* TODO: Error */ | 
|---|
 | 46 |         } | 
|---|
 | 47 |     } | 
|---|
 | 48 |  | 
|---|
| [2889] | 49 |     void GGZClient::initGGZ() | 
|---|
 | 50 |     { | 
|---|
| [3107] | 51 |         COUT(3) << "Initializing GGZ\n"; | 
|---|
| [2889] | 52 |         ggzmod = ggzmod_new(GGZMOD_GAME); | 
|---|
 | 53 |         ggzmod_set_handler(ggzmod, GGZMOD_EVENT_SERVER, | 
|---|
 | 54 |                 &orxonox::GGZClient::handleGGZModServer); | 
|---|
| [3107] | 55 |         if (ggzmod_connect(ggzmod) < 0) | 
|---|
 | 56 |         { | 
|---|
| [2889] | 57 |             /* TODO: Error */ | 
|---|
 | 58 |         } | 
|---|
| [2997] | 59 |         int fd = ggzmod_get_fd(ggzmod); | 
|---|
| [3107] | 60 |         if (fd < 0) | 
|---|
 | 61 |         { | 
|---|
| [2889] | 62 |             /* TODO: Error */ | 
|---|
 | 63 |         } | 
|---|
| [2997] | 64 |         /* TODO: Error */ | 
|---|
| [3000] | 65 |         ggzSocket.assign(boost::asio::local::stream_protocol(), fd); | 
|---|
 | 66 |         ggzSocket.async_read_some(boost::asio::null_buffers(), boost::bind(&handleGGZ, boost::asio::placeholders::error)); | 
|---|
| [2889] | 67 |     } | 
|---|
 | 68 |  | 
|---|
 | 69 |     void GGZClient::deinitGGZ() | 
|---|
 | 70 |     { | 
|---|
 | 71 |         ggzmod_disconnect(ggzmod); | 
|---|
 | 72 |         ggzmod_free(ggzmod); | 
|---|
 | 73 |     } | 
|---|
 | 74 |  | 
|---|
 | 75 |     /* Got data from game server */ | 
|---|
| [3000] | 76 |     void GGZClient::handleGame(const boost::system::error_code& /*e*/) | 
|---|
| [2889] | 77 |     { | 
|---|
| [2997] | 78 |         /* TODO: read from gameSocket */ | 
|---|
| [2889] | 79 |     } | 
|---|
 | 80 |  | 
|---|
 | 81 |     /* Got data from GGZ */ | 
|---|
| [2997] | 82 |     void GGZClient::handleGGZ(const boost::system::error_code& /*e*/) | 
|---|
| [2889] | 83 |     { | 
|---|
 | 84 |         ggzmod_dispatch(getInstance().ggzmod); | 
|---|
 | 85 |     } | 
|---|
 | 86 |  | 
|---|
 | 87 |     /* Connection to game server established */ | 
|---|
 | 88 |     void GGZClient::handleGGZModServer(GGZMod * ggzmod, GGZModEvent e, | 
|---|
 | 89 |             const void *data) | 
|---|
 | 90 |     { | 
|---|
| [3107] | 91 |         COUT(3) << "GGZ Initialized\n"; | 
|---|
| [2889] | 92 |         ggzmod_set_state(ggzmod, GGZMOD_STATE_PLAYING); | 
|---|
| [3000] | 93 |         getInstance().gameSocket.assign(boost::asio::local::stream_protocol(), *(int*)data); | 
|---|
 | 94 |         getInstance().gameSocket.async_read_some(boost::asio::null_buffers(), boost::bind(&handleGame, boost::asio::placeholders::error)); | 
|---|
| [2889] | 95 |     } | 
|---|
 | 96 | } | 
|---|