| 1 | #include "GGZClient.h" | 
|---|
| 2 |  | 
|---|
| 3 | #include <cassert> | 
|---|
| 4 | #include <boost/bind.hpp> | 
|---|
| 5 |  | 
|---|
| 6 | namespace orxonox | 
|---|
| 7 | { | 
|---|
| 8 |     GGZClient* GGZClient::singletonRef_s = 0; | 
|---|
| 9 |  | 
|---|
| 10 |     GGZClient::GGZClient(GSClient * c) | 
|---|
| 11 |         : ggzSocket(io), gameSocket(io) | 
|---|
| 12 |     { | 
|---|
| 13 |         assert(singletonRef_s == 0); | 
|---|
| 14 |         singletonRef_s = this; | 
|---|
| 15 |  | 
|---|
| 16 |         client = c; | 
|---|
| 17 |         initGGZ(); | 
|---|
| 18 |     } | 
|---|
| 19 |  | 
|---|
| 20 |     GGZClient::~GGZClient() | 
|---|
| 21 |     { | 
|---|
| 22 |         deinitGGZ(); | 
|---|
| 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 |  | 
|---|
| 34 |     bool GGZClient::isActive() | 
|---|
| 35 |     { | 
|---|
| 36 |         return ggzmod_is_ggz_mode(); | 
|---|
| 37 |     } | 
|---|
| 38 |  | 
|---|
| 39 |     void GGZClient::tick(const float /*dt*/) | 
|---|
| 40 |     { | 
|---|
| 41 |         boost::system::error_code ec; | 
|---|
| 42 |         io.poll(ec); | 
|---|
| 43 |         if (ec) | 
|---|
| 44 |         { | 
|---|
| 45 |             /* TODO: Error */ | 
|---|
| 46 |         } | 
|---|
| 47 |     } | 
|---|
| 48 |  | 
|---|
| 49 |     void GGZClient::initGGZ() | 
|---|
| 50 |     { | 
|---|
| 51 |         COUT(3) << "Initializing GGZ\n"; | 
|---|
| 52 |         ggzmod = ggzmod_new(GGZMOD_GAME); | 
|---|
| 53 |         ggzmod_set_handler(ggzmod, GGZMOD_EVENT_SERVER, | 
|---|
| 54 |                 &orxonox::GGZClient::handleGGZModServer); | 
|---|
| 55 |         if (ggzmod_connect(ggzmod) < 0) | 
|---|
| 56 |         { | 
|---|
| 57 |             /* TODO: Error */ | 
|---|
| 58 |         } | 
|---|
| 59 |         int fd = ggzmod_get_fd(ggzmod); | 
|---|
| 60 |         if (fd < 0) | 
|---|
| 61 |         { | 
|---|
| 62 |             /* TODO: Error */ | 
|---|
| 63 |         } | 
|---|
| 64 |         /* TODO: Error */ | 
|---|
| 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)); | 
|---|
| 67 |     } | 
|---|
| 68 |  | 
|---|
| 69 |     void GGZClient::deinitGGZ() | 
|---|
| 70 |     { | 
|---|
| 71 |         ggzmod_disconnect(ggzmod); | 
|---|
| 72 |         ggzmod_free(ggzmod); | 
|---|
| 73 |     } | 
|---|
| 74 |  | 
|---|
| 75 |     /* Got data from game server */ | 
|---|
| 76 |     void GGZClient::handleGame(const boost::system::error_code& /*e*/) | 
|---|
| 77 |     { | 
|---|
| 78 |         /* TODO: read from gameSocket */ | 
|---|
| 79 |     } | 
|---|
| 80 |  | 
|---|
| 81 |     /* Got data from GGZ */ | 
|---|
| 82 |     void GGZClient::handleGGZ(const boost::system::error_code& /*e*/) | 
|---|
| 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 |     { | 
|---|
| 91 |         COUT(3) << "GGZ Initialized\n"; | 
|---|
| 92 |         ggzmod_set_state(ggzmod, GGZMOD_STATE_PLAYING); | 
|---|
| 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)); | 
|---|
| 95 |     } | 
|---|
| 96 | } | 
|---|