#ifndef SERVERWS_HXX
#define SERVERWS_HXX
//
#include <windows.h>
#include <windowsx.h>
#include <wtypes.h>
#include <winuser.h>
#include <winsock2.h>
#include <vector>
const int MAX_PLAYERS = 256;
const int MAX_MESGSIZE = 4096;
const int NAMELEN = 30;
const int ERRLEN = 100;
struct PLAYER_LIST_ELEM
{
SOCKET sock;
sockaddr_in info;
char name[NAMELEN];
bool connected;
};
typedef struct WS_STRING_MSG
{
DWORD dwType;
DWORD dwSize;
bool bcast;
int src;
int dest;
char szMsg[1];
} *WS_STRING_MSG_PTR;
enum { WS_MSG_NAME, WS_MSG_CHATSTRING, WS_MSG_GAMEPLAY, WS_MSG_DROP };
class ServerWS
{
private:
bool isHost;
bool isConnected;
SOCKET sock;
char player_name[NAMELEN];
PLAYER_LIST_ELEM player_list[MAX_PLAYERS];
int max_players;
int players;
int myIndex;
char lasterr[ERRLEN];
WS_STRING_MSG_PTR theMesg;
public:
ServerWS();
~ServerWS();
bool CheckIP(char *ip, char *reason) const;
bool Connect(HWND hwnd, UINT msg, char *ip, int port, char *play_name);
void Disconnect();
int GetIndex() const;
bool GetPlayerName(char *name, int index);
void HandleEvent(HWND hwnd, UINT msg, WPARAM socket, LPARAM event, char *mesg, int& type, int& from);
bool HostGame(HWND hwnd, UINT msg, int port, char *play_name, int maxplayers);
bool IsConnected() const;
bool IsConnected(int index) const;
bool IsHost() const;
void LastError(char *err) const;
int NumPlayers() const;
bool SendChatMesg(int indexTo, LPSTR lpstr, bool everyone);
bool SendGameMesg(int indexTo, LPSTR lpstr);
private:
bool ReceiveMesg(SOCKET s, char *mesg, int& type, int& from);
bool SendDropMesg(int who, int indexTo);
bool SendNameMesg(int indexTo, int indexFrom, LPSTR lpstr);
};
#endif