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 $
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
14
* Copyright (C) 2001 GGZ Dev Team.
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.
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.
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
32
# include <config.h> /* Site-specific config */
40
#include "ggzmod-ggz.h"
45
/* Private IO reading functions (ggz side) */
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);
56
/* Functions for sending IO messages */
57
int _io_ggz_send_launch(int fd)
59
if (ggz_write_int(fd, MSG_GAME_LAUNCH) < 0)
65
int _io_ggz_send_server(int fd, const char *host, unsigned int port,
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)
78
int _io_ggz_send_server_fd(int fd, int server_fd)
80
if (ggz_write_int(fd, MSG_GAME_SERVER_FD) < 0
81
|| ggz_write_fd(fd, server_fd) < 0)
88
int _io_ggz_send_state(int fd, GGZModState state)
90
if (ggz_write_int(fd, MSG_GAME_STATE) < 0
91
|| ggz_write_char(fd, state) < 0)
97
int _io_ggz_send_player(int fd, const char *name, int is_spectator, int seat_num)
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)
108
int _io_ggz_send_seat(int fd, GGZSeat *seat)
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)
119
int _io_ggz_send_spectator_seat(int fd, GGZSpectatorSeat *seat)
121
const char * name = seat->name ? seat->name : "";
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)
131
int _io_ggz_send_msg_chat(int fd, const char *player, const char *chat_msg)
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)
140
int _io_ggz_send_stats(int fd, int num_players, GGZStat *player_stats,
141
int num_spectators, GGZStat *spectator_stats)
146
if (ggz_write_int(fd, MSG_GAME_STATS) < 0)
149
for (i = 0; i < num_players + num_spectators; i++) {
150
if (i >= num_players) {
151
stat = &spectator_stats[i - num_players];
153
stat = &player_stats[i];
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) {
173
int _io_ggz_send_msg_info(int fd, int num, GGZList *infos)
177
if (ggz_write_int(fd, MSG_GAME_INFO) < 0
178
|| ggz_write_int(fd, num) < 0)
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)
194
/* Functions for reading messages */
195
int _io_ggz_read_data(GGZMod *ggzmod)
199
if (ggz_read_int(ggzmod->fd, &op) < 0)
202
if (ggzmod->type == GGZMOD_GGZ) {
203
switch ((TableToControl)op) {
205
return _io_ggz_read_msg_state(ggzmod);
207
return _io_ggz_read_req_stand(ggzmod);
209
return _io_ggz_read_req_sit(ggzmod);
211
return _io_ggz_read_req_boot(ggzmod);
213
return _io_ggz_read_req_bot(ggzmod);
215
return _io_ggz_read_req_open(ggzmod);
217
return _io_ggz_read_req_chat(ggzmod);
219
return _io_ggz_read_req_info(ggzmod);
227
static int _io_ggz_read_msg_state(GGZMod *ggzmod)
231
if (ggz_read_char(ggzmod->fd, &state) < 0)
234
_ggzmod_ggz_handle_state(ggzmod, state);
239
static int _io_ggz_read_req_stand(GGZMod *ggzmod)
241
_ggzmod_ggz_handle_stand_request(ggzmod);
245
static int _io_ggz_read_req_sit(GGZMod *ggzmod)
249
if (ggz_read_int(ggzmod->fd, &seat_num) < 0)
252
_ggzmod_ggz_handle_sit_request(ggzmod, seat_num);
256
static int _io_ggz_read_req_boot(GGZMod *ggzmod)
260
if (ggz_read_string_alloc(ggzmod->fd, &name) < 0)
262
_ggzmod_ggz_handle_boot_request(ggzmod, name);
267
static int _io_ggz_read_req_bot(GGZMod *ggzmod)
270
if (ggz_read_int(ggzmod->fd, &seat_num) < 0)
272
_ggzmod_ggz_handle_bot_request(ggzmod, seat_num);
276
static int _io_ggz_read_req_open(GGZMod *ggzmod)
279
if (ggz_read_int(ggzmod->fd, &seat_num) < 0)
281
_ggzmod_ggz_handle_open_request(ggzmod, seat_num);
286
static int _io_ggz_read_req_chat(GGZMod *ggzmod)
290
if (ggz_read_string_alloc(ggzmod->fd, &chat_msg) < 0)
292
_ggzmod_ggz_handle_chat_request(ggzmod, chat_msg);
297
static int _io_ggz_read_req_info(GGZMod *ggzmod)
301
if (ggz_read_int(ggzmod->fd, &seat_num) < 0)
303
_ggzmod_ggz_handle_info_request(ggzmod, seat_num);