Last Update (partial) have to link every file in the right place
This commit is contained in:
33
server/Database.cpp
Normal file
33
server/Database.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include "Database.hpp"
|
||||
#include "Config.hpp"
|
||||
|
||||
Database::Database(){
|
||||
Config conf("config.txt");
|
||||
std::string pgsqlConnectingString;
|
||||
|
||||
pgsqlConnectingString
|
||||
.append(" host=").append(conf.getValue("db_host"))
|
||||
.append(" dbname=").append(conf.getValue("db_name"))
|
||||
.append(" user=").append(conf.getValue("db_user"))
|
||||
.append(" password=").append(conf.getValue("db_pass"));
|
||||
std::cout << pgsqlConnectingString << std::endl;
|
||||
|
||||
dbconn = PQconnectdb(pgsqlConnectingString.c_str());
|
||||
}
|
||||
|
||||
void Database::execute(std::string cmd){
|
||||
PQexec(dbconn, cmd.c_str());
|
||||
}
|
||||
|
||||
PGresult* Database::fetch(std::string cmd){
|
||||
PGresult *r = PQexec(dbconn, cmd.c_str());
|
||||
return r;
|
||||
|
||||
// Don't forget to close after using this request
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Database::disconnect(){
|
||||
PQfinish(dbconn);
|
||||
}
|
25
server/Database.hpp
Normal file
25
server/Database.hpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <libpq-fe.h>
|
||||
#include <vector>
|
||||
|
||||
#ifndef DATABASE_H
|
||||
#define DATABASE_H
|
||||
|
||||
class Database
|
||||
{
|
||||
private:
|
||||
|
||||
PGconn *dbconn;
|
||||
|
||||
public:
|
||||
Database();
|
||||
|
||||
void execute(std::string cmd);
|
||||
|
||||
PGresult* fetch(std::string cmd);
|
||||
|
||||
void disconnect();
|
||||
};
|
||||
|
||||
#endif /* DATABASE_H */
|
82
server/Network.cpp
Normal file
82
server/Network.cpp
Normal file
@ -0,0 +1,82 @@
|
||||
#include "Network.hpp"
|
||||
#include <cstring>
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#include <algorithm>
|
||||
|
||||
Network::Network(){
|
||||
conf = new Config("config.txt");
|
||||
|
||||
strcpy(motd, conf->getValue("motd").c_str());
|
||||
strcat(motd, "\n");
|
||||
|
||||
memset(&server, 0, sizeof(server));
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = htons(stoi(conf->getValue("port")));
|
||||
inet_aton(conf->getValue("ip").c_str(), &server.sin_addr);
|
||||
|
||||
main_socket = socket(server.sin_family, SOCK_STREAM, 0);
|
||||
if(main_socket == -1){
|
||||
perror("Socket");
|
||||
}
|
||||
|
||||
if(bind(main_socket, (sockaddr*)&server, sizeof(server))){
|
||||
perror("Bind");
|
||||
}
|
||||
|
||||
if(listen(main_socket, 5) == -1){
|
||||
perror("Listeen");
|
||||
}
|
||||
}
|
||||
void Network::run(){
|
||||
|
||||
accepted_sock.resize(stoi(conf->getValue("conn_limit")));
|
||||
|
||||
while(!will_close){
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(main_socket, &readfds);
|
||||
max_fd = main_socket;
|
||||
|
||||
//construct fd_set and get max fd
|
||||
for(int fd: accepted_sock){
|
||||
if(fd > 0){
|
||||
FD_SET(fd, &readfds);
|
||||
if(max_fd < fd){ max_fd = fd; }
|
||||
}
|
||||
}
|
||||
|
||||
if(select(max_fd+1, &readfds, NULL, NULL, NULL) < 0){
|
||||
perror("select");
|
||||
}
|
||||
|
||||
if(FD_ISSET(main_socket, &readfds)){
|
||||
int _incomming_socket = accept(main_socket, (struct sockaddr*)&new_sockaddr, &sin_size);
|
||||
if(_incomming_socket == -1){
|
||||
perror("accept");
|
||||
}
|
||||
accepted_sock.push_back(_incomming_socket);
|
||||
std::cout << "New Connection: " << inet_ntoa(new_sockaddr.sin_addr) << std::endl;
|
||||
if(send(_incomming_socket, motd, strlen(motd), 0) == -1){perror("sendt");}
|
||||
}else{
|
||||
for(int fd: accepted_sock){
|
||||
if(FD_ISSET(fd, &readfds)){
|
||||
if((rcv_bytes = recv(fd, &msg, sizeof(msg),0)) == 0){
|
||||
std::cout << "disconnected" << std::endl;
|
||||
close(fd);
|
||||
accepted_sock.erase(std::find(accepted_sock.begin(), accepted_sock.end(), fd));
|
||||
}else{
|
||||
std::cout << "he sent :" << rcv_bytes << " bytes and it says: " << msg;
|
||||
if(std::strncmp("ping", msg, rcv_bytes-1) == 0){
|
||||
send(fd, "pong", 5, 0);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
46
server/Network.hpp
Normal file
46
server/Network.hpp
Normal file
@ -0,0 +1,46 @@
|
||||
/******************************************************************************
|
||||
* File: network.hpp
|
||||
*
|
||||
* Author: Tonitch
|
||||
* Created: 12/10/21
|
||||
* Description: Header for meuporg (name might change) for network part
|
||||
*****************************************************************************/
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
#include <vector>
|
||||
#include <sys/select.h>
|
||||
#include "Config.hpp"
|
||||
|
||||
#ifndef NETWORK_H
|
||||
#define NETWORK_H
|
||||
class Network
|
||||
{
|
||||
private:
|
||||
int main_socket;
|
||||
std::vector<int> accepted_sock;
|
||||
int max_fd;
|
||||
|
||||
fd_set readfds;
|
||||
|
||||
sockaddr_in server, new_sockaddr;
|
||||
bool will_close = false;
|
||||
char msg[1025], motd[1025];
|
||||
|
||||
int rcv_bytes;
|
||||
unsigned int sin_size = sizeof(struct sockaddr*);
|
||||
|
||||
Config* conf;
|
||||
|
||||
public:
|
||||
Network();
|
||||
void run();
|
||||
};
|
||||
|
||||
|
||||
#endif /* NETWORK_H */
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user