source: network-game/server/server.cpp@ 4da5aa3

Last change on this file since 4da5aa3 was 633f42a, checked in by dportnoy <dmp1488@…>, 12 years ago

Fixed a bug related to a player logging out

  • Property mode set to 100644
File size: 5.6 KB
Line 
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
41using namespace std;
42
43void error(const char *msg)
44{
45 perror(msg);
46 exit(0);
47}
48
49player *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
62int 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 case MSG_TYPE_LOGIN:
105 {
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_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 {
138 vctPlayers.erase((vector<player>::iterator)p);
139 strcpy(serverMsg.buffer, "You have successfully logged out. You may quit the game.");
140 }
141
142 break;
143 }
144 case MSG_TYPE_CHAT:
145 {
146 int guess = atoi(clientMsg.buffer);
147
148 cout << "guess: " << guess << endl;
149
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 }
160
161 serverMsg.type = MSG_TYPE_CHAT;
162
163 break;
164 }
165 default:
166 {
167 strcpy(serverMsg.buffer, "Server error occured. Report this please.");
168
169 serverMsg.type = MSG_TYPE_CHAT;
170
171 break;
172 }
173 }
174
175 cout << "msg: " << serverMsg.buffer << endl;
176
177 n = sendMessage(&serverMsg, sock, &from);
178 if (n < 0)
179 error("sendMessage");
180 }
181 return 0;
182}
183
184int 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}
Note: See TracBrowser for help on using the repository browser.