1 | #include "../common/compiler.h"
|
---|
2 |
|
---|
3 | #include <sys/types.h>
|
---|
4 | #include <cstdlib>
|
---|
5 | #include <unistd.h>
|
---|
6 | #include <sys/socket.h>
|
---|
7 | #include <netinet/in.h>
|
---|
8 | #include <string>
|
---|
9 | #include <netdb.h>
|
---|
10 | #include <cstdio>
|
---|
11 | #include <iostream>
|
---|
12 | #include <vector>
|
---|
13 | #include <algorithm>
|
---|
14 |
|
---|
15 | #include <mysql/mysql.h>
|
---|
16 |
|
---|
17 | #include <openssl/bio.h>
|
---|
18 | #include <openssl/ssl.h>
|
---|
19 | #include <openssl/err.h>
|
---|
20 |
|
---|
21 | #include "player.h"
|
---|
22 | #include "../common/message.h"
|
---|
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 |
|
---|
41 | using namespace std;
|
---|
42 |
|
---|
43 | void error(const char *msg)
|
---|
44 | {
|
---|
45 | perror(msg);
|
---|
46 | exit(0);
|
---|
47 | }
|
---|
48 |
|
---|
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 |
|
---|
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;
|
---|
68 | NETWORK_MSG clientMsg, serverMsg;
|
---|
69 | vector<player> vctPlayers;
|
---|
70 |
|
---|
71 | srand(time(NULL));
|
---|
72 | int num = (rand() % 1000) + 1;
|
---|
73 |
|
---|
74 | cout << "num: " << num << endl;
|
---|
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]));
|
---|
91 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
92 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
93 | error("binding");
|
---|
94 | fromlen = sizeof(struct sockaddr_in);
|
---|
95 | while (true) {
|
---|
96 | // if n == 0, means the client disconnected. may want to check this
|
---|
97 | n = receiveMessage(&clientMsg, sock, &from);
|
---|
98 | if (n < 0)
|
---|
99 | error("recieveMessage");
|
---|
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 | }
|
---|
155 |
|
---|
156 | /*
|
---|
157 | if (strcmp(clientMsg.buffer, "Hello") == 0)
|
---|
158 | {
|
---|
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 | }
|
---|
170 | }else {
|
---|
171 | int guess = atoi(clientMsg.buffer);
|
---|
172 |
|
---|
173 | cout << "guess: " << guess << endl;
|
---|
174 |
|
---|
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 | }
|
---|
186 | */
|
---|
187 |
|
---|
188 | cout << "msg: " << serverMsg.buffer << endl;
|
---|
189 |
|
---|
190 | n = sendMessage(&serverMsg, sock, &from);
|
---|
191 | if (n < 0)
|
---|
192 | error("sendMessage");
|
---|
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 | }
|
---|