[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"
|
---|
| 23 | ///////////
|
---|
[e3535b3] | 24 | using namespace std;
|
---|
| 25 |
|
---|
| 26 | void error(const char *msg)
|
---|
| 27 | {
|
---|
| 28 | perror(msg);
|
---|
| 29 | exit(0);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
[2488852] | 32 | player *findPlayerByName(vector<player> &vec, string name)
|
---|
| 33 | {
|
---|
| 34 | vector<player>::iterator it;
|
---|
| 35 |
|
---|
| 36 | for (it = vec.begin(); it != vec.end(); it++)
|
---|
| 37 | {
|
---|
| 38 | if ( it->name.compare(name) == 0 )
|
---|
| 39 | return &(*it);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | return NULL;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[e3535b3] | 45 | int main(int argc, char *argv[])
|
---|
| 46 | {
|
---|
| 47 | int sock, length, n;
|
---|
| 48 | socklen_t fromlen;
|
---|
| 49 | struct sockaddr_in server;
|
---|
| 50 | struct sockaddr_in from;
|
---|
[e084950] | 51 | NETWORK_MSG clientMsg, serverMsg;
|
---|
[2488852] | 52 | vector<player> vctPlayers;
|
---|
[e084950] | 53 |
|
---|
| 54 | srand(time(NULL));
|
---|
| 55 | int num = (rand() % 1000) + 1;
|
---|
| 56 |
|
---|
| 57 | cout << "num: " << num << endl;
|
---|
[e3535b3] | 58 |
|
---|
| 59 | SSL_load_error_strings();
|
---|
| 60 | ERR_load_BIO_strings();
|
---|
| 61 | OpenSSL_add_all_algorithms();
|
---|
| 62 |
|
---|
| 63 | if (argc < 2) {
|
---|
| 64 | fprintf(stderr, "ERROR, no port provided\n");
|
---|
| 65 | exit(0);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | sock=socket(AF_INET, SOCK_DGRAM, 0);
|
---|
| 69 | if (sock < 0) error("Opening socket");
|
---|
| 70 | length = sizeof(server);
|
---|
| 71 | bzero(&server,length);
|
---|
| 72 | server.sin_family=AF_INET;
|
---|
| 73 | server.sin_port=htons(atoi(argv[1]));
|
---|
[2488852] | 74 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
| 75 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
[e084950] | 76 | error("binding");
|
---|
[e3535b3] | 77 | fromlen = sizeof(struct sockaddr_in);
|
---|
[cb1f288] | 78 | while (true) {
|
---|
[2488852] | 79 | // if n == 0, means the client disconnected. may want to check this
|
---|
[e084950] | 80 | n = receiveMessage(&clientMsg, sock, &from);
|
---|
| 81 | if (n < 0)
|
---|
| 82 | error("recieveMessage");
|
---|
| 83 | cout << "msg: " << clientMsg.buffer << endl;
|
---|
| 84 |
|
---|
[2488852] | 85 | // Ptoyovol Design
|
---|
| 86 | //
|
---|
| 87 | // Client sends a login message
|
---|
| 88 | // Server replies with client's position in the world and positions of
|
---|
| 89 | // oall other logged in players
|
---|
| 90 | // Merver sends player ids along with locations
|
---|
| 91 | // This means a newly logged in client will need to know which id is
|
---|
| 92 | // assigned to it
|
---|
| 93 | // So server needs to send an id message, wait for an ack, and then send
|
---|
| 94 | // the location messages
|
---|
| 95 |
|
---|
| 96 | // When a client shuts down, it sends a message to indicate this so the
|
---|
| 97 | // server can remove it from the list of connected clients
|
---|
| 98 | // Eventually, there'll need to be a way to detect timeouts from clients
|
---|
| 99 | // (if they crashed or otherwise failed to send the logout message)
|
---|
| 100 |
|
---|
[cb1f288] | 101 | if (strcmp(clientMsg.buffer, "Hello") == 0)
|
---|
[e084950] | 102 | {
|
---|
[2488852] | 103 | player *p = findPlayerByName(vctPlayers, "Boberty");
|
---|
| 104 |
|
---|
| 105 | if (p == NULL)
|
---|
| 106 | {
|
---|
| 107 | vctPlayers.push_back(player("Boberty", from));
|
---|
| 108 | strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is.");
|
---|
| 109 | }
|
---|
| 110 | else
|
---|
| 111 | {
|
---|
| 112 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
| 113 | }
|
---|
[e084950] | 114 | }else {
|
---|
| 115 | int guess = atoi(clientMsg.buffer);
|
---|
| 116 |
|
---|
[cb1f288] | 117 | cout << "guess: " << guess << endl;
|
---|
| 118 |
|
---|
[e084950] | 119 | if (guess < 1 || guess > 1000) {
|
---|
| 120 | strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
|
---|
| 121 | }else if(guess > num)
|
---|
| 122 | strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
|
---|
| 123 | else if(guess < num)
|
---|
| 124 | strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
|
---|
| 125 | else if(guess == num) {
|
---|
| 126 | strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
|
---|
| 127 | num = (rand() % 1000) + 1;
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | cout << "msg: " << serverMsg.buffer << endl;
|
---|
| 132 |
|
---|
| 133 | n = sendMessage(&serverMsg, sock, &from);
|
---|
| 134 | if (n < 0)
|
---|
| 135 | error("sendMessage");
|
---|
[e3535b3] | 136 | }
|
---|
| 137 | return 0;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | int dbtest()
|
---|
| 141 | {
|
---|
| 142 | MYSQL *connection, mysql;
|
---|
| 143 | MYSQL_RES *result;
|
---|
| 144 | MYSQL_ROW row;
|
---|
| 145 | int query_state;
|
---|
| 146 |
|
---|
| 147 | mysql_init(&mysql);
|
---|
| 148 |
|
---|
| 149 | connection = mysql_real_connect(&mysql,"localhost","pythonAdmin","pyMaster09*","pythondb",0,0,0);
|
---|
| 150 |
|
---|
| 151 | if (connection == NULL) {
|
---|
| 152 | cout << mysql_error(&mysql) << endl;
|
---|
| 153 | return 1;
|
---|
| 154 | }else
|
---|
| 155 | cout << "Connection successful" << endl;
|
---|
| 156 |
|
---|
| 157 | query_state = mysql_query(connection, "SELECT * FROM users");
|
---|
| 158 |
|
---|
| 159 | if (query_state !=0) {
|
---|
| 160 | cout << mysql_error(connection) << endl;
|
---|
| 161 | return 1;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | result = mysql_store_result(connection);
|
---|
| 165 |
|
---|
| 166 | while ( ( row = mysql_fetch_row(result)) != NULL ) {
|
---|
| 167 | cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | mysql_free_result(result);
|
---|
| 171 | mysql_close(connection);
|
---|
| 172 |
|
---|
| 173 | cout << "Test finished" << endl;
|
---|
| 174 |
|
---|
| 175 | return 0;
|
---|
| 176 | }
|
---|