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

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

Changed the player serialization code

  • Property mode set to 100644
File size: 7.7 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
[da692b9]218 strcpy(serverMsg.buffer, "Login successful. Enjoy chatting with other players.");
[07028b9]219 }
220
[8e540f4]221 serverMsg.type = MSG_TYPE_LOGIN;
[41ad8ed]222
223 delete(p);
[07028b9]224
[8e540f4]225 break;
226 }
227 case MSG_TYPE_LOGOUT:
228 {
229 string name(clientMsg.buffer);
230 cout << "Player logging out: " << name << endl;
231
[01d0d00]232 Player *p = findPlayerByName(mapPlayers, name);
[633f42a]233
[8e540f4]234 if (p == NULL)
235 {
236 strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
[07028b9]237 }
[01d0d00]238 else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
239 p->addr.sin_port != from.sin_port )
[07028b9]240 {
[8e540f4]241 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.");
[2488852]242 }
[8e540f4]243 else
[2488852]244 {
[01d0d00]245 if (p->id < unusedId)
246 unusedId = p->id;
247 mapPlayers.erase(p->id);
[41ad8ed]248 strcpy(serverMsg.buffer, "You have successfully logged out.");
[8e540f4]249 }
[07028b9]250
[8e540f4]251 break;
252 }
253 case MSG_TYPE_CHAT:
254 {
[da692b9]255 cout << "Got a chat message" << endl;
256
[01d0d00]257 Player *p = findPlayerByAddr(mapPlayers, from);
[07028b9]258
[8e540f4]259 if (p == NULL)
260 {
261 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]262 }
[8e540f4]263 else
264 {
[3b1efcc]265 broadcastResponse = true;
266
267 stringstream ss;
[da692b9]268 ss << p->name << ": " << clientMsg.buffer;
[3b1efcc]269
270 strcpy(serverMsg.buffer, ss.str().c_str());
[8e540f4]271 }
272
273 serverMsg.type = MSG_TYPE_CHAT;
274
275 break;
[e084950]276 }
[8e540f4]277 default:
278 {
279 strcpy(serverMsg.buffer, "Server error occured. Report this please.");
[e084950]280
[8e540f4]281 serverMsg.type = MSG_TYPE_CHAT;
[e084950]282
[8e540f4]283 break;
284 }
[e3535b3]285 }
[da692b9]286
287 return broadcastResponse;
[e3535b3]288}
[da692b9]289
[1106210]290void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
[01d0d00]291{
[1106210]292 while (mapPlayers.find(id) != mapPlayers.end())
293 id++;
[01d0d00]294}
Note: See TracBrowser for help on using the repository browser.