author | Tero Marttila <terom@fixme.fi> |
Tue, 27 Jan 2009 03:02:02 +0200 | |
changeset 441 | f769fab21a6c |
parent 425 | 567144562978 |
permissions | -rw-r--r-- |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
1 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
2 |
#include <cassert> |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
3 |
#include <cstring> |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
4 |
|
186 | 5 |
#include "Packet.hh" |
400
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
391
diff
changeset
|
6 |
#include "Platform.hh" |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
7 |
|
425
567144562978
more compile-fixing, this is GCC 4.3 with a 64-bit libc and it's damn stupid
Tero Marttila <terom@fixme.fi>
parents:
400
diff
changeset
|
8 |
#include <stdlib.h> |
391
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
9 |
#include <zlib.h> |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
10 |
|
365 | 11 |
// XXX: assumes that sizeof(float32) == sizeof(int32); |
12 |
||
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
13 |
/* |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
14 |
* NetworkPacketInput |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
15 |
*/ |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
16 |
template <typename T> T NetworkPacketInput::read_val (void) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
17 |
T val; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
18 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
19 |
// read |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
20 |
read(&val, sizeof(T)); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
21 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
22 |
// return |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
23 |
return val; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
24 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
25 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
26 |
uint32_t NetworkPacketInput::read_uint32 (void) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
27 |
return ntohl(read_val<uint32_t>()); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
28 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
29 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
30 |
uint16_t NetworkPacketInput::read_uint16 (void) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
31 |
return ntohs(read_val<uint16_t>()); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
32 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
33 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
34 |
uint8_t NetworkPacketInput::read_uint8 (void) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
35 |
return read_val<uint8_t>(); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
36 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
37 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
38 |
int32_t NetworkPacketInput::read_int32 (void) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
39 |
return ntohl(read_val<int32_t>()); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
40 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
41 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
42 |
int16_t NetworkPacketInput::read_int16 (void) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
43 |
return ntohs(read_val<int16_t>()); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
44 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
45 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
46 |
int8_t NetworkPacketInput::read_int8 (void) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
47 |
return read_val<int8_t>(); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
48 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
49 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
50 |
float NetworkPacketInput::read_float32 (void) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
51 |
int32_t ival = read_int32(); |
365 | 52 |
float fval; |
53 |
||
54 |
assert(sizeof(ival) == sizeof(fval)); |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
55 |
|
365 | 56 |
memcpy(&fval, &ival, sizeof(float)); |
57 |
||
58 |
return fval; |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
59 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
60 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
61 |
Vector NetworkPacketInput::read_vector (void) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
62 |
float fx = read_float32(); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
63 |
float fy = read_float32(); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
64 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
65 |
return Vector(fx, fy); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
66 |
} |
391
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
67 |
|
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
68 |
size_t NetworkPacketInput::read_uncompressed (void *buf, size_t len) { |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
69 |
// given len |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
70 |
unsigned long inflate_size = len; |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
71 |
|
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
72 |
// read deflate size |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
73 |
unsigned long deflate_size = read_uint32(); |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
74 |
|
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
75 |
// pointer to packet data |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
76 |
const void *deflate_ptr = read_ptr(deflate_size); |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
77 |
|
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
78 |
// uncompress the rest of the packet data |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
79 |
if (uncompress((uint8_t *) buf, &inflate_size, (const uint8_t *) deflate_ptr, deflate_size) != Z_OK) |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
80 |
throw Error("uncompress"); |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
81 |
|
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
82 |
// return inflated size |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
83 |
return inflate_size; |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
84 |
} |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
85 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
86 |
/* |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
87 |
* NetworkPacketOutput |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
88 |
*/ |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
89 |
template <typename T> void NetworkPacketOutput::write_val (const T &val) { |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
90 |
// write |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
91 |
write(&val, sizeof(T)); |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
92 |
} |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
93 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
94 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
95 |
void NetworkPacketOutput::write_uint32 (uint32_t val) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
96 |
write_val<uint32_t>(htonl(val)); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
97 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
98 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
99 |
void NetworkPacketOutput::write_uint16 (uint16_t val) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
100 |
write_val<uint16_t>(htons(val)); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
101 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
102 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
103 |
void NetworkPacketOutput::write_uint8 (uint8_t val) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
104 |
write_val<uint8_t>(val); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
105 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
106 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
107 |
void NetworkPacketOutput::write_int32 (int32_t val) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
108 |
write_val<int32_t>(htonl(val)); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
109 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
110 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
111 |
void NetworkPacketOutput::write_int16 (int16_t val) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
112 |
write_val<int16_t>(htons(val)); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
113 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
114 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
115 |
void NetworkPacketOutput::write_int8 (int8_t val) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
116 |
write_val<int8_t>(val); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
117 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
118 |
|
365 | 119 |
void NetworkPacketOutput::write_float32 (float fval) { |
120 |
int32_t ival; |
|
121 |
||
122 |
assert(sizeof(ival) == sizeof(fval)); |
|
123 |
||
124 |
memcpy(&ival, &fval, sizeof(int32_t)); |
|
125 |
||
126 |
write_int32(ival); |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
127 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
128 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
129 |
void NetworkPacketOutput::write_vector (const Vector &vec) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
130 |
write_float32(vec.x); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
131 |
write_float32(vec.y); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
132 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
133 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
134 |
void NetworkPacketOutput::write_packet (const NetworkPacketBuffer &pkt) { |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
135 |
// just write() it |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
136 |
write(pkt.get_buf(), pkt.get_data_size()); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
137 |
} |
391
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
138 |
|
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
139 |
void NetworkPacketOutput::write_compressed (const void *ptr, uint32_t len) { |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
140 |
// full compression buffer size, including header |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
141 |
unsigned long deflate_buf_size = write_compressed_size(len); |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
142 |
|
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
143 |
// size of compressed data area after header |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
144 |
unsigned long deflate_size = deflate_buf_size - sizeof(uint32_t); |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
145 |
|
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
146 |
// packet buffer |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
147 |
void *deflate_buf = write_ptr(deflate_buf_size); |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
148 |
|
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
149 |
// compress |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
150 |
if (compress((uint8_t *) deflate_buf + sizeof(uint32_t), &deflate_size, (const uint8_t *) ptr, len) != Z_OK) |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
151 |
throw Error("compress failed"); |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
152 |
|
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
153 |
// write data size |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
154 |
*((uint32_t *) deflate_buf) = htonl(deflate_size); |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
155 |
|
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
156 |
// adjust final area |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
157 |
write_ptr_adjust(deflate_buf_size, sizeof(uint32_t) + deflate_size); |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
158 |
} |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
159 |
|
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
160 |
size_t NetworkPacketOutput::write_compressed_size (uint32_t len) { |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
161 |
return sizeof(uint32_t) + compressBound(len); |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
162 |
} |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
163 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
164 |
/* |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
165 |
* NetworkPacketBuffer |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
166 |
*/ |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
167 |
NetworkPacketBuffer::NetworkPacketBuffer (char *buf_ptr, size_t buf_size, size_t data_size) : |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
168 |
buf_ptr(buf_ptr), buf_size(buf_size), data_size(data_size), offset(0) { |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
169 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
170 |
// nothing |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
171 |
if (buf_ptr == NULL) |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
172 |
throw NetworkPacketError("buf_ptr may not be NULL"); |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
173 |
} |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
174 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
175 |
void NetworkPacketBuffer::check_write_size (size_t item_size) { |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
176 |
if (offset + item_size > buf_size) |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
177 |
throw NetworkPacketError("not enough space to write"); |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
178 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
179 |
} |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
180 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
181 |
void NetworkPacketBuffer::check_read_size (size_t item_size) { |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
182 |
if (offset + item_size > data_size) |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
183 |
throw NetworkPacketError("not enough data to read"); |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
184 |
} |
387
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
185 |
|
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
186 |
const void* NetworkPacketBuffer::read_ptr (size_t len) { |
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
187 |
// check buffer overflow |
391
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
188 |
check_read_size(len); |
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
189 |
|
387
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
190 |
// pointer value... |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
191 |
const void *ptr = buf_ptr + offset; |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
192 |
|
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
193 |
// update offset |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
194 |
offset += len; |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
195 |
|
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
196 |
// return pointer |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
197 |
return ptr; |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
198 |
} |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
199 |
|
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
200 |
void* NetworkPacketBuffer::write_ptr (size_t len) { |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
201 |
// check buffer overflow |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
202 |
check_write_size(len); |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
203 |
|
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
204 |
// pointer value... |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
205 |
void *ptr = buf_ptr + offset; |
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
206 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
207 |
// update offset and size |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
208 |
offset += len; |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
209 |
data_size += len; |
387
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
210 |
|
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
211 |
// return pointer |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
212 |
return ptr; |
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
213 |
} |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
214 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
215 |
void NetworkPacketBuffer::read (void *ptr, size_t len) { |
387
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
216 |
// get input pointer... |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
217 |
const void *input_ptr = read_ptr(len); |
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
218 |
|
387
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
219 |
// copy value out |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
220 |
memcpy(ptr, input_ptr, len); |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
221 |
} |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
222 |
|
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
223 |
void NetworkPacketBuffer::write (const void *ptr, size_t len) { |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
224 |
// get output pointer... |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
225 |
void *output_ptr = write_ptr(len); |
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
226 |
|
387
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
227 |
// copy value in |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
228 |
memcpy(output_ptr, ptr, len); |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
229 |
} |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
230 |
|
391
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
231 |
void NetworkPacketBuffer::write_ptr_adjust (size_t buf_len, size_t data_len) { |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
232 |
assert(data_len <= buf_len); |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
233 |
|
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
234 |
// adjust offset backwards |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
235 |
offset -= (buf_len - data_len); |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
236 |
data_size -= (buf_len - data_len); |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
237 |
} |
59c2499fe7bb
add compressed read/write NetworkPacket API and move zlib stuff out of NetworkServer/Client
Tero Marttila <terom@fixme.fi>
parents:
387
diff
changeset
|
238 |
|
387
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
239 |
size_t NetworkPacketBuffer::tell_remaining (void) { |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
240 |
// number of bytes in total - number of bytes read |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
365
diff
changeset
|
241 |
return data_size - offset; |
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
242 |
} |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
243 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
244 |
/* |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
245 |
* NetworkPacket |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
246 |
*/ |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
247 |
NetworkPacket::NetworkPacket (void) : |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
248 |
NetworkPacketBuffer(_buf, NETWORK_PACKET_SIZE, 0) |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
249 |
{ |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
250 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
251 |
} |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
252 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
253 |
/* |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
254 |
* BigNetworkPacket |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
255 |
*/ |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
256 |
BigNetworkPacket::BigNetworkPacket (size_t size) : |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
257 |
NetworkPacketBuffer((char *) malloc(size), size, 0) |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
258 |
{ |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
259 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
260 |
} |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
261 |
|
202 | 262 |
BigNetworkPacket::~BigNetworkPacket (void) { |
263 |
free(buf_ptr); |
|
264 |
} |
|
265 |