[e084950] | 1 | #include "../common/compiler.h"
|
---|
| 2 |
|
---|
[2488852] | 3 | #include <cstdlib>
|
---|
[e3535b3] | 4 | #include <unistd.h>
|
---|
[2488852] | 5 | #include <string>
|
---|
[e3535b3] | 6 | #include <netdb.h>
|
---|
[2488852] | 7 | #include <cstdio>
|
---|
[e3535b3] | 8 | #include <iostream>
|
---|
[2488852] | 9 | #include <vector>
|
---|
| 10 | #include <algorithm>
|
---|
[e3535b3] | 11 |
|
---|
[73f75c1] | 12 | #include <sys/socket.h>
|
---|
| 13 | #include <netinet/in.h>
|
---|
| 14 | #include <arpa/inet.h>
|
---|
| 15 |
|
---|
[e3535b3] | 16 | #include <mysql/mysql.h>
|
---|
| 17 |
|
---|
| 18 | #include <openssl/bio.h>
|
---|
| 19 | #include <openssl/ssl.h>
|
---|
| 20 | #include <openssl/err.h>
|
---|
| 21 |
|
---|
[2488852] | 22 | #include "player.h"
|
---|
| 23 | #include "../common/message.h"
|
---|
[d2b411a] | 24 |
|
---|
| 25 | /*
|
---|
| 26 | Protocol Design
|
---|
| 27 |
|
---|
| 28 | Client sends a login message
|
---|
| 29 | Server replies with client's position in the world and positions of
|
---|
| 30 | oall other logged in players
|
---|
| 31 | Merver sends player ids along with locations
|
---|
| 32 | This means a newly logged in client will need to know which id is
|
---|
| 33 | assigned to it
|
---|
| 34 | So server needs to send an id message, wait for an ack, and then send
|
---|
| 35 | the location messages
|
---|
| 36 | When a client shuts down, it sends a message to indicate this so the
|
---|
| 37 | server can remove it from the list of connected clients
|
---|
| 38 | Eventually, there'll need to be a way to detect timeouts from clients
|
---|
| 39 | (if they crashed or otherwise failed to send the logout message)
|
---|
| 40 | */
|
---|
| 41 |
|
---|
[e3535b3] | 42 | using namespace std;
|
---|
| 43 |
|
---|
[73f75c1] | 44 | // this should probably go somewhere in the common folder
|
---|
[e3535b3] | 45 | void error(const char *msg)
|
---|
| 46 | {
|
---|
| 47 | perror(msg);
|
---|
| 48 | exit(0);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[2488852] | 51 | player *findPlayerByName(vector<player> &vec, string name)
|
---|
| 52 | {
|
---|
| 53 | vector<player>::iterator it;
|
---|
| 54 |
|
---|
| 55 | for (it = vec.begin(); it != vec.end(); it++)
|
---|
| 56 | {
|
---|
| 57 | if ( it->name.compare(name) == 0 )
|
---|
| 58 | return &(*it);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | return NULL;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[73f75c1] | 64 | // not sure if we actually need this function
|
---|
| 65 | // when I made it, I thought we did
|
---|
| 66 | player *findPlayerByAddr(vector<player> &vec, sockaddr_in &addr)
|
---|
| 67 | {
|
---|
| 68 | vector<player>::iterator it;
|
---|
| 69 |
|
---|
| 70 | for (it = vec.begin(); it != vec.end(); it++)
|
---|
| 71 | {
|
---|
| 72 | if ( it->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
|
---|
| 73 | it->addr.sin_port == addr.sin_port )
|
---|
| 74 | return &(*it);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | return NULL;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[e3535b3] | 80 | int main(int argc, char *argv[])
|
---|
| 81 | {
|
---|
| 82 | int sock, length, n;
|
---|
| 83 | struct sockaddr_in server;
|
---|
[73f75c1] | 84 | struct sockaddr_in from; // holds the info on the connected client
|
---|
[e084950] | 85 | NETWORK_MSG clientMsg, serverMsg;
|
---|
[2488852] | 86 | vector<player> vctPlayers;
|
---|
[e084950] | 87 |
|
---|
| 88 | srand(time(NULL));
|
---|
| 89 | int num = (rand() % 1000) + 1;
|
---|
| 90 |
|
---|
| 91 | cout << "num: " << num << endl;
|
---|
[e3535b3] | 92 |
|
---|
| 93 | SSL_load_error_strings();
|
---|
| 94 | ERR_load_BIO_strings();
|
---|
| 95 | OpenSSL_add_all_algorithms();
|
---|
| 96 |
|
---|
| 97 | if (argc < 2) {
|
---|
[73f75c1] | 98 | cerr << "ERROR, no port provided" << endl;
|
---|
| 99 | exit(1);
|
---|
[e3535b3] | 100 | }
|
---|
| 101 |
|
---|
| 102 | sock=socket(AF_INET, SOCK_DGRAM, 0);
|
---|
| 103 | if (sock < 0) error("Opening socket");
|
---|
| 104 | length = sizeof(server);
|
---|
| 105 | bzero(&server,length);
|
---|
| 106 | server.sin_family=AF_INET;
|
---|
| 107 | server.sin_port=htons(atoi(argv[1]));
|
---|
[2488852] | 108 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
| 109 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
[e084950] | 110 | error("binding");
|
---|
[73f75c1] | 111 |
|
---|
[cb1f288] | 112 | while (true) {
|
---|
[2488852] | 113 | // if n == 0, means the client disconnected. may want to check this
|
---|
[e084950] | 114 | n = receiveMessage(&clientMsg, sock, &from);
|
---|
| 115 | if (n < 0)
|
---|
| 116 | error("recieveMessage");
|
---|
[73f75c1] | 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;
|
---|
[d2b411a] | 121 |
|
---|
[73f75c1] | 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
|
---|
[d2b411a] | 124 | switch(clientMsg.type)
|
---|
| 125 | {
|
---|
[07028b9] | 126 | case MSG_TYPE_LOGIN:
|
---|
[d2b411a] | 127 | {
|
---|
[07028b9] | 128 | string name(clientMsg.buffer);
|
---|
| 129 | cout << "Player logging in: " << name << endl;
|
---|
[d2b411a] | 130 |
|
---|
[07028b9] | 131 | player *p = findPlayerByName(vctPlayers, name);
|
---|
[d2b411a] | 132 |
|
---|
[07028b9] | 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 | }
|
---|
[d2b411a] | 142 |
|
---|
[07028b9] | 143 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
[d2b411a] | 144 |
|
---|
[07028b9] | 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 | }
|
---|
[73f75c1] | 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 | }
|
---|
[07028b9] | 163 | else
|
---|
| 164 | {
|
---|
[633f42a] | 165 | vctPlayers.erase((vector<player>::iterator)p);
|
---|
[07028b9] | 166 | strcpy(serverMsg.buffer, "You have successfully logged out. You may quit the game.");
|
---|
| 167 | }
|
---|
[633f42a] | 168 |
|
---|
| 169 | break;
|
---|
[07028b9] | 170 | }
|
---|
| 171 | case MSG_TYPE_CHAT:
|
---|
| 172 | {
|
---|
[73f75c1] | 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 | }
|
---|
[07028b9] | 195 | }
|
---|
[2488852] | 196 |
|
---|
[07028b9] | 197 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
[2488852] | 198 |
|
---|
[07028b9] | 199 | break;
|
---|
[2488852] | 200 | }
|
---|
[07028b9] | 201 | default:
|
---|
[2488852] | 202 | {
|
---|
[07028b9] | 203 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
| 204 |
|
---|
| 205 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 206 |
|
---|
| 207 | break;
|
---|
[2488852] | 208 | }
|
---|
[e084950] | 209 | }
|
---|
| 210 |
|
---|
| 211 | cout << "msg: " << serverMsg.buffer << endl;
|
---|
| 212 |
|
---|
| 213 | n = sendMessage(&serverMsg, sock, &from);
|
---|
| 214 | if (n < 0)
|
---|
| 215 | error("sendMessage");
|
---|
[e3535b3] | 216 | }
|
---|
| 217 | return 0;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | int dbtest()
|
---|
| 221 | {
|
---|
| 222 | MYSQL *connection, mysql;
|
---|
| 223 | MYSQL_RES *result;
|
---|
| 224 | MYSQL_ROW row;
|
---|
| 225 | int query_state;
|
---|
| 226 |
|
---|
| 227 | mysql_init(&mysql);
|
---|
| 228 |
|
---|
| 229 | connection = mysql_real_connect(&mysql,"localhost","pythonAdmin","pyMaster09*","pythondb",0,0,0);
|
---|
| 230 |
|
---|
| 231 | if (connection == NULL) {
|
---|
| 232 | cout << mysql_error(&mysql) << endl;
|
---|
| 233 | return 1;
|
---|
| 234 | }else
|
---|
| 235 | cout << "Connection successful" << endl;
|
---|
| 236 |
|
---|
| 237 | query_state = mysql_query(connection, "SELECT * FROM users");
|
---|
| 238 |
|
---|
| 239 | if (query_state !=0) {
|
---|
| 240 | cout << mysql_error(connection) << endl;
|
---|
| 241 | return 1;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | result = mysql_store_result(connection);
|
---|
| 245 |
|
---|
| 246 | while ( ( row = mysql_fetch_row(result)) != NULL ) {
|
---|
| 247 | cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | mysql_free_result(result);
|
---|
| 251 | mysql_close(connection);
|
---|
| 252 |
|
---|
| 253 | cout << "Test finished" << endl;
|
---|
| 254 |
|
---|
| 255 | return 0;
|
---|
| 256 | }
|
---|