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

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

The server sends back an error on failed registration and sends chat messages to all players, not just the sender. The non-blocking socket function was moved to a new file in the common folder so the client can use it.

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