Skip to content

Commit

Permalink
Refact: Webserver 클래스, main catch&throw 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
PennyBlack2008 committed Apr 29, 2021
1 parent 2d621a6 commit 556c19f
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 77 deletions.
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

SRCS = $(wildcard ./srcs/*.cpp)\
SRCS = srcs/main.cpp\
srcs/Server/Socket.cpp\
srcs/HttpMessage/HttpMessageRequest.cpp\
srcs/HttpMessage/HttpMessageResponse.cpp\
srcs/Webserver/Webserver.cpp\
srcs/Utils/utils.cpp
# $(wildcard ./srcs/*.cpp)\



Expand All @@ -13,8 +15,8 @@ NAME = webserv

CC = clang++

CFLAG = -Wall -Wextra -Werror -std=c++98 -g3 -fsanitize=address
# CFLAG = -Wall -Wextra -Werror -std=c++98 -g3
# CFLAG = -Wall -Wextra -Werror -std=c++98 -g3 -fsanitize=address
CFLAG = -Wall -Wextra -Werror -std=c++98 -g3

RM = -rm -rf

Expand All @@ -32,7 +34,7 @@ re : fclean all
$(NAME) : $(OBJS)
# $(CC) -c $(SRCS)
$(CC) $(CFLAG) -o $(NAME) $(OBJS)
# ./webserv
./webserv
# $(CC) $(CFLAG) -c $(SRCS)
# $(CC) $(CFLAG) -o $(NAME) $(OBJS)

Expand Down
61 changes: 32 additions & 29 deletions srcs/Server/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@
#include "../Utils/utils.hpp"
#include <fstream>
#include <sstream>
/*
?? port 와 ip 로 들어온 것이 Socket 에서 무슨 역할인지 모르겠다.
Socket::Socket(uint32_t ip, uint16_t port)
{
}
*/

/* Socket 에 이런식으로 넣으면 binding, listen 작업까지 */
Socket::Socket(uint32_t ip, uint16_t port)
{
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
std::cout << "Failed to create socket. errno: " << errno << std::endl;
exit(EXIT_FAILURE);
}
if (fd == -1)
throw (socket_failed_exception());
SetAddr();
Bind(ip, port);
Listen();
Expand All @@ -26,10 +18,7 @@ Socket::Socket(int fd)
{
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
std::cout << "Failed to create socket. errno: " << errno << std::endl;
exit(EXIT_FAILURE);
}
throw (socket_failed_exception());
}

Socket::~Socket()
Expand All @@ -50,23 +39,16 @@ void Socket::Bind(uint32_t ip, uint16_t port)

int opt = 1; // 소켓을 재사용하려면 희한하게도 1로 설정해야한다.
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

// network byte order
if (bind(fd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) < 0) {
std::cout << "Failed to bind to port " << port << ". errno: " << errno << std::endl;
exit(EXIT_FAILURE);
}
if (bind(fd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) < 0)
throw (bind_failed_exception());
// std::cout << "finished binding\n";
}

void Socket::Listen(void)
{
// Start listening. Hold at most 10 connections in the queue
if (listen(fd, 10) < 0)
{
std::cout << "Failed to listen on socket. errno: " << errno << std::endl;
exit(EXIT_FAILURE);
}
throw (listen_failed_exception());
// std::cout << "finished listening\n";
}

Expand All @@ -76,15 +58,36 @@ size_t Socket::Accept(size_t connections)
socklen = sizeof(sockaddr);
connections = accept(fd, (struct sockaddr*)&sockaddr, (socklen_t*)&socklen);
if (connections < 0)
{
std::cout << "Failed to grab connection. errno: " << errno << std::endl;
exit(EXIT_FAILURE);
}
throw (accept_failed_exception());
// std::cout << "finished accepting\n";
return (connections);
}

int Socket::GetFd(void)
{
return (fd);
}
}

/*************************************************************
exceptions
**************************************************************/

const char* Socket::socket_failed_exception::what() const throw()
{
return ("Failed to create socket.");
};

const char* Socket::bind_failed_exception::what() const throw()
{
return ("Failed to bind to port.");
};

const char* Socket::listen_failed_exception::what() const throw()
{
return ("Failed to listen on socket.");
};

const char* Socket::accept_failed_exception::what() const throw()
{
return ("Failed to grab connection.");
};
50 changes: 50 additions & 0 deletions srcs/Webserver/Webserver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "Webserver.hpp"
#define BUFFER_SIZE 100

Webserver::Webserver(int argc, char** argv, char** envp)
{
(void)argc;
(void)argv;
(void)envp;
}

void Webserver::start_server(void)
{
Socket socket_one(INADDR_ANY, 8000);
size_t connections; // 통신 소켓
char buffer[BUFFER_SIZE];
int bytesRead = BUFFER_SIZE - 1;

/* 테스트 진행 */
while (1)
{
bytesRead = BUFFER_SIZE - 1;
connections = socket_one.Accept(connections);
// STUB : Read from the connection
while (bytesRead == BUFFER_SIZE - 1)
{
ft_memset(buffer, 0, BUFFER_SIZE); // REVIEW : 전체 탐색하는 것들은 성능 개선의 여지가 있음
bytesRead = read(connections, buffer, BUFFER_SIZE - 1); // request 를 여기서 받아서..
if (bytesRead == -1)
std::cerr << "Could not read request." << std::endl;
}
if (bytesRead != -1)
{
// STUB : HttpMessageRequest
HttpMessageRequest request(buffer);
request.Parser(); // request 를 parsing 한 후,

// STUB : HttpMessageResponse
HttpMessageResponse response(request); // reponse 를 정리한다.
response.SetMessage();

// STUB : Send a message to the connection
int len = response.GetMessage().size();
int ret = send(connections, response.GetMessage().c_str(), len, 0);
}
// Close the connections
close(connections);
}
close(socket_one.GetFd());

}
27 changes: 27 additions & 0 deletions srcs/Webserver/Webserver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include "../Server/Socket.hpp"
#include "../HttpMessage/HttpMessageRequest.hpp"
#include "../HttpMessage/HttpMessageResponse.hpp"
#include <sys/select.h>
#include <iostream>
#include <vector>
// #include "ConfigWebserver.hpp"
// #include "Path.hpp"

class Webserver
{
// ConfigWebserver config;
// fd_set to_be_checked;
// fd_set to_be_checked_read;
// fd_set to_be_checked_write;
// std::vector<Server> servers;

public:
Webserver(int argc, char** argv, char** envp);
void start_server(void); // 서버 시작
// Webserver(const Path&); // config 파일의 경로를 받아서 초기화
// void create_server(const std::vector<Config>& config_locations);

// private:
// void parse_config(const Path& path_config); // config 파일 해석
};
52 changes: 9 additions & 43 deletions srcs/main.cpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,12 @@
#include "Server/Socket.hpp"
#include "HttpMessage/HttpMessageRequest.hpp"
#include "HttpMessage/HttpMessageResponse.hpp"
#include "Webserver/Webserver.hpp"

#define BUFFER_SIZE 100

int main()
int main(int argc, char** argv, char** env)
{
// Create a socket (IPv4, TCP)
Socket socket(INADDR_ANY, 80); // Listen 작업까지 되어 있는 소켓
size_t connections; // 통신 소켓
char buffer[BUFFER_SIZE];
int bytesRead = BUFFER_SIZE - 1;

/* 테스트 진행 */
while (1)
{
bytesRead = BUFFER_SIZE - 1;
connections = socket.Accept(connections);
// STUB : Read from the connection
while (bytesRead == BUFFER_SIZE - 1)
{
ft_memset(buffer, 0, BUFFER_SIZE); // REVIEW : 전체 탐색하는 것들은 성능 개선의 여지가 있음
bytesRead = read(connections, buffer, BUFFER_SIZE - 1); // request 를 여기서 받아서..
if (bytesRead == -1)
std::cerr << "Could not read request." << std::endl;
}
if (bytesRead != -1)
{
// STUB : HttpMessageRequest
HttpMessageRequest request(buffer);
request.Parser(); // request 를 parsing 한 후,

// STUB : HttpMessageResponse
HttpMessageResponse response(request); // reponse 를 정리한다.
response.SetMessage();

// STUB : Send a message to the connection
int len = response.GetMessage().size();
int ret = send(connections, response.GetMessage().c_str(), len, 0);
}
// Close the connections
close(connections);
try {
Webserver s(argc, argv, env);
s.start_server();
}
catch(const std::exception &e) {
std::cerr << e.what() << std::endl;
}
close(socket.GetFd());
}
}

0 comments on commit 556c19f

Please sign in to comment.