[2488852] | 1 | #include <cstdlib>
|
---|
[371ce29] | 2 | #include <cstdio>
|
---|
[e3535b3] | 3 | #include <unistd.h>
|
---|
[2488852] | 4 | #include <string>
|
---|
[e3535b3] | 5 | #include <iostream>
|
---|
[3b1efcc] | 6 | #include <sstream>
|
---|
[2488852] | 7 | #include <vector>
|
---|
| 8 | #include <algorithm>
|
---|
[edfd1d0] | 9 | #include <cstring>
|
---|
[371ce29] | 10 |
|
---|
[73f75c1] | 11 | #include <sys/socket.h>
|
---|
[371ce29] | 12 | #include <netdb.h>
|
---|
[73f75c1] | 13 | #include <netinet/in.h>
|
---|
| 14 | #include <arpa/inet.h>
|
---|
| 15 |
|
---|
[edfd1d0] | 16 | /*
|
---|
[e3535b3] | 17 | #include <openssl/bio.h>
|
---|
| 18 | #include <openssl/ssl.h>
|
---|
| 19 | #include <openssl/err.h>
|
---|
[edfd1d0] | 20 | */
|
---|
[e3535b3] | 21 |
|
---|
[b53c6b3] | 22 | #include "../common/Compiler.h"
|
---|
[3b1efcc] | 23 | #include "../common/Common.h"
|
---|
[edfd1d0] | 24 | #include "../common/Message.h"
|
---|
| 25 | #include "../common/Player.h"
|
---|
[b53c6b3] | 26 |
|
---|
[36082e8] | 27 | #include "DataAccess.h"
|
---|
[d2b411a] | 28 |
|
---|
[e3535b3] | 29 | using namespace std;
|
---|
| 30 |
|
---|
[3b1efcc] | 31 | bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg);
|
---|
[8e540f4] | 32 |
|
---|
[73f75c1] | 33 | // this should probably go somewhere in the common folder
|
---|
[e3535b3] | 34 | void error(const char *msg)
|
---|
| 35 | {
|
---|
| 36 | perror(msg);
|
---|
| 37 | exit(0);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[8e540f4] | 40 | Player *findPlayerByName(vector<Player> &vec, string name)
|
---|
[2488852] | 41 | {
|
---|
[8e540f4] | 42 | vector<Player>::iterator it;
|
---|
[2488852] | 43 |
|
---|
| 44 | for (it = vec.begin(); it != vec.end(); it++)
|
---|
| 45 | {
|
---|
| 46 | if ( it->name.compare(name) == 0 )
|
---|
| 47 | return &(*it);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | return NULL;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[8e540f4] | 53 | Player *findPlayerByAddr(vector<Player> &vec, const sockaddr_in &addr)
|
---|
[73f75c1] | 54 | {
|
---|
[8e540f4] | 55 | vector<Player>::iterator it;
|
---|
[73f75c1] | 56 |
|
---|
| 57 | for (it = vec.begin(); it != vec.end(); it++)
|
---|
| 58 | {
|
---|
| 59 | if ( it->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
|
---|
| 60 | it->addr.sin_port == addr.sin_port )
|
---|
| 61 | return &(*it);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | return NULL;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[edfd1d0] | 67 | void 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 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[e3535b3] | 86 | int main(int argc, char *argv[])
|
---|
| 87 | {
|
---|
| 88 | int sock, length, n;
|
---|
| 89 | struct sockaddr_in server;
|
---|
[3b1efcc] | 90 | struct sockaddr_in from; // info of client sending the message
|
---|
[e084950] | 91 | NETWORK_MSG clientMsg, serverMsg;
|
---|
[8e540f4] | 92 | vector<Player> vctPlayers;
|
---|
[e084950] | 93 |
|
---|
[edfd1d0] | 94 | //SSL_load_error_strings();
|
---|
| 95 | //ERR_load_BIO_strings();
|
---|
| 96 | //OpenSSL_add_all_algorithms();
|
---|
[e3535b3] | 97 |
|
---|
| 98 | if (argc < 2) {
|
---|
[73f75c1] | 99 | cerr << "ERROR, no port provided" << endl;
|
---|
| 100 | exit(1);
|
---|
[e3535b3] | 101 | }
|
---|
[41ad8ed] | 102 |
|
---|
[371ce29] | 103 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
[e3535b3] | 104 | if (sock < 0) error("Opening socket");
|
---|
| 105 | length = sizeof(server);
|
---|
| 106 | bzero(&server,length);
|
---|
| 107 | server.sin_family=AF_INET;
|
---|
| 108 | server.sin_port=htons(atoi(argv[1]));
|
---|
[2488852] | 109 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
| 110 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
[e084950] | 111 | error("binding");
|
---|
[73f75c1] | 112 |
|
---|
[371ce29] | 113 | set_nonblock(sock);
|
---|
| 114 |
|
---|
[da692b9] | 115 | bool broadcastResponse;
|
---|
[cb1f288] | 116 | while (true) {
|
---|
[371ce29] | 117 |
|
---|
| 118 | usleep(5000);
|
---|
| 119 |
|
---|
[e084950] | 120 | n = receiveMessage(&clientMsg, sock, &from);
|
---|
[8e540f4] | 121 |
|
---|
[371ce29] | 122 | if (n >= 0) {
|
---|
| 123 | cout << "Got a message" << endl;
|
---|
[8e540f4] | 124 |
|
---|
[da692b9] | 125 | broadcastResponse = processMessage(clientMsg, from, vctPlayers, serverMsg);
|
---|
[371ce29] | 126 |
|
---|
| 127 | cout << "msg: " << serverMsg.buffer << endl;
|
---|
| 128 |
|
---|
[da692b9] | 129 | if (broadcastResponse)
|
---|
[3b1efcc] | 130 | {
|
---|
[da692b9] | 131 | cout << "Should be broadcasting the message" << endl;
|
---|
| 132 |
|
---|
[3b1efcc] | 133 | vector<Player>::iterator it;
|
---|
| 134 |
|
---|
| 135 | for (it = vctPlayers.begin(); it != vctPlayers.end(); it++)
|
---|
| 136 | {
|
---|
| 137 | if ( sendMessage(&serverMsg, sock, &(it->addr)) < 0 )
|
---|
| 138 | error("sendMessage");
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | else
|
---|
| 142 | {
|
---|
[da692b9] | 143 | cout << "Should be sending back the message" << endl;
|
---|
| 144 |
|
---|
[3b1efcc] | 145 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
| 146 | error("sendMessage");
|
---|
| 147 | }
|
---|
[8e540f4] | 148 |
|
---|
[edfd1d0] | 149 | broadcastPlayerPositions(vctPlayers, sock);
|
---|
| 150 | }
|
---|
[8e540f4] | 151 | }
|
---|
[371ce29] | 152 |
|
---|
[8e540f4] | 153 | return 0;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[3b1efcc] | 156 | bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg)
|
---|
[8e540f4] | 157 | {
|
---|
[41ad8ed] | 158 | DataAccess da;
|
---|
| 159 |
|
---|
[8e540f4] | 160 | cout << "ip address: " << inet_ntoa(from.sin_addr) << endl;
|
---|
| 161 | cout << "port: " << from.sin_port << endl;
|
---|
| 162 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
| 163 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
| 164 |
|
---|
[da692b9] | 165 | // maybe we should make a message class and have this be a member
|
---|
[3b1efcc] | 166 | bool broadcastResponse = false;
|
---|
| 167 |
|
---|
[8e540f4] | 168 | // Check that if an invalid message is sent, the client will correctly
|
---|
| 169 | // receive and display the response. Maybe make a special error msg type
|
---|
| 170 | switch(clientMsg.type)
|
---|
| 171 | {
|
---|
| 172 | case MSG_TYPE_REGISTER:
|
---|
[d2b411a] | 173 | {
|
---|
[8e540f4] | 174 | string username(clientMsg.buffer);
|
---|
| 175 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[d2b411a] | 176 |
|
---|
[8e540f4] | 177 | cout << "username: " << username << endl;
|
---|
| 178 | cout << "password: " << password << endl;
|
---|
[d2b411a] | 179 |
|
---|
[3b1efcc] | 180 | int error = da.insertPlayer(username, password);
|
---|
[41ad8ed] | 181 |
|
---|
[3b1efcc] | 182 | if (!error)
|
---|
| 183 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
| 184 | else
|
---|
| 185 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
[8e540f4] | 186 |
|
---|
| 187 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
[d2b411a] | 188 |
|
---|
[8e540f4] | 189 | break;
|
---|
| 190 | }
|
---|
| 191 | case MSG_TYPE_LOGIN:
|
---|
| 192 | {
|
---|
| 193 | string username(clientMsg.buffer);
|
---|
[41ad8ed] | 194 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[8e540f4] | 195 | cout << "Player logging in: " << username << endl;
|
---|
| 196 |
|
---|
[41ad8ed] | 197 | Player* p = da.getPlayer(username);
|
---|
[d2b411a] | 198 |
|
---|
[41ad8ed] | 199 | if (p == NULL || p->password != password)
|
---|
| 200 | {
|
---|
| 201 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
| 202 | }
|
---|
| 203 | else if(findPlayerByName(vctPlayers, username) != NULL)
|
---|
| 204 | {
|
---|
| 205 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
| 206 | }
|
---|
| 207 | else
|
---|
[8e540f4] | 208 | {
|
---|
[59061f6] | 209 | Player newP(username, "");
|
---|
| 210 | newP.setAddr(from);
|
---|
| 211 |
|
---|
| 212 | vctPlayers.push_back(newP);
|
---|
[da692b9] | 213 | strcpy(serverMsg.buffer, "Login successful. Enjoy chatting with other players.");
|
---|
[07028b9] | 214 | }
|
---|
| 215 |
|
---|
[8e540f4] | 216 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
[41ad8ed] | 217 |
|
---|
| 218 | delete(p);
|
---|
[07028b9] | 219 |
|
---|
[8e540f4] | 220 | break;
|
---|
| 221 | }
|
---|
| 222 | case MSG_TYPE_LOGOUT:
|
---|
| 223 | {
|
---|
| 224 | string name(clientMsg.buffer);
|
---|
| 225 | cout << "Player logging out: " << name << endl;
|
---|
| 226 |
|
---|
| 227 | Player *p = findPlayerByName(vctPlayers, name);
|
---|
[633f42a] | 228 |
|
---|
[8e540f4] | 229 | if (p == NULL)
|
---|
| 230 | {
|
---|
| 231 | strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
[07028b9] | 232 | }
|
---|
[8e540f4] | 233 | else if( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
| 234 | p->addr.sin_port != from.sin_port )
|
---|
[07028b9] | 235 | {
|
---|
[8e540f4] | 236 | 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.");
|
---|
[2488852] | 237 | }
|
---|
[8e540f4] | 238 | else
|
---|
[2488852] | 239 | {
|
---|
[8e540f4] | 240 | vctPlayers.erase((vector<Player>::iterator)p);
|
---|
[41ad8ed] | 241 | strcpy(serverMsg.buffer, "You have successfully logged out.");
|
---|
[8e540f4] | 242 | }
|
---|
[07028b9] | 243 |
|
---|
[8e540f4] | 244 | break;
|
---|
| 245 | }
|
---|
| 246 | case MSG_TYPE_CHAT:
|
---|
| 247 | {
|
---|
[da692b9] | 248 | cout << "Got a chat message" << endl;
|
---|
| 249 |
|
---|
[8e540f4] | 250 | Player *p = findPlayerByAddr(vctPlayers, from);
|
---|
[07028b9] | 251 |
|
---|
[8e540f4] | 252 | if (p == NULL)
|
---|
| 253 | {
|
---|
| 254 | strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
|
---|
[2488852] | 255 | }
|
---|
[8e540f4] | 256 | else
|
---|
| 257 | {
|
---|
[3b1efcc] | 258 | broadcastResponse = true;
|
---|
| 259 |
|
---|
| 260 | stringstream ss;
|
---|
[da692b9] | 261 | ss << p->name << ": " << clientMsg.buffer;
|
---|
[3b1efcc] | 262 |
|
---|
| 263 | strcpy(serverMsg.buffer, ss.str().c_str());
|
---|
[8e540f4] | 264 | }
|
---|
| 265 |
|
---|
| 266 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 267 |
|
---|
| 268 | break;
|
---|
[e084950] | 269 | }
|
---|
[8e540f4] | 270 | default:
|
---|
| 271 | {
|
---|
| 272 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
[e084950] | 273 |
|
---|
[8e540f4] | 274 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
[e084950] | 275 |
|
---|
[8e540f4] | 276 | break;
|
---|
| 277 | }
|
---|
[e3535b3] | 278 | }
|
---|
[da692b9] | 279 |
|
---|
| 280 | return broadcastResponse;
|
---|
[e3535b3] | 281 | }
|
---|
[da692b9] | 282 |
|
---|