1 | #include "GGZClient.h" |
---|
2 | |
---|
3 | #include <cassert> |
---|
4 | |
---|
5 | namespace orxonox |
---|
6 | { |
---|
7 | GGZClient* GGZClient::singletonRef_s = 0; |
---|
8 | |
---|
9 | GGZClient::GGZClient() |
---|
10 | : ggzSocket(io), gameSocket(io) |
---|
11 | { |
---|
12 | assert(singletonRef_s == 0); |
---|
13 | singletonRef_s = this; |
---|
14 | |
---|
15 | boost::asio::ip::tcp::socket::non_blocking_io non_blocking_io(true); |
---|
16 | ggzSocket.io_control(non_blocking_io); |
---|
17 | gameSocket.io_control(non_blocking_io); |
---|
18 | |
---|
19 | active = ggzmod_is_ggz_mode(); |
---|
20 | if (active) { |
---|
21 | initGGZ(); |
---|
22 | } |
---|
23 | } |
---|
24 | |
---|
25 | GGZClient::~GGZClient() |
---|
26 | { |
---|
27 | if (active) { |
---|
28 | deinitGGZ(); |
---|
29 | } |
---|
30 | |
---|
31 | assert(singletonRef_s); |
---|
32 | singletonRef_s = 0; |
---|
33 | } |
---|
34 | |
---|
35 | GGZClient& GGZClient::getInstance() |
---|
36 | { |
---|
37 | assert(singletonRef_s); |
---|
38 | return *singletonRef_s; |
---|
39 | } |
---|
40 | |
---|
41 | void GGZClient::tick(const float /*dt*/) |
---|
42 | { |
---|
43 | boost::system::error_code ec; |
---|
44 | io.poll(ec); |
---|
45 | if (ec) { |
---|
46 | /* TODO: Error */ |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | void GGZClient::initGGZ() |
---|
51 | { |
---|
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 | /* TODO: Error */ |
---|
57 | } |
---|
58 | int fd = ggzmod_get_fd(ggzmod); |
---|
59 | if (fd < 0) { |
---|
60 | /* TODO: Error */ |
---|
61 | } |
---|
62 | /* TODO: Error */ |
---|
63 | ggzSocket.assign(boost::asio::ip::tcp::v4(), fd); |
---|
64 | ggzSocket.async_read_some(boost::asio::null_buffers(), handleGGZ); |
---|
65 | } |
---|
66 | |
---|
67 | void GGZClient::deinitGGZ() |
---|
68 | { |
---|
69 | ggzmod_disconnect(ggzmod); |
---|
70 | ggzmod_free(ggzmod); |
---|
71 | } |
---|
72 | |
---|
73 | /* Got data from game server */ |
---|
74 | void handleGame(const boost::system::error_code& /*e*/) |
---|
75 | { |
---|
76 | /* TODO: read from gameSocket */ |
---|
77 | } |
---|
78 | |
---|
79 | /* Got data from GGZ */ |
---|
80 | void GGZClient::handleGGZ(const boost::system::error_code& /*e*/) |
---|
81 | { |
---|
82 | ggzmod_dispatch(getInstance().ggzmod); |
---|
83 | } |
---|
84 | |
---|
85 | /* Connection to game server established */ |
---|
86 | void GGZClient::handleGGZModServer(GGZMod * ggzmod, GGZModEvent e, |
---|
87 | const void *data) |
---|
88 | { |
---|
89 | ggzmod_set_state(ggzmod, GGZMOD_STATE_PLAYING); |
---|
90 | gameSocket.assign(boost::asio::ip::tcp::v4(), *(int*)data); |
---|
91 | gameSocket.async_read_some(boost::asio::null_buffers(), handleGame); |
---|
92 | } |
---|
93 | } |
---|