1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
//============================================================================
// Name : drawdown_server.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <iostream>
#include <pthread.h>
#include <mysql/mysql.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <unistd.h>
#include <vector>
#include <poll.h>
#define PORT 6666
#define BUFFSIZE 255
using namespace std;
struct sockaddr_in address;
bool server_online;
int master_socket;
int test_user_count = 1;
struct player {
int socket;
int database_id;
int online; // 0 OFFLINE (SAVE), 1 ONLINE, 2 SAVED
int available; // 1 AVAILABLE, 0 NOT AVAILABLE
string first_name;
string profile_picture;
int points;
int wins;
int diamonds;
int sos;
};
struct request {
int socket;
player* player_info;
string action;
vector<string> token;
vector<int> token_int;
};
struct room {
player* players;
int pot;
};
pthread_mutex_t players_lock;
vector<request>database_requests;
vector<request>game_requests;
vector<player>players;
int sendall(int s, char *buf, int *len) {
int total = 0; // how many bytes we've sent
int bytesleft = *len; // how many we have left to send
int n;
while (total < *len) {
n = send(s, buf + total, bytesleft, 0);
if (n == -1) {
break;
}
total += n;
bytesleft -= n;
}
*len = total; // return number actually sent here
return n == -1 ? -1 : 0; // return -1 on failure, 0 on success
}
void* connectionThread(void*){
vector<pollfd> poll_sockets;
pollfd* poll_sockets_ptr;
pollfd main_socket;
main_socket.fd = master_socket;
main_socket.events = POLLIN;
poll_sockets.push_back(main_socket);
int addrlen = sizeof(address);
while(server_online){
poll_sockets_ptr = &poll_sockets[0];
int poll_activity = poll(poll_sockets_ptr, poll_sockets.size(), 0);
if(poll_activity <0){
perror("poll");
}
if(poll_sockets[0].revents & POLLIN){
int new_socket;
if ((new_socket = accept(master_socket, (struct sockaddr*)&address, (socklen_t*)&addrlen))<0){
perror("accept");
}
cout << "Client " << new_socket << " (" << inet_ntoa(address.sin_addr) << ":" << ntohs(address.sin_port) << ") has connected!" << endl;
pollfd add_socket;
add_socket.fd = new_socket;
add_socket.events = POLLIN;
poll_sockets.push_back(add_socket);
}
for(int i=0; i<poll_sockets.size();i++){
int s = poll_sockets[i].fd;
if(i!=0){
if(poll_sockets[i].revents & POLLIN){
char buffer[BUFFSIZE];
if((read(s, buffer, BUFFSIZE))==0){
cout << "Client " << s << " has disconnected!" << endl;
poll_sockets.erase(poll_sockets.begin()+i);
pthread_mutex_lock(&players_lock);
for(int b = 0; b<players.size(); b++){
if(players[b].socket == s){
players[b].online = 0;
}
}
pthread_mutex_unlock(&players_lock);
}else{
vector<string> client_messages;
char *msg_tok;
msg_tok = strtok(buffer,";");
while(msg_tok != NULL){
client_messages.push_back(msg_tok);
msg_tok = strtok(NULL,";");
}
bool is_player_logged_in = false;
pthread_mutex_lock(&players_lock);
for(int a = 0; a<players.size(); a++){
if(s == players[a].socket){
is_player_logged_in = true;
}
}
pthread_mutex_unlock(&players_lock);
if(client_messages.size() == 4){
if(client_messages[0] == "LOGIN" && !is_player_logged_in){
request add_request;
add_request.socket = s;
add_request.action = client_messages[0];
add_request.token.push_back(client_messages[1]);
add_request.token.push_back(client_messages[2]);
database_requests.push_back(add_request);
}
if(client_messages[0] == "JOIN_QUEUE" && is_player_logged_in){
request add_request;
add_request.socket = s;
add_request.action = client_messages[0];
add_request.token.push_back(client_messages[1]);
add_request.token.push_back(client_messages[2]);
game_requests.push_back(add_request);
}
}
}
}
}
}
}
return 0;
}
player loginUser(int socket, string username, string password){
player create_player;
create_player.socket = socket;
create_player.first_name = "TestUser";
create_player.database_id = test_user_count;
create_player.online = 1;
create_player.available = 1;
create_player.profile_picture = "TestUser.png";
create_player.diamonds = 50;
create_player.points = 0;
create_player.wins = 0;
create_player.sos = 0;
test_user_count++;
return create_player;
}
void* databaseRequestThread(void*){
while(server_online){
if(database_requests.size()> 0){
if(database_requests[0].action == "LOGIN"){
player add_player = loginUser(database_requests[0].socket, database_requests[0].token[0], database_requests[0].token[1]);
pthread_mutex_lock(&players_lock);
players.push_back(add_player);
cout << "Added player " << endl;
pthread_mutex_unlock(&players_lock);
}
database_requests.erase(database_requests.begin());
}
}
return 0;
}
void* gameRequestThread(void*){
while(server_online){
if(game_requests.size()>0){
if(game_requests[0].action == "JOIN_QUEUE"){
}
game_requests.erase(game_requests.begin());
}
}
return 0;
}
void* saveSweepThread(void*){
while(server_online){
pthread_mutex_lock(&players_lock);
for(int i=0;i<players.size();i++){
if(players[i].online == 0){
players[i].online = -1;
//SAVE
cout << "Saved player " << players[i].first_name << endl;
players.erase(players.begin()+i);
}
}
pthread_mutex_lock(&players_lock);
}
return 0;
}
void initServer(){
master_socket = socket(AF_INET,SOCK_STREAM,0);
address.sin_port = htons(PORT);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
if(bind(master_socket, (struct sockaddr*)&address, sizeof(address))<0){
perror("bind");
exit(EXIT_FAILURE);
}
if((listen(master_socket, 20))<0){
perror("listen");
exit(EXIT_FAILURE);
}
server_online = true;
}
bool close_server(){
close(master_socket);
return true;
}
int main(int argc, char* argv[]){
initServer();
pthread_t t1, t2, t3, t4;
if (pthread_mutex_init(&players_lock, NULL) != 0)
{
perror("mutexlock");
exit(EXIT_FAILURE);
}
pthread_create(&t1, NULL, connectionThread, NULL);
pthread_create(&t2, NULL, gameRequestThread, NULL);
pthread_create(&t3, NULL, databaseRequestThread, NULL);
pthread_create(&t4, NULL, saveSweepThread, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
pthread_join(t4, NULL);
pthread_mutex_destroy(&players_lock);
if(close_server()){
return 0;
}
}
|