Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some more work on ggzclient

File size: 2.4 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(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        COUT(3) << "Tick\n";
42        boost::system::error_code ec;
43        io.poll(ec);
44        if (ec)
45        {
46            /* TODO: Error */
47        }
48    }
49
50    void GGZClient::initGGZ()
51    {
52        COUT(3) << "Initializing GGZ\n";
53        ggzmod = ggzmod_new(GGZMOD_GAME);
54        ggzmod_set_handler(ggzmod, GGZMOD_EVENT_SERVER,
55                &orxonox::GGZClient::handleGGZModServer);
56        if (ggzmod_connect(ggzmod) < 0)
57        {
58            /* TODO: Error */
59        }
60        int fd = ggzmod_get_fd(ggzmod);
61        if (fd < 0)
62        {
63            /* TODO: Error */
64        }
65        /* TODO: Error */
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));
68    }
69
70    void GGZClient::deinitGGZ()
71    {
72        ggzmod_disconnect(ggzmod);
73        ggzmod_free(ggzmod);
74    }
75
76    /* Got data from game server */
77    void GGZClient::handleGame(const boost::system::error_code& /*e*/)
78    {
79        /* TODO: read from gameSocket */
80    }
81
82    /* Got data from GGZ */
83    void GGZClient::handleGGZ(const boost::system::error_code& /*e*/)
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    {
92        COUT(3) << "GGZ Initialized\n";
93        ggzmod_set_state(ggzmod, GGZMOD_STATE_PLAYING);
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));
96    }
97}
Note: See TracBrowser for help on using the repository browser.