First Commit

Doing Network, in Developement
This commit is contained in:
Debucquoy Anthony 2021-12-15 21:59:17 +01:00
commit 9e418f9193
9 changed files with 243 additions and 0 deletions

36
Config.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "Config.hpp"
#include <iostream>
Config::Config(const char* filename){
setConfFile(filename);
loadConfig();
}
void Config::setConfFile(const char * filename){
config_file.open(filename);
}
void Config::loadConfig(){
std::string key, value;
while(config_file){
getline(config_file, key,':');
config_file >> value;
config_file >> std::ws;
if(!config_file){
break;
}
config_map[key] = value;
}
config_file.close();
}
void Config::printConfig(){
for(std::map<std::string,std::string>::iterator it = config_map.begin(); it != config_map.end(); it++){
std::cout << it->first << " : " << it->second << std::endl;
}
}
std::string Config::getValue(std::string key){
return config_map[key];
}

26
Config.hpp Normal file
View File

@ -0,0 +1,26 @@
#include <map>
#include <string>
#include <fstream>
#ifndef CONFIG_H
#define CONFIG_H
class Config
{
private:
std::map<std::string, std::string> config_map;
std::ifstream config_file;
public:
Config(const char* filename);
void loadConfig();
void setConfFile(const char* filename);
void printConfig();
std::string getValue(std::string key);
};
#endif /* CONFIG_H */

4
Database.cpp Normal file
View File

@ -0,0 +1,4 @@
#include "Database.hpp"
Database::Database(){
}

15
Database.hpp Normal file
View File

@ -0,0 +1,15 @@
#include <pqxx/pqxx>
#ifndef DATABASE_H
#define DATABASE_H
class Database
{
private:
public:
Database();
};
#endif /* DATABASE_H */

18
Makefile Normal file
View File

@ -0,0 +1,18 @@
CC=g++
CFLAGS= -Wall -g
LDLIBS= -lpqxx
SOURCES= $(wildcard *.cpp)
all: main
main: $(SOURCES)
$(CC) $(CFLAGS) -o main $(SOURCES) $(LDLIBS)
clean:
$(RM) main *.o *~
run: main
./main
.PHONY: all clean run

80
Network.cpp Normal file
View File

@ -0,0 +1,80 @@
#include "Network.hpp"
#include <cstring>
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <algorithm>
Network::Network(){
conf = new Config("config.txt");
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);
/* server.sin_addr.s_addr = INADDR_ANY; */
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")));
/* motd = conf->getValue("motd").c_str(); */
/* std::cout << motd << std::endl; */
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, sizeof(&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{
msg[rcv_bytes] = '\0';
std::cout << "he sent :" << msg << std::endl;
if(std::strcmp(msg, "ping")){
send(fd, "pong", 4, 0);
}
}
break;
}
}
}
}
}

46
Network.hpp Normal file
View 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[1024];
const char* motd;
int rcv_bytes;
unsigned int sin_size = sizeof(struct sockaddr*);
Config* conf;
public:
Network();
void run();
};
#endif /* NETWORK_H */

4
config.txt Normal file
View File

@ -0,0 +1,4 @@
ip: 127.0.0.1
port: 2142
conn_limit: 30
motd: welcome

14
main.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "Database.hpp"
#include "Network.hpp"
#include "Config.hpp"
#include <iostream>
int main( int carg, char* varg[] ){
Config conf("config.txt");
Network net;
net.run();
}