From 07650796618d3d5f86bc3749e817bdc4848458b9 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 28 May 2019 19:03:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=80=A7=E8=83=BD=E4=BC=98=E5=8C=96=E6=94=B9?= =?UTF-8?q?=E4=B8=BAlt=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- a8/tcplistener.cc | 25 +++++++++++++++++++++---- a8/tcpsession2.cc | 20 +++++++++++++++++++- a8/tcpsessionpool.cc | 4 ++++ a8/udplog.cc | 17 +++++++++++++++++ a8/xvalue.cc | 10 ++++++++++ a8/xvalue.h | 1 + 6 files changed, 72 insertions(+), 5 deletions(-) diff --git a/a8/tcplistener.cc b/a8/tcplistener.cc index 15a79fa..2cb5716 100644 --- a/a8/tcplistener.cc +++ b/a8/tcplistener.cc @@ -35,7 +35,7 @@ namespace a8 unsigned short curr_socket_handle = 1000; unsigned short max_clients = 0xEFFF; a8::TcpSessionPool free_client_pool; - int epoll_fd = a8::INVALID_FD; + volatile int epoll_fd = a8::INVALID_FD; #if 0 list_head session_list; #endif @@ -121,9 +121,14 @@ namespace a8 void ActiveStop() { if (listen_socket != a8::INVALID_SOCKET) { + ::shutdown(listen_socket, SHUT_RDWR); ::close(listen_socket); listen_socket = a8::INVALID_SOCKET; } + if(epoll_fd != a8::INVALID_FD) { + ::close(epoll_fd); + epoll_fd = a8::INVALID_FD; + } if (accept_thread) { accept_thread_shutdown = true; accept_thread->join(); @@ -136,9 +141,17 @@ namespace a8 delete worker_thread; worker_thread = nullptr; } - if(epoll_fd != a8::INVALID_FD) { - ::close(epoll_fd); - epoll_fd = a8::INVALID_FD; + { + clients_mutex.lock(); + std::vector del_sessions; + for (auto& pair : client_hash) { + del_sessions.push_back(pair.second); + } + for (a8::TcpSession* session : del_sessions) { + session->_ForceClose(); + } + client_hash.clear(); + clients_mutex.unlock(); } } @@ -274,6 +287,9 @@ namespace a8 ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP; #else ev.events = EPOLLIN | EPOLLOUT | EPOLLET | EPOLLRDHUP; +#endif +#ifdef A8_TCP_SESSION2 + ev.events = EPOLLIN | EPOLLRDHUP; #endif ev.data.ptr = p; int n = ::epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev); @@ -350,6 +366,7 @@ namespace a8 session->_ForceClose(); } client_handle_hash.erase(session->socket_handle); + session->ClearSendBuff(); session->OnDisConnect(); free_client_pool.Add(session); client_count--; diff --git a/a8/tcpsession2.cc b/a8/tcpsession2.cc index 2b0ddc2..8b27fa0 100644 --- a/a8/tcpsession2.cc +++ b/a8/tcpsession2.cc @@ -81,6 +81,18 @@ namespace a8 memmove(p->buff, buff, bufflen); p->bufflen = bufflen; send_buffer_mutex_.lock(); +#if 1 + if (bot_node_) { + bot_node_->next = p; + bot_node_ = p; + } else { + top_node_ = p; + bot_node_ = p; + } + if (!sending_) { + NotifyEpollSend(); + } +#else if (sending_) { if (bot_node_) { bot_node_->next = p; @@ -123,6 +135,7 @@ namespace a8 abort(); } } +#endif send_buffer_mutex_.unlock(); } } @@ -344,6 +357,11 @@ namespace a8 } if (!work_node_) { sending_ = false; + struct epoll_event ev; + ev.data.fd = socket_; + ev.events = EPOLLIN | EPOLLRDHUP; + ev.data.ptr = this; + ::epoll_ctl(epoll_fd, EPOLL_CTL_MOD, socket_, &ev); } send_buffer_mutex_.unlock(); } @@ -360,7 +378,7 @@ namespace a8 sending_ = true; struct epoll_event ev; ev.data.fd = socket_; - ev.events = EPOLLIN | EPOLLET | EPOLLOUT | EPOLLRDHUP; + ev.events = EPOLLIN | EPOLLOUT | EPOLLRDHUP; ev.data.ptr = this; ::epoll_ctl(epoll_fd, EPOLL_CTL_MOD, socket_, &ev); } diff --git a/a8/tcpsessionpool.cc b/a8/tcpsessionpool.cc index d7acd7b..f9fb828 100644 --- a/a8/tcpsessionpool.cc +++ b/a8/tcpsessionpool.cc @@ -1,7 +1,11 @@ #include #include +#ifdef A8_TCP_SESSION2 +#include +#else #include +#endif #include namespace a8 diff --git a/a8/udplog.cc b/a8/udplog.cc index c5aa79a..9e98825 100644 --- a/a8/udplog.cc +++ b/a8/udplog.cc @@ -66,6 +66,23 @@ namespace a8 impl_->save_thread->join(); delete impl_->save_thread; impl_->save_thread = nullptr; + impl_->msg_mutex.lock(); + if (impl_->work_node) { + impl_->work_node = impl_->top_node; + impl_->top_node = nullptr; + impl_->bot_node = nullptr; + } + while (impl_->work_node) { + UdpLogMsgNode* pdelnode = impl_->work_node; + impl_->work_node = impl_->work_node->next; + if (!impl_->work_node) { + impl_->work_node = impl_->top_node; + impl_->top_node = nullptr; + impl_->bot_node = nullptr; + } + delete pdelnode; + } + impl_->msg_mutex.unlock(); } void UdpLog::SetLogFileName(const std::string& filename) diff --git a/a8/xvalue.cc b/a8/xvalue.cc index b25cb03..7a376d9 100644 --- a/a8/xvalue.cc +++ b/a8/xvalue.cc @@ -172,6 +172,11 @@ namespace a8 } } + void XValue::Set(const std::string& v) + { + SetDynData(v.data(), v.size()); + } + void XValue::Set(const wchar_t* v) { OnReset(); @@ -462,6 +467,11 @@ namespace a8 const a8::XValue& XValue::operator=(const a8::XValue& xv) { + if (type_ == XVT_STRING && value_.str_value){ + free(value_.str_value); + }else if (type_ == XVT_WSTRING && value_.wstr_value){ + free(value_.wstr_value); + } type_ = XVT_INT; value_.int_value = 0; data_size_ = 0; diff --git a/a8/xvalue.h b/a8/xvalue.h index c4e1306..2b57a26 100644 --- a/a8/xvalue.h +++ b/a8/xvalue.h @@ -41,6 +41,7 @@ namespace a8 void Set(long long v); void Set(unsigned long long v); void Set(const char* v); + void Set(const std::string& v); void Set(const wchar_t* v); void SetDynData(const char* v, unsigned int len); void SetUserData(void *userdata, int datasize);