diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6a8bc10 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode +build \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..fa0b79d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.15) + +project(ouc_server VERSION 0.0.1 LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static -g -O2") + +include_directories(${PROJECT_SOURCE_DIR}/include) + +add_executable(test "${PROJECT_SOURCE_DIR}/example/epoll_tcp_loop.cpp") \ No newline at end of file diff --git a/example/epoll_tcp_loop.cpp b/example/epoll_tcp_loop.cpp new file mode 100644 index 0000000..1d1381c --- /dev/null +++ b/example/epoll_tcp_loop.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include +#include +#include + +/* set nonblock socket */ +int set_nonblock(int fd) +{ + int flags = fcntl(fd, F_GETFL /* Get file discriptor state */, 0); + return fcntl(fd, F_SETFL /* Set new file discriptor */, flags | O_NONBLOCK); +} + +int main() +{ + int sock_fd = socket( + AF_INET, /* IPv4 */ + SOCK_STREAM, /* TCP */ + 0 /* File Descriptor */); + if (sock_fd < 0) + { + perror("socket create error"); + return 1; + } + + int opt = 1; + setsockopt( + sock_fd, + SOL_SOCKET, + SO_REUSEADDR, /* Allow to bind port muiltiple times */ + &opt, + sizeof(opt)); + + sockaddr_in addr{}; /* Internet socket address */ + addr.sin_family = AF_INET; /* IPv4 */ + addr.sin_port = htons(8080); /* Port */ + addr.sin_addr.s_addr = INADDR_ANY; /* listen all */ + + if (bind(sock_fd, (sockaddr *)&addr, sizeof(addr))) /* Bind file descriptor with socket address. */ + { + perror("bind address error"); + return 1; + } + if (listen(sock_fd, SOMAXCONN /* Max wait queue that system allows. */)) /* Listen the file descriptor */ + { + perror("set listen queue error"); + return 1; + } + set_nonblock(sock_fd); + + puts("Server listening on port 8080..."); + + int epoll_fd = epoll_create1(0); /* create epoll instance */ + epoll_event ev{}; /* create epoll event instance*/ + ev.events = EPOLLIN; /**/ + ev.data.fd = sock_fd; + epoll_ctl( + epoll_fd, + EPOLL_CTL_ADD, /* Add a file descriptor into epoll to be listened. */ + sock_fd, + &ev); + + const int MAX_EVENTS = 64; + epoll_event events[MAX_EVENTS]; + + while (true) + { + int n = epoll_wait(epoll_fd, events, MAX_EVENTS, -1); + for (int i = 0; i < n; ++i) + { + int fd = events[i].data.fd; + if (fd == sock_fd) + { + while (true) + { + int client_fd = accept(sock_fd, nullptr, nullptr); + if (client_fd < 0) + break; + printf("New client connected: %d\n", client_fd); + set_nonblock(client_fd); + + epoll_event cli_ev{}; + cli_ev.events = EPOLLIN; /* Wait for new epoll connection */ + cli_ev.data.fd = client_fd; + epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &cli_ev); + } + } + else + { + char buf[1024]; + memset(buf, 0, sizeof(buf)); + ssize_t r = read(fd, buf, sizeof(buf)); + write(fd, buf, r); /* Echo data into buffer */ + + if (r <= 0 || (!memcmp("exit", buf, 4))) + { + epoll_ctl( + epoll_fd, + EPOLL_CTL_DEL, /* Delete file descriptor in epoll */ + fd, + nullptr); + close(fd); + continue; + } + + printf(buf); + } + } + } + + close(epoll_fd); + close(sock_fd); + return 0; +} \ No newline at end of file diff --git a/example/simpliest_tcp_loop.cpp b/example/simpliest_tcp_loop.cpp new file mode 100644 index 0000000..49e01f6 --- /dev/null +++ b/example/simpliest_tcp_loop.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include + +int main() +{ + int sock_fd = socket( + AF_INET, /* IPv4 */ + SOCK_STREAM, /* TCP */ + 0 /* File Descriptor */); + if (sock_fd < 0) + { + perror("socket create error"); + return 1; + } + + int opt = 1; + setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + + sockaddr_in addr{}; /* Internet socket address */ + addr.sin_family = AF_INET; /* IPv4 */ + addr.sin_port = htons(8080); /* Port */ + addr.sin_addr.s_addr = INADDR_ANY; /* listen all */ + + if (bind(sock_fd, (sockaddr *)&addr, sizeof(addr))) /* Bind file descriptor with socket address. */ + { + perror("bind address error"); + return 1; + } + if (listen(sock_fd, SOMAXCONN /* Max wait queue that system allows. */)) /* Listen the file descriptor */ + { + perror("set listen queue error"); + return 1; + } + + puts("Server listening on port 8080..."); + + int client_fd = 0; + while (true) + { + if (client_fd < 0) + { + perror("accept socket error"); + continue; + } + else if (client_fd == 0) + { + client_fd = accept(sock_fd, nullptr, nullptr); /* Accept from client */ + if (client_fd > 0) + printf("New client connected: %d\n", client_fd); + continue; + } + + char buf[1024]; + memset(buf, 0, sizeof(buf)); + ssize_t n = read(client_fd, buf, sizeof(buf)); /* Get data length */ + if (n > 0) + { + if (!memcmp("exit", buf, 4)) + break; + write(client_fd, buf, n); /* Echo data into buffer */ + printf(buf); + } + } + + close(client_fd); + close(sock_fd); + return 0; +} \ No newline at end of file diff --git a/example/test_executable.cpp b/example/test_executable.cpp new file mode 100644 index 0000000..f265c9a --- /dev/null +++ b/example/test_executable.cpp @@ -0,0 +1,6 @@ +#include +int main() +{ + puts("Hello world!"); + return 0; +} \ No newline at end of file diff --git a/include/ouc_server/http/http_request.hpp b/include/ouc_server/http/http_request.hpp new file mode 100644 index 0000000..f62ade4 --- /dev/null +++ b/include/ouc_server/http/http_request.hpp @@ -0,0 +1,29 @@ +#ifndef INCLUDE_OUC_SERVER_HTTP_REQUEST +#define INCLUDE_OUC_SERVER_HTTP_REQUEST + +#include + +namespace ouc_server +{ + namespace http + { + enum class HttpMethodType + { + Get, + Head, + Post, + Put, + Delete, + Connect, + Options, + Trace + }; + + struct HttpRequest + { + HttpMethodType method; + }; + } +} + +#endif \ No newline at end of file