Changeset edfd1d0 in network-game


Ignore:
Timestamp:
Dec 25, 2012, 6:27:14 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
4c202e0
Parents:
baaf6c8
Message:

Moved the Player class to the common directory, added a position to Player, added a new message type for sending player info, and made the server broadcast player positions everytime it receives and replies to a message

Files:
1 added
6 edited
2 moved

Legend:

Unmodified
Added
Removed
  • client/makefile

    rbaaf6c8 redfd1d0  
    33FLAGS = $(LIB_FLAGS)
    44COMMON_PATH = ../common
    5 DEPENDENCIES = Message.o chat.o GuiComponent.o Window.o Textbox.o Button.o
     5DEPENDENCIES = Message.o Player.o chat.o GuiComponent.o Window.o Textbox.o Button.o
    66
    77gameClient : Client/main.cpp $(DEPENDENCIES)
     
    99
    1010Message.o : $(COMMON_PATH)/Message.cpp
     11        $(CC) -c -o $@ $? $(FLAGS)
     12
     13Player.o : $(COMMON_PATH)/Player.cpp
    1114        $(CC) -c -o $@ $? $(FLAGS)
    1215
  • common/Common.h

    rbaaf6c8 redfd1d0  
    22#define _COMMON_H
    33
    4 void set_nonblock(int sock)
     4#include <fcntl.h>
     5#include <assert.h>
     6
     7void set_nonblock(int sock);
     8
     9typedef struct
    510{
    6    #ifdef WIN32
    7       unsigned long mode = 1;
    8       ioctlsocket(sock, FIONBIO, &mode);
    9    #else
    10       int flags;
    11       flags = fcntl(sock, F_GETFL,0);
    12       assert(flags != -1);
    13       fcntl(sock, F_SETFL, flags | O_NONBLOCK);
    14    #endif
    15 }
     11   int x;
     12   int y;
     13} PLAYER_POS;
    1614
    1715#endif
  • common/Message.h

    rbaaf6c8 redfd1d0  
    66#define MSG_TYPE_LOGOUT       3
    77#define MSG_TYPE_CHAT         4
     8#define MSG_TYPE_PLAYER       5
    89
    910typedef struct
  • common/Player.cpp

    rbaaf6c8 redfd1d0  
    1010   this->name = name;
    1111   this->password = password;
     12   this->pos.x = 200;
     13   this->pos.y = 200;
    1214
    1315   cout << "Created new player: " << this->name << endl;
     
    1820   this->name = name;
    1921   this->password = "";
     22   this->pos.x = 200;
     23   this->pos.y = 200;
    2024   this->addr = addr;
    2125
     
    3135   this->addr = addr;
    3236}
     37
     38void Player::clearSensitiveInfo()
     39{
     40   this->password = "";
     41   this->addr.sin_family = 0;
     42   this->addr.sin_port = 0;
     43   this->addr.sin_addr.s_addr = 0;
     44}
  • common/Player.h

    rbaaf6c8 redfd1d0  
    44#include <netinet/in.h>
    55#include <string>
     6
     7#include "Common.h"
    68
    79using namespace std;
     
    1416
    1517   void setAddr(sockaddr_in addr);
     18   void clearSensitiveInfo();
    1619
    1720   string name;
    1821   string password;
    1922   sockaddr_in addr;
     23   PLAYER_POS pos;
    2024};
    2125
  • server/DataAccess.h

    rbaaf6c8 redfd1d0  
    66#include <mysql/mysql.h>
    77
    8 #include "Player.h"
     8#include "../common/Player.h"
    99
    1010using namespace std;
  • server/makefile

    rbaaf6c8 redfd1d0  
    33FLAGS = $(LIB_FLAGS)
    44COMMON_PATH = ../common
    5 DEPENDENCIES = Message.o Player.o DataAccess.o
     5DEPENDENCIES = Common.o Message.o Player.o DataAccess.o
    66
    77server : server.cpp $(DEPENDENCIES)
    88        $(CC) -o $@ $+ $(FLAGS)
    99
     10Common.o : $(COMMON_PATH)/Common.cpp
     11        $(CC) -c -o $@ $?
     12
    1013Message.o : $(COMMON_PATH)/Message.cpp
     14        $(CC) -c -o $@ $?
     15
     16Player.o : $(COMMON_PATH)/Player.cpp
    1117        $(CC) -c -o $@ $?
    1218
  • server/server.cpp

    rbaaf6c8 redfd1d0  
    77#include <vector>
    88#include <algorithm>
    9 
    10 #include <fcntl.h>
    11 #include <assert.h>
     9#include <cstring>
    1210
    1311#include <sys/socket.h>
     
    1614#include <arpa/inet.h>
    1715
     16/*
    1817#include <openssl/bio.h>
    1918#include <openssl/ssl.h>
    2019#include <openssl/err.h>
     20*/
    2121
    2222#include "../common/Compiler.h"
     23#include "../common/Common.h"
    2324#include "../common/Message.h"
    24 #include "../common/Common.h"
    25 
    26 #include "Player.h"
     25#include "../common/Player.h"
     26
    2727#include "DataAccess.h"
    2828
     
    6363
    6464   return NULL;
     65}
     66
     67void broadcastPlayerPositions(vector<Player> &vec, int sock)
     68{
     69   vector<Player>::iterator it, it2;
     70   NETWORK_MSG serverMsg;
     71
     72   serverMsg.type = MSG_TYPE_PLAYER;   
     73
     74   for (it = vec.begin(); it != vec.end(); it++)
     75   {
     76      strncpy(serverMsg.buffer, (char*)&*it, sizeof(Player));
     77
     78      for (it2 = vec.begin(); it2 != vec.end(); it2++)
     79      {
     80         if ( sendMessage(&serverMsg, sock, &(it2->addr)) < 0 )
     81            error("sendMessage");
     82      }
     83   }
    6584}
    6685
     
    7392   vector<Player> vctPlayers;
    7493
    75    SSL_load_error_strings();
    76    ERR_load_BIO_strings();
    77    OpenSSL_add_all_algorithms();
     94   //SSL_load_error_strings();
     95   //ERR_load_BIO_strings();
     96   //OpenSSL_add_all_algorithms();
    7897
    7998   if (argc < 2) {
     
    127146               error("sendMessage");
    128147         }
    129       }
    130 
     148
     149         broadcastPlayerPositions(vctPlayers, sock);
     150      }
    131151   }
    132152
Note: See TracChangeset for help on using the changeset viewer.