From 9e418f9193f62259af4de56fcbbf314208c440c0 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Wed, 15 Dec 2021 21:59:17 +0100 Subject: [PATCH] First Commit Doing Network, in Developement --- Config.cpp | 36 +++++++++++++++++++++++ Config.hpp | 26 +++++++++++++++++ Database.cpp | 4 +++ Database.hpp | 15 ++++++++++ Makefile | 18 ++++++++++++ Network.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Network.hpp | 46 ++++++++++++++++++++++++++++++ config.txt | 4 +++ main.cpp | 14 +++++++++ 9 files changed, 243 insertions(+) create mode 100644 Config.cpp create mode 100644 Config.hpp create mode 100644 Database.cpp create mode 100644 Database.hpp create mode 100644 Makefile create mode 100644 Network.cpp create mode 100644 Network.hpp create mode 100644 config.txt create mode 100644 main.cpp diff --git a/Config.cpp b/Config.cpp new file mode 100644 index 0000000..896717f --- /dev/null +++ b/Config.cpp @@ -0,0 +1,36 @@ +#include "Config.hpp" +#include + +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::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]; +} + diff --git a/Config.hpp b/Config.hpp new file mode 100644 index 0000000..3d19521 --- /dev/null +++ b/Config.hpp @@ -0,0 +1,26 @@ +#include +#include +#include + +#ifndef CONFIG_H +#define CONFIG_H + +class Config +{ +private: + std::map 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 */ diff --git a/Database.cpp b/Database.cpp new file mode 100644 index 0000000..fe106ee --- /dev/null +++ b/Database.cpp @@ -0,0 +1,4 @@ +#include "Database.hpp" + +Database::Database(){ +} diff --git a/Database.hpp b/Database.hpp new file mode 100644 index 0000000..47aa1b7 --- /dev/null +++ b/Database.hpp @@ -0,0 +1,15 @@ +#include + +#ifndef DATABASE_H +#define DATABASE_H + +class Database +{ +private: + + +public: + Database(); +}; + +#endif /* DATABASE_H */ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..86c8229 --- /dev/null +++ b/Makefile @@ -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 diff --git a/Network.cpp b/Network.cpp new file mode 100644 index 0000000..e540fcf --- /dev/null +++ b/Network.cpp @@ -0,0 +1,80 @@ +#include "Network.hpp" +#include +#include +#include +#include +#include + +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; + } + } + + } + } +} diff --git a/Network.hpp b/Network.hpp new file mode 100644 index 0000000..f122645 --- /dev/null +++ b/Network.hpp @@ -0,0 +1,46 @@ +/****************************************************************************** +* File: network.hpp +* +* Author: Tonitch +* Created: 12/10/21 +* Description: Header for meuporg (name might change) for network part +*****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include "Config.hpp" + +#ifndef NETWORK_H +#define NETWORK_H +class Network +{ +private: + int main_socket; + std::vector 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 */ + + + diff --git a/config.txt b/config.txt new file mode 100644 index 0000000..90fbde1 --- /dev/null +++ b/config.txt @@ -0,0 +1,4 @@ +ip: 127.0.0.1 +port: 2142 +conn_limit: 30 +motd: welcome diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..2222d54 --- /dev/null +++ b/main.cpp @@ -0,0 +1,14 @@ +#include "Database.hpp" +#include "Network.hpp" +#include "Config.hpp" + +#include + +int main( int carg, char* varg[] ){ + +Config conf("config.txt"); + +Network net; +net.run(); + +}