author | celestar |
Mon, 15 Jan 2007 20:14:06 +0000 | |
branch | custombridgeheads |
changeset 5650 | aefc131bf5ce |
parent 5649 | 55c8267c933f |
permissions | -rw-r--r-- |
5642 | 1 |
/* $Id$ */ |
2 |
||
3 |
#ifdef ENABLE_NETWORK |
|
4 |
||
5 |
#include "../../stdafx.h" |
|
6 |
#include "../../macros.h" |
|
7 |
#include "../../string.h" |
|
5650
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
5649
diff
changeset
|
8 |
#include "../../helpers.hpp" |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
5649
diff
changeset
|
9 |
#include "../network_data.h" |
5642 | 10 |
|
11 |
#include "packet.h" |
|
12 |
||
13 |
/** |
|
14 |
* @file packet.h Basic functions to create, fill and read packets. |
|
15 |
*/ |
|
16 |
||
17 |
||
18 |
/* Do not want to include functions.h and all required headers */ |
|
19 |
extern void NORETURN CDECL error(const char *str, ...); |
|
20 |
||
21 |
||
22 |
/** |
|
23 |
* Create a packet for sending |
|
24 |
* @param type the of packet |
|
25 |
* @return the newly created packet |
|
26 |
*/ |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
27 |
Packet *NetworkSend_Init(const PacketType type) |
5642 | 28 |
{ |
5650
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
5649
diff
changeset
|
29 |
Packet *packet; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
5649
diff
changeset
|
30 |
MallocT(&packet, 1); |
5642 | 31 |
/* An error is inplace here, because it simply means we ran out of memory. */ |
32 |
if (packet == NULL) error("Failed to allocate Packet"); |
|
33 |
||
34 |
/* Skip the size so we can write that in before sending the packet */ |
|
35 |
packet->size = sizeof(packet->size); |
|
36 |
packet->buffer[packet->size++] = type; |
|
37 |
packet->pos = 0; |
|
38 |
||
39 |
return packet; |
|
40 |
} |
|
41 |
||
42 |
/** |
|
43 |
* Writes the packet size from the raw packet from packet->size |
|
44 |
* @param packet the packet to write the size of |
|
45 |
*/ |
|
46 |
void NetworkSend_FillPacketSize(Packet *packet) |
|
47 |
{ |
|
48 |
packet->buffer[0] = GB(packet->size, 0, 8); |
|
49 |
packet->buffer[1] = GB(packet->size, 8, 8); |
|
50 |
} |
|
51 |
||
52 |
/** |
|
53 |
* The next couple of functions make sure we can send |
|
54 |
* uint8, uint16, uint32 and uint64 endian-safe |
|
55 |
* over the network. The least significant bytes are |
|
56 |
* sent first. |
|
57 |
* |
|
58 |
* So 0x01234567 would be sent as 67 45 23 01. |
|
59 |
*/ |
|
60 |
||
61 |
void NetworkSend_uint8(Packet *packet, uint8 data) |
|
62 |
{ |
|
63 |
assert(packet->size < sizeof(packet->buffer) - sizeof(data)); |
|
64 |
packet->buffer[packet->size++] = data; |
|
65 |
} |
|
66 |
||
67 |
void NetworkSend_uint16(Packet *packet, uint16 data) |
|
68 |
{ |
|
69 |
assert(packet->size < sizeof(packet->buffer) - sizeof(data)); |
|
70 |
packet->buffer[packet->size++] = GB(data, 0, 8); |
|
71 |
packet->buffer[packet->size++] = GB(data, 8, 8); |
|
72 |
} |
|
73 |
||
74 |
void NetworkSend_uint32(Packet *packet, uint32 data) |
|
75 |
{ |
|
76 |
assert(packet->size < sizeof(packet->buffer) - sizeof(data)); |
|
77 |
packet->buffer[packet->size++] = GB(data, 0, 8); |
|
78 |
packet->buffer[packet->size++] = GB(data, 8, 8); |
|
79 |
packet->buffer[packet->size++] = GB(data, 16, 8); |
|
80 |
packet->buffer[packet->size++] = GB(data, 24, 8); |
|
81 |
} |
|
82 |
||
83 |
void NetworkSend_uint64(Packet *packet, uint64 data) |
|
84 |
{ |
|
85 |
assert(packet->size < sizeof(packet->buffer) - sizeof(data)); |
|
86 |
packet->buffer[packet->size++] = GB(data, 0, 8); |
|
87 |
packet->buffer[packet->size++] = GB(data, 8, 8); |
|
88 |
packet->buffer[packet->size++] = GB(data, 16, 8); |
|
89 |
packet->buffer[packet->size++] = GB(data, 24, 8); |
|
90 |
packet->buffer[packet->size++] = GB(data, 32, 8); |
|
91 |
packet->buffer[packet->size++] = GB(data, 40, 8); |
|
92 |
packet->buffer[packet->size++] = GB(data, 48, 8); |
|
93 |
packet->buffer[packet->size++] = GB(data, 56, 8); |
|
94 |
} |
|
95 |
||
96 |
/** |
|
97 |
* Sends a string over the network. It sends out |
|
98 |
* the string + '\0'. No size-byte or something. |
|
99 |
*/ |
|
100 |
void NetworkSend_string(Packet *packet, const char* data) |
|
101 |
{ |
|
102 |
assert(data != NULL); |
|
103 |
assert(packet->size < sizeof(packet->buffer) - strlen(data) - 1); |
|
104 |
while ((packet->buffer[packet->size++] = *data++) != '\0') {} |
|
105 |
} |
|
106 |
||
107 |
||
108 |
/** |
|
109 |
* Receiving commands |
|
110 |
* Again, the next couple of functions are endian-safe |
|
111 |
* see the comment before NetworkSend_uint8 for more info. |
|
112 |
*/ |
|
113 |
||
114 |
||
5650
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
5649
diff
changeset
|
115 |
extern NetworkRecvStatus CloseConnection(NetworkClientState *cs); |
5642 | 116 |
|
117 |
/** Is it safe to read from the packet, i.e. didn't we run over the buffer ? */ |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
118 |
static inline bool CanReadFromPacket(NetworkClientState *cs, const Packet *packet, const uint bytes_to_read) |
5642 | 119 |
{ |
120 |
/* Don't allow reading from a closed socket */ |
|
121 |
if (HasClientQuit(cs)) return false; |
|
122 |
||
123 |
/* Check if variable is within packet-size */ |
|
124 |
if (packet->pos + bytes_to_read > packet->size) { |
|
125 |
CloseConnection(cs); |
|
126 |
return false; |
|
127 |
} |
|
128 |
||
129 |
return true; |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* Reads the packet size from the raw packet and stores it in the packet->size |
|
134 |
* @param packet the packet to read the size of |
|
135 |
*/ |
|
136 |
void NetworkRecv_ReadPacketSize(Packet *packet) |
|
137 |
{ |
|
138 |
packet->size = (uint16)packet->buffer[0]; |
|
139 |
packet->size += (uint16)packet->buffer[1] << 8; |
|
140 |
} |
|
141 |
||
142 |
uint8 NetworkRecv_uint8(NetworkClientState *cs, Packet *packet) |
|
143 |
{ |
|
144 |
uint8 n; |
|
145 |
||
146 |
if (!CanReadFromPacket(cs, packet, sizeof(n))) return 0; |
|
147 |
||
148 |
n = packet->buffer[packet->pos++]; |
|
149 |
return n; |
|
150 |
} |
|
151 |
||
152 |
uint16 NetworkRecv_uint16(NetworkClientState *cs, Packet *packet) |
|
153 |
{ |
|
154 |
uint16 n; |
|
155 |
||
156 |
if (!CanReadFromPacket(cs, packet, sizeof(n))) return 0; |
|
157 |
||
158 |
n = (uint16)packet->buffer[packet->pos++]; |
|
159 |
n += (uint16)packet->buffer[packet->pos++] << 8; |
|
160 |
return n; |
|
161 |
} |
|
162 |
||
163 |
uint32 NetworkRecv_uint32(NetworkClientState *cs, Packet *packet) |
|
164 |
{ |
|
165 |
uint32 n; |
|
166 |
||
167 |
if (!CanReadFromPacket(cs, packet, sizeof(n))) return 0; |
|
168 |
||
169 |
n = (uint32)packet->buffer[packet->pos++]; |
|
170 |
n += (uint32)packet->buffer[packet->pos++] << 8; |
|
171 |
n += (uint32)packet->buffer[packet->pos++] << 16; |
|
172 |
n += (uint32)packet->buffer[packet->pos++] << 24; |
|
173 |
return n; |
|
174 |
} |
|
175 |
||
176 |
uint64 NetworkRecv_uint64(NetworkClientState *cs, Packet *packet) |
|
177 |
{ |
|
178 |
uint64 n; |
|
179 |
||
180 |
if (!CanReadFromPacket(cs, packet, sizeof(n))) return 0; |
|
181 |
||
182 |
n = (uint64)packet->buffer[packet->pos++]; |
|
183 |
n += (uint64)packet->buffer[packet->pos++] << 8; |
|
184 |
n += (uint64)packet->buffer[packet->pos++] << 16; |
|
185 |
n += (uint64)packet->buffer[packet->pos++] << 24; |
|
186 |
n += (uint64)packet->buffer[packet->pos++] << 32; |
|
187 |
n += (uint64)packet->buffer[packet->pos++] << 40; |
|
188 |
n += (uint64)packet->buffer[packet->pos++] << 48; |
|
189 |
n += (uint64)packet->buffer[packet->pos++] << 56; |
|
190 |
return n; |
|
191 |
} |
|
192 |
||
193 |
/** Reads a string till it finds a '\0' in the stream */ |
|
194 |
void NetworkRecv_string(NetworkClientState *cs, Packet *p, char *buffer, size_t size) |
|
195 |
{ |
|
196 |
PacketSize pos; |
|
197 |
char *bufp = buffer; |
|
198 |
||
199 |
/* Don't allow reading from a closed socket */ |
|
200 |
if (HasClientQuit(cs)) return; |
|
201 |
||
202 |
pos = p->pos; |
|
203 |
while (--size > 0 && pos < p->size && (*buffer++ = p->buffer[pos++]) != '\0') {} |
|
204 |
||
205 |
if (size == 0 || pos == p->size) { |
|
206 |
*buffer = '\0'; |
|
207 |
/* If size was sooner to zero then the string in the stream |
|
208 |
* skip till the \0, so than packet can be read out correctly for the rest */ |
|
209 |
while (pos < p->size && p->buffer[pos] != '\0') pos++; |
|
210 |
pos++; |
|
211 |
} |
|
212 |
p->pos = pos; |
|
213 |
||
214 |
str_validate(bufp); |
|
215 |
} |
|
216 |
||
217 |
#endif /* ENABLE_NETWORK */ |