#ifndef __I_NET_H__
#define __I_NET_H__

#include "doomtype.h"

#define	MAX_UDP_PACKET	8192

#define SERVERPORT  10666
#define CLIENTPORT  10667

#define PLAYER_FULLBRIGHTFRAME 70

#define CLIENT_CHALLENGE   5560020  // challenge
#define LAUNCHER_CHALLENGE 777123  // csdl challenge
#define SERVER_CHALLENGE   123456	// Server sending out info
#define CSVERSION 70

#define CM_X      1
#define CM_Y      2
#define CM_Z      4
#define CM_ANGLE  8
#define CM_MOMX   16
#define CM_MOMY   32
#define CM_MOMZ   64
#define CM_FRAME  128

extern int   localport;
extern byte	 net_message_buffer[MAX_UDP_PACKET];
extern int   msg_badread;

#define  S2C_CHALLENGE           10
#define  S2C_CONNECTION          11
#define  S2C_FULL                12

typedef enum
{
	svc_nop,
	svc_full,
	svc_specialsettings,
	svc_mobjangle,
	svc_missilespeedangle,
	svc_movemobj_xy,
	svc_actor_movedir,
	svc_actor_target,
	svc_changeswitchtexture,
	svc_activateline,
	svc_pickupscripts,
	svc_returnscripts,
	svc_giveitem,
	svc_lostitem,
	svc_givemedal,
	svc_playerteam,
	svc_changeplayerteam,
	svc_giveammo,
	svc_endmessage,
	svc_dmspawnplayer,
	svc_moveplayer,
	svc_updatelocalplayer,
	svc_updatemobjz,
	svc_updatelimits,
	svc_svgametic,
	svc_updateping,
	svc_spawnmobj,
	svc_disconnectclient,
	svc_loadmap,
	svc_consoleplayer,
	svc_mobjspeedangle,
	svc_explodemissile,
	svc_removemobj,
	svc_netcmd,
	svc_userinfo,
	svc_movemobj,
	svc_spawnplayer,
	svc_damageplayer,
	svc_killmobj,
	svc_firepistol,
	svc_fireshotgun,
	svc_firessg,
	svc_firechaingun,
	svc_firerailgun,
	svc_fireweapon,
	svc_sector,
	svc_print,
	svc_gameskill,
	svc_updatefrags,
	svc_updatescores,
	svc_updateplayerscores,
	svc_floorheight,
	svc_ceilingheight,
	svc_startsound,
	svc_reconnect,
	svc_exitlevel,
	svc_midprint,
	svc_touchspecial,
	svc_changeweapon,
	svc_startchat,
	svc_endchat,
	svc_dmflags,
	svc_dmflags2,
	svc_botskill,
	svc_deathmatch,
	svc_instagib,
	svc_scoremode,
	svc_teamplay,
	svc_teamgame,
	svc_soundorigin,
	svc_startsectorsequence,
	svc_stopsectorsequence,
	svc_pingclient,
	svc_disconnect,
	svc_missedpacket
} svnetmsg_t;

typedef enum
{
	clc_nothing,
	clc_endmessage,
	clc_disconnect,
	clc_startchat,
	clc_endchat,
	clc_move,
	clc_netcmd,
	clc_userinfo,
	clc_svgametic,
	clc_changeplayerteam,
	clc_suicide,
	clc_rate,
	clc_ack,
	clc_rcon,
	clc_ping,
	clc_rcon_password
} clnetmsg_t;

typedef struct
{
   byte    ip[4];
   unsigned short  port;
   unsigned short  pad;
} netadr_t;

extern  netadr_t  net_from;  // address of who sent the packet


typedef struct buf_s
{
   byte    *data;
   int      maxsize;
   int      cursize;
   bool     overflowed;  // set to true if the buffer size failed
} buf_t;

extern buf_t net_message;

void CloseNetwork (void);
void InitNetCommon(void);
void I_SetPort(netadr_t &addr, int port);
void I_DoSelect (void);

char *NET_AdrToString (netadr_t a);
bool NET_StringToAdr (char *s, netadr_t *a);
bool NET_CompareAdr (netadr_t a, netadr_t b);
int  NET_GetPacket (void);
void NET_SendPacket (int length, byte *data, netadr_t to);
netadr_t NET_GetLocalAddress (void);

void SZ_Clear (buf_t *buf);
void SZ_Write (buf_t *b, void *data, int length);
void SZ_Write (buf_t *b, byte *data, int startpos, int length);

void MSG_WriteByte (buf_t *b, int c);
void MSG_WriteShort (buf_t *b, int c);
void MSG_WriteLong (buf_t *b, int c);
void MSG_WriteString (buf_t *b, char *s);

int MSG_ReadByte (void);
int MSG_ReadShort (void);
int MSG_ReadLong (void);
char *MSG_ReadString (void);



#endif
