[e084950] | 1 | #include "../common/compiler.h"
|
---|
| 2 |
|
---|
[e3535b3] | 3 | #include <sys/types.h>
|
---|
[2488852] | 4 | #include <cstdlib>
|
---|
[e3535b3] | 5 | #include <unistd.h>
|
---|
| 6 | #include <sys/socket.h>
|
---|
| 7 | #include <netinet/in.h>
|
---|
[2488852] | 8 | #include <string>
|
---|
[e3535b3] | 9 | #include <netdb.h>
|
---|
[2488852] | 10 | #include <cstdio>
|
---|
[e3535b3] | 11 | #include <iostream>
|
---|
[2488852] | 12 | #include <vector>
|
---|
| 13 | #include <algorithm>
|
---|
[e3535b3] | 14 |
|
---|
| 15 | #include <mysql/mysql.h>
|
---|
| 16 |
|
---|
| 17 | #include <openssl/bio.h>
|
---|
| 18 | #include <openssl/ssl.h>
|
---|
| 19 | #include <openssl/err.h>
|
---|
| 20 |
|
---|
[2488852] | 21 | #include "player.h"
|
---|
| 22 | #include "../common/message.h"
|
---|
[d2b411a] | 23 |
|
---|
| 24 | /*
|
---|
| 25 | Protocol Design
|
---|
| 26 |
|
---|
| 27 | Client sends a login message
|
---|
| 28 | Server replies with client's position in the world and positions of
|
---|
| 29 | oall other logged in players
|
---|
| 30 | Merver sends player ids along with locations
|
---|
| 31 | This means a newly logged in client will need to know which id is
|
---|
| 32 | assigned to it
|
---|
| 33 | So server needs to send an id message, wait for an ack, and then send
|
---|
| 34 | the location messages
|
---|
| 35 | When a client shuts down, it sends a message to indicate this so the
|
---|
| 36 | server can remove it from the list of connected clients
|
---|
| 37 | Eventually, there'll need to be a way to detect timeouts from clients
|
---|
| 38 | (if they crashed or otherwise failed to send the logout message)
|
---|
| 39 | */
|
---|
| 40 |
|
---|
[e3535b3] | 41 | using namespace std;
|
---|
| 42 |
|
---|
| 43 | void error(const char *msg)
|
---|
| 44 | {
|
---|
| 45 | perror(msg);
|
---|
| 46 | exit(0);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[2488852] | 49 | player *findPlayerByName(vector<player> &vec, string name)
|
---|
| 50 | {
|
---|
| 51 | vector<player>::iterator it;
|
---|
| 52 |
|
---|
| 53 | for (it = vec.begin(); it != vec.end(); it++)
|
---|
| 54 | {
|
---|
| 55 | if ( it->name.compare(name) == 0 )
|
---|
| 56 | return &(*it);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | return NULL;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[e3535b3] | 62 | int main(int argc, char *argv[])
|
---|
| 63 | {
|
---|
| 64 | int sock, length, n;
|
---|
| 65 | socklen_t fromlen;
|
---|
| 66 | struct sockaddr_in server;
|
---|
| 67 | struct sockaddr_in from;
|
---|
[e084950] | 68 | NETWORK_MSG clientMsg, serverMsg;
|
---|
[2488852] | 69 | vector<player> vctPlayers;
|
---|
[e084950] | 70 |
|
---|
| 71 | srand(time(NULL));
|
---|
| 72 | int num = (rand() % 1000) + 1;
|
---|
| 73 |
|
---|
| 74 | cout << "num: " << num << endl;
|
---|
[e3535b3] | 75 |
|
---|
| 76 | SSL_load_error_strings();
|
---|
| 77 | ERR_load_BIO_strings();
|
---|
| 78 | OpenSSL_add_all_algorithms();
|
---|
| 79 |
|
---|
| 80 | if (argc < 2) {
|
---|
| 81 | fprintf(stderr, "ERROR, no port provided\n");
|
---|
| 82 | exit(0);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | sock=socket(AF_INET, SOCK_DGRAM, 0);
|
---|
| 86 | if (sock < 0) error("Opening socket");
|
---|
| 87 | length = sizeof(server);
|
---|
| 88 | bzero(&server,length);
|
---|
| 89 | server.sin_family=AF_INET;
|
---|
| 90 | server.sin_port=htons(atoi(argv[1]));
|
---|
[2488852] | 91 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
| 92 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
[e084950] | 93 | error("binding");
|
---|
[e3535b3] | 94 | fromlen = sizeof(struct sockaddr_in);
|
---|
[cb1f288] | 95 | while (true) {
|
---|
[2488852] | 96 | // if n == 0, means the client disconnected. may want to check this
|
---|
[e084950] | 97 | n = receiveMessage(&clientMsg, sock, &from);
|
---|
| 98 | if (n < 0)
|
---|
| 99 | error("recieveMessage");
|
---|
[d2b411a] | 100 | cout << "MSG: type: " << clientMsg.type << " contents: " << clientMsg.buffer << endl;
|
---|
| 101 |
|
---|
| 102 | switch(clientMsg.type)
|
---|
| 103 | {
|
---|
[07028b9] | 104 | case MSG_TYPE_LOGIN:
|
---|
[d2b411a] | 105 | {
|
---|
[07028b9] | 106 | string name(clientMsg.buffer);
|
---|
| 107 | cout << "Player logging in: " << name << endl;
|
---|
[d2b411a] | 108 |
|
---|
[07028b9] | 109 | player *p = findPlayerByName(vctPlayers, name);
|
---|
[d2b411a] | 110 |
|
---|
[07028b9] | 111 | if (p == NULL)
|
---|
| 112 | {
|
---|
| 113 | vctPlayers.push_back(player(name, from));
|
---|
| 114 | strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is.");
|
---|
| 115 | }
|
---|
| 116 | else
|
---|
| 117 | {
|
---|
| 118 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
| 119 | }
|
---|
[d2b411a] | 120 |
|
---|
[07028b9] | 121 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
[d2b411a] | 122 |
|
---|
[07028b9] | 123 | break;
|
---|
| 124 | }
|
---|
| 125 | case MSG_TYPE_LOGOUT:
|
---|
| 126 | {
|
---|
| 127 | string name(clientMsg.buffer);
|
---|
| 128 | cout << "Player logging out: " << name << endl;
|
---|
| 129 |
|
---|
| 130 | player *p = findPlayerByName(vctPlayers, name);
|
---|
| 131 |
|
---|
| 132 | if (p == NULL)
|
---|
| 133 | {
|
---|
| 134 | strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
| 135 | }
|
---|
| 136 | else
|
---|
| 137 | {
|
---|
[633f42a] | 138 | vctPlayers.erase((vector<player>::iterator)p);
|
---|
[07028b9] | 139 | strcpy(serverMsg.buffer, "You have successfully logged out. You may quit the game.");
|
---|
| 140 | }
|
---|
[633f42a] | 141 |
|
---|
| 142 | break;
|
---|
[07028b9] | 143 | }
|
---|
| 144 | case MSG_TYPE_CHAT:
|
---|
| 145 | {
|
---|
| 146 | int guess = atoi(clientMsg.buffer);
|
---|
[d2b411a] | 147 |
|
---|
[07028b9] | 148 | cout << "guess: " << guess << endl;
|
---|
[d2b411a] | 149 |
|
---|
[07028b9] | 150 | if (guess < 1 || guess > 1000) {
|
---|
| 151 | strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
|
---|
| 152 | }else if(guess > num)
|
---|
| 153 | strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
|
---|
| 154 | else if(guess < num)
|
---|
| 155 | strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
|
---|
| 156 | else if(guess == num) {
|
---|
| 157 | strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
|
---|
| 158 | num = (rand() % 1000) + 1;
|
---|
| 159 | }
|
---|
[2488852] | 160 |
|
---|
[07028b9] | 161 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
[2488852] | 162 |
|
---|
[07028b9] | 163 | break;
|
---|
[2488852] | 164 | }
|
---|
[07028b9] | 165 | default:
|
---|
[2488852] | 166 | {
|
---|
[07028b9] | 167 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
| 168 |
|
---|
| 169 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 170 |
|
---|
| 171 | break;
|
---|
[2488852] | 172 | }
|
---|
[e084950] | 173 | }
|
---|
| 174 |
|
---|
| 175 | cout << "msg: " << serverMsg.buffer << endl;
|
---|
| 176 |
|
---|
| 177 | n = sendMessage(&serverMsg, sock, &from);
|
---|
| 178 | if (n < 0)
|
---|
| 179 | error("sendMessage");
|
---|
[e3535b3] | 180 | }
|
---|
| 181 | return 0;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | int dbtest()
|
---|
| 185 | {
|
---|
| 186 | MYSQL *connection, mysql;
|
---|
| 187 | MYSQL_RES *result;
|
---|
| 188 | MYSQL_ROW row;
|
---|
| 189 | int query_state;
|
---|
| 190 |
|
---|
| 191 | mysql_init(&mysql);
|
---|
| 192 |
|
---|
| 193 | connection = mysql_real_connect(&mysql,"localhost","pythonAdmin","pyMaster09*","pythondb",0,0,0);
|
---|
| 194 |
|
---|
| 195 | if (connection == NULL) {
|
---|
| 196 | cout << mysql_error(&mysql) << endl;
|
---|
| 197 | return 1;
|
---|
| 198 | }else
|
---|
| 199 | cout << "Connection successful" << endl;
|
---|
| 200 |
|
---|
| 201 | query_state = mysql_query(connection, "SELECT * FROM users");
|
---|
| 202 |
|
---|
| 203 | if (query_state !=0) {
|
---|
| 204 | cout << mysql_error(connection) << endl;
|
---|
| 205 | return 1;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | result = mysql_store_result(connection);
|
---|
| 209 |
|
---|
| 210 | while ( ( row = mysql_fetch_row(result)) != NULL ) {
|
---|
| 211 | cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | mysql_free_result(result);
|
---|
| 215 | mysql_close(connection);
|
---|
| 216 |
|
---|
| 217 | cout << "Test finished" << endl;
|
---|
| 218 |
|
---|
| 219 | return 0;
|
---|
| 220 | }
|
---|