Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ggz/src/orxonox/GGZClient.cc @ 3000

Last change on this file since 3000 was 3000, checked in by adrfried, 15 years ago

some ggz things

File size: 2.2 KB
Line 
1#include "GGZClient.h"
2
3#include <cassert>
4#include <boost/bind.hpp>
5
6namespace orxonox
7{
8    GGZClient* GGZClient::singletonRef_s = 0;
9
10    GGZClient::GGZClient()
11        : ggzSocket(io), gameSocket(io)
12    {
13        assert(singletonRef_s == 0);
14        singletonRef_s = this;
15
16        initGGZ();
17    }
18
19    GGZClient::~GGZClient()
20    {
21        deinitGGZ();
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
33    bool GGZClient::isActive()
34    {
35        return ggzmod_is_ggz_mode();
36    }
37
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
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        }
55        int fd = ggzmod_get_fd(ggzmod);
56        if (fd < 0) {
57            /* TODO: Error */
58        }
59        /* TODO: Error */
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));
62    }
63
64    void GGZClient::deinitGGZ()
65    {
66        ggzmod_disconnect(ggzmod);
67        ggzmod_free(ggzmod);
68    }
69
70    /* Got data from game server */
71    void GGZClient::handleGame(const boost::system::error_code& /*e*/)
72    {
73        /* TODO: read from gameSocket */
74    }
75
76    /* Got data from GGZ */
77    void GGZClient::handleGGZ(const boost::system::error_code& /*e*/)
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);
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));
89    }
90}
Note: See TracBrowser for help on using the repository browser.