frodo-wii/Src/Network.h

213 lines
4.1 KiB
C
Raw Normal View History

2009-01-19 07:44:19 +01:00
#ifndef NETWORK_H
#define NETWORK_H
#include <arpa/inet.h>
#include <SDL.h>
#define NETWORK_UPDATE_SIZE (256 * 1024)
enum
{
HEADER = 0,
DISPLAY_UPDATE_RAW = 1,
DISPLAY_UPDATE_RLE = 2,
SOUND_UPDATE_RAW = 3,
SOUND_UPDATE_RLE = 4,
KEYBOARD_UPDATE = 5,
JOYSTICK_UPDATE = 6,
};
struct NetworkDisplayUpdate
{
Uint8 type; /* raw data or RLE */
Uint8 square;
Uint16 size;
Uint8 data[];
};
struct NetworkSoundUpdate
{
Uint8 type; /* raw data or RLE */
Uint8 dummy;
Uint16 size;
Uint8 data[];
};
struct NetworkJoystickUpdate
{
Uint8 type;
Uint8 which;
2009-01-24 16:48:43 +01:00
Uint16 size;
2009-01-19 07:44:19 +01:00
Uint8 data; /* New value */
};
struct NetworkUpdate
{
Uint8 type;
Uint8 dummy;
Uint16 size;
/* The rest is just data of some type */
Uint8 data[];
};
class Network
{
public:
2009-01-24 16:48:43 +01:00
Network();
~Network();
2009-01-19 07:44:19 +01:00
/** Encode part of a screen into @a dst
*
* @param dst the destination update structure
* @param screen the screen to encode
* @param square the square index of the screen to encode
*
* @return the size of the encoded message
*/
size_t EncodeDisplaySquare(struct NetworkDisplayUpdate *dst,
Uint8 *screen, int square);
2009-01-24 16:48:43 +01:00
size_t EncodeDisplay(Uint8 *master, Uint8 *remote);
2009-01-19 07:44:19 +01:00
/**
* Encode the @a buf sound buffer into @a dst
*
* @param dst the destination update structure
* @param buf the buffer to encode
* @param len the length of the in-buffer
*
* @return the size of the encoded message
*/
size_t EncodeSoundBuffer(struct NetworkSoundUpdate *dst,
Uint8 *buf, size_t len);
void EncodeJoystickUpdate(struct NetworkJoystickUpdate *dst,
Uint8 which, Uint8 v);
/**
* Decode a display update message onto @a screen
*
* @param screen the screen to draw to
* @param src the message to decode
*/
bool DecodeDisplayUpdate(Uint8 *screen, struct NetworkDisplayUpdate *src);
size_t DecodeSoundUpdate(struct NetworkSoundUpdate *src, char *buf);
bool DecodeUpdate(uint8 *screen);
2009-01-24 16:48:43 +01:00
void ResetNetworkUpdate(void);
2009-01-19 07:44:19 +01:00
2009-01-24 16:48:43 +01:00
bool SendUpdate(int sock);
2009-01-19 07:44:19 +01:00
2009-01-24 16:48:43 +01:00
bool ReceiveUpdate(int sock);
2009-01-19 07:44:19 +01:00
2009-01-24 16:48:43 +01:00
bool ReceiveUpdateBlock(int sock);
2009-01-19 07:44:19 +01:00
size_t GetBytesSent() {
return this->bytes_sent;
}
void ResetBytesSent() {
this->bytes_sent = 0;
}
2009-01-19 07:44:19 +01:00
private:
size_t EncodeDisplayRLE(struct NetworkDisplayUpdate *dst, Uint8 *screen,
int x, int y);
size_t EncodeDisplayRaw(struct NetworkDisplayUpdate *dst, Uint8 *screen,
int x, int y);
size_t EncodeSoundRLE(struct NetworkSoundUpdate *dst,
Uint8 *buffer, size_t len);
size_t EncodeSoundRaw(struct NetworkSoundUpdate *dst,
Uint8 *buffer, size_t len);
2009-01-24 16:48:43 +01:00
NetworkUpdate *IterateFirst(NetworkUpdate *p, unsigned int *cookie);
NetworkUpdate *IterateNext(NetworkUpdate *p, unsigned int *cookie);
void AddNetworkUpdate(struct NetworkUpdate *update);
2009-01-24 16:48:43 +01:00
/**
* Compare two display squares.
*
* @param a the first square (first byte)
* @param b the second square (first byte)
*
* @return true if they are equal
*/
bool CompareSquare(Uint8 *a, Uint8 *b);
2009-01-19 07:44:19 +01:00
bool DecodeDisplayRLE(Uint8 *screen, struct NetworkDisplayUpdate *src,
int x, int y);
bool DecodeDisplayRaw(Uint8 *screen, struct NetworkDisplayUpdate *src,
int x, int y);
bool ReceiveUpdate(NetworkUpdate *dst, int sock, struct timeval *tv);
void MarshalData(NetworkUpdate *ud);
void DeMarshalData(NetworkUpdate *ud);
2009-01-24 16:48:43 +01:00
NetworkUpdate *ud;
Uint8 *cur_ud;
size_t bytes_sent;
2009-01-19 07:44:19 +01:00
};
class NetworkClient : public Network
{
public:
NetworkClient(int sock);
2009-01-24 16:48:43 +01:00
~NetworkClient();
NetworkClient(const char *hostname, int port);
bool isConnected()
{
return this->sock >= 0;
}
bool SendUpdate()
{
return ((Network*)this)->SendUpdate(this->sock);
}
bool ReceiveUpdate()
{
return ((Network*)this)->ReceiveUpdate(this->sock);
}
bool ReceiveUpdateBlock()
{
return ((Network*)this)->ReceiveUpdateBlock(this->sock);
}
2009-01-19 07:44:19 +01:00
Uint8 *screen;
int joystick_port;
private:
int sock;
};
2009-01-24 16:48:43 +01:00
#define MAX_NETWORK_CLIENTS 8
2009-01-19 07:44:19 +01:00
class NetworkServer : public Network
{
public:
2009-01-24 16:48:43 +01:00
NetworkServer(int port);
2009-01-19 07:44:19 +01:00
2009-01-24 16:48:43 +01:00
bool CheckNewConnection();
2009-01-19 07:44:19 +01:00
2009-01-24 16:48:43 +01:00
NetworkClient *clients[MAX_NETWORK_CLIENTS];
2009-01-19 07:44:19 +01:00
int n_clients;
private:
2009-01-24 16:48:43 +01:00
void AddClient(int sock);
2009-01-19 07:44:19 +01:00
int listen_sock;
};
#endif /* NETWORK_H */