diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e43c93..7c3e386 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,6 @@ include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/include ) -add_executable(test1 - src/test1.cpp -) \ No newline at end of file +add_executable(web + ${CMAKE_CURRENT_SOURCE_DIR}/src/web.cpp +) diff --git a/include/asyncio.hpp b/include/asyncio.hpp index e69de29..c7b6baa 100644 --- a/include/asyncio.hpp +++ b/include/asyncio.hpp @@ -0,0 +1,3 @@ +namespace zt{ + +} \ No newline at end of file diff --git a/include/demo.hpp b/include/demo.hpp new file mode 100644 index 0000000..b2fd4cf --- /dev/null +++ b/include/demo.hpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include + +namespace zt { + namespace demo{ + template + struct Promise; + + template + struct coroutine: std::coroutine_handle>{ + using promise_type = Promise; + }; + + struct RAII{ + std::string name; + RAII(const std::string &s):name(s){ + std::cout<<__FUNCTION__<<" "< + struct Promise{ + Promise():raii("in promise"){}; + RAII raii; + auto initial_suspend(){ + std::cout<<__FUNCTION__<<'\n'; + return std::suspend_always{}; + // return std::suspend_never{}; + } + auto final_suspend()noexcept{//最后要不要把句柄执行权限归还,如果不归还,那么协程被销毁,继续检查.done()则Segmentation fault + std::cout<<__FUNCTION__<<'\n'; + return std::suspend_always{}; + } + void unhandled_exception(){ + std::cout<<__FUNCTION__<<'\n'; + throw std::runtime_error("unhandled exeception\n"); + } + void return_value(const std::decay_t &t){ + std::cout<<__FUNCTION__<<' '< get_return_object(){ + std::cout<<__FUNCTION__<<'\n'; + return {coroutine::from_promise(*this)}; + } + auto yield_value(const std::decay_t &t){ + std::cout<<__FUNCTION__<<' '< h; + }; + } +} \ No newline at end of file diff --git a/src/test1.cpp b/src/test1.cpp deleted file mode 100644 index 67fcb13..0000000 --- a/src/test1.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include -#include -#include -#include -#include - -template -struct Promise; - -template -struct coroutine: std::coroutine_handle>{ - using promise_type = ::Promise; -}; - -struct RAII{ - std::string name; - RAII(const std::string &s):name(s){ - std::cout<<__FUNCTION__<<" "< -struct Promise{ - Promise():raii("in promise"){}; - RAII raii; - auto initial_suspend(){ - std::cout<<__FUNCTION__<<'\n'; - return std::suspend_always{}; - // return std::suspend_never{}; - } - auto final_suspend()noexcept{//最后要不要把句柄执行权限归还,如果不归还,那么协程被销毁,继续检查.done()则Segmentation fault - std::cout<<__FUNCTION__<<'\n'; - return std::suspend_always{}; - } - void unhandled_exception(){ - std::cout<<__FUNCTION__<<'\n'; - throw std::runtime_error("unhandled exeception\n"); - } - void return_value(const std::decay_t &t){ - std::cout<<__FUNCTION__<<' '< get_return_object(){ - std::cout<<__FUNCTION__<<'\n'; - return {coroutine::from_promise(*this)}; - } - auto yield_value(const std::decay_t &t){ - std::cout<<__FUNCTION__<<' '< h; -}; - - - -coroutine test(){ - RAII raii("in test"); - std::cout<<"testing\n"; - co_yield 1; - co_yield 2; - co_return 0; -} - -int main(){ - std::cout<<__FUNCTION__<<'\n'; - auto handle = test(); - std::cout<<__FUNCTION__<<'\n'; - while (!handle.done()) { - handle.resume(); - std::cout<<__FUNCTION__<<'\n'; - } - handle.destroy();//如果不destroy()那么~RAII in promise将不会显示,也不能将destroy()交给~Promise()管理,也不会~RAII in promise -} \ No newline at end of file diff --git a/src/web.cpp b/src/web.cpp new file mode 100644 index 0000000..27bb42d --- /dev/null +++ b/src/web.cpp @@ -0,0 +1,173 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAXLINE 5 +#define OPEN_MAX 100 +#define LISTENQ 20 +#define SERV_PORT 5000 +#define INFTIM 1000 + +void setnonblocking(int sock) +{ + int opts; + opts=fcntl(sock,F_GETFL); + if(opts<0) + { + perror("fcntl(sock,GETFL)"); + exit(1); + } + opts = opts|O_NONBLOCK; + if(fcntl(sock,F_SETFL,opts)<0) + { + perror("fcntl(sock,SETFL,opts)"); + exit(1); + } +} + +int main(int argc, char* argv[]) +{ + int i, maxi, listenfd, connfd, sockfd,epfd,nfds, portnumber; + ssize_t n; + char line[MAXLINE]; + socklen_t clilen; + + + if ( 2 == argc ) + { + if( (portnumber = atoi(argv[1])) < 0 ) + { + fprintf(stderr,"Usage:%s portnumber/a/n",argv[0]); + return 1; + } + } + else + { + fprintf(stderr,"Usage:%s portnumber/a/n",argv[0]); + return 1; + } + + + + //声明epoll_event结构体的变量,ev用于注册事件,数组用于回传要处理的事件 + + struct epoll_event ev,events[20]; + //生成用于处理accept的epoll专用的文件描述符 + + epfd=epoll_create(256); + struct sockaddr_in clientaddr; + struct sockaddr_in serveraddr; + listenfd = socket(AF_INET, SOCK_STREAM, 0); + //把socket设置为非阻塞方式 + + //setnonblocking(listenfd); + + //设置与要处理的事件相关的文件描述符 + + ev.data.fd=listenfd; + //设置要处理的事件类型 + + ev.events=EPOLLIN|EPOLLET; + //ev.events=EPOLLIN; + + //注册epoll事件 + + epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,&ev); + bzero(&serveraddr, sizeof(serveraddr)); + serveraddr.sin_family = AF_INET; + char *local_addr="127.0.0.1"; + inet_aton(local_addr,&(serveraddr.sin_addr));//htons(portnumber); + + serveraddr.sin_port=htons(portnumber); + bind(listenfd,(sockaddr *)&serveraddr, sizeof(serveraddr)); + listen(listenfd, LISTENQ); + maxi = 0; + for ( ; ; ) { + //等待epoll事件的发生 + + nfds=epoll_wait(epfd,events,20,500); + //处理所发生的所有事件 + + for(i=0;i