Skip to content

Commit

Permalink
Merge pull request #25 from Webserve4242/17-implement-socket
Browse files Browse the repository at this point in the history
implement) socket
  • Loading branch information
molhot authored Sep 7, 2023
2 parents 927fef3 + 374b356 commit daf8a7d
Show file tree
Hide file tree
Showing 9 changed files with 527 additions and 10 deletions.
28 changes: 24 additions & 4 deletions .github/workflows/gtest.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: UNIT_TEST
on: [push]
jobs:
Unit_test:
Linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -11,12 +11,32 @@ jobs:
cmake -S . -B build -DCUSTOM_FLAGS="-D DEBUG"
cmake --build build
- name: run all tests
run: ./build/unit_test 2>/dev/null
- name: run all tests on Linux
run: ./build/unit_test

- uses: sarisia/actions-status-discord@v1
if: always()
with:
title: "UNIT_TEST"
title: "UNIT_TEST_ON_LINUX"
webhook: ${{ secrets.DISCORD_WEBHOOK }}
status: ${{ job.status }}

macOS:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3

- name: build
run: |
cmake -S . -B build -DCUSTOM_FLAGS="-D DEBUG"
cmake --build build
- name: run all tests on macOS
run: ./build/unit_test

- uses: sarisia/actions-status-discord@v1
if: always()
with:
title: "UNIT_TEST_ON_MACOS"
webhook: ${{ secrets.DISCORD_WEBHOOK }}
status: ${{ job.status }}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
!srcs
!srcs/Debug
!srcs/Error
!srcs/Socket
!www
!includes
!test
!playground

!*.cpp
!*.hpp
Expand All @@ -29,4 +31,4 @@
.vscode
.DS_Store
CMakeCache.txt
Dockerfile
Dockerfile
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ include_directories(
includes
srcs/Debug
srcs/Error
srcs/Socket
)

# webserv_srcs -----------------------------------------------------------------
set(webserv_srcs
srcs/get_valid_config_file_path.cpp
srcs/Debug/Debug.cpp
srcs/Error/Error.cpp
)
srcs/Socket/Socket.cpp
)

add_executable(webserv
srcs/main.cpp
Expand All @@ -53,7 +55,8 @@ set (unit_test_srcs
test/unit_test/is_valid_file_path/test_get_valid_config_file_path.cpp
test/unit_test/TestError.cpp
test/unit_test/TestResult.cpp
)
test/unit_test/TestSocket.cpp
)

add_executable(unit_test
${webserv_srcs}
Expand Down
23 changes: 20 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ SRCS_DIR = srcs
#main
SRCS = main.cpp \
get_valid_config_file_path.cpp

#error
ERROR_DIR = Error
SRCS += $(ERROR_DIR)/Error.cpp

#debug
DEBUG_DIR = Debug
SRCS += $(DEBUG_DIR)/Debug.cpp

#socket
SOCKET_DIR = Socket
SRCS += $(SOCKET_DIR)/Socket.cpp


# OBJS -------------------------------------------------------------------------
OBJS_DIR = objs
Expand All @@ -27,7 +36,9 @@ DEPS = $(OBJS:%.o=%.d)
# INCLUDES ---------------------------------------------------------------------
INCLUDES_DIR = includes \
$(SRCS_DIR)/$(DEBUG_DIR) \
$(SRCS_DIR)/$(ERROR_DIR)
$(SRCS_DIR)/$(ERROR_DIR) \
$(SRCS_DIR)/$(SOCKET_DIR)

INCLUDES = $(addprefix -I, $(INCLUDES_DIR))


Expand Down Expand Up @@ -57,7 +68,6 @@ re : fclean all
lint :
cpplint --recursive srcs


.PHONY : run_unit_test
run_unit_test :
#rm -rf build
Expand All @@ -67,7 +77,6 @@ run_unit_test :
./build/unit_test 2>/dev/null
#./build/unit_test


.PHONY : run_result_test
run_result_test :
#rm -rf build
Expand All @@ -82,4 +91,12 @@ run_err_test :
cmake --build build
./build/unit_test --gtest_filter=ErrorMessage*

.PHONY : run_socket_test
run_socket_test :
#rm -rf build
cmake -S . -B build -DCUSTOM_FLAGS="-D DEBUG"
cmake --build build
./build/unit_test --gtest_filter=SocketUnitTest.*:SocketIntegrationTest.*


-include $(DEPS)
5 changes: 5 additions & 0 deletions playground/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*

!.gitignore
!*.cpp
!*.hpp
69 changes: 69 additions & 0 deletions playground/playground_socket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <fcntl.h>
#include <netdb.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include <string>
#include <iostream>

int test_socket(const char *server_ip, const char *server_port) {
int errcode;
int socket_fd;
int ai_family, ai_socktype, ai_protocol;
struct addrinfo *addr_info;
struct addrinfo hints = {};
// const char *server_ip = "127.0.0.1";
// const char *server_port = "65536";

printf("ip:%s, port:%s\n", server_ip, server_port);

hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_UNSPEC; // allows IPv4 and IPv6
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV; // socket, IP, PORT
hints.ai_protocol = IPPROTO_TCP;

errcode = getaddrinfo(server_ip, server_port, &hints, &addr_info);
if (errcode != 0) {
std::cerr << "[Error] getaddrinfo:" << gai_strerror(errcode) << std::endl;
return 1;
}
// std::cout << "errcode:" << errcode << ", " <<

ai_family = addr_info->ai_family;
ai_socktype = addr_info->ai_socktype;
ai_protocol = addr_info->ai_protocol;
errno = 0;
std::cout << "socket" << std::endl;
socket_fd = socket(ai_family, ai_socktype, ai_protocol);
if (socket_fd == -1) {
std::cerr << "[Error] socket:" << strerror(errno) << std::endl;
return 1;
}

std::cout << "bind" << std::endl;
errno = 0;
if (bind(socket_fd, addr_info->ai_addr, addr_info->ai_addrlen) == -1) {
std::cerr << "[Error] bind:" << strerror(errno) << std::endl;
return 1;
}

std::cout << "listen" << std::endl;
if (listen(socket_fd, SOMAXCONN) == -1) {
std::cerr << "[Error] listen:" << strerror(errno) << std::endl;
return 1;
}

std::cout << "OK" << std::endl;
return 0;
}

// port 0, 0はエラーにならない
int main() {
int ret1 = test_socket("255.255.255.254", "8080");
int ret2 = test_socket("255.255.255.254", "8080");

std::cout << "ret1:" << ret1 << std::endl;
std::cout << "ret2:" << ret2 << std::endl;
}
Loading

0 comments on commit daf8a7d

Please sign in to comment.