[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 | {
|
---|
| 104 | {
|
---|
| 105 | case MSG_TYPE_LOGIN:
|
---|
| 106 | string name(clientMsg.buffer);
|
---|
| 107 | cout << "Player logging in: " << name << endl;
|
---|
| 108 |
|
---|
| 109 | player *p = findPlayerByName(vctPlayers, name);
|
---|
| 110 |
|
---|
| 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 | }
|
---|
| 120 |
|
---|
| 121 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
| 122 |
|
---|
| 123 | break;
|
---|
| 124 | }
|
---|
| 125 | case MSG_TYPE_CHAT:
|
---|
| 126 | {
|
---|
| 127 | int guess = atoi(clientMsg.buffer);
|
---|
| 128 |
|
---|
| 129 | cout << "guess: " << guess << endl;
|
---|
| 130 |
|
---|
| 131 | if (guess < 1 || guess > 1000) {
|
---|
| 132 | strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
|
---|
| 133 | }else if(guess > num)
|
---|
| 134 | strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
|
---|
| 135 | else if(guess < num)
|
---|
| 136 | strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
|
---|
| 137 | else if(guess == num) {
|
---|
| 138 | strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
|
---|
| 139 | num = (rand() % 1000) + 1;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 143 |
|
---|
| 144 | break;
|
---|
| 145 | }
|
---|
| 146 | default:
|
---|
| 147 | {
|
---|
| 148 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
| 149 |
|
---|
| 150 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 151 |
|
---|
| 152 | break;
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
[2488852] | 155 |
|
---|
[d2b411a] | 156 | /*
|
---|
[cb1f288] | 157 | if (strcmp(clientMsg.buffer, "Hello") == 0)
|
---|
[e084950] | 158 | {
|
---|
[2488852] | 159 | player *p = findPlayerByName(vctPlayers, "Boberty");
|
---|
| 160 |
|
---|
| 161 | if (p == NULL)
|
---|
| 162 | {
|
---|
| 163 | vctPlayers.push_back(player("Boberty", from));
|
---|
| 164 | strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is.");
|
---|
| 165 | }
|
---|
| 166 | else
|
---|
| 167 | {
|
---|
| 168 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
| 169 | }
|
---|
[e084950] | 170 | }else {
|
---|
| 171 | int guess = atoi(clientMsg.buffer);
|
---|
| 172 |
|
---|
[cb1f288] | 173 | cout << "guess: " << guess << endl;
|
---|
| 174 |
|
---|
[e084950] | 175 | if (guess < 1 || guess > 1000) {
|
---|
| 176 | strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
|
---|
| 177 | }else if(guess > num)
|
---|
| 178 | strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
|
---|
| 179 | else if(guess < num)
|
---|
| 180 | strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
|
---|
| 181 | else if(guess == num) {
|
---|
| 182 | strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
|
---|
| 183 | num = (rand() % 1000) + 1;
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
[d2b411a] | 186 | */
|
---|
[e084950] | 187 |
|
---|
| 188 | cout << "msg: " << serverMsg.buffer << endl;
|
---|
| 189 |
|
---|
| 190 | n = sendMessage(&serverMsg, sock, &from);
|
---|
| 191 | if (n < 0)
|
---|
| 192 | error("sendMessage");
|
---|
[e3535b3] | 193 | }
|
---|
| 194 | return 0;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | int dbtest()
|
---|
| 198 | {
|
---|
| 199 | MYSQL *connection, mysql;
|
---|
| 200 | MYSQL_RES *result;
|
---|
| 201 | MYSQL_ROW row;
|
---|
| 202 | int query_state;
|
---|
| 203 |
|
---|
| 204 | mysql_init(&mysql);
|
---|
| 205 |
|
---|
| 206 | connection = mysql_real_connect(&mysql,"localhost","pythonAdmin","pyMaster09*","pythondb",0,0,0);
|
---|
| 207 |
|
---|
| 208 | if (connection == NULL) {
|
---|
| 209 | cout << mysql_error(&mysql) << endl;
|
---|
| 210 | return 1;
|
---|
| 211 | }else
|
---|
| 212 | cout << "Connection successful" << endl;
|
---|
| 213 |
|
---|
| 214 | query_state = mysql_query(connection, "SELECT * FROM users");
|
---|
| 215 |
|
---|
| 216 | if (query_state !=0) {
|
---|
| 217 | cout << mysql_error(connection) << endl;
|
---|
| 218 | return 1;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | result = mysql_store_result(connection);
|
---|
| 222 |
|
---|
| 223 | while ( ( row = mysql_fetch_row(result)) != NULL ) {
|
---|
| 224 | cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | mysql_free_result(result);
|
---|
| 228 | mysql_close(connection);
|
---|
| 229 |
|
---|
| 230 | cout << "Test finished" << endl;
|
---|
| 231 |
|
---|
| 232 | return 0;
|
---|
| 233 | }
|
---|