Moved network data structures out of the union, added some future defines

This commit is contained in:
simon.kagstrom 2009-02-03 18:10:09 +00:00
parent 568e6eb5b7
commit 138854cc88
2 changed files with 74 additions and 48 deletions

View File

@ -99,6 +99,7 @@ size_t Network::EncodeSoundRLE(struct NetworkUpdate *dst,
size_t len = 0; size_t len = 0;
Uint8 volume = buffer[0]; Uint8 volume = buffer[0];
printf("Not implemented\n");
dst->type = SOUND_UPDATE_RLE; dst->type = SOUND_UPDATE_RLE;
for (unsigned int i = 0; i < buf_len; i++) for (unsigned int i = 0; i < buf_len; i++)
@ -129,6 +130,7 @@ size_t Network::EncodeSoundRLE(struct NetworkUpdate *dst,
size_t Network::EncodeSoundRaw(struct NetworkUpdate *dst, size_t Network::EncodeSoundRaw(struct NetworkUpdate *dst,
Uint8 *buffer, size_t len) Uint8 *buffer, size_t len)
{ {
printf("Not implemented\n");
dst->type = SOUND_UPDATE_RAW; dst->type = SOUND_UPDATE_RAW;
memcpy(dst->data, buffer, len); memcpy(dst->data, buffer, len);
@ -138,10 +140,11 @@ size_t Network::EncodeSoundRaw(struct NetworkUpdate *dst,
bool Network::DecodeDisplayDiff(Uint8 *screen, struct NetworkUpdate *src, bool Network::DecodeDisplayDiff(Uint8 *screen, struct NetworkUpdate *src,
int x_start, int y_start) int x_start, int y_start)
{ {
struct NetworkUpdateDisplay *dp = (struct NetworkUpdateDisplay *)src->data;
int p = 0; int p = 0;
int x = x_start; int x = x_start;
int y = y_start; int y = y_start;
int sz = src->size - sizeof(NetworkUpdate); int sz = src->size - sizeof(NetworkUpdate) - sizeof(NetworkUpdateDisplay);
/* Something is wrong if this is true... */ /* Something is wrong if this is true... */
if (sz % 2 != 0) if (sz % 2 != 0)
@ -149,8 +152,8 @@ bool Network::DecodeDisplayDiff(Uint8 *screen, struct NetworkUpdate *src,
while (p < sz) while (p < sz)
{ {
Uint8 len = src->data[p]; Uint8 len = dp->data[p];
Uint8 color = src->data[p+1]; Uint8 color = dp->data[p+1];
int x_diff = (x - x_start + len) % SQUARE_W; int x_diff = (x - x_start + len) % SQUARE_W;
int y_diff = (x - x_start + len) / SQUARE_W; int y_diff = (x - x_start + len) / SQUARE_W;
@ -166,10 +169,11 @@ bool Network::DecodeDisplayDiff(Uint8 *screen, struct NetworkUpdate *src,
bool Network::DecodeDisplayRLE(Uint8 *screen, struct NetworkUpdate *src, bool Network::DecodeDisplayRLE(Uint8 *screen, struct NetworkUpdate *src,
int x_start, int y_start) int x_start, int y_start)
{ {
struct NetworkUpdateDisplay *dp = (struct NetworkUpdateDisplay *)src->data;
int p = 0; int p = 0;
int x = x_start; int x = x_start;
int y = y_start; int y = y_start;
int sz = src->size - sizeof(NetworkUpdate); int sz = src->size - sizeof(NetworkUpdate) - sizeof(NetworkUpdateDisplay);
/* Something is wrong if this is true... */ /* Something is wrong if this is true... */
if (sz % 2 != 0) if (sz % 2 != 0)
@ -177,8 +181,8 @@ bool Network::DecodeDisplayRLE(Uint8 *screen, struct NetworkUpdate *src,
while (p < sz) while (p < sz)
{ {
Uint8 len = src->data[p]; Uint8 len = dp->data[p];
Uint8 color = src->data[p+1]; Uint8 color = dp->data[p+1];
while (len > 0) while (len > 0)
{ {
@ -200,13 +204,14 @@ bool Network::DecodeDisplayRLE(Uint8 *screen, struct NetworkUpdate *src,
bool Network::DecodeDisplayRaw(Uint8 *screen, struct NetworkUpdate *src, bool Network::DecodeDisplayRaw(Uint8 *screen, struct NetworkUpdate *src,
int x_start, int y_start) int x_start, int y_start)
{ {
struct NetworkUpdateDisplay *dp = (struct NetworkUpdateDisplay *)src->data;
const int raw_w = SQUARE_W / 2; const int raw_w = SQUARE_W / 2;
for (int y = y_start; y < y_start + SQUARE_H; y++) for (int y = y_start; y < y_start + SQUARE_H; y++)
{ {
for (int x = x_start; x < x_start + SQUARE_W; x += 2) for (int x = x_start; x < x_start + SQUARE_W; x += 2)
{ {
Uint8 v = src->data[(y - y_start) * raw_w + (x - x_start) / 2]; Uint8 v = dp->data[(y - y_start) * raw_w + (x - x_start) / 2];
Uint8 a = v >> 4; Uint8 a = v >> 4;
Uint8 b = v & 0xf; Uint8 b = v & 0xf;
@ -262,6 +267,7 @@ void Network::EncodeDisplay(Uint8 *master, Uint8 *remote)
size_t Network::EncodeDisplaySquare(struct NetworkUpdate *dst, size_t Network::EncodeDisplaySquare(struct NetworkUpdate *dst,
Uint8 *screen, Uint8 *remote, int square) Uint8 *screen, Uint8 *remote, int square)
{ {
struct NetworkUpdateDisplay *dp = (struct NetworkUpdateDisplay *)dst->data;
const int x_start = SQUARE_TO_X(square); const int x_start = SQUARE_TO_X(square);
const int y_start = SQUARE_TO_Y(square); const int y_start = SQUARE_TO_Y(square);
Uint8 rle_color = screen[ y_start * DISPLAY_X + x_start ]; Uint8 rle_color = screen[ y_start * DISPLAY_X + x_start ];
@ -322,23 +328,23 @@ size_t Network::EncodeDisplaySquare(struct NetworkUpdate *dst,
out = RAW_SIZE; out = RAW_SIZE;
if (diff_sz < rle_sz && diff_sz < RAW_SIZE) if (diff_sz < rle_sz && diff_sz < RAW_SIZE)
{ {
memcpy(dst->data, this->diff_buf, diff_sz); memcpy(dp->data, this->diff_buf, diff_sz);
type = DISPLAY_UPDATE_DIFF; type = DISPLAY_UPDATE_DIFF;
out = diff_sz; out = diff_sz;
} }
else if (rle_sz < RAW_SIZE) else if (rle_sz < RAW_SIZE)
{ {
memcpy(dst->data, this->rle_buf, rle_sz); memcpy(dp->data, this->rle_buf, rle_sz);
type = DISPLAY_UPDATE_RLE; type = DISPLAY_UPDATE_RLE;
out = rle_sz; out = rle_sz;
} }
else else
memcpy(dst->data, this->raw_buf, RAW_SIZE); memcpy(dp->data, this->raw_buf, RAW_SIZE);
/* Setup the structure */ /* Setup the structure */
dst->type = type; dp->square = square;
dst->u.display.square = square; dst = InitNetworkUpdate(dst, type,
dst->size = out + sizeof(struct NetworkUpdate); sizeof(struct NetworkUpdate) + sizeof(struct NetworkUpdateDisplay) + out);
this->square_updated[square] = out | (type << 16); this->square_updated[square] = out | (type << 16);
return dst->size; return dst->size;
@ -347,7 +353,8 @@ size_t Network::EncodeDisplaySquare(struct NetworkUpdate *dst,
bool Network::DecodeDisplayUpdate(Uint8 *screen, bool Network::DecodeDisplayUpdate(Uint8 *screen,
struct NetworkUpdate *src) struct NetworkUpdate *src)
{ {
int square = src->u.display.square; struct NetworkUpdateDisplay *dp = (struct NetworkUpdateDisplay *)src->data;
int square = dp->square;
const int square_x = SQUARE_TO_X(square); const int square_x = SQUARE_TO_X(square);
const int square_y = SQUARE_TO_Y(square); const int square_y = SQUARE_TO_Y(square);
@ -366,6 +373,7 @@ size_t Network::EncodeSoundBuffer(struct NetworkUpdate *dst, Uint8 *buf, size_t
{ {
size_t out; size_t out;
printf("Not implemented\n");
dst->size = 0; dst->size = 0;
/* Try encoding as RLE, but if it's too large, go for RAW */ /* Try encoding as RLE, but if it's too large, go for RAW */
out = this->EncodeSoundRLE(dst, buf, len); out = this->EncodeSoundRLE(dst, buf, len);
@ -400,12 +408,13 @@ void Network::PushSound(uint8 vol)
void Network::EncodeJoystickUpdate(Uint8 v) void Network::EncodeJoystickUpdate(Uint8 v)
{ {
struct NetworkUpdate *dst = this->cur_ud; struct NetworkUpdate *dst = this->cur_ud;
struct NetworkUpdateJoystick *j = (NetworkUpdateJoystick *)dst->data;
if (this->is_master || this->cur_joystick_data == v) if (this->is_master || this->cur_joystick_data == v)
return; return;
dst->type = JOYSTICK_UPDATE; dst = InitNetworkUpdate(dst, JOYSTICK_UPDATE,
dst->u.joystick.val = v; sizeof(NetworkUpdate) + sizeof(NetworkUpdateJoystick));
dst->size = sizeof(NetworkUpdate); j->val = v;
this->AddNetworkUpdate(dst); this->AddNetworkUpdate(dst);
this->cur_joystick_data = v; this->cur_joystick_data = v;
@ -432,9 +441,7 @@ void Network::ResetNetworkUpdate(void)
memset(this->ud, 0, NETWORK_UPDATE_SIZE); memset(this->ud, 0, NETWORK_UPDATE_SIZE);
memset(this->tmp_ud, 0, NETWORK_UPDATE_SIZE); memset(this->tmp_ud, 0, NETWORK_UPDATE_SIZE);
this->ud->type = STOP; this->cur_ud = InitNetworkUpdate(this->ud, STOP, sizeof(NetworkUpdate));
this->ud->size = sizeof(NetworkUpdate);
this->cur_ud = this->ud;
} }
void Network::DrawTransferredBlocks(SDL_Surface *screen) void Network::DrawTransferredBlocks(SDL_Surface *screen)
@ -502,9 +509,12 @@ bool Network::ReceiveUpdate(NetworkUpdate *dst, struct timeval *tv)
return false; return false;
pp = pp + sizeof(NetworkUpdate); pp = pp + sizeof(NetworkUpdate);
/* Drop if the magic is wrong */
if (ntohs(p->magic) != FRODO_NETWORK_MAGIC)
return false;
/* And the rest of the update */ /* And the rest of the update */
size_t sz = ntohs(p->size); size_t sz = ntohl(p->size);
if (sz > sizeof(NetworkUpdate)) if (sz > sizeof(NetworkUpdate))
{ {
size_t sz_diff = sz - sizeof(NetworkUpdate); size_t sz_diff = sz - sizeof(NetworkUpdate);
@ -515,7 +525,7 @@ bool Network::ReceiveUpdate(NetworkUpdate *dst, struct timeval *tv)
} }
if (this->DeMarshalData(p) == false) if (this->DeMarshalData(p) == false)
return false; return false;
} while ( !(p->type == STOP && p->u.stop.val == STOP) ); } while ( !(p->type == STOP) );
return true; return true;
} }
@ -523,7 +533,7 @@ bool Network::ReceiveUpdate(NetworkUpdate *dst, struct timeval *tv)
bool Network::SendUpdate() bool Network::SendUpdate()
{ {
NetworkUpdate *src = this->ud; NetworkUpdate *src = this->ud;
NetworkUpdate *stop = this->cur_ud; NetworkUpdate *stop = InitNetworkUpdate(this->cur_ud, STOP, sizeof(NetworkUpdate));
size_t sz; size_t sz;
/* Nothing to send, that's OK */ /* Nothing to send, that's OK */
@ -531,9 +541,6 @@ bool Network::SendUpdate()
return true; return true;
/* Add a stop at the end of the update */ /* Add a stop at the end of the update */
stop->type = STOP;
stop->u.stop.val = STOP;
stop->size = sizeof(NetworkUpdate);
this->AddNetworkUpdate(stop); this->AddNetworkUpdate(stop);
if (this->MarshalAllData(src) == false) if (this->MarshalAllData(src) == false)
@ -560,7 +567,6 @@ void Network::AddNetworkUpdate(NetworkUpdate *update)
bool Network::MarshalData(NetworkUpdate *p) bool Network::MarshalData(NetworkUpdate *p)
{ {
p->size = htons(p->size);
switch (p->type) switch (p->type)
{ {
case DISPLAY_UPDATE_RAW: case DISPLAY_UPDATE_RAW:
@ -579,6 +585,10 @@ bool Network::MarshalData(NetworkUpdate *p)
return false; return false;
} }
p->size = htonl(p->size);
p->magic = htons(p->magic);
p->type = htons(p->type);
return true; return true;
} }
@ -601,7 +611,12 @@ bool Network::MarshalAllData(NetworkUpdate *ud)
bool Network::DeMarshalData(NetworkUpdate *p) bool Network::DeMarshalData(NetworkUpdate *p)
{ {
p->size = ntohs(p->size); p->size = ntohl(p->size);
p->magic = ntohs(p->magic);
p->type = ntohs(p->type);
if (p->magic != FRODO_NETWORK_MAGIC)
return false;
switch (p->type) switch (p->type)
{ {
@ -644,7 +659,10 @@ bool Network::DecodeUpdate(uint8 *screen, uint8 *js)
case JOYSTICK_UPDATE: case JOYSTICK_UPDATE:
/* No joystick updates _from_ the master */ /* No joystick updates _from_ the master */
if (js && this->is_master) if (js && this->is_master)
*js = p->u.joystick.val; {
NetworkUpdateJoystick *j = (NetworkUpdateJoystick *)p->data;
*js = j->val;
}
break; break;
case DISCONNECT: case DISCONNECT:
out = false; out = false;
@ -685,12 +703,10 @@ void Network::RemovePeer(Network *peer)
void Network::Disconnect() void Network::Disconnect()
{ {
NetworkUpdate *disconnect= this->cur_ud; NetworkUpdate *disconnect = InitNetworkUpdate(this->cur_ud, DISCONNECT,
size_t sz; sizeof(NetworkUpdate));
/* Add a stop at the end of the update */ /* Add a stop at the end of the update */
disconnect->type = DISCONNECT;
disconnect->size = sizeof(NetworkUpdate);
this->AddNetworkUpdate(disconnect); this->AddNetworkUpdate(disconnect);
this->SendUpdate(); this->SendUpdate();

View File

@ -9,13 +9,16 @@
#include <SDL.h> #include <SDL.h>
#define MAX_NETWORK_PEERS 8 #define MAX_NETWORK_PEERS 8
#define FRODO_NETWORK_MAGIC 0x1976
#define NETWORK_UPDATE_SIZE (256 * 1024) #define NETWORK_UPDATE_SIZE (256 * 1024)
#define NETWORK_SOUND_BUF_SIZE 1024 #define NETWORK_SOUND_BUF_SIZE 1024
enum enum
{ {
STOP = 99, STOP = 99,
LIST_PEERS = 88,
DISCONNECT = 77, DISCONNECT = 77,
PEER_INFO = 66,
DISPLAY_UPDATE_RAW = 1, DISPLAY_UPDATE_RAW = 1,
DISPLAY_UPDATE_RLE = 2, DISPLAY_UPDATE_RLE = 2,
DISPLAY_UPDATE_DIFF= 3, DISPLAY_UPDATE_DIFF= 3,
@ -24,30 +27,37 @@ enum
KEYBOARD_UPDATE = 6, KEYBOARD_UPDATE = 6,
JOYSTICK_UPDATE = 7, JOYSTICK_UPDATE = 7,
ENTER_MENU = 8, ENTER_MENU = 8,
GET_PEER_INFO = 9,
}; };
struct NetworkUpdate struct NetworkUpdate
{ {
Uint16 size; uint16 magic; /* Should be 0x1976 */
Uint8 type; uint16 type; /* See above */
uint32 size;
union {
struct {
Uint8 square;
} display;
struct {
Uint8 val;
} joystick;
struct {
Uint8 val; /* Should be STOP */
} stop;
} u;
/* The rest is just data of some type */ /* The rest is just data of some type */
Uint8 data[]; Uint8 data[];
}; };
struct NetworkUpdateDisplay
{
Uint8 square;
Uint8 data[];
};
struct NetworkUpdateJoystick
{
Uint8 val;
};
static inline NetworkUpdate *InitNetworkUpdate(NetworkUpdate *ud, uint16 type, uint32 size)
{
ud->magic = FRODO_NETWORK_MAGIC;
ud->size = size;
ud->type = type;
return ud;
}
class Network class Network
{ {