-
Notifications
You must be signed in to change notification settings - Fork 1
/
comm.c
183 lines (151 loc) · 3.32 KB
/
comm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Race for the Galaxy AI
*
* Copyright (C) 2009-2011 Keldon Jones
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "rftg.h"
#include "comm.h"
/*
* Copy a string from a message.
*
* We advance the message pointer past the end of the string.
*/
void get_string(char *dest, char **msg)
{
/* Copy until we reach the end of the string */
while (1)
{
/* Copy a byte */
*dest = **msg;
/* Advance message pointer */
(*msg)++;
/* Check for end of string */
if (!(*dest)) break;
/* Advance destination pointer */
dest++;
}
}
/*
* Copy an integer from a message.
*
* We advance the message pointer past the end of the integer.
*/
int get_integer(char **msg)
{
int x;
/* Copy four bytes from message */
memcpy(&x, *msg, 4);
/* Advance message pointer */
(*msg) += 4;
/* Convert integer to host form */
return ntohl(x);
}
/*
* Copy a string to a message.
*
* We advance the message pointer past the end of the string.
*/
void put_string(char *ptr, char **msg)
{
/* Copy bytes */
while (1)
{
/* Copy a byte */
**msg = *ptr;
/* Advance message pointer */
(*msg)++;
/* Check for end of string */
if (!(*ptr)) break;
/* Advance string pointer */
ptr++;
}
}
/*
* Copy an integer to a message.
*
* We advance the message pointer past the end of the integer.
*/
void put_integer(int x, char **msg)
{
/* Translate integer to network form */
x = htonl(x);
/* Copy 4 bytes to message */
memcpy(*msg, &x, 4);
/* Advance message pointer */
(*msg) += 4;
}
/*
* Start creating a message with the given type.
*
* We come back and fill in the size later.
*/
void start_msg(char **msg, int type)
{
/* Start with message type */
put_integer(type, msg);
/* Skip past size */
(*msg) += 4;
}
/*
* Finish a message by storing the size.
*/
void finish_msg(char *start, char *end)
{
int size;
/* Get size */
size = end - start;
/* Advance to size area of message */
start += 4;
/* Write size */
put_integer(size, &start);
}
/*
* Send a formatted message.
*/
void send_msgf(int fd, int type, char *fmt, ...)
{
char msg[1024], *ptr = msg;
va_list ap;
/* Start message */
start_msg(&ptr, type);
/* Start processing variable arguments */
va_start(ap, fmt);
/* Loop over format characters */
while (*fmt)
{
/* Switch on format type */
switch (*fmt++)
{
/* Integer */
case 'd':
/* Add integer to message */
put_integer(va_arg(ap, int), &ptr);
break;
/* String */
case 's':
/* Add string to message */
put_string(va_arg(ap, char *), &ptr);
break;
}
}
/* Stop processing arguments */
va_end(ap);
/* Finish message */
finish_msg(msg, ptr);
/* Send message */
send_msg(fd, msg);
}