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