~ubuntu-branches/ubuntu/trusty/ggz-client-libs/trusty

« back to all changes in this revision

Viewing changes to ggzmod-ggz/io-ggz.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Eisentraut, Josef Spillner, Peter Eisentraut
  • Date: 2006-09-09 13:37:14 UTC
  • mfrom: (2.1.2 edgy)
  • Revision ID: james.westby@ubuntu.com-20060909133714-q49a9kvjfkc0wcc3
Tags: 0.0.13-3
[ Josef Spillner ]
* Change ggzcore-bin dependency from ggzmod to recommends from ggzcore
  (closes: #384671).

[ Peter Eisentraut ]
* Make package dependencies binNMU-safe through use of ${binary:Version}
  (closes: #386126)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * File: io-ggz.c
 
3
 * Author: GGZ Dev Team
 
4
 * Project: ggzmod
 
5
 * Date: 10/14/01
 
6
 * Desc: Functions for reading/writing messages from/to game modules, GGZ side
 
7
 * $Id: io-ggz.c 7527 2005-09-17 19:31:46Z josef $
 
8
 *
 
9
 * This file contains the backend for the ggzmod library.  This
 
10
 * library facilitates the communication between the GGZ core client (ggz)
 
11
 * and game clients.  This file provides backend code that can be
 
12
 * used at both ends.
 
13
 *
 
14
 * Copyright (C) 2001 GGZ Dev Team.
 
15
 *
 
16
 * This program is free software; you can redistribute it and/or modify
 
17
 * it under the terms of the GNU General Public License as published by
 
18
 * the Free Software Foundation; either version 2 of the License, or
 
19
 * (at your option) any later version.
 
20
 *
 
21
 * This program is distributed in the hope that it will be useful,
 
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
24
 * GNU General Public License for more details.
 
25
 *
 
26
 * You should have received a copy of the GNU General Public License
 
27
 * along with this program; if not, write to the Free Software
 
28
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
29
 */
 
30
 
 
31
#ifdef HAVE_CONFIG_H
 
32
#  include <config.h>                   /* Site-specific config */
 
33
#endif
 
34
 
 
35
#include <assert.h>
 
36
#include <stdlib.h>
 
37
 
 
38
#include <ggz.h>
 
39
 
 
40
#include "ggzmod-ggz.h"
 
41
#include "mod-ggz.h"
 
42
#include "io-ggz.h"
 
43
#include "protocol.h"
 
44
 
 
45
/* Private IO reading functions (ggz side) */
 
46
 
 
47
static int _io_ggz_read_msg_state(GGZMod *ggzmod);
 
48
static int _io_ggz_read_req_stand(GGZMod *ggzmod);
 
49
static int _io_ggz_read_req_sit(GGZMod *ggzmod);
 
50
static int _io_ggz_read_req_boot(GGZMod *ggzmod);
 
51
static int _io_ggz_read_req_bot(GGZMod *ggzmod);
 
52
static int _io_ggz_read_req_open(GGZMod *ggzmod);
 
53
static int _io_ggz_read_req_chat(GGZMod *ggzmod);
 
54
static int _io_ggz_read_req_info(GGZMod *ggzmod);
 
55
 
 
56
/* Functions for sending IO messages */
 
57
int _io_ggz_send_launch(int fd)
 
58
{
 
59
        if (ggz_write_int(fd, MSG_GAME_LAUNCH) < 0)
 
60
                return -1;
 
61
 
 
62
        return 0;
 
63
}
 
64
 
 
65
int _io_ggz_send_server(int fd, const char *host, unsigned int port,
 
66
                    const char *handle)
 
67
{
 
68
        if (ggz_write_int(fd, MSG_GAME_SERVER) < 0
 
69
            || ggz_write_string(fd, host) < 0
 
70
            || ggz_write_int(fd, port) < 0
 
71
            || ggz_write_string(fd, handle) < 0)
 
72
                return -1;
 
73
        else
 
74
                return 0;
 
75
}
 
76
 
 
77
 
 
78
int _io_ggz_send_server_fd(int fd, int server_fd)
 
79
{
 
80
        if (ggz_write_int(fd, MSG_GAME_SERVER_FD) < 0
 
81
            || ggz_write_fd(fd, server_fd) < 0)
 
82
                return -1;
 
83
        else
 
84
                return 0;
 
85
}
 
86
 
 
87
 
 
88
int _io_ggz_send_state(int fd, GGZModState state)
 
89
{
 
90
        if (ggz_write_int(fd, MSG_GAME_STATE) < 0
 
91
            || ggz_write_char(fd, state) < 0)
 
92
                return -1;
 
93
        else
 
94
                return 0;
 
95
}
 
96
 
 
97
int _io_ggz_send_player(int fd, const char *name, int is_spectator, int seat_num)
 
98
{
 
99
        if (ggz_write_int(fd, MSG_GAME_PLAYER) < 0
 
100
            || ggz_write_string(fd, name ? name : "") < 0
 
101
            || ggz_write_int(fd, is_spectator) < 0
 
102
            || ggz_write_int(fd, seat_num) < 0)
 
103
                return -1;
 
104
 
 
105
        return 0;
 
106
}
 
107
 
 
108
int _io_ggz_send_seat(int fd, GGZSeat *seat)
 
109
{
 
110
        if (ggz_write_int(fd, MSG_GAME_SEAT) < 0
 
111
            || ggz_write_int(fd, seat->num) < 0
 
112
            || ggz_write_int(fd, seat->type) < 0
 
113
            || ggz_write_string(fd, seat->name ? seat->name : "") < 0)
 
114
                return -1;
 
115
 
 
116
        return 0;
 
117
}
 
118
 
 
119
int _io_ggz_send_spectator_seat(int fd, GGZSpectatorSeat *seat)
 
120
{
 
121
        const char * name = seat->name ? seat->name : "";
 
122
 
 
123
        if (ggz_write_int(fd, MSG_GAME_SPECTATOR_SEAT) < 0
 
124
            || ggz_write_int(fd, seat->num) < 0
 
125
            || ggz_write_string(fd, name) < 0)
 
126
                return -1;
 
127
 
 
128
        return 0;
 
129
}
 
130
 
 
131
int _io_ggz_send_msg_chat(int fd, const char *player, const char *chat_msg)
 
132
{
 
133
        if (ggz_write_int(fd, MSG_GAME_CHAT) < 0
 
134
            || ggz_write_string(fd, player) < 0
 
135
            || ggz_write_string(fd, chat_msg) < 0)
 
136
                return -1;
 
137
        return 0;
 
138
}
 
139
 
 
140
int _io_ggz_send_stats(int fd, int num_players, GGZStat *player_stats,
 
141
                   int num_spectators, GGZStat *spectator_stats)
 
142
{
 
143
        int i;
 
144
        GGZStat *stat;
 
145
 
 
146
        if (ggz_write_int(fd, MSG_GAME_STATS) < 0)
 
147
                return -1;
 
148
 
 
149
        for (i = 0; i < num_players + num_spectators; i++) {
 
150
                if (i >= num_players) {
 
151
                        stat = &spectator_stats[i - num_players];
 
152
                } else {
 
153
                        stat = &player_stats[i];
 
154
                }
 
155
 
 
156
                if (ggz_write_int(fd, stat->have_record) < 0
 
157
                    || ggz_write_int(fd, stat->have_rating) < 0
 
158
                    || ggz_write_int(fd, stat->have_ranking) < 0
 
159
                    || ggz_write_int(fd, stat->have_highscore) < 0
 
160
                    || ggz_write_int(fd, stat->wins) < 0
 
161
                    || ggz_write_int(fd, stat->losses) < 0
 
162
                    || ggz_write_int(fd, stat->ties) < 0
 
163
                    || ggz_write_int(fd, stat->forfeits) < 0
 
164
                    || ggz_write_int(fd, stat->rating) < 0
 
165
                    || ggz_write_int(fd, stat->ranking) < 0
 
166
                    || ggz_write_int(fd, stat->highscore) < 0) {
 
167
                        return -1;
 
168
                }
 
169
        }
 
170
        return 0;
 
171
}
 
172
 
 
173
int _io_ggz_send_msg_info(int fd, int num, GGZList *infos)
 
174
{
 
175
        GGZListEntry *entry;
 
176
 
 
177
        if (ggz_write_int(fd, MSG_GAME_INFO) < 0
 
178
            || ggz_write_int(fd, num) < 0)
 
179
                return -1;
 
180
 
 
181
        for (entry = ggz_list_head(infos); entry; entry = ggz_list_next(entry)) {
 
182
                GGZPlayerInfo *info = ggz_list_get_data(entry);
 
183
                if (ggz_write_int(fd, info->num) < 0
 
184
                    || ggz_write_string(fd, info->realname) < 0
 
185
                    || ggz_write_string(fd, info->photo) < 0
 
186
                    || ggz_write_string(fd, info->host) < 0)
 
187
                        return -1;
 
188
        }
 
189
 
 
190
        return 0;
 
191
}
 
192
 
 
193
 
 
194
/* Functions for reading messages */
 
195
int _io_ggz_read_data(GGZMod *ggzmod)
 
196
{
 
197
        int op;
 
198
 
 
199
        if (ggz_read_int(ggzmod->fd, &op) < 0)
 
200
                return -1;
 
201
 
 
202
        if (ggzmod->type == GGZMOD_GGZ) {
 
203
                switch ((TableToControl)op) {
 
204
                case MSG_GAME_STATE:
 
205
                        return _io_ggz_read_msg_state(ggzmod);
 
206
                case REQ_STAND:
 
207
                        return _io_ggz_read_req_stand(ggzmod);
 
208
                case REQ_SIT:
 
209
                        return _io_ggz_read_req_sit(ggzmod);
 
210
                case REQ_BOOT:
 
211
                        return _io_ggz_read_req_boot(ggzmod);
 
212
                case REQ_BOT:
 
213
                        return _io_ggz_read_req_bot(ggzmod);
 
214
                case REQ_OPEN:
 
215
                        return _io_ggz_read_req_open(ggzmod);
 
216
                case REQ_CHAT:
 
217
                        return _io_ggz_read_req_chat(ggzmod);
 
218
                case REQ_INFO:
 
219
                        return _io_ggz_read_req_info(ggzmod);
 
220
                }
 
221
        }
 
222
 
 
223
        return -2;
 
224
}
 
225
 
 
226
 
 
227
static int _io_ggz_read_msg_state(GGZMod *ggzmod)
 
228
{
 
229
        char state;
 
230
 
 
231
        if (ggz_read_char(ggzmod->fd, &state) < 0)
 
232
                return -1;
 
233
        else
 
234
                _ggzmod_ggz_handle_state(ggzmod, state);
 
235
        
 
236
        return 0;
 
237
}
 
238
 
 
239
static int _io_ggz_read_req_stand(GGZMod *ggzmod)
 
240
{
 
241
        _ggzmod_ggz_handle_stand_request(ggzmod);
 
242
        return 0;
 
243
}
 
244
 
 
245
static int _io_ggz_read_req_sit(GGZMod *ggzmod)
 
246
{
 
247
        int seat_num;
 
248
 
 
249
        if (ggz_read_int(ggzmod->fd, &seat_num) < 0)
 
250
                return -1;
 
251
 
 
252
        _ggzmod_ggz_handle_sit_request(ggzmod, seat_num);
 
253
        return 0;
 
254
}
 
255
 
 
256
static int _io_ggz_read_req_boot(GGZMod *ggzmod)
 
257
{
 
258
        char *name;
 
259
 
 
260
        if (ggz_read_string_alloc(ggzmod->fd, &name) < 0)
 
261
                return -1;
 
262
        _ggzmod_ggz_handle_boot_request(ggzmod, name);
 
263
        ggz_free(name);
 
264
        return 0;
 
265
}
 
266
 
 
267
static int _io_ggz_read_req_bot(GGZMod *ggzmod)
 
268
{
 
269
        int seat_num;
 
270
        if (ggz_read_int(ggzmod->fd, &seat_num) < 0)
 
271
                return -1;
 
272
        _ggzmod_ggz_handle_bot_request(ggzmod, seat_num);
 
273
        return 0;
 
274
}
 
275
 
 
276
static int _io_ggz_read_req_open(GGZMod *ggzmod)
 
277
{
 
278
        int seat_num;
 
279
        if (ggz_read_int(ggzmod->fd, &seat_num) < 0)
 
280
                return -1;
 
281
        _ggzmod_ggz_handle_open_request(ggzmod, seat_num);
 
282
        return 0;
 
283
}
 
284
 
 
285
 
 
286
static int _io_ggz_read_req_chat(GGZMod *ggzmod)
 
287
{
 
288
        char *chat_msg;
 
289
 
 
290
        if (ggz_read_string_alloc(ggzmod->fd, &chat_msg) < 0)
 
291
                return -1;
 
292
        _ggzmod_ggz_handle_chat_request(ggzmod, chat_msg);
 
293
        ggz_free(chat_msg);
 
294
        return 0;
 
295
}
 
296
 
 
297
static int _io_ggz_read_req_info(GGZMod *ggzmod)
 
298
{
 
299
        int seat_num;
 
300
 
 
301
        if (ggz_read_int(ggzmod->fd, &seat_num) < 0)
 
302
                return -1;
 
303
        _ggzmod_ggz_handle_info_request(ggzmod, seat_num);
 
304
        return 0;
 
305
}
 
306