#ifndef SERVERWS_HXX
#define SERVERWS_HXX
 
// Class ServerWS ///////////////////////////////////////////////////////
//
// Description:  This class is used for each independent TCP connection.
//               It supports a client/server setup, with support for
//               2 or more interconnected users.  Once connected, you can
//               send chat messages or game-control data.
//////////////////////////////////////////////////////////////////////////
 
#include <windows.h>
#include <windowsx.h>
#include <wtypes.h>
#include <winuser.h>
#include <winsock2.h>
#include <vector>
 
// Constants /////////////////////////////////////////////////////////////
 
const int MAX_PLAYERS = 256;
const int MAX_MESGSIZE = 4096;
const int NAMELEN = 30;
const int ERRLEN = 100;
 
// Structures ////////////////////////////////////////////////////////////
 
struct PLAYER_LIST_ELEM
  {
   SOCKET       sock;           // the id of player
   sockaddr_in  info;
   char         name[NAMELEN];  // name of player
   bool         connected;
};
 
typedef struct WS_STRING_MSG
{
    DWORD  dwType;     // type of message
    DWORD  dwSize;     // structure size
    bool   bcast;      // send to everyone?
    int    src;        // source index
    int    dest;       // destination index
    char   szMsg[1];   // variable length message
 
} *WS_STRING_MSG_PTR;
 
 
enum { WS_MSG_NAME, WS_MSG_CHATSTRING, WS_MSG_GAMEPLAY, WS_MSG_DROP };
 
 
 
// Class Definition //////////////////////////////////////////////////////
 
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

All material Copyright (c) 2001-2010 Hexar
Please direct all inquiries, love letters, and hate mail to .

Home •  Blog •  Photos •  Code •  Features •  Resume •  WinTetris