source: network-game/server/server.cpp@ 594d2e9

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