source: network-game/server/server.cpp@ 80b3f94

Last change on this file since 80b3f94 was 594d2e9, checked in by dportnoy <dmp1488@…>, 12 years ago

When a user logs in, the server sends a message with the player info for that user

  • Property mode set to 100644
File size: 8.0 KB
Line 
1#include <cstdlib>
2#include <cstdio>
3#include <unistd.h>
4#include <string>
5#include <iostream>
6#include <sstream>
7#include <cstring>
8
9#include <vector>
10#include <map>
11
12#include <sys/socket.h>
13#include <netdb.h>
14#include <netinet/in.h>
15#include <arpa/inet.h>
16
17/*
18#include <openssl/bio.h>
19#include <openssl/ssl.h>
20#include <openssl/err.h>
21*/
22
23#include "../common/Compiler.h"
24#include "../common/Common.h"
25#include "../common/Message.h"
26#include "../common/Player.h"
27
28#include "DataAccess.h"
29
30using namespace std;
31
32bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG &serverMsg);
33
34void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
35
36// this should probably go somewhere in the common folder
37void error(const char *msg)
38{
39 perror(msg);
40 exit(0);
41}
42
43Player *findPlayerByName(map<unsigned int, Player> &m, string name)
44{
45 map<unsigned int, Player>::iterator it;
46
47 for (it = m.begin(); it != m.end(); it++)
48 {
49 if ( it->second.name.compare(name) == 0 )
50 return &(it->second);
51 }
52
53 return NULL;
54}
55
56Player *findPlayerByAddr(map<unsigned int, Player> &m, const sockaddr_in &addr)
57{
58 map<unsigned int, Player>::iterator it;
59
60 for (it = m.begin(); it != m.end(); it++)
61 {
62 if ( it->second.addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
63 it->second.addr.sin_port == addr.sin_port )
64 return &(it->second);
65 }
66
67 return NULL;
68}
69
70void broadcastPlayerPositions(map<unsigned int, Player> &m, int sock)
71{
72 map<unsigned int, Player>::iterator it, it2;
73 NETWORK_MSG serverMsg;
74
75 serverMsg.type = MSG_TYPE_PLAYER;
76
77 for (it = m.begin(); it != m.end(); it++)
78 {
79 it->second.serialize(serverMsg.buffer);
80
81 for (it2 = m.begin(); it2 != m.end(); it2++)
82 {
83 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
84 error("sendMessage");
85 }
86 }
87}
88
89int main(int argc, char *argv[])
90{
91 int sock, length, n;
92 struct sockaddr_in server;
93 struct sockaddr_in from; // info of client sending the message
94 NETWORK_MSG clientMsg, serverMsg;
95 map<unsigned int, Player> mapPlayers;
96 unsigned int unusedId = 1;
97
98 //SSL_load_error_strings();
99 //ERR_load_BIO_strings();
100 //OpenSSL_add_all_algorithms();
101
102 if (argc < 2) {
103 cerr << "ERROR, no port provided" << endl;
104 exit(1);
105 }
106
107 sock = socket(AF_INET, SOCK_DGRAM, 0);
108 if (sock < 0) error("Opening socket");
109 length = sizeof(server);
110 bzero(&server,length);
111 server.sin_family=AF_INET;
112 server.sin_port=htons(atoi(argv[1]));
113 server.sin_addr.s_addr=INADDR_ANY;
114 if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
115 error("binding");
116
117 set_nonblock(sock);
118
119 bool broadcastResponse;
120 while (true) {
121
122 usleep(5000);
123
124 n = receiveMessage(&clientMsg, sock, &from);
125
126 if (n >= 0) {
127 cout << "Got a message" << endl;
128
129 broadcastResponse = processMessage(clientMsg, from, mapPlayers, unusedId, serverMsg);
130
131 cout << "msg: " << serverMsg.buffer << endl;
132
133 if (broadcastResponse)
134 {
135 cout << "Should be broadcasting the message" << endl;
136
137 map<unsigned int, Player>::iterator it;
138
139 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
140 {
141 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
142 error("sendMessage");
143 }
144 }
145 else
146 {
147 cout << "Should be sending back the message" << endl;
148
149 if ( sendMessage(&serverMsg, sock, &from) < 0 )
150 error("sendMessage");
151 }
152
153 broadcastPlayerPositions(mapPlayers, sock);
154 }
155 }
156
157 return 0;
158}
159
160bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG& serverMsg)
161{
162 DataAccess da;
163
164 cout << "ip address: " << inet_ntoa(from.sin_addr) << endl;
165 cout << "port: " << from.sin_port << endl;
166 cout << "MSG: type: " << clientMsg.type << endl;
167 cout << "MSG contents: " << clientMsg.buffer << endl;
168
169 // maybe we should make a message class and have this be a member
170 bool broadcastResponse = false;
171
172 // Check that if an invalid message is sent, the client will correctly
173 // receive and display the response. Maybe make a special error msg type
174 switch(clientMsg.type)
175 {
176 case MSG_TYPE_REGISTER:
177 {
178 string username(clientMsg.buffer);
179 string password(strchr(clientMsg.buffer, '\0')+1);
180
181 cout << "username: " << username << endl;
182 cout << "password: " << password << endl;
183
184 int error = da.insertPlayer(username, password);
185
186 if (!error)
187 strcpy(serverMsg.buffer, "Registration successful.");
188 else
189 strcpy(serverMsg.buffer, "Registration failed. Please try again.");
190
191 serverMsg.type = MSG_TYPE_REGISTER;
192
193 break;
194 }
195 case MSG_TYPE_LOGIN:
196 {
197 string username(clientMsg.buffer);
198 string password(strchr(clientMsg.buffer, '\0')+1);
199 cout << "Player logging in: " << username << endl;
200
201 Player* p = da.getPlayer(username);
202
203 if (p == NULL || p->password != password)
204 {
205 strcpy(serverMsg.buffer, "Incorrect username or password");
206 }
207 else if(findPlayerByName(mapPlayers, username) != NULL)
208 {
209 strcpy(serverMsg.buffer, "Player has already logged in.");
210 }
211 else
212 {
213 p->setAddr(from);
214 updateUnusedId(unusedId, mapPlayers);
215 p->id = unusedId;
216 mapPlayers[unusedId] = *p;
217
218 // sendd back the new player info to the user
219 p->serialize(serverMsg.buffer);
220 }
221
222 serverMsg.type = MSG_TYPE_LOGIN;
223
224 delete(p);
225
226 break;
227 }
228 case MSG_TYPE_LOGOUT:
229 {
230 string name(clientMsg.buffer);
231 cout << "Player logging out: " << name << endl;
232
233 Player *p = findPlayerByName(mapPlayers, name);
234
235 if (p == NULL)
236 {
237 strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
238 cout << "Player not logged in" << endl;
239 }
240 else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
241 p->addr.sin_port != from.sin_port )
242 {
243 strcpy(serverMsg.buffer, "That player is logged in using a differemt connection. This is either a bug, or you're trying to hack the server.");
244 cout << "Player logged in using a different connection" << endl;
245 }
246 else
247 {
248 if (p->id < unusedId)
249 unusedId = p->id;
250 mapPlayers.erase(p->id);
251 strcpy(serverMsg.buffer, "You have successfully logged out.");
252 cout << "Player logged out successfuly" << endl;
253 }
254
255 // should really be serverMsg.type = MSG_TYPE_LOGOUT;
256 serverMsg.type = MSG_TYPE_LOGIN;
257
258 break;
259 }
260 case MSG_TYPE_CHAT:
261 {
262 cout << "Got a chat message" << endl;
263
264 Player *p = findPlayerByAddr(mapPlayers, from);
265
266 if (p == NULL)
267 {
268 strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
269 }
270 else
271 {
272 broadcastResponse = true;
273
274 stringstream ss;
275 ss << p->name << ": " << clientMsg.buffer;
276
277 strcpy(serverMsg.buffer, ss.str().c_str());
278 }
279
280 serverMsg.type = MSG_TYPE_CHAT;
281
282 break;
283 }
284 default:
285 {
286 strcpy(serverMsg.buffer, "Server error occured. Report this please.");
287
288 serverMsg.type = MSG_TYPE_CHAT;
289
290 break;
291 }
292 }
293
294 return broadcastResponse;
295}
296
297void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
298{
299 while (mapPlayers.find(id) != mapPlayers.end())
300 id++;
301}
Note: See TracBrowser for help on using the repository browser.