-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement) socket #18
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
44636ce
socket作成部分を作成
molhot 2584313
update: mv files and update Makefile after cherry-pick
ak0327 03f81d2
update(socket): update Socket class, and add getter
ak0327 a6f9155
fix(is_valid_config_file_path): change to throw error when argc == 1
ak0327 53d8f2a
add(test): add test for get_valid_confg_file_path, use gtest
ak0327 2286b8d
add(test): add test_socket
ak0327 034e571
add playground_socket
ak0327 8e999c4
update(yml): add notification title
ak0327 abf7002
update(cmake): add CMAKE_CXX_FLAGS
ak0327 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: UNIT_TEST | ||
on: [push] | ||
jobs: | ||
Linux: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: build | ||
run: | | ||
cmake -S . -B build | ||
cmake --build build | ||
|
||
- name: run all tests on Linux | ||
run: ./build/unit_test | ||
|
||
- uses: sarisia/actions-status-discord@v1 | ||
if: always() | ||
with: | ||
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 | ||
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: UNIT_TEST | ||
name: UNIT_TEST_SH | ||
on: [push] | ||
jobs: | ||
run-unit-test: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
cmake_minimum_required(VERSION 3.23) | ||
project(webserv) | ||
|
||
set(CMAKE_CXX_STANDARD 98) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
set(CMAKE_CXX_FLAGS "-std=c++98 -Wall -Wextra -Werror -pedantic") | ||
|
||
# google test ------------------------------------------------------------------ | ||
include(FetchContent) | ||
include(GoogleTest) | ||
|
||
FetchContent_Declare( | ||
googletest | ||
DOWNLOAD_EXTRACT_TIMESTAMP true | ||
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip | ||
) | ||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) | ||
FetchContent_MakeAvailable(googletest) | ||
enable_testing() | ||
|
||
|
||
# when you .cpp or .hpp is added, add the following | ||
# include, webserv srcs, unit_test_srcs | ||
# includes --------------------------------------------------------------------- | ||
include_directories( | ||
includes | ||
srcs/Socket | ||
) | ||
|
||
# webserv_srcs ----------------------------------------------------------------- | ||
set(webserv_srcs | ||
srcs/get_valid_config_file_path.cpp | ||
srcs/Socket/Socket.cpp | ||
) | ||
|
||
add_executable(webserv | ||
srcs/main.cpp | ||
${webserv_srcs} | ||
) | ||
|
||
# unit_test_srcs --------------------------------------------------------------- | ||
set (unit_test_srcs | ||
test/unit_test/is_valid_file_path/test_get_valid_config_file_path.cpp | ||
test/unit_test/TestSocket.cpp | ||
) | ||
|
||
add_executable(unit_test | ||
${webserv_srcs} | ||
${unit_test_srcs} | ||
) | ||
|
||
# test ------------------------------------------------------------------------- | ||
target_link_libraries( | ||
unit_test | ||
GTest::gtest_main | ||
) | ||
|
||
gtest_discover_tests(unit_test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
* | ||
|
||
!.gitignore | ||
!*.cpp | ||
!*.hpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このファイルはどういう背景で存在しているのかお聞きしたいです!