Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

FDWatcher and GGZClient Objects introduced

File size: 1.7 KB
Line 
1#include "GGZClient.h"
2
3#include <cassert>
4
5namespace orxonox
6{
7    GGZClient* GGZClient::singletonRef_s = 0;
8
9    GGZClient::GGZClient()
10    {
11        assert(singletonRef_s == 0);
12        singletonRef_s = this;
13
14        active = ggzmod_is_ggz_mode();
15        if (active) {
16            initGGZ();
17        }
18    }
19
20    GGZClient::~GGZClient()
21    {
22        if (active) {
23            deinitGGZ();
24        }
25
26        assert(singletonRef_s);
27        singletonRef_s = 0;
28    }
29
30    GGZClient& GGZClient::getInstance()
31    {
32        assert(singletonRef_s);
33        return *singletonRef_s;
34    }
35
36    void GGZClient::initGGZ()
37    {
38        ggzmod = ggzmod_new(GGZMOD_GAME);
39        ggzmod_set_handler(ggzmod, GGZMOD_EVENT_SERVER,
40                &orxonox::GGZClient::handleGGZModServer);
41        if (ggzmod_connect(ggzmod) < 0) {
42            /* TODO: Error */
43        }
44        int ggzSocket = ggzmod_get_fd(ggzmod);
45        if (ggzSocket < 0) {
46            /* TODO: Error */
47        }
48        sockets.add(ggzSocket, &orxonox::GGZClient::handleGGZ);
49    }
50
51    void GGZClient::deinitGGZ()
52    {
53        ggzmod_disconnect(ggzmod);
54        ggzmod_free(ggzmod);
55    }
56
57    /* Got data from game server */
58    void handleGame(int fd)
59    {
60        /* TODO: read from fd */
61    }
62
63    /* Got data from GGZ */
64    void GGZClient::handleGGZ(int fd)
65    {
66        ggzmod_dispatch(getInstance().ggzmod);
67    }
68
69    /* Connection to game server established */
70    void GGZClient::handleGGZModServer(GGZMod * ggzmod, GGZModEvent e,
71            const void *data)
72    {
73        ggzmod_set_state(ggzmod, GGZMOD_STATE_PLAYING);
74        int gameSocket = *(int*)data;
75        getInstance().sockets.add(gameSocket, &orxonox::GGZClient::handleGGZ);
76    }
77}
Note: See TracBrowser for help on using the repository browser.