diff --git a/Cppnet.vcxproj b/Cppnet.vcxproj index 2f7d857..a39da70 100644 --- a/Cppnet.vcxproj +++ b/Cppnet.vcxproj @@ -21,14 +21,14 @@ {E98183C7-10C3-4B1A-B8FB-D60BB6217962} CppNet - 10.0.17763.0 + 10.0 Cppnet StaticLibrary true - v141 + v142 Unicode @@ -41,13 +41,13 @@ StaticLibrary true - v141 + v142 MultiByte StaticLibrary false - v141 + v142 true MultiByte @@ -182,15 +182,13 @@ + + - - - - @@ -223,15 +221,13 @@ + + - - - - diff --git a/Cppnet.vcxproj.filters b/Cppnet.vcxproj.filters index a5c0c2b..346ba0d 100644 --- a/Cppnet.vcxproj.filters +++ b/Cppnet.vcxproj.filters @@ -46,17 +46,14 @@ {ea04e906-9556-4346-9ed2-40ae19f6ede3} - - {d20855db-db9f-4ef5-8779-2ec195a43da8} - - - {bacb660a-99ea-4b26-81a4-cee347e2beeb} - {75988d4c-ae9d-4a95-8f7a-87fa3268a3be} - - {edd8021a-c10e-4e26-9908-17f3d83c7b07} + + {80edc4cc-d262-4ae6-9ef1-917471b6d26d} + + + {01f24cd1-baff-401e-a739-9d41d1035612} @@ -207,17 +204,11 @@ include - - cppnet\event\win + + cppnet\event\epoll\wepoll - - cppnet\socket\win - - - cppnet\socket\win - - - cppnet\event\win\wepoll + + cppnet\event\epoll @@ -314,17 +305,11 @@ common\log - - cppnet\event\win + + cppnet\event\epoll\wepoll - - cppnet\socket\win - - - cppnet\socket\win - - - cppnet\event\win\wepoll + + cppnet\event\epoll \ No newline at end of file diff --git a/cppnet/cppnet_base.cpp b/cppnet/cppnet_base.cpp index 77278ad..89cd758 100644 --- a/cppnet/cppnet_base.cpp +++ b/cppnet/cppnet_base.cpp @@ -88,6 +88,16 @@ void CppNetBase::RemoveTimer(uint64_t timer_id) { } bool CppNetBase::ListenAndAccept(const std::string& ip, uint16_t port) { +#ifdef __win__ + auto ret = OsHandle::TcpSocket(); + if (ret._return_value < 0) { + LOG_ERROR("create socket failed. err:%d", ret._errno); + return false; + } + for (size_t i = 0; i < _dispatchers.size(); i++) { + _dispatchers[i]->Listen(ret._return_value, ip, port); + } +#else if (__reuse_port) { for (size_t i = 0; i < _dispatchers.size(); i++) { auto ret = OsHandle::TcpSocket(); @@ -99,7 +109,8 @@ bool CppNetBase::ListenAndAccept(const std::string& ip, uint16_t port) { _dispatchers[i]->Listen(ret._return_value, ip, port); } - } else { + } + else { auto ret = OsHandle::TcpSocket(); if (ret._return_value < 0) { LOG_ERROR("create socket failed. err:%d", ret._errno); @@ -109,6 +120,7 @@ bool CppNetBase::ListenAndAccept(const std::string& ip, uint16_t port) { _dispatchers[i]->Listen(ret._return_value, ip, port); } } +#endif return true; } diff --git a/cppnet/dispatcher.cpp b/cppnet/dispatcher.cpp index 7a000eb..2a230f3 100644 --- a/cppnet/dispatcher.cpp +++ b/cppnet/dispatcher.cpp @@ -16,11 +16,6 @@ #include "common/timer/timer.h" #include "common/alloter/pool_alloter.h" -#ifdef __win__ -#include "cppnet/socket/win/win_rw_socket.h" -#else -#endif - namespace cppnet { thread_local std::unordered_map> Dispatcher::__all_timer_event_map; diff --git a/cppnet/event/epoll/epoll_action.cpp b/cppnet/event/epoll/epoll_action.cpp index 710db2a..68cdb49 100644 --- a/cppnet/event/epoll/epoll_action.cpp +++ b/cppnet/event/epoll/epoll_action.cpp @@ -5,12 +5,17 @@ #include #include +#ifdef __win__ +#include +#pragma comment(lib,"ws2_32.lib") +#else #include #include #include #include #include #include +#endif #include "epoll_action.h" #include "include/cppnet_type.h" @@ -33,7 +38,11 @@ std::shared_ptr MakeEventActions() { } EpollEventActions::EpollEventActions(): +#ifdef __win__ + _epoll_handler(nullptr) { +#else _epoll_handler(-1) { +#endif _active_list.resize(1024); memset(_pipe, 0, sizeof(_pipe)); memset(&_pipe_content, 0, sizeof(_pipe_content)); @@ -41,19 +50,27 @@ EpollEventActions::EpollEventActions(): EpollEventActions::~EpollEventActions() { if (_epoll_handler > 0) { +#ifdef __win__ + epoll_close(_epoll_handler); +#else close(_epoll_handler); +#endif } } bool EpollEventActions::Init(uint32_t thread_num) { - //get epoll handle. the param is invalid since linux 2.6.8 + // get EPOLL handle. the param is invalid since LINUX 2.6.8 _epoll_handler = epoll_create(1500); +#ifdef __win__ + if (_epoll_handler == nullptr) { +#else if (_epoll_handler == -1) { - LOG_FATAL("epoll init failed! error : %d", errno); +#endif + LOG_FATAL("EPOLL init failed! error : %d", errno); return false; } #ifdef __win__ - if (!Pipe((_pipe)) { + if (!Pipe(_pipe)) { #else if (pipe((int*)_pipe) == -1) { #endif @@ -68,7 +85,7 @@ bool EpollEventActions::Init(uint32_t thread_num) { _pipe_content.data.fd = _pipe[0]; int32_t ret = epoll_ctl(_epoll_handler, EPOLL_CTL_ADD, _pipe[0], &_pipe_content); if (ret < 0) { - LOG_FATAL("add pipe handle to epoll faild! error :%d", errno); + LOG_FATAL("add pipe handle to EPOLL failed! error :%d", errno); return false; } return true; @@ -92,14 +109,14 @@ bool EpollEventActions::AddSendEvent(Event* event) { } } - // already in epoll + // already in EPOLL if (ep_event->events & EPOLLOUT) { return true; } auto sock = event->GetSocket(); if (!sock) { - LOG_WARN("socket is already distroyed! event %s", "AddSendEvent"); + LOG_WARN("socket is already destroyed! event %s", "AddSendEvent"); return false; } @@ -125,14 +142,14 @@ bool EpollEventActions::AddRecvEvent(Event* event) { } } - // already in epoll + // already in EPOLL if (ep_event->events & EPOLLIN) { return true; } auto sock = event->GetSocket(); if (!sock) { - LOG_WARN("socket is already distroyed! event %s", "AddSendEvent"); + LOG_WARN("socket is already destroyed! event %s", "AddSendEvent"); return false; } @@ -161,14 +178,14 @@ bool EpollEventActions::AddAcceptEvent(Event* event) { ep_event->data.ptr = (void*)event; } - // already in epoll + // already in EPOLL if (ep_event->events & EPOLLIN) { return true; } auto sock = event->GetSocket(); if (!sock) { - LOG_WARN("socket is already distroyed! event %s", "AddSendEvent"); + LOG_WARN("socket is already destroyed! event %s", "AddSendEvent"); return false; } @@ -188,12 +205,12 @@ bool EpollEventActions::AddConnection(Event* event, Address& addr) { auto sock = event->GetSocket(); if (sock) { - //the socket must not in epoll + // the socket must not in EPOLL if (event->GetType() & ET_INACTIONS) { return false; } - //block here in linux + // block here in LINUX SocketNoblocking(sock->GetSocket()); auto ret = OsHandle::Connect(sock->GetSocket(), addr); @@ -245,12 +262,12 @@ bool EpollEventActions::DelEvent(Event* event) { epoll_event* ev = (epoll_event*)event->GetData(); int32_t ret = epoll_ctl(_epoll_handler, EPOLL_CTL_DEL, sock->GetSocket(), ev); if (ret < 0) { - LOG_ERROR("remove event from epoll faild! error :%d, socket : %d", errno, sock->GetSocket()); + LOG_ERROR("remove event from EPOLL failed! error :%d, socket : %d", errno, sock->GetSocket()); return false; } event->ClearType(); - LOG_DEBUG("del a socket from epoll, %d", sock->GetSocket()); + LOG_DEBUG("remove a socket from EPOLL, %d", sock->GetSocket()); return true; } @@ -260,19 +277,23 @@ void EpollEventActions::ProcessEvent(int32_t wait_ms) { if (errno == EINTR) { return; } - LOG_ERROR("epoll wait faild! error:%d, info:%s", errno, ErrnoInfo(errno)); + LOG_ERROR("EPOLL wait failed! error:%d, info:%s", errno, ErrnoInfo(errno)); } else { - LOG_DEBUG("epoll get events! num:%d, TheadId: %lld", ret, std::this_thread::get_id()); + LOG_DEBUG("EPOLL get events! num:%d, TheadId: %ld", ret, std::this_thread::get_id()); OnEvent(_active_list, ret); } } void EpollEventActions::Wakeup() { - if(write(_pipe[1], "1", 1) <= 0) { - LOG_ERROR_S << "write to pipe failed when weak up."; - } +#ifdef __win__ + if (send(_pipe[1], "1", 1, 0) <= 0) { +#else + if (write(_pipe[1], "1", 1) <= 0) { +#endif + LOG_ERROR_S << "write to pipe failed when weak up."; + } } void EpollEventActions::OnEvent(std::vector& event_vec, int16_t num) { @@ -281,9 +302,13 @@ void EpollEventActions::OnEvent(std::vector& event_vec, int16_t num for (int i = 0; i < num; i++) { if ((uint32_t)event_vec[i].data.fd == _pipe[0]) { - LOG_WARN("weak up the io thread, index : %d", i); + LOG_WARN("weak up the IO thread, index : %d", i); char buf[4]; - if(read(_pipe[0], buf, 1) <= 0) { +#ifdef __win__ + if (recv(_pipe[0], buf, 1, 0) <= 0) { +#else + if (read(_pipe[0], buf, 1) <= 0) { +#endif LOG_ERROR_S << "read from pipe failed when weak up."; } continue; @@ -292,7 +317,7 @@ void EpollEventActions::OnEvent(std::vector& event_vec, int16_t num event = (Event*)event_vec[i].data.ptr; sock = event->GetSocket(); if (!sock) { - LOG_WARN("epoll weak up but socket already destroy, index : %d", i); + LOG_WARN("EPOLL weak up but socket already destroy, index : %d", i); continue; } @@ -319,10 +344,10 @@ void EpollEventActions::OnEvent(std::vector& event_vec, int16_t num } bool EpollEventActions::AddEvent(epoll_event* ev, int32_t event_flag, uint64_t sock, bool in_actions) { - //if not add to epoll + //if not add to EPOLL if (!(ev->events & event_flag)) { #ifdef __win__ - ev->events |= event_flag + ev->events |= event_flag; #else if (__epoll_use_et) { ev->events |= event_flag | EPOLLET; @@ -330,12 +355,11 @@ bool EpollEventActions::AddEvent(epoll_event* ev, int32_t event_flag, uint64_t s } else { ev->events |= event_flag; } -#endif - -#if !defined(__win__) && LINUX_VERSION_CODE >= KERNEL_VERSION(4,5,0) +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,5,0) if (__epoll_exclusive) { ev->events |= EPOLLEXCLUSIVE; } +#endif #endif int32_t ret = 0; @@ -349,7 +373,7 @@ bool EpollEventActions::AddEvent(epoll_event* ev, int32_t event_flag, uint64_t s if (ret == 0) { return true; } - LOG_ERROR("modify event to epoll faild! error :%d, sock: %d", errno, sock); + LOG_ERROR("modify event to EPOLL failed! error :%d, sock: %d", errno, sock); } return false; } @@ -357,7 +381,7 @@ bool EpollEventActions::AddEvent(epoll_event* ev, int32_t event_flag, uint64_t s bool EpollEventActions::MakeEpollEvent(Event* event, epoll_event* &ep_event) { auto sock = event->GetSocket(); if (!sock) { - LOG_WARN("socket is already distroyed! event %s", "AddSendEvent"); + LOG_WARN("socket is already destroyed! event %s", "AddSendEvent"); return false; } diff --git a/cppnet/event/epoll/epoll_action.h b/cppnet/event/epoll/epoll_action.h index 75ee7d8..1b6c1cc 100644 --- a/cppnet/event/epoll/epoll_action.h +++ b/cppnet/event/epoll/epoll_action.h @@ -6,6 +6,7 @@ #ifndef NET_EVENT_LINUX_EPOLL_ACTION #define NET_EVENT_LINUX_EPOLL_ACTION +#include #ifdef __win__ #include "wepoll/wepoll.h" #else diff --git a/cppnet/socket/connect_socket.cpp b/cppnet/socket/connect_socket.cpp index b309f9a..57238a9 100644 --- a/cppnet/socket/connect_socket.cpp +++ b/cppnet/socket/connect_socket.cpp @@ -22,7 +22,8 @@ namespace cppnet { -ConnectSocket::ConnectSocket() { +ConnectSocket::ConnectSocket(): + _accept_event(nullptr) { } diff --git a/cppnet/socket/rw_socket.cpp b/cppnet/socket/rw_socket.cpp index b39693b..1e14ae7 100644 --- a/cppnet/socket/rw_socket.cpp +++ b/cppnet/socket/rw_socket.cpp @@ -33,6 +33,7 @@ RWSocket::RWSocket(uint64_t sock, std::shared_ptr alloter): _context(nullptr), _timer_id(0), _listen_port(0), + _event(nullptr), _shutdown(false), _alloter(alloter) { diff --git a/test/http/http_server_test.cpp b/test/http/http_server_test.cpp index fed56db..2267363 100644 --- a/test/http/http_server_test.cpp +++ b/test/http/http_server_test.cpp @@ -68,7 +68,7 @@ void DisConnectionFunc(const cppnet::Handle& , uint32_t ) { int main() { cppnet::CppNet net; - net.Init(2); + net.Init(8); HttpServer server; server.SetHttpCallback(OnRequest);