source: network-game/server/server.cpp@ b13b001

Last change on this file since b13b001 was 07028b9, checked in by dportnoy <dmp1488@…>, 12 years ago

The server now accepts and processes logout messages

  • Property mode set to 100644
File size: 5.5 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(p);
139 strcpy(serverMsg.buffer, "You have successfully logged out. You may quit the game.");
140 }
141 }
142 case MSG_TYPE_CHAT:
143 {
144 int guess = atoi(clientMsg.buffer);
145
146 cout << "guess: " << guess << endl;
147
148 if (guess < 1 || guess > 1000) {
149 strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
150 }else if(guess > num)
151 strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
152 else if(guess < num)
153 strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
154 else if(guess == num) {
155 strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
156 num = (rand() % 1000) + 1;
157 }
158
159 serverMsg.type = MSG_TYPE_CHAT;
160
161 break;
162 }
163 default:
164 {
165 strcpy(serverMsg.buffer, "Server error occured. Report this please.");
166
167 serverMsg.type = MSG_TYPE_CHAT;
168
169 break;
170 }
171 }
172
173 cout << "msg: " << serverMsg.buffer << endl;
174
175 n = sendMessage(&serverMsg, sock, &from);
176 if (n < 0)
177 error("sendMessage");
178 }
179 return 0;
180}
181
182int dbtest()
183{
184 MYSQL *connection, mysql;
185 MYSQL_RES *result;
186 MYSQL_ROW row;
187 int query_state;
188
189 mysql_init(&mysql);
190
191 connection = mysql_real_connect(&mysql,"localhost","pythonAdmin","pyMaster09*","pythondb",0,0,0);
192
193 if (connection == NULL) {
194 cout << mysql_error(&mysql) << endl;
195 return 1;
196 }else
197 cout << "Connection successful" << endl;
198
199 query_state = mysql_query(connection, "SELECT * FROM users");
200
201 if (query_state !=0) {
202 cout << mysql_error(connection) << endl;
203 return 1;
204 }
205
206 result = mysql_store_result(connection);
207
208 while ( ( row = mysql_fetch_row(result)) != NULL ) {
209 cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
210 }
211
212 mysql_free_result(result);
213 mysql_close(connection);
214
215 cout << "Test finished" << endl;
216
217 return 0;
218}
Note: See TracBrowser for help on using the repository browser.