Changes in server/server.cpp [3b1efcc:b53c6b3] in network-game
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
server/server.cpp
r3b1efcc rb53c6b3 4 4 #include <string> 5 5 #include <iostream> 6 #include <sstream>7 6 #include <vector> 8 7 #include <algorithm> … … 22 21 #include "../common/Compiler.h" 23 22 #include "../common/Message.h" 24 #include "../common/Common.h"25 23 26 24 #include "Player.h" … … 29 27 using namespace std; 30 28 31 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg);29 void processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, int &num, NETWORK_MSG &serverMsg); 32 30 33 31 // this should probably go somewhere in the common folder … … 65 63 } 66 64 65 void set_nonblock(int sock) 66 { 67 int flags; 68 flags = fcntl(sock, F_GETFL,0); 69 assert(flags != -1); 70 fcntl(sock, F_SETFL, flags | O_NONBLOCK); 71 } 72 67 73 int main(int argc, char *argv[]) 68 74 { 69 75 int sock, length, n; 70 76 struct sockaddr_in server; 71 struct sockaddr_in from; // info of client sending the message77 struct sockaddr_in from; // holds the info about the connected client 72 78 NETWORK_MSG clientMsg, serverMsg; 73 79 vector<Player> vctPlayers; 80 81 srand(time(NULL)); 82 int num = (rand() % 1000) + 1; 83 84 cout << "num: " << num << endl; 74 85 75 86 SSL_load_error_strings(); … … 94 105 set_nonblock(sock); 95 106 96 bool broadcastMessage;97 107 while (true) { 98 108 … … 104 114 cout << "Got a message" << endl; 105 115 106 broadcastMessage = processMessage(clientMsg, from, vctPlayers, serverMsg);116 processMessage(clientMsg, from, vctPlayers, num, serverMsg); 107 117 108 118 cout << "msg: " << serverMsg.buffer << endl; 109 119 110 if (broadcastMessage) 111 { 112 vector<Player>::iterator it; 113 114 for (it = vctPlayers.begin(); it != vctPlayers.end(); it++) 115 { 116 if ( sendMessage(&serverMsg, sock, &(it->addr)) < 0 ) 117 error("sendMessage"); 118 } 119 } 120 else 121 { 122 if ( sendMessage(&serverMsg, sock, &from) < 0 ) 123 error("sendMessage"); 124 } 120 n = sendMessage(&serverMsg, sock, &from); 121 if (n < 0) 122 error("sendMessage"); 125 123 } 126 124 … … 130 128 } 131 129 132 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg)130 void processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, int &num, NETWORK_MSG &serverMsg) 133 131 { 134 132 DataAccess da; … … 138 136 cout << "MSG: type: " << clientMsg.type << endl; 139 137 cout << "MSG contents: " << clientMsg.buffer << endl; 140 141 bool broadcastResponse = false;142 138 143 139 // Check that if an invalid message is sent, the client will correctly … … 153 149 cout << "password: " << password << endl; 154 150 155 int error = da.insertPlayer(username, password); 156 157 if (!error) 158 strcpy(serverMsg.buffer, "Registration successful."); 159 else 160 strcpy(serverMsg.buffer, "Registration failed. Please try again."); 151 da.insertPlayer(username, password); 152 153 strcpy(serverMsg.buffer, "Registration successful"); 161 154 162 155 serverMsg.type = MSG_TYPE_REGISTER; … … 186 179 187 180 vctPlayers.push_back(newP); 188 strcpy(serverMsg.buffer, " Login successful. Enjoy chatting with outher players.");181 strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is."); 189 182 } 190 183 … … 229 222 else 230 223 { 231 broadcastResponse = true; 232 233 stringstream ss; 234 ss << p->name << ": " << clientMsg.buffer << endl; 235 236 strcpy(serverMsg.buffer, ss.str().c_str()); 224 int guess = atoi(clientMsg.buffer); 225 226 cout << "guess: " << guess << endl; 227 228 if (guess < 1 || guess > 1000) { 229 strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000"); 230 }else if(guess > num) 231 strcpy(serverMsg.buffer, "The number I'm thinking of is less than that."); 232 else if(guess < num) 233 strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that."); 234 else if(guess == num) { 235 strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number."); 236 num = (rand() % 1000) + 1; 237 } 237 238 } 238 239 … … 249 250 break; 250 251 } 251 252 return broadcastResponse; 253 } 254 } 252 } 253 }
Note:
See TracChangeset
for help on using the changeset viewer.