- Timestamp:
- Nov 27, 2012, 4:03:52 PM (12 years ago)
- Branches:
- master
- Children:
- 36082e8
- Parents:
- 2318fff
- Location:
- server
- Files:
-
- 2 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
server/Player.cpp
r2318fff r8e540f4 1 #include " player.h"1 #include "Player.h" 2 2 3 3 #include <iostream> … … 6 6 using namespace std; 7 7 8 player::player(string name, sockaddr_in addr)8 Player::Player(string name, sockaddr_in addr) 9 9 { 10 10 this->name = name; … … 14 14 } 15 15 16 player::~player()16 Player::~Player() 17 17 { 18 18 } 19 19 20 20 // was meant for the find find function. Currently unused 21 bool player::operator == (const player &p)21 bool Player::operator == (const Player &p) 22 22 { 23 23 bool eq = addr.sin_addr.s_addr == p.addr.sin_addr.s_addr; -
server/Player.h
r2318fff r8e540f4 7 7 using namespace std; 8 8 9 class player {9 class Player { 10 10 public: 11 Player(string name, sockaddr_in addr); 12 ~Player(); 13 14 bool operator == (const Player &p); 15 11 16 string name; 12 17 sockaddr_in addr; 13 14 player(string name, sockaddr_in addr);15 ~player();16 17 bool operator == (const player &p);18 18 }; 19 19 -
server/makefile
r2318fff r8e540f4 3 3 FLAGS = $(LIB_FLAGS) 4 4 COMMON_PATH = ../common 5 DEPENDENCIES = message.o player.o5 DEPENDENCIES = message.o Player.o 6 6 7 7 server : server.cpp $(DEPENDENCIES) -
server/server.cpp
r2318fff r8e540f4 20 20 #include <openssl/err.h> 21 21 22 #include " player.h"22 #include "Player.h" 23 23 #include "../common/message.h" 24 24 … … 42 42 using namespace std; 43 43 44 void processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, int &num, NETWORK_MSG &serverMsg); 45 44 46 // this should probably go somewhere in the common folder 45 47 void error(const char *msg) … … 49 51 } 50 52 51 player *findPlayerByName(vector<player> &vec, string name)52 { 53 vector< player>::iterator it;53 Player *findPlayerByName(vector<Player> &vec, string name) 54 { 55 vector<Player>::iterator it; 54 56 55 57 for (it = vec.begin(); it != vec.end(); it++) … … 64 66 // not sure if we actually need this function 65 67 // when I made it, I thought we did 66 player *findPlayerByAddr(vector<player> &vec,sockaddr_in &addr)67 { 68 vector< player>::iterator it;68 Player *findPlayerByAddr(vector<Player> &vec, const sockaddr_in &addr) 69 { 70 vector<Player>::iterator it; 69 71 70 72 for (it = vec.begin(); it != vec.end(); it++) … … 84 86 struct sockaddr_in from; // holds the info on the connected client 85 87 NETWORK_MSG clientMsg, serverMsg; 86 vector< player> vctPlayers;88 vector<Player> vctPlayers; 87 89 88 90 srand(time(NULL)); … … 115 117 if (n < 0) 116 118 error("recieveMessage"); 117 cout << "ip address: " << inet_ntoa(from.sin_addr) << endl; 118 cout << "port: " << from.sin_port << endl; 119 cout << "MSG: type: " << clientMsg.type << endl; 120 cout << "MSG contents: " << clientMsg.buffer << endl; 121 122 // Check that if an invalid message is sent, the client will corectly 123 // receive and display the response. Maybe make a special error msg type 124 switch(clientMsg.type) 125 { 126 case MSG_TYPE_LOGIN: 127 { 128 string name(clientMsg.buffer); 129 cout << "Player logging in: " << name << endl; 130 131 player *p = findPlayerByName(vctPlayers, name); 132 133 if (p == NULL) 134 { 135 vctPlayers.push_back(player(name, from)); 136 strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is."); 137 } 138 else 139 { 140 strcpy(serverMsg.buffer, "Player has already logged in."); 141 } 142 143 serverMsg.type = MSG_TYPE_LOGIN; 144 145 break; 146 } 147 case MSG_TYPE_LOGOUT: 148 { 149 string name(clientMsg.buffer); 150 cout << "Player logging out: " << name << endl; 151 152 player *p = findPlayerByName(vctPlayers, name); 153 154 if (p == NULL) 155 { 156 strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server."); 157 } 158 else if( p->addr.sin_addr.s_addr != from.sin_addr.s_addr || 159 p->addr.sin_port != from.sin_port ) 160 { 161 strcpy(serverMsg.buffer, "That player is logged in using a differemt connection. This is either a bug, or you're trying to hack the server."); 162 } 163 else 164 { 165 vctPlayers.erase((vector<player>::iterator)p); 166 strcpy(serverMsg.buffer, "You have successfully logged out. You may quit the game."); 167 } 168 169 break; 170 } 171 case MSG_TYPE_CHAT: 172 { 173 player *p = findPlayerByAddr(vctPlayers, from); 174 175 if (p == NULL) 176 { 177 strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server."); 178 } 179 else 180 { 181 int guess = atoi(clientMsg.buffer); 182 183 cout << "guess: " << guess << endl; 184 185 if (guess < 1 || guess > 1000) { 186 strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000"); 187 }else if(guess > num) 188 strcpy(serverMsg.buffer, "The number I'm thinking of is less than that."); 189 else if(guess < num) 190 strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that."); 191 else if(guess == num) { 192 strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number."); 193 num = (rand() % 1000) + 1; 194 } 195 } 196 197 serverMsg.type = MSG_TYPE_CHAT; 198 199 break; 200 } 201 default: 202 { 203 strcpy(serverMsg.buffer, "Server error occured. Report this please."); 204 205 serverMsg.type = MSG_TYPE_CHAT; 206 207 break; 208 } 209 } 119 120 processMessage(clientMsg, from, vctPlayers, num, serverMsg); 210 121 211 122 cout << "msg: " << serverMsg.buffer << endl; … … 216 127 } 217 128 return 0; 129 } 130 131 void processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, int &num, NETWORK_MSG &serverMsg) 132 { 133 cout << "ip address: " << inet_ntoa(from.sin_addr) << endl; 134 cout << "port: " << from.sin_port << endl; 135 cout << "MSG: type: " << clientMsg.type << endl; 136 cout << "MSG contents: " << clientMsg.buffer << endl; 137 138 // Check that if an invalid message is sent, the client will correctly 139 // receive and display the response. Maybe make a special error msg type 140 switch(clientMsg.type) 141 { 142 case MSG_TYPE_REGISTER: 143 { 144 string username(clientMsg.buffer); 145 string password(strchr(clientMsg.buffer, '\0')+1); 146 147 cout << "username: " << username << endl; 148 cout << "password: " << password << endl; 149 150 strcpy(serverMsg.buffer, "Registration test"); 151 152 serverMsg.type = MSG_TYPE_REGISTER; 153 154 break; 155 } 156 case MSG_TYPE_LOGIN: 157 { 158 string username(clientMsg.buffer); 159 cout << "Player logging in: " << username << endl; 160 161 Player *p = findPlayerByName(vctPlayers, username); 162 163 if (p == NULL) 164 { 165 vctPlayers.push_back(Player(username, from)); 166 strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is."); 167 } 168 else 169 { 170 strcpy(serverMsg.buffer, "Player has already logged in."); 171 } 172 173 serverMsg.type = MSG_TYPE_LOGIN; 174 175 break; 176 } 177 case MSG_TYPE_LOGOUT: 178 { 179 string name(clientMsg.buffer); 180 cout << "Player logging out: " << name << endl; 181 182 Player *p = findPlayerByName(vctPlayers, name); 183 184 if (p == NULL) 185 { 186 strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server."); 187 } 188 else if( p->addr.sin_addr.s_addr != from.sin_addr.s_addr || 189 p->addr.sin_port != from.sin_port ) 190 { 191 strcpy(serverMsg.buffer, "That player is logged in using a differemt connection. This is either a bug, or you're trying to hack the server."); 192 } 193 else 194 { 195 vctPlayers.erase((vector<Player>::iterator)p); 196 strcpy(serverMsg.buffer, "You have successfully logged out. You may quit the game."); 197 } 198 199 break; 200 } 201 case MSG_TYPE_CHAT: 202 { 203 Player *p = findPlayerByAddr(vctPlayers, from); 204 205 if (p == NULL) 206 { 207 strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server."); 208 } 209 else 210 { 211 int guess = atoi(clientMsg.buffer); 212 213 cout << "guess: " << guess << endl; 214 215 if (guess < 1 || guess > 1000) { 216 strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000"); 217 }else if(guess > num) 218 strcpy(serverMsg.buffer, "The number I'm thinking of is less than that."); 219 else if(guess < num) 220 strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that."); 221 else if(guess == num) { 222 strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number."); 223 num = (rand() % 1000) + 1; 224 } 225 } 226 227 serverMsg.type = MSG_TYPE_CHAT; 228 229 break; 230 } 231 default: 232 { 233 strcpy(serverMsg.buffer, "Server error occured. Report this please."); 234 235 serverMsg.type = MSG_TYPE_CHAT; 236 237 break; 238 } 239 } 218 240 } 219 241
Note:
See TracChangeset
for help on using the changeset viewer.