From 5ac59bc49a59b69dd34b61c11506941b6768b457 Mon Sep 17 00:00:00 2001 From: logwang <logwang@tencent.com> Date: Mon, 20 Nov 2017 22:39:00 +0800 Subject: [PATCH] Micro_thread: convert encoding of source files and remove chinese comments. --- app/micro_thread/Makefile | 21 +- app/micro_thread/epoll_proxy.cpp.bak | 561 ------------------------- app/micro_thread/epoll_proxy.h.bak | 434 ------------------- app/micro_thread/hash_list.h | 129 ++---- app/micro_thread/heap.h | 387 +++++++---------- app/micro_thread/heap_timer.cpp | 25 -- app/micro_thread/heap_timer.h | 53 +-- app/micro_thread/kqueue_proxy.cpp | 487 +++++++++++----------- app/micro_thread/kqueue_proxy.h | 218 ++++------ app/micro_thread/micro_thread.cpp | 424 +++---------------- app/micro_thread/micro_thread.h | 566 +++++-------------------- app/micro_thread/mt_action.cpp | 45 +- app/micro_thread/mt_action.h | 216 ++-------- app/micro_thread/mt_api.cpp | 594 +++++++-------------------- app/micro_thread/mt_api.h | 283 +------------ app/micro_thread/mt_cache.cpp | 189 +-------- app/micro_thread/mt_cache.h | 210 +--------- app/micro_thread/mt_concurrent.cpp | 66 +-- app/micro_thread/mt_concurrent.h | 44 -- app/micro_thread/mt_connection.cpp | 159 +------ app/micro_thread/mt_connection.h | 293 ++----------- app/micro_thread/mt_incl.h | 1 - app/micro_thread/mt_mbuf_pool.cpp | 24 -- app/micro_thread/mt_mbuf_pool.h | 122 +----- app/micro_thread/mt_msg.h | 13 +- app/micro_thread/mt_net.cpp | 265 +----------- app/micro_thread/mt_net.h | 293 +++---------- app/micro_thread/mt_net_api.h | 65 +-- app/micro_thread/mt_notify.cpp | 134 +----- app/micro_thread/mt_notify.h | 305 ++------------ app/micro_thread/mt_self_echo.cpp | 251 ----------- app/micro_thread/mt_session.cpp | 31 +- app/micro_thread/mt_session.h | 76 +--- app/micro_thread/mt_sys_hook.cpp | 94 +---- app/micro_thread/mt_sys_hook.h | 34 +- app/micro_thread/mt_version.h | 11 - 36 files changed, 1137 insertions(+), 5986 deletions(-) delete mode 100644 app/micro_thread/epoll_proxy.cpp.bak delete mode 100644 app/micro_thread/epoll_proxy.h.bak delete mode 100644 app/micro_thread/mt_self_echo.cpp diff --git a/app/micro_thread/Makefile b/app/micro_thread/Makefile index a91e6cf5c..9e3e33810 100644 --- a/app/micro_thread/Makefile +++ b/app/micro_thread/Makefile @@ -20,17 +20,20 @@ ########MAKEFILE########## ifeq ($(FF_PATH),) - $(error variable FF_PATH is not set) + $(error variable FF_PATH is not set) +endif + +ifeq ($(FF_DPDK),) + FF_DPDK= $(FF_PATH)/dpdk/x86_64-native-linuxapp-gcc endif DEBUG= -g BINARY = libmt.a -FF_LIB=$(FF_PATH)/libfstack.a -DPDK_LIBS+= -L${FF_PATH}/lib -L${FF_DPDK}/lib -Wl,--whole-archive,-lfstack,--no-whole-archive -DPDK_LIBS+= -Wl,--whole-archive -lrte_pmd_vmxnet3_uio -lrte_pmd_i40e -lrte_pmd_ixgbe -lrte_pmd_e1000 -lrte_pmd_ring -DPDK_LIBS+= -Wl,--whole-archive -lrte_hash -lrte_kvargs -Wl,-lrte_mbuf -lethdev -lrte_eal -Wl,-lrte_mempool -DPDK_LIBS+= -lrte_ring -lrte_cmdline -lrte_cfgfile -lrte_kni -lrte_timer -Wl,-lrte_pmd_virtio -DPDK_LIBS+= -Wl,--no-whole-archive -lrt -lm -ldl -lcrypto -pthread +FF_LIBS+= -L${FF_PATH}/lib -L${FF_DPDK}/lib -Wl,--whole-archive,-lfstack,--no-whole-archive +FF_LIBS+= -Wl,--whole-archive -lrte_pmd_vmxnet3_uio -lrte_pmd_i40e -lrte_pmd_ixgbe -lrte_pmd_e1000 -lrte_pmd_ring +FF_LIBS+= -Wl,--whole-archive -lrte_hash -lrte_kvargs -Wl,-lrte_mbuf -lethdev -lrte_eal -Wl,-lrte_mempool +FF_LIBS+= -lrte_ring -lrte_cmdline -lrte_cfgfile -lrte_kni -lrte_timer -Wl,-lrte_pmd_virtio +FF_LIBS+= -Wl,--no-whole-archive -lrt -lm -ldl -lcrypto -pthread # Comment the following line if you are not using the gnu c compiler #C_ARGS = -Wall -g -fPIC -D_DEBUG @@ -91,9 +94,9 @@ LIB_OBJ = micro_thread.o kqueue_proxy.o arch_ctx.o mt_session.o mt_notify.o mt_a libmt.a: $(LIB_OBJ) @echo -e Linking $(CYAN)$@$(RESET) ...$(RED) @-rm -f $@ - @ar crs $@ $^ $(FST_LIB) $(CRESET) + @ar crs $@ $^ $(CRESET) @chmod +x $@ echo: echo.o libmt.a @echo -e Compile $(CYAN)$@$(RESET) ...$(RED) - @$(CC) -O -gdwarf-2 -o $@ $^ -lstdc++ -ldl -lm $(DPDK_LIBS) $(CRESET) + @$(CC) -O -gdwarf-2 -o $@ $^ -lstdc++ -ldl -lm $(FF_LIBS) $(CRESET) diff --git a/app/micro_thread/epoll_proxy.cpp.bak b/app/micro_thread/epoll_proxy.cpp.bak deleted file mode 100644 index 185824fa9..000000000 --- a/app/micro_thread/epoll_proxy.cpp.bak +++ /dev/null @@ -1,561 +0,0 @@ - -/** - * Tencent is pleased to support the open source community by making MSEC available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the GNU General Public License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. You may - * obtain a copy of the License at - * - * https://opensource.org/licenses/GPL-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the - * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, - * either express or implied. See the License for the specific language governing permissions - * and limitations under the License. - */ - - -/** - * @filename epoll_proxy.cpp - * @info epoll for micro thread manage - */ - -#include "epoll_proxy.h" -#include "micro_thread.h" - -using namespace NS_MICRO_THREAD; - -/** - * @brief ���캯�� - */ -EpollProxy::EpollProxy() -{ - _maxfd = EpollProxy::DEFAULT_MAX_FD_NUM; - _epfd = -1; - _evtlist = NULL; - _eprefs = NULL; -} - -/** - * @brief epoll��ʼ��, ���붯̬�ڴ�� - */ -int EpollProxy::InitEpoll(int max_num) -{ - int rc = 0; - if (max_num > _maxfd) // ������õ���Ŀ�ϴ�, ��������fd��Ŀ - { - _maxfd = max_num; - } - - _epfd = epoll_create(_maxfd); - if (_epfd < 0) - { - rc = -1; - goto EXIT_LABEL; - } - fcntl(_epfd, F_SETFD, FD_CLOEXEC); - - _eprefs = new FdRef[_maxfd]; - if (NULL == _eprefs) - { - rc = -2; - goto EXIT_LABEL; - } - - _evtlist = (EpEvent*)calloc(_maxfd, sizeof(EpEvent)); - if (NULL == _evtlist) - { - rc = -3; - goto EXIT_LABEL; - } - - struct rlimit rlim; - memset(&rlim, 0, sizeof(rlim)); - if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) - { - if ((int)rlim.rlim_max < _maxfd) - { - rlim.rlim_cur = rlim.rlim_max; - setrlimit(RLIMIT_NOFILE, &rlim); - rlim.rlim_cur = _maxfd; - rlim.rlim_max = _maxfd; - setrlimit(RLIMIT_NOFILE, &rlim); - } - } - -EXIT_LABEL: - - if (rc < 0) - { - TermEpoll(); - } - - return rc; -} - -/** - * @brief epoll����ʼ�� - */ -void EpollProxy::TermEpoll() -{ - if (_epfd > 0) - { - close(_epfd); - _epfd = -1; - } - - if (_evtlist != NULL) - { - free(_evtlist); - _evtlist = NULL; - } - - if (_eprefs != NULL) - { - delete []_eprefs; - _eprefs = NULL; - } -} - -/** - * @brief ��һ���߳�����������socket����epoll���� - * @param fdset �߳�������socket���� - * @return true �ɹ�, false ʧ��, ʧ�ܻᾡ���ع�, ����Ӱ�� - */ -bool EpollProxy::EpollAdd(EpObjList& obj_list) -{ - bool ret = true; - EpollerObj *epobj = NULL; - EpollerObj *epobj_error = NULL; - TAILQ_FOREACH(epobj, &obj_list, _entry) - { - if (!EpollAddObj(epobj)) - { - MTLOG_ERROR("epobj add failed, fd: %d", epobj->GetOsfd()); - epoll_assert(0); - epobj_error = epobj; - ret = false; - goto EXIT_LABEL; - } - } - -EXIT_LABEL: - - if (!ret) - { - TAILQ_FOREACH(epobj, &obj_list, _entry) - { - if (epobj == epobj_error) - { - break; - } - EpollDelObj(epobj); - } - } - - return ret; -} - - -/** - * @brief ��һ���߳�����������socket�Ƴ�epoll���� - * @param fdset �߳�������socket���� - * @return true �ɹ�, false ʧ�� - */ -bool EpollProxy::EpollDel(EpObjList& obj_list) -{ - bool ret = true; - - EpollerObj *epobj = NULL; - TAILQ_FOREACH(epobj, &obj_list, _entry) - { - if (!EpollDelObj(epobj)) // failed also need continue, be sure ref count ok - { - MTLOG_ERROR("epobj del failed, fd: %d", epobj->GetOsfd()); - epoll_assert(0); - ret = false; - } - } - - return ret; -} - - -/** - * @brief ����epfd����epctrl, �ɹ���Ҫ���µ�ǰ�����¼�ֵ - */ -bool EpollProxy::EpollCtrlAdd(int fd, int events) -{ - FdRef* item = FdRefGet(fd); - if (NULL == item) - { - MT_ATTR_API(320851, 1); // fd error - MTLOG_ERROR("epfd ref not find, failed, fd: %d", fd); - epoll_assert(0); - return false; - } - - // �������ü���, �������̻������ü���, ʧ��Ҫ�ع� - item->AttachEvents(events); - - int old_events = item->GetListenEvents(); - int new_events = old_events | events; - if (old_events == new_events) { - return true; - } - - int op = old_events ? EPOLL_CTL_MOD : EPOLL_CTL_ADD; - EpEvent ev; - ev.events = new_events; - ev.data.fd = fd; - if ((epoll_ctl(_epfd, op, fd, &ev) < 0) && !(op == EPOLL_CTL_ADD && errno == EEXIST)) - { - MT_ATTR_API(320850, 1); // epoll error - MTLOG_ERROR("epoll ctrl failed, fd: %d, op: %d, errno: %d", fd, op, errno); - item->DetachEvents(events); - epoll_assert(0); - return false; - } - item->SetListenEvents(new_events); - - return true; - -} - -/** - * @brief ����epfd����epctrl, �ɹ���Ҫ���µ�ǰ�����¼�ֵ - */ -bool EpollProxy::EpollCtrlDel(int fd, int events) -{ - return EpollCtrlDelRef(fd, events, false); -} - -/** - * @brief ����epfd����epctrl, ������ü���, ����Ԥ�賤����, ����ÿ�ζ�epollctl - */ -bool EpollProxy::EpollCtrlDelRef(int fd, int events, bool use_ref) -{ - FdRef* item = FdRefGet(fd); - if (NULL == item) - { - MT_ATTR_API(320851, 1); // fd error - MTLOG_ERROR("epfd ref not find, failed, fd: %d", fd); - epoll_assert(0); - return false; - } - - item->DetachEvents(events); // delete ʧ�ܲ��ع����� - int old_events = item->GetListenEvents(); - int new_events = old_events &~ events; // Ĭ����� - - // ���Ҫ������ɾ��, ��Ҫ�˲��Ƿ�����ɾ������ - if (use_ref) - { - new_events = old_events; - if (0 == item->ReadRefCnt()) { - new_events = new_events & ~EPOLLIN; - } - if (0 == item->WriteRefCnt()) { - new_events = new_events & ~EPOLLOUT; - } - } - - if (old_events == new_events) - { - return true; - } - - int op = new_events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL; - EpEvent ev; - ev.events = new_events; - ev.data.fd = fd; - if ((epoll_ctl(_epfd, op, fd, &ev) < 0) && !(op == EPOLL_CTL_DEL && errno == ENOENT)) - { - MT_ATTR_API(320850, 1); // epoll error - MTLOG_ERROR("epoll ctrl failed, fd: %d, op: %d, errno: %d", fd, op, errno); - epoll_assert(0); - return false; - } - item->SetListenEvents(new_events); - - return true; -} - - - -/** - * @brief ����epfd����epctrl, ���ʧ��, �������� - */ -bool EpollProxy::EpollAddObj(EpollerObj* obj) -{ - if (NULL == obj) - { - MTLOG_ERROR("epobj input invalid, %p", obj); - return false; - } - - FdRef* item = FdRefGet(obj->GetOsfd()); - if (NULL == item) - { - MT_ATTR_API(320851, 1); // fd error - MTLOG_ERROR("epfd ref not find, failed, fd: %d", obj->GetOsfd()); - epoll_assert(0); - return false; - } - - // ��ͬ�Ļص�״̬, ��ͬ�ķ�ʽ���� del �¼�, �������Ӹ��÷�ʽ�Ĵ��������� - int ret = obj->EpollCtlAdd(item); - if (ret < 0) - { - MTLOG_ERROR("epoll ctrl callback failed, fd: %d, obj: %p", obj->GetOsfd(), obj); - epoll_assert(0); - return false; - } - - return true; -} - - -/** - * @brief ��һ���߳�����������socket�Ƴ�epoll���� - * @param fdset �߳�������socket���� - * @return true �ɹ�, false ʧ�� - */ -bool EpollProxy::EpollDelObj(EpollerObj* obj) -{ - if (NULL == obj) - { - MTLOG_ERROR("fdobj input invalid, %p", obj); - return false; - } - - FdRef* item = FdRefGet(obj->GetOsfd()); - if (NULL == item) - { - MT_ATTR_API(320851, 1); // fd error - MTLOG_ERROR("epfd ref not find, failed, fd: %d", obj->GetOsfd()); - epoll_assert(0); - return false; - } - - // ��ͬ�Ļص�״̬, ��ͬ�ķ�ʽ���� del �¼�, �������Ӹ��÷�ʽ�Ĵ��������� - int ret = obj->EpollCtlDel(item); - if (ret < 0) - { - MTLOG_ERROR("epoll ctrl callback failed, fd: %d, obj: %p", obj->GetOsfd(), obj); - epoll_assert(0); - return false; - } - - return true; -} - - -/** - * @brief ����ÿ��socket�����½����¼���Ϣ - * @param evtfdnum �յ��¼���fd������Ŀ - */ -void EpollProxy::EpollRcvEventList(int evtfdnum) -{ - int ret = 0; - int osfd = 0; - int revents = 0; - FdRef* item = NULL; - EpollerObj* obj = NULL; - - for (int i = 0; i < evtfdnum; i++) - { - osfd = _evtlist[i].data.fd; - item = FdRefGet(osfd); - if (NULL == item) - { - MT_ATTR_API(320851, 1); // fd error - MTLOG_ERROR("epfd ref not find, failed, fd: %d", osfd); - epoll_assert(0); - continue; - } - - revents = _evtlist[i].events; - obj = item->GetNotifyObj(); - if (NULL == obj) - { - MTLOG_ERROR("fd notify obj null, failed, fd: %d", osfd); - EpollCtrlDel(osfd, (revents & (EPOLLIN | EPOLLOUT))); - continue; - } - obj->SetRcvEvents(revents); - - // 1. ������, ��Ϻ�ֱ������ - if (revents & (EPOLLERR | EPOLLHUP)) - { - obj->HangupNotify(); - continue; - } - - // 2. �ɶ��¼�, ��0����ֵ������ - if (revents & EPOLLIN) { - ret = obj->InputNotify(); - if (ret != 0) { - continue; - } - } - - // 3. ��д�¼�, ��0����ֵ������ - if (revents & EPOLLOUT) { - ret = obj->OutputNotify(); - if (ret != 0) { - continue; - } - } - } -} - - -/** - * @brief epoll_wait �Լ��ַ��������� - */ -void EpollProxy::EpollDispath() -{ - int wait_time = EpollGetTimeout(); - int nfd = epoll_wait(_epfd, _evtlist, _maxfd, wait_time); - if (nfd <= 0) { - return; - } - - EpollRcvEventList(nfd); -} - - -/** - * @brief �ɶ��¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ -int EpollerObj::InputNotify() -{ - MicroThread* thread = this->GetOwnerThread(); - if (NULL == thread) - { - epoll_assert(0); - MTLOG_ERROR("Epoll fd obj, no thread ptr, wrong"); - return -1; - } - - // ����¼�ͬʱ����, ���ظ����� - if (thread->HasFlag(MicroThread::IO_LIST)) - { - MtFrame* frame = MtFrame::Instance(); - frame->RemoveIoWait(thread); - frame->InsertRunable(thread); - } - - return 0; -} - -/** - * @brief ��д�¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ -int EpollerObj::OutputNotify() -{ - MicroThread* thread = this->GetOwnerThread(); - if (NULL == thread) - { - epoll_assert(0); - MTLOG_ERROR("Epoll fd obj, no thread ptr, wrong"); - return -1; - } - - // ����¼�ͬʱ����, ���ظ����� - if (thread->HasFlag(MicroThread::IO_LIST)) - { - MtFrame* frame = MtFrame::Instance(); - frame->RemoveIoWait(thread); - frame->InsertRunable(thread); - } - - return 0; -} - -/** - * @brief �쳣֪ͨ�ӿ�, �ر�fd����, thread�ȴ�������ʱ - * @return ���Է���ֵ, ���������¼����� - */ -int EpollerObj::HangupNotify() -{ - MtFrame* frame = MtFrame::Instance(); - frame->EpollCtrlDel(this->GetOsfd(), this->GetEvents()); - return 0; -} - -/** - * @brief ����epoll�����¼��Ļص��ӿ�, ������ʼ��EPOLLIN, ż��EPOLLOUT - * @param args fd���ö����ָ�� - * @return 0 �ɹ�, < 0 ʧ��, Ҫ������ع�������ǰ״̬ - */ -int EpollerObj::EpollCtlAdd(void* args) -{ - MtFrame* frame = MtFrame::Instance(); - FdRef* fd_ref = (FdRef*)args; - epoll_assert(fd_ref != NULL); - - int osfd = this->GetOsfd(); - int new_events = this->GetEvents(); - - // ֪ͨ������Ҫ����, FD֪ͨ���������ϲ��Ḵ��, ��������ͻ���, �쳣log��¼ - EpollerObj* old_obj = fd_ref->GetNotifyObj(); - if ((old_obj != NULL) && (old_obj != this)) - { - MTLOG_ERROR("epfd ref conflict, fd: %d, old: %p, now: %p", osfd, old_obj, this); - return -1; - } - fd_ref->SetNotifyObj(this); - - // ���ÿ�ܵ�epoll ctl�ӿ�, ����epoll ctrlϸ�� - if (!frame->EpollCtrlAdd(osfd, new_events)) - { - MTLOG_ERROR("epfd ref add failed, log"); - fd_ref->SetNotifyObj(old_obj); - return -2; - } - - return 0; -} - -/** - * @brief ����epoll�����¼��Ļص��ӿ�, ������ʼ��EPOLLIN, ż��EPOLLOUT - * @param args fd���ö����ָ�� - * @return 0 �ɹ�, < 0 ʧ��, Ҫ������ع�������ǰ״̬ - */ -int EpollerObj::EpollCtlDel(void* args) -{ - MtFrame* frame = MtFrame::Instance(); - FdRef* fd_ref = (FdRef*)args; - epoll_assert(fd_ref != NULL); - - int osfd = this->GetOsfd(); - int events = this->GetEvents(); - - // ֪ͨ������Ҫ����, FD֪ͨ���������ϲ��Ḵ��, ��������ͻ���, �쳣log��¼ - EpollerObj* old_obj = fd_ref->GetNotifyObj(); - if (old_obj != this) - { - MTLOG_ERROR("epfd ref conflict, fd: %d, old: %p, now: %p", osfd, old_obj, this); - return -1; - } - fd_ref->SetNotifyObj(NULL); - - // ���ÿ�ܵ�epoll ctl�ӿ�, ����epoll ctrlϸ�� - if (!frame->EpollCtrlDelRef(osfd, events, false)) // �����з���, �״�����, �رյ� - { - MTLOG_ERROR("epfd ref del failed, log"); - fd_ref->SetNotifyObj(old_obj); - return -2; - } - - return 0; - -} - - diff --git a/app/micro_thread/epoll_proxy.h.bak b/app/micro_thread/epoll_proxy.h.bak deleted file mode 100644 index b08e89fd1..000000000 --- a/app/micro_thread/epoll_proxy.h.bak +++ /dev/null @@ -1,434 +0,0 @@ - -/** - * Tencent is pleased to support the open source community by making MSEC available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the GNU General Public License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. You may - * obtain a copy of the License at - * - * https://opensource.org/licenses/GPL-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the - * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, - * either express or implied. See the License for the specific language governing permissions - * and limitations under the License. - */ - - -/** - * @filename epoll_proxy.h - * @info epoll for micro thread manage - */ - -#ifndef _EPOLL_PROXY___ -#define _EPOLL_PROXY___ - -#include <stdlib.h> -#include <unistd.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <sys/ioctl.h> -#include <sys/uio.h> -#include <sys/time.h> -#include <sys/queue.h> -#include <sys/resource.h> -#include <fcntl.h> -#include <signal.h> -#include <errno.h> -#include <sys/epoll.h> -#include <assert.h> - -#include <set> -#include <vector> -using std::set; -using std::vector; - -#define epoll_assert(statement) -//#define epoll_assert(statement) assert(statement) - - -namespace NS_MICRO_THREAD { - - -/******************************************************************************/ -/* ����ϵͳͷ�ļ����䶨�� */ -/******************************************************************************/ - -/** - * @brief add more detail for linux <sys/queue.h>, freebsd and University of California - * @info queue.h version 8.3 (suse) diff version 8.5 (tlinux) - */ -#ifndef TAILQ_CONCAT - -#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) -#define TAILQ_FIRST(head) ((head)->tqh_first) -#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) - -#define TAILQ_LAST(head, headname) \ - (*(((struct headname *)((head)->tqh_last))->tqh_last)) - -#define TAILQ_FOREACH(var, head, field) \ - for ((var) = TAILQ_FIRST((head)); \ - (var); \ - (var) = TAILQ_NEXT((var), field)) - -#define TAILQ_CONCAT(head1, head2, field) \ -do { \ - if (!TAILQ_EMPTY(head2)) { \ - *(head1)->tqh_last = (head2)->tqh_first; \ - (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ - (head1)->tqh_last = (head2)->tqh_last; \ - TAILQ_INIT((head2)); \ - } \ -} while (0) - -#endif - -#ifndef TAILQ_FOREACH_SAFE // tlinux no this define -#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = TAILQ_FIRST((head)); \ - (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ - (var) = (tvar)) -#endif - - - -/******************************************************************************/ -/* Epoll proxy ������ʵ�ֲ��� */ -/******************************************************************************/ - -class EpollProxy; -class MicroThread; - -/** - * @brief epoll֪ͨ������ඨ�� - */ -class EpollerObj -{ -protected: - int _fd; ///< ϵͳFD �� socket - int _events; ///< �������¼����� - int _revents; ///< �յ����¼����� - int _type; ///< ��������� - MicroThread* _thread; ///< �����߳�ָ����� - -public: - - TAILQ_ENTRY(EpollerObj) _entry; ///< �����̵߳Ĺ������ - - /** - * @brief �������������� - */ - explicit EpollerObj(int fd = -1) { - _fd = fd; - _events = 0; - _revents = 0; - _type = 0; - _thread = NULL; - }; - virtual ~EpollerObj(){}; - - /** - * @brief �ɶ��¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ - virtual int InputNotify(); - - /** - * @brief ��д�¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ - virtual int OutputNotify(); - - /** - * @brief �쳣֪ͨ�ӿ� - * @return ���Է���ֵ, ���������¼����� - */ - virtual int HangupNotify(); - - /** - * @brief ����epoll�����¼��Ļص��ӿ�, ������ʼ��EPOLLIN, ż��EPOLLOUT - * @param args fd���ö����ָ�� - * @return 0 �ɹ�, < 0 ʧ��, Ҫ������ع�������ǰ״̬ - */ - virtual int EpollCtlAdd(void* args); - - /** - * @brief ����epoll�����¼��Ļص��ӿ�, ������ʼ��EPOLLIN, ż��EPOLLOUT - * @param args fd���ö����ָ�� - * @return 0 �ɹ�, < 0 ʧ��, Ҫ������ع�������ǰ״̬ - */ - virtual int EpollCtlDel(void* args); - - /** - * @brief fd�ɶ��¼����� - */ - void EnableInput() { _events |= EPOLLIN; }; - - /** - * @brief fd��д�¼����� - */ - void EnableOutput() { _events |= EPOLLOUT; }; - - /** - * @brief fd�رտɶ��¼����� - */ - void DisableInput() { _events &= ~EPOLLIN; }; - - /** - * @brief fd�رտ�д�¼����� - */ - void DisableOutput() { _events &= ~EPOLLOUT; }; - - /** - * @brief ϵͳsocket���ö�ȡ��װ - */ - int GetOsfd() { return _fd; }; - void SetOsfd(int fd) { _fd = fd; }; - - /** - * @brief �����¼����յ��¼��ķ��ʷ��� - */ - int GetEvents() { return _events; }; - void SetRcvEvents(int revents) { _revents = revents; }; - int GetRcvEvents() { return _revents; }; - - /** - * @brief ������������, ��ȡ��ʵ���� - */ - int GetNtfyType() { return _type; }; - virtual void Reset() { - _fd = -1; - _events = 0; - _revents = 0; - _type = 0; - _thread = NULL; - }; - - /** - * @brief �������ȡ�������߳̾���ӿ� - * @param thread �������߳�ָ�� - */ - void SetOwnerThread(MicroThread* thread) { _thread = thread; }; - MicroThread* GetOwnerThread() { return _thread; }; - -}; - -typedef TAILQ_HEAD(__EpFdList, EpollerObj) EpObjList; ///< ��Ч��˫������ -typedef struct epoll_event EpEvent; ///< �ض���һ��epoll event - - -/** - * @brief EPOLL֧��ͬһFD����߳�����, ����һ�����ü�������, Ԫ�ض��� - * @info ���ü����״�����, û��ʵ������, �ֶα���, �����Ƴ��� 20150623 - */ -class FdRef -{ -private: - int _wr_ref; ///< ����д�����ü��� - int _rd_ref; ///< �����������ü��� - int _events; ///< ��ǰ�����������¼��б� - int _revents; ///< ��ǰ��fd�յ����¼���Ϣ, ����epoll_wait��������Ч - EpollerObj* _epobj; ///< ����ע�����������һ��fd����һ������ - -public: - - /** - * @brief �������������� - */ - FdRef() { - _wr_ref = 0; - _rd_ref = 0; - _events = 0; - _revents = 0; - _epobj = NULL; - }; - ~FdRef(){}; - - /** - * @brief �����¼���ȡ�����ýӿ� - */ - void SetListenEvents(int events) { - _events = events; - }; - int GetListenEvents() { - return _events; - }; - - /** - * @brief ���������ȡ�����ýӿ� - */ - void SetNotifyObj(EpollerObj* ntfy) { - _epobj = ntfy; - }; - EpollerObj* GetNotifyObj() { - return _epobj; - }; - - /** - * @brief �������ü����ĸ��� - */ - void AttachEvents(int event) { - if (event & EPOLLIN) { - _rd_ref++; - } - if (event & EPOLLOUT){ - _wr_ref++; - } - }; - void DetachEvents(int event) { - if (event & EPOLLIN) { - if (_rd_ref > 0) { - _rd_ref--; - } else { - _rd_ref = 0; - } - } - if (event & EPOLLOUT){ - if (_wr_ref > 0) { - _wr_ref--; - } else { - _wr_ref = 0; - } - } - }; - - /** - * @brief ��ȡ���ü��� - */ - int ReadRefCnt() { return _rd_ref; }; - int WriteRefCnt() { return _wr_ref; }; - -}; - - -/** - * @brief EPOLL����, ��װepoll������epollȫ������ - */ -class EpollProxy -{ -public: - static const int DEFAULT_MAX_FD_NUM = 100000; ///< Ĭ������ص�fd - -private: - int _epfd; ///< epoll ����� - int _maxfd; ///< �����ļ������ - EpEvent* _evtlist; ///< epoll���ظ��û����¼��б�ָ�� - FdRef* _eprefs; ///< �û��������¼����ع������� - -public: - - /** - * @brief �������������� - */ - EpollProxy(); - virtual ~EpollProxy(){}; - - /** - * @brief epoll��ʼ������ֹ����, ���붯̬�ڴ�� - * @param max_num ���ɹ�����fd��Ŀ - */ - int InitEpoll(int max_num); - void TermEpoll(void); - - /** - * @brief epoll_wait ��ȡ���ȴ�ʱ��ӿ� - * @return Ŀǰ��Ҫ�ȴ���ʱ��, ��λMS - */ - virtual int EpollGetTimeout(void) { return 0;}; - - /** - * @brief epoll �������Ƚӿ� - * @param fdlist ��·��������, ���з��͵�socket���� - * @param fd ����socket, �����ȴ� - * @param timeout ��ʱʱ������, ���� - * @return true �ɹ�, false ʧ�� - */ - virtual bool EpollSchedule(EpObjList* fdlist, EpollerObj* fd, int timeout) { return false;}; - - /** - * @brief ��һ���߳�����������socket����epoll���� - * @param fdset �߳�������socket���� - * @return true �ɹ�, false ʧ�� - */ - bool EpollAdd(EpObjList& fdset); - - /** - * @brief ��һ���߳�����������socket�Ƴ�epoll���� - * @param fdset �߳�������socket���� - * @return true �ɹ�, false ʧ�� - */ - bool EpollDel(EpObjList& fdset); - - /** - * @brief epoll_wait �Լ��ַ��������� - */ - void EpollDispath(void); - - /** - * @brief ����һ��fdע��, ���������¼� - * @param fd �ļ�������¼���Ϣ - * @param obj epoll�ص����� - */ - bool EpollAddObj(EpollerObj* obj); - - /** - * @brief ȡ��һ��fdע��, ���������¼� - * @param fd �ļ�������¼���Ϣ - * @param obj epoll�ص����� - */ - bool EpollDelObj(EpollerObj* obj); - - /** - * @brief ��װepoll ctl�Ĵ����뵱ǰ�����¼��ļ�¼, �ڲ��ӿ� - * @param fd �������ļ���� - * @param new_events ��Ҫ�����ļ����¼� - */ - bool EpollCtrlAdd(int fd, int new_events); - - /** - * @brief ��װepoll ctl�Ĵ����뵱ǰ�����¼��ļ�¼, �ڲ��ӿ� - * @param fd �������ļ���� - * @param new_events ��Ҫ��ɾ���ļ����¼� - */ - bool EpollCtrlDel(int fd, int new_events); - bool EpollCtrlDelRef(int fd, int new_events, bool use_ref); - - /** - * @brief ����fd��ȡ�������õĽṹ, ��fd���ɲ���, Ŀǰ���� - * @param fd �ļ������� - * @return �����ļ����ýṹ, NULL ��ʾʧ�� - */ - FdRef* FdRefGet(int fd) { - return ((fd >= _maxfd) || (fd < 0)) ? (FdRef*)NULL : &_eprefs[fd]; - }; - - - /** - * @brief ������ע��ӿ�, ����ע���ȡ��ע��֪ͨ���� - * @param fd �������ļ���� - * @param obj ��ע���ȡ��ע��Ķ��� - */ - void EpollNtfyReg(int fd, EpollerObj* obj) { - FdRef* ref = FdRefGet(fd); - if (ref) { - ref->SetNotifyObj(obj); - } - }; - -protected: - - /** - * @brief ����ÿ��socket�����½����¼���Ϣ - * @param evtfdnum �յ��¼���fd������Ŀ - */ - void EpollRcvEventList(int evtfdnum); - -}; -}//NAMESPCE - -#endif - - diff --git a/app/micro_thread/hash_list.h b/app/micro_thread/hash_list.h index 09d338da0..e75823e2b 100644 --- a/app/micro_thread/hash_list.h +++ b/app/micro_thread/hash_list.h @@ -19,8 +19,6 @@ /** * @filename hash_list.h - * @info ����hash��, ��hash�洢ʵ��; �̳���ʵ��hashӳ��, ע�����Ԫ�ص� - * ��������, ��ջ�����Ȼ��Զ�������Ԫ��, ��Ҫ����ڴ�, ����ỵ�� * @time 2013-06-11 */ @@ -35,94 +33,59 @@ namespace NS_MICRO_THREAD { -/** - * @brief Hash���Ԫ�صĻ���, �̳и�Ԫ�ؼ���ʵ����չ - */ class HashKey { private: - HashKey* _next_entry; ///< ����hash������Ԫ�� - uint32_t _hash_value; ///< hash value��Ϣ, ��Լ�Ƚϵ�ʱ�� - void* _data_ptr; ///< hash data����ָ��, ��key - value �ۺϴ洢 + HashKey* _next_entry; + uint32_t _hash_value; + void* _data_ptr; public: - friend class HashList; ///< hash������ֱ�ӷ���nextָ�� + friend class HashList; - /** - * @brief ���������������� - */ HashKey():_next_entry(NULL), _hash_value(0), _data_ptr(NULL) {}; virtual ~HashKey(){}; - /** - * @brief �ڵ�Ԫ�ص�hash�㷨, ��ȡkey��hashֵ - * @return �ڵ�Ԫ�ص�hashֵ - */ virtual uint32_t HashValue() = 0; - /** - * @brief �ڵ�Ԫ�ص�cmp����, ͬһͰID��, ��key�Ƚ� - * @return �ڵ�Ԫ�ص�hashֵ - */ virtual int HashCmp(HashKey* rhs) = 0; - - /** - * @brief �ѱ����ӿ�, ���ڵ���, �ڱ���ÿ��Ԫ��ʱ������, ��ѡʵ�� - */ + virtual void HashIterate() { return; }; - /** - * @brief �ڵ�Ԫ�ص�ʵ������ָ���������ȡ - */ void* GetDataPtr() { return _data_ptr; }; void SetDataPtr(void* data) { _data_ptr = data; }; - - }; - -/** - * @brief Hash������, ����ʽhash, ע��ѡ����ʵ�hash����, �����ͻ���� - */ class HashList { public: - /** - * @brief ���캯������������ - */ - explicit HashList(int max = 100000) { + explicit HashList(int max = 100000) { _max = GetMaxPrimeNum((max > 2) ? max : 100000); - _buckets = (HashKey**)calloc(_max, sizeof(HashKey*)); - _count = 0; - }; - virtual ~HashList() { - if (_buckets) { - free(_buckets); - _buckets = NULL; - } + _buckets = (HashKey**)calloc(_max, sizeof(HashKey*)); _count = 0; - }; + }; + virtual ~HashList() { + if (_buckets) { + free(_buckets); + _buckets = NULL; + } + _count = 0; + }; - /** - * @brief ��ȡhash��Ԫ�ظ��� - * @return ��Ԫ��ʵ����Ŀ - */ int HashSize() { return _count; }; /** - * @brief hash����Ԫ��, Ҫ�ڸ�Ԫ������ǰ, ����remove - * @param key �������Ԫ��ָ��, ע��Ԫ�ص���������, ��Ҫ����ջ���� - * @return 0 �ɹ�, -1 ������Ч��δ��ʼ��, -2 �ظ������������ + * @brief hash insert key. */ int HashInsert(HashKey* key) { if (!key || !_buckets) { @@ -144,9 +107,7 @@ public: } /** - * @brief hash����Ԫ�� - * @param key ����ѯ��keyָ�� - * @return ��ѯ�������ָ��, NULL���������� + * @brief hash lookup key. */ HashKey* HashFind(HashKey* key) { if (!key || !_buckets) { @@ -171,9 +132,7 @@ public: } /** - * @brief hash����Ԫ�� - * @param key ����ѯ��keyָ�� - * @return ��ѯ�������ָ��, NULL���������� + * @brief hash lookup key. */ void* HashFindData(HashKey* key) { HashKey* item = HashFind(key); @@ -186,8 +145,7 @@ public: /** - * @brief hashɾ��Ԫ�� - * @param key ��ɾ����keyָ�� + * @brief hash remove key. */ void HashRemove(HashKey* key) { if (!key || !_buckets) { @@ -215,7 +173,7 @@ public: } /** - * @brief hash����Ԫ��, ���õ������� + * @brief hash loop. */ void HashForeach() { if (!_buckets) { @@ -231,7 +189,7 @@ public: } /** - * @brief hash��������, ���ܵ���, ֻ�������ձ������� + * @brief traverse hash list, low performance, only for remove. */ HashKey* HashGetFirst() { if (!_buckets) { @@ -249,38 +207,35 @@ public: private: - /** - * @brief ��ȡͰ���ȵ�������� - */ int GetMaxPrimeNum(int num) { - int sqrt_value = (int)sqrt(num); - for (int i = num; i > 0; i--) - { - int flag = 1; - for (int k = 2; k <= sqrt_value; k++) - { - if (i % k == 0) - { - flag = 0; - break; - } - } + int sqrt_value = (int)sqrt(num); + for (int i = num; i > 0; i--) + { + int flag = 1; + for (int k = 2; k <= sqrt_value; k++) + { + if (i % k == 0) + { + flag = 0; + break; + } + } - if (flag == 1) - { - return i; - } - } + if (flag == 1) + { + return i; + } + } - return 0; + return 0; }; private: - HashKey** _buckets; ///< Ͱָ�� - int _count; ///< ��ЧԪ�ظ��� - int _max; ///< ���ڵ���� + HashKey** _buckets; + int _count; + int _max; }; } diff --git a/app/micro_thread/heap.h b/app/micro_thread/heap.h index 6a90caece..6b44e7dd5 100644 --- a/app/micro_thread/heap.h +++ b/app/micro_thread/heap.h @@ -19,7 +19,7 @@ /** * @filename heap.h - * @info ������ɾ���Ķ�, �����û�����ɾ������, ������std::make_heap + * @info flexible insert and delete heap, if no random deletion, use std::make_heap * @time 2013-06-11 */ @@ -36,62 +36,36 @@ namespace NS_MICRO_THREAD { -class HeapEntry; // ��Ԫ����, �̳�ʵ����չ -class HeapList; // �ѹ�����, ͨ�� +class HeapEntry; +class HeapList; /** - * @brief ��С�ѵĶ�Ԫ�ض���, ���ڹ���ͨ�õĶ�, �̳и�Ԫ�ؼ�����չ + * @brief definition of heap elements for minimum heap */ class HeapEntry { private: - int _index; ///< ��Ԫ���±�, ���ڿ�������ɾ������ + int _index; public: - friend class HeapList; + friend class HeapList; - /** - * @brief ���������������� - */ - HeapEntry():_index(0){}; - virtual ~HeapEntry(){}; + HeapEntry():_index(0){}; + virtual ~HeapEntry(){}; - /** - * @brief ��Ԫ��ȡֵ����, ���ڷ���ֵ�Ƚ�, ���Ӻ���ʵ��, ����Ĭ������ - * @return ��Ԫ��ӳ���ֵ - */ virtual unsigned long long HeapValue() = 0; - - /** - * @brief �ѱ����ӿ�, ���ڵ���, �ڱ���ÿ��Ԫ��ʱ������, ��ѡʵ�� - */ virtual void HeapIterate() { return; }; - /** - * @brief ��Ԫ�ز������ - * @param list ��ָ�� - * @return 0 �ɹ�; ����ʧ�� -1 ����; -2 �ظ����� - */ inline int InsertIntoHeap(HeapList* list); - /** - * @brief ��Ԫ�شӶ���ɾ�� - * @param list ��ָ�� - * @return 0 �ɹ�; ����ʧ�� -1 �ѿ�; -2 �ظ�ɾ���������� - */ inline int DeleteFromHeap(HeapList* list); - - /** - * @brief ��Ԫ���±���Ϣ��ȡ, �ڲ�����ʹ�� - * @return ��Ԫ���ڶ����±���Ϣ - */ - inline int GetIndex() { - return _index; - }; + inline int GetIndex() { + return _index; + }; private: @@ -105,50 +79,42 @@ private: } }; - inline void SetIndex(int index) { - _index = index; - }; + inline void SetIndex(int index) { + _index = index; + }; }; /** - * @brief ��С�Ѷ�����, ͨ���� + * @brief minimum heap queue. */ class HeapList { private: - HeapEntry** _list; // ��Ԫ�ص�ָ������, Ŀǰ���� - int _max; // �ѿɹ������Ԫ�ظ��� - int _count; // ���Ѿ�������Ԫ�ظ��� + HeapEntry** _list; + int _max; + int _count; public: - - /** - * @brief ���캯������������ - */ - explicit HeapList(int max = 100000) { + + explicit HeapList(int max = 100000) { _max = (max > 0) ? max : 100000; - _list = (HeapEntry**)malloc (sizeof(HeapEntry*) * (_max+1)); - heap_assert(_list); - memset(_list, 0, sizeof(HeapEntry*) * (_max+1)); - _count = 0; - }; - virtual ~HeapList() { - if (_list) { - free(_list); - _list = NULL; - } + _list = (HeapEntry**)malloc (sizeof(HeapEntry*) * (_max+1)); + heap_assert(_list); + memset(_list, 0, sizeof(HeapEntry*) * (_max+1)); + _count = 0; + }; + virtual ~HeapList() { + if (_list) { + free(_list); + _list = NULL; + } _max = 0; _count = 0; - }; + }; - /** - * @brief ��չheap�Ĵ�С, ��С����� - * @param size �µĶ�Ԫ�ظ��� - * @return 0 �ɹ�; -1 ʧ�� - */ int HeapResize(int size) { if (_max >= size) { return 0; @@ -166,270 +132,199 @@ public: return 0; }; - + - /** - * @brief �����Ԫ�� - * @param entry ��Ԫ��ָ�� - * @return 0 �ɹ�; ����ʧ�� -1 ����; -2 �ظ����� - */ - int HeapPush(HeapEntry* entry); + int HeapPush(HeapEntry* entry); - /** - * @brief ȡ�Ѷ�Ԫ��, ���Ƴ���Ԫ�� - * @return �Ѷ�Ԫ��ָ��, NULL ��ʾ��Ϊ�� - */ - HeapEntry* HeapPop(); + HeapEntry* HeapPop(); - /** - * @brief �Ƴ������Ԫ�� - * @param entry ��Ԫ��ָ�� - * @return 0 �ɹ�; ����ʧ�� -1 �ѿ�; -2 �ظ�ɾ���������� - */ - int HeapDelete(HeapEntry* entry); + int HeapDelete(HeapEntry* entry); - /** - * @brief ���Խӿ�, ��2��ѷ�ʽ��ӡԪ��, ͬʱ����ÿԪ�صĵ����ӿ� - */ - void HeapForeach(); + void HeapForeach(); - /** - * @brief ��ȡ�ѵ�Ԫ�ظ��� - * @return ��Ԫ��ʵ����Ŀ - */ int HeapSize() { return _count; }; - /** - * @brief ȡ�Ѷ�Ԫ��, ���Ƴ���Ԫ�� - * @return �Ѷ�Ԫ��ָ��, NULL ��ʾ��Ϊ�� - */ HeapEntry* HeapTop() { return (_count > 0) ? _list[1] : NULL; }; private: - /** - * @brief �ж����Ƿ��� - * @return true �� - */ - bool HeapFull() { - return (_count >= _max); - }; + bool HeapFull() { + return (_count >= _max); + }; - /** - * @brief �ж����Ƿ�� - * @return true �� - */ - bool HeapEmpty() { - return (_count == 0); - }; - /** - * @brief ���ȽϺ���, �������Ŷ�Ԫ�� - */ - void HeapUp(); + bool HeapEmpty() { + return (_count == 0); + }; - /** - * @brief ���ȽϺ���, �������Ŷ�Ԫ�� - */ - void HeapDown(int index); + void HeapUp(); + + void HeapDown(int index); }; -/** - * @brief ���ȽϺ���, �������Ŷ�Ԫ�� - */ + inline void HeapList::HeapUp() { - for (int pos = _count; pos > 0; pos = pos/2) - { - if (pos/2 < 1) // pos == 1 �Ѿ�����, 0 ���ڱ��� - { - break; - } + for (int pos = _count; pos > 0; pos = pos/2) + { + if (pos/2 < 1) // pos == 1 peaked, 0 reserved. + { + break; + } if (_list[pos]->HeapValueCmp(_list[pos/2]) < 0) - { - HeapEntry* tmp = _list[pos/2]; - _list[pos/2] = _list[pos]; - _list[pos] = tmp; + { + HeapEntry* tmp = _list[pos/2]; + _list[pos/2] = _list[pos]; + _list[pos] = tmp; - _list[pos]->SetIndex(pos); - _list[pos/2]->SetIndex(pos/2); - } - else - { - break; - } - } + _list[pos]->SetIndex(pos); + _list[pos/2]->SetIndex(pos/2); + } + else + { + break; + } + } } -/** - * @brief ���ȽϺ���, �������Ŷ�Ԫ�� - * @param index �Ӹ�λ�ÿ�ʼ���� - */ inline void HeapList::HeapDown(int index) { - int min_son; - for (int pos = index; pos <= _count; pos = min_son) - { - if (pos*2 > _count) // pos��Ҷ�ӽڵ��� - { - break; - } - else if (pos*2 == _count) - { - min_son = pos*2; - } - else - { + int min_son; + for (int pos = index; pos <= _count; pos = min_son) + { + if (pos*2 > _count) // pos is a leaf node. + { + break; + } + else if (pos*2 == _count) + { + min_son = pos*2; + } + else + { if (_list[pos*2+1]->HeapValueCmp(_list[pos*2]) < 0) - { - min_son = pos*2+1; - } - else - { - min_son = pos*2; - } - } + { + min_son = pos*2+1; + } + else + { + min_son = pos*2; + } + } - if (_list[pos]->HeapValueCmp(_list[min_son]) > 0) - { - HeapEntry* tmp = _list[min_son]; - _list[min_son] = _list[pos]; - _list[pos] = tmp; + if (_list[pos]->HeapValueCmp(_list[min_son]) > 0) + { + HeapEntry* tmp = _list[min_son]; + _list[min_son] = _list[pos]; + _list[pos] = tmp; - _list[pos]->SetIndex(pos); - _list[min_son]->SetIndex(min_son); - } - else - { - break; - } - } + _list[pos]->SetIndex(pos); + _list[min_son]->SetIndex(min_son); + } + else + { + break; + } + } } -/** - * @brief �����Ԫ�� - * @param entry ��Ԫ��ָ�� - * @return 0 �ɹ�; ����ʧ�� -1 ����; -2 �ظ����� - */ inline int HeapList::HeapPush(HeapEntry* item) { - if (HeapFull()) { - heap_assert(0); // ��, �������ǿ��ܵ�, ʵ�����в�̫���ܹ�10W - return -1; - } + if (HeapFull()) { + heap_assert(0); // it's possible in theory but not in fact. + return -1; + } if (item->GetIndex() != 0) { - heap_assert(0); // �ظ����� + heap_assert(0); // duplicated insertion. return -2; } - _count++; - _list[_count] = item; + _count++; + _list[_count] = item; item->SetIndex(_count); - HeapUp(); + HeapUp(); - return 0; + return 0; } -/** - * @brief ȡ�Ѷ�Ԫ��, ���Ƴ���Ԫ�� - * @return �Ѷ�Ԫ��ָ��, NULL ��ʾ��Ϊ�� - */ inline HeapEntry* HeapList::HeapPop() { - if (HeapEmpty()) { - return NULL; - } + if (HeapEmpty()) { + return NULL; + } - HeapEntry* top = _list[1]; // 0 ���� + HeapEntry* top = _list[1]; // 0 reserved. - _list[1] = _list[_count]; + _list[1] = _list[_count]; _list[1]->SetIndex(1); _list[_count] = 0; - _count--; - HeapDown(1); - + _count--; + HeapDown(1); + heap_assert(top->GetIndex() == 1); - top->SetIndex(0); - return top; + top->SetIndex(0); + return top; } -/** - * @brief �Ƴ������Ԫ�� - * @param entry ��Ԫ��ָ�� - * @return 0 �ɹ�; ����ʧ�� -1 �ѿ�; -2 �ظ�ɾ���������� - */ inline int HeapList::HeapDelete(HeapEntry* item) { - if (HeapEmpty()) { - return -1; - } + if (HeapEmpty()) { + return -1; + } - int pos = item->GetIndex() ; - if ((pos > _count) ||(pos <= 0)) - { - heap_assert(0); // �Ƿ����ݻ��ظ�ɾ�� - return -2; - } + int pos = item->GetIndex() ; + if ((pos > _count) ||(pos <= 0)) + { + heap_assert(0); // duplicated deletion or illegal data. + return -2; + } - HeapEntry* del = _list[pos]; - _list[pos] = _list[_count]; + HeapEntry* del = _list[pos]; + _list[pos] = _list[_count]; _list[pos]->SetIndex(pos); _list[_count] = 0; - _count--; + _count--; - HeapDown(pos); + HeapDown(pos); heap_assert(pos == del->GetIndex()); - del->SetIndex(0); - return 0; + del->SetIndex(0); + return 0; } -/** - * @brief ���Խӿ�, ��2��ѷ�ʽ��ӡԪ��, ͬʱ����ÿԪ�صĵ����ӿ� - */ inline void HeapList::HeapForeach() { int per = 1; - for (int i = 1; i <= _count; i++) - { - if (i >= per*2) - { - printf("\n"); - per *=2; - } - printf("%llu ", _list[i]->HeapValue()); + for (int i = 1; i <= _count; i++) + { + if (i >= per*2) + { + printf("\n"); + per *=2; + } + printf("%llu ", _list[i]->HeapValue()); _list[i]->HeapIterate(); - } + } } -/** - * @brief ��Ԫ�ز������ - * @param list ��ָ�� - * @return 0 �ɹ�; ����ʧ�� -1 ����; -2 �ظ����� - */ inline int HeapEntry::InsertIntoHeap(HeapList* list) { return list->HeapPush(this); }; -/** - * @brief ��Ԫ�شӶ���ɾ�� - * @param list ��ָ�� - * @return 0 �ɹ�; ����ʧ�� -1 �ѿ�; -2 �ظ�ɾ���������� - */ inline int HeapEntry::DeleteFromHeap(HeapList* list) { return list->HeapDelete(this); }; diff --git a/app/micro_thread/heap_timer.cpp b/app/micro_thread/heap_timer.cpp index c30c7fd98..db59b9572 100644 --- a/app/micro_thread/heap_timer.cpp +++ b/app/micro_thread/heap_timer.cpp @@ -26,10 +26,6 @@ using namespace NS_MICRO_THREAD; - -/** - * @brief ���캯�� - */ CTimerMng::CTimerMng(uint32_t max_item) { #define TIMER_MIN 100000 @@ -42,10 +38,6 @@ CTimerMng::CTimerMng(uint32_t max_item) _heap = new HeapList(max_item); } - -/** - * @brief �������� - */ CTimerMng::~CTimerMng() { if (_heap) { @@ -54,13 +46,6 @@ CTimerMng::~CTimerMng() } } - -/** - * @brief ��ʱ�����ú��� - * @param timerable ��ʱ������ - * @param interval ��ʱ�ļ�� ms��λ - * @return �ɹ�����true, ����ʧ�� - */ bool CTimerMng::start_timer(CTimerNotify* timerable, uint32_t interval) { if (!_heap || !timerable) { @@ -78,10 +63,6 @@ bool CTimerMng::start_timer(CTimerNotify* timerable, uint32_t interval) return true; } -/** - * @brief ��ʱ��ֹͣ�ӿں��� - * @param timerable ��ʱ������ - */ void CTimerMng::stop_timer(CTimerNotify* timerable) { if (!_heap || !timerable) { @@ -92,9 +73,6 @@ void CTimerMng::stop_timer(CTimerNotify* timerable) return; } -/** - * @brief ��ʱ����ʱ��⺯�� - */ void CTimerMng::check_expired() { if (!_heap) { @@ -110,6 +88,3 @@ void CTimerMng::check_expired() timer = dynamic_cast<CTimerNotify*>(_heap->HeapTop()); } }; - - - diff --git a/app/micro_thread/heap_timer.h b/app/micro_thread/heap_timer.h index e20791f7e..950a2674e 100644 --- a/app/micro_thread/heap_timer.h +++ b/app/micro_thread/heap_timer.h @@ -30,99 +30,52 @@ namespace NS_MICRO_THREAD { -/** - * @brief ��ʱ��������� - */ class CTimerNotify : public HeapEntry { public: - /** - * @brief ��ʱ֪ͨ����, ����ʵ���� - */ virtual void timer_notify() { return;}; - - /** - * @brief ��Ԫ��ȡֵ����, ���ڷ���ֵ�Ƚ�, ���Ӻ���ʵ��, ����Ĭ������ - * @return ��Ԫ��ӳ���ֵ - */ + virtual unsigned long long HeapValue() { return (unsigned long long)_time_expired; }; - /** - * @brief ���캯�� - */ CTimerNotify() : _time_expired(0) {}; - /** - * @brief ���������� - */ virtual ~CTimerNotify(){}; - /** - * @brief ���þ��Գ�ʱʱ��, ��λms - * @param expired ���Գ�ʱʱ�� ms��λ - */ void set_expired_time(uint64_t expired) { _time_expired = expired; }; - /** - * @brief ��ȡ���Գ�ʱʱ��, ��λms - * @return ���Գ�ʱʱ�� ms��λ - */ uint64_t get_expired_time() { return _time_expired; }; private: - uint64_t _time_expired; // ���Եij�ʱʱ��ms��λ + uint64_t _time_expired; }; -/** - * @brief ��ʱ������������ - */ class CTimerMng { public: - /** - * @brief ���캯�� - * @param max_item ���ɹ����Ķ�ʱ��������Ŀ(ָ����Ŀ) - */ explicit CTimerMng(uint32_t max_item = 100000); - /** - * @brief �������� - */ ~CTimerMng(); - /** - * @brief ��ʱ�����ú��� - * @param timerable ��ʱ������ - * @param interval ��ʱ�ļ�� ms��λ - * @return �ɹ�����true, ����ʧ�� - */ bool start_timer(CTimerNotify* timerable, uint32_t interval); - /** - * @brief ��ʱ��ֹͣ�ӿں��� - * @param timerable ��ʱ������ - */ void stop_timer(CTimerNotify* timerable); - /** - * @brief ��ʱ����ʱ��⺯�� - */ void check_expired(); private: - HeapList* _heap; // ��С��ָ�� + HeapList* _heap; }; } diff --git a/app/micro_thread/kqueue_proxy.cpp b/app/micro_thread/kqueue_proxy.cpp index edf578ee6..fa18d2bd0 100644 --- a/app/micro_thread/kqueue_proxy.cpp +++ b/app/micro_thread/kqueue_proxy.cpp @@ -39,34 +39,34 @@ KqueueProxy::KqueueProxy() int KqueueProxy::InitKqueue(int max_num) { - int rc = 0; - if (max_num > _maxfd) - { - _maxfd = max_num; - } + int rc = 0; + if (max_num > _maxfd) + { + _maxfd = max_num; + } - _kqfd = ff_kqueue(); - if (_kqfd < 0) - { - rc = -1; - goto EXIT_LABEL; - } + _kqfd = ff_kqueue(); + if (_kqfd < 0) + { + rc = -1; + goto EXIT_LABEL; + } - ff_fcntl(_kqfd, F_SETFD, FD_CLOEXEC); + ff_fcntl(_kqfd, F_SETFD, FD_CLOEXEC); - _kqrefs = new KqFdRef[_maxfd]; - if (_kqrefs == NULL) - { - rc = -2; - goto EXIT_LABEL; - } + _kqrefs = new KqFdRef[_maxfd]; + if (_kqrefs == NULL) + { + rc = -2; + goto EXIT_LABEL; + } - _evtlist = (KqEvent*)calloc(_maxfd, sizeof(KqEvent)); - if (_evtlist == NULL) - { - rc = -3; - goto EXIT_LABEL; - } + _evtlist = (KqEvent*)calloc(_maxfd, sizeof(KqEvent)); + if (_evtlist == NULL) + { + rc = -3; + goto EXIT_LABEL; + } struct rlimit rlim; memset(&rlim, 0, sizeof(rlim)); @@ -115,20 +115,20 @@ void KqueueProxy::TermKqueue() bool KqueueProxy::KqueueAdd(KqObjList& obj_list) { - bool ret = true; - KqueuerObj *kqobj = NULL; - KqueuerObj *kqobj_error = NULL; - TAILQ_FOREACH(kqobj, &obj_list, _entry) - { - if (!KqueueAddObj(kqobj)) - { + bool ret = true; + KqueuerObj *kqobj = NULL; + KqueuerObj *kqobj_error = NULL; + TAILQ_FOREACH(kqobj, &obj_list, _entry) + { + if (!KqueueAddObj(kqobj)) + { MTLOG_ERROR("kqobj add failed, fd: %d", kqobj->GetOsfd()); kqueue_assert(0); kqobj_error = kqobj; ret = false; goto EXIT_LABEL; - } - } + } + } EXIT_LABEL: @@ -167,303 +167,303 @@ bool KqueueProxy::KqueueDel(KqObjList& obj_list) bool KqueueProxy::KqueueCtrlAdd(int fd, int events) { - KqFdRef* item = KqFdRefGet(fd); - if (item == NULL) - { + KqFdRef* item = KqFdRefGet(fd); + if (item == NULL) + { MT_ATTR_API(320851, 1); // fd error, wtf? MTLOG_ERROR("kqfd ref not find, failed, fd: %d", fd); kqueue_assert(0); return false; - } + } - item->AttachEvents(events); + item->AttachEvents(events); - int old_events = item->GetListenEvents(); - int new_events = old_events | events; - if (old_events == new_events) - { - return true; - } - - KqEvent ke; - int ret; - if (old_events & KQ_EVENT_WRITE) { - EV_SET(&ke, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); - ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); - if (ret == -1) { - // TODO, error check - item->DetachEvents(events); - kqueue_assert(0); - return false; - } - } - if (old_events & KQ_EVENT_READ) { - EV_SET(&ke, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); - ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); - if (ret == -1) { - // TODO, error check - item->DetachEvents(events); - kqueue_assert(0); - return false; - } - } - if (events & KQ_EVENT_WRITE) { - EV_SET(&ke, fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL); - ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); - if (ret == -1) { - // TODO, error check - item->DetachEvents(events); - kqueue_assert(0); - return false; - } - } - if (events & KQ_EVENT_READ) { - EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL); - ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); - if (ret == -1) { - // TODO, error check - item->DetachEvents(events); - kqueue_assert(0); - return false; - } - } + int old_events = item->GetListenEvents(); + int new_events = old_events | events; + if (old_events == new_events) + { + return true; + } + + KqEvent ke; + int ret; + if (old_events & KQ_EVENT_WRITE) { + EV_SET(&ke, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); + ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); + if (ret == -1) { + // TODO, error check + item->DetachEvents(events); + kqueue_assert(0); + return false; + } + } + if (old_events & KQ_EVENT_READ) { + EV_SET(&ke, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); + ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); + if (ret == -1) { + // TODO, error check + item->DetachEvents(events); + kqueue_assert(0); + return false; + } + } + if (events & KQ_EVENT_WRITE) { + EV_SET(&ke, fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL); + ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); + if (ret == -1) { + // TODO, error check + item->DetachEvents(events); + kqueue_assert(0); + return false; + } + } + if (events & KQ_EVENT_READ) { + EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL); + ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); + if (ret == -1) { + // TODO, error check + item->DetachEvents(events); + kqueue_assert(0); + return false; + } + } - item->SetListenEvents(new_events); + item->SetListenEvents(new_events); - return true; + return true; } bool KqueueProxy::KqueueCtrlDel(int fd, int events) { - return KqueueCtrlDelRef(fd, events, false); + return KqueueCtrlDelRef(fd, events, false); } bool KqueueProxy::KqueueCtrlDelRef(int fd, int events, bool use_ref) { - KqFdRef* item = KqFdRefGet(fd); - if (item == NULL) - { + KqFdRef* item = KqFdRefGet(fd); + if (item == NULL) + { MT_ATTR_API(320851, 1); // fd error MTLOG_ERROR("kqfd ref not find, failed, fd: %d", fd); kqueue_assert(0); return false; - } + } - item->DetachEvents(events); - int old_events = item->GetListenEvents(); - int new_events = old_events &~ events; + item->DetachEvents(events); + int old_events = item->GetListenEvents(); + int new_events = old_events &~ events; - if (use_ref) { - new_events = old_events; - if (item->ReadRefCnt() == 0) { - new_events = new_events & ~KQ_EVENT_READ; - } - if (item->WriteRefCnt() == 0) { - new_events = new_events & ~KQ_EVENT_WRITE; - } - } + if (use_ref) { + new_events = old_events; + if (item->ReadRefCnt() == 0) { + new_events = new_events & ~KQ_EVENT_READ; + } + if (item->WriteRefCnt() == 0) { + new_events = new_events & ~KQ_EVENT_WRITE; + } + } - if (old_events == new_events) - { - return true; - } - KqEvent ke; - int ret; - if (old_events & KQ_EVENT_WRITE) { - EV_SET(&ke, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); - ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); - if (ret == -1) { - kqueue_assert(0); - return false; - } - } - if (old_events & KQ_EVENT_READ) { - EV_SET(&ke, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); - ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); - if (ret == -1) { - kqueue_assert(0); - return false; - } - } + if (old_events == new_events) + { + return true; + } + KqEvent ke; + int ret; + if (old_events & KQ_EVENT_WRITE) { + EV_SET(&ke, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); + ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); + if (ret == -1) { + kqueue_assert(0); + return false; + } + } + if (old_events & KQ_EVENT_READ) { + EV_SET(&ke, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); + ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); + if (ret == -1) { + kqueue_assert(0); + return false; + } + } - if (new_events & KQ_EVENT_WRITE) { - EV_SET(&ke, fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL); - ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); - if (ret == -1) { - kqueue_assert(0); - return false; - } - } - if (new_events & KQ_EVENT_READ) { - EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL); - ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); - if (ret == -1) { - kqueue_assert(0); - return false; - } - } + if (new_events & KQ_EVENT_WRITE) { + EV_SET(&ke, fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL); + ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); + if (ret == -1) { + kqueue_assert(0); + return false; + } + } + if (new_events & KQ_EVENT_READ) { + EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL); + ret = ff_kevent(_kqfd, &ke, 1, NULL, 0, NULL); + if (ret == -1) { + kqueue_assert(0); + return false; + } + } - item->SetListenEvents(new_events); + item->SetListenEvents(new_events); - return true; + return true; } bool KqueueProxy::KqueueAddObj(KqueuerObj* obj) { - if (obj == NULL) - { + if (obj == NULL) + { MTLOG_ERROR("kqobj input invalid, %p", obj); return false; - } + } - KqFdRef* item = KqFdRefGet(obj->GetOsfd()); - if (item == NULL) - { + KqFdRef* item = KqFdRefGet(obj->GetOsfd()); + if (item == NULL) + { MT_ATTR_API(320851, 1); // fd error MTLOG_ERROR("kqfd ref not find, failed, fd: %d", obj->GetOsfd()); kqueue_assert(0); return false; - } + } - int ret = obj->KqueueCtlAdd(item); - if (ret < 0) { + int ret = obj->KqueueCtlAdd(item); + if (ret < 0) { MTLOG_ERROR("kqueue ctrl callback failed, fd: %d, obj: %p", obj->GetOsfd(), obj); kqueue_assert(0); return false; - } + } - return true; + return true; } bool KqueueProxy::KqueueDelObj(KqueuerObj* obj) { - if (obj == NULL) - { + if (obj == NULL) + { MTLOG_ERROR("kqobj input invalid, %p", obj); return false; - } - KqFdRef* item = KqFdRefGet(obj->GetOsfd()); - if (item == NULL) - { + } + KqFdRef* item = KqFdRefGet(obj->GetOsfd()); + if (item == NULL) + { MT_ATTR_API(320851, 1); // fd error MTLOG_ERROR("kqfd ref not find, failed, fd: %d", obj->GetOsfd()); kqueue_assert(0); return false; - } + } - int ret = obj->KqueueCtlDel(item); - if (ret < 0) { + int ret = obj->KqueueCtlDel(item); + if (ret < 0) { MTLOG_ERROR("kqueue ctrl callback failed, fd: %d, obj: %p", obj->GetOsfd(), obj); kqueue_assert(0); return false; - } + } - return true; + return true; } void KqueueProxy::KqueueRcvEventList(int evtfdnum) { - int ret = 0; - int osfd = 0; - int revents = 0; - int tmp_evts = 0; - KqFdRef* item = NULL; - KqueuerObj* obj = NULL; + int ret = 0; + int osfd = 0; + int revents = 0; + int tmp_evts = 0; + KqFdRef* item = NULL; + KqueuerObj* obj = NULL; - for (int i = 0; i < evtfdnum; i++) - { - osfd = _evtlist[i].ident; + for (int i = 0; i < evtfdnum; i++) + { + osfd = _evtlist[i].ident; - item = KqFdRefGet(osfd); - if (item == NULL) - { + item = KqFdRefGet(osfd); + if (item == NULL) + { MT_ATTR_API(320851, 1); // fd error MTLOG_ERROR("kqfd ref not find, failed, fd: %d", osfd); kqueue_assert(0); continue; - } - tmp_evts = _evtlist[i].filter; - if (tmp_evts == EVFILT_READ) { - revents |= KQ_EVENT_READ; - } - if (tmp_evts == EVFILT_WRITE) { - revents |= KQ_EVENT_WRITE; - } - obj = item->GetNotifyObj(); - if (obj == NULL) - { + } + tmp_evts = _evtlist[i].filter; + if (tmp_evts == EVFILT_READ) { + revents |= KQ_EVENT_READ; + } + if (tmp_evts == EVFILT_WRITE) { + revents |= KQ_EVENT_WRITE; + } + obj = item->GetNotifyObj(); + if (obj == NULL) + { MTLOG_ERROR("fd notify obj null, failed, fd: %d", osfd); KqueueCtrlDel(osfd, (revents & (KQ_EVENT_READ | KQ_EVENT_WRITE))); continue; - } - obj->SetRcvEvents(revents); + } + obj->SetRcvEvents(revents); - if (tmp_evts == EV_ERROR) - { - obj->HangupNotify(); - continue; - } + if (tmp_evts == EV_ERROR) + { + obj->HangupNotify(); + continue; + } - if (revents & KQ_EVENT_READ) - { - ret = obj->InputNotify(); - if (ret != 0) - { - continue; - } - } + if (revents & KQ_EVENT_READ) + { + ret = obj->InputNotify(); + if (ret != 0) + { + continue; + } + } - if (revents & KQ_EVENT_WRITE) - { - ret = obj->OutputNotify(); - if (ret != 0) - { - continue; - } - } - } + if (revents & KQ_EVENT_WRITE) + { + ret = obj->OutputNotify(); + if (ret != 0) + { + continue; + } + } + } } void KqueueProxy::KqueueDispatch() { - int nfd; - int wait_time = KqueueGetTimeout(); - if (wait_time) { - struct timespec ts; - ts.tv_sec = wait_time / 1000; - ts.tv_nsec = 0; - nfd = ff_kevent(_kqfd, NULL, 0, _evtlist, _maxfd, &ts); - } else { - nfd = ff_kevent(_kqfd, NULL, 0, _evtlist, _maxfd, NULL); - } - if (nfd <= 0) - { - return; - } + int nfd; + int wait_time = KqueueGetTimeout(); + if (wait_time) { + struct timespec ts; + ts.tv_sec = wait_time / 1000; + ts.tv_nsec = 0; + nfd = ff_kevent(_kqfd, NULL, 0, _evtlist, _maxfd, &ts); + } else { + nfd = ff_kevent(_kqfd, NULL, 0, _evtlist, _maxfd, NULL); + } + if (nfd <= 0) + { + return; + } - KqueueRcvEventList(nfd); + KqueueRcvEventList(nfd); } int KqueuerObj::InputNotify() { - MicroThread* thread = this->GetOwnerThread(); - if (thread == NULL) - { - kqueue_assert(0); + MicroThread* thread = this->GetOwnerThread(); + if (thread == NULL) + { + kqueue_assert(0); MTLOG_ERROR("kqueue fd obj, no thread ptr, wrong"); return -1; - } + } - if (thread->HasFlag(MicroThread::IO_LIST)) - { + if (thread->HasFlag(MicroThread::IO_LIST)) + { MtFrame* frame = MtFrame::Instance(); frame->RemoveIoWait(thread); frame->InsertRunable(thread); - } + } - return 0; + return 0; } int KqueuerObj::OutputNotify() @@ -476,7 +476,7 @@ int KqueuerObj::OutputNotify() return -1; } - // 多个事件同时到达, 防重复操作 + // Multiple events arrive at the same time if (thread->HasFlag(MicroThread::IO_LIST)) { MtFrame* frame = MtFrame::Instance(); @@ -503,7 +503,7 @@ int KqueuerObj::KqueueCtlAdd(void* args) int osfd = this->GetOsfd(); int new_events = this->GetEvents(); - // 通知对象需要更新, FD通知对象理论上不会复用, 这里做冲突检查, 异常log记录 + // Notify object needs updating KqueuerObj* old_obj = fd_ref->GetNotifyObj(); if ((old_obj != NULL) && (old_obj != this)) { @@ -512,7 +512,6 @@ int KqueuerObj::KqueueCtlAdd(void* args) } fd_ref->SetNotifyObj(this); - // 调用框架的epoll ctl接口, 屏蔽epoll ctrl细节 if (!frame->KqueueCtrlAdd(osfd, new_events)) { MTLOG_ERROR("kqfd ref add failed, log"); @@ -532,7 +531,6 @@ int KqueuerObj::KqueueCtlDel(void* args) int osfd = this->GetOsfd(); int events = this->GetEvents(); - // 通知对象需要更新, FD通知对象理论上不会复用, 这里做冲突检查, 异常log记录 KqueuerObj* old_obj = fd_ref->GetNotifyObj(); if (old_obj != this) { @@ -541,8 +539,7 @@ int KqueuerObj::KqueueCtlDel(void* args) } fd_ref->SetNotifyObj(NULL); - // 调用框架的epoll ctl接口, 屏蔽epoll ctrl细节 - if (!frame->KqueueCtrlDelRef(osfd, events, false)) // 引用有风险, 弊大于利, 关闭掉 + if (!frame->KqueueCtrlDelRef(osfd, events, false)) { MTLOG_ERROR("kqfd ref del failed, log"); fd_ref->SetNotifyObj(old_obj); diff --git a/app/micro_thread/kqueue_proxy.h b/app/micro_thread/kqueue_proxy.h index 67e992a90..4e094f7ca 100644 --- a/app/micro_thread/kqueue_proxy.h +++ b/app/micro_thread/kqueue_proxy.h @@ -45,10 +45,6 @@ namespace NS_MICRO_THREAD { #define KQ_EVENT_READ 1 #define KQ_EVENT_WRITE 2 -/******************************************************************************/ -/* 操作系统头文件适配定义 */ -/******************************************************************************/ - /** * @brief add more detail for linux <sys/queue.h>, freebsd and University of California * @info queue.h version 8.3 (suse) diff version 8.5 (tlinux) @@ -90,119 +86,84 @@ do { \ /******************************************************************************/ -/* Kqueue proxy 定义与实现部分 */ +/* Kqueue proxy definition and implementation */ /******************************************************************************/ class KqueueProxy; class MicroThread; -/** - * @brief kqueue通知对象基类定义 - */ class KqueuerObj { - protected: - int _fd; - int _events; - int _revents; - int _type; - MicroThread* _thread; + protected: + int _fd; + int _events; + int _revents; + int _type; + MicroThread* _thread; - public: + public: - TAILQ_ENTRY(KqueuerObj) _entry; + TAILQ_ENTRY(KqueuerObj) _entry; - explicit KqueuerObj(int fd = -1) { - _fd = fd; - _events = 0; - _revents = 0; - _type = 0; - _thread = NULL; - }; - virtual ~KqueuerObj(){}; + explicit KqueuerObj(int fd = -1) { + _fd = fd; + _events = 0; + _revents = 0; + _type = 0; + _thread = NULL; + }; + virtual ~KqueuerObj(){}; - virtual int InputNotify(); - virtual int OutputNotify(); - virtual int HangupNotify(); - virtual int KqueueCtlAdd(void* args); - virtual int KqueueCtlDel(void* args); + virtual int InputNotify(); + virtual int OutputNotify(); + virtual int HangupNotify(); + virtual int KqueueCtlAdd(void* args); + virtual int KqueueCtlDel(void* args); - /** - * @brief fd打开可读事件侦听 - */ - void EnableInput() { _events |= KQ_EVENT_READ; }; + void EnableInput() { _events |= KQ_EVENT_READ; }; - /** - * @brief fd打开可写事件侦听 - */ - void EnableOutput() { _events |= KQ_EVENT_WRITE; }; + void EnableOutput() { _events |= KQ_EVENT_WRITE; }; - /** - * @brief fd关闭可读事件侦听 - */ - void DisableInput() { _events &= ~KQ_EVENT_READ; }; + void DisableInput() { _events &= ~KQ_EVENT_READ; }; - /** - * @brief fd关闭可写事件侦听 - */ - void DisableOutput() { _events &= ~KQ_EVENT_WRITE; }; + void DisableOutput() { _events &= ~KQ_EVENT_WRITE; }; - /** - * @brief 系统socket设置读取封装 - */ - int GetOsfd() { return _fd; }; - void SetOsfd(int fd) { _fd = fd; }; + int GetOsfd() { return _fd; }; + void SetOsfd(int fd) { _fd = fd; }; - /** - * @brief 监听事件与收到事件的访问方法 - */ - int GetEvents() { return _events; }; - void SetRcvEvents(int revents) { _revents = revents; }; - int GetRcvEvents() { return _revents; }; + int GetEvents() { return _events; }; + void SetRcvEvents(int revents) { _revents = revents; }; + int GetRcvEvents() { return _revents; }; - /** - * @brief 工厂管理方法, 获取真实类型 - */ - int GetNtfyType() { return _type; }; - virtual void Reset() { - _fd = -1; - _events = 0; - _revents = 0; - _type = 0; - _thread = NULL; - }; - - /** - * @brief 设置与获取所属的微线程句柄接口 - * @param thread 关联的线程指针 - */ - void SetOwnerThread(MicroThread* thread) { _thread = thread; }; - MicroThread* GetOwnerThread() { return _thread; }; + int GetNtfyType() { return _type; }; + virtual void Reset() { + _fd = -1; + _events = 0; + _revents = 0; + _type = 0; + _thread = NULL; + }; + + void SetOwnerThread(MicroThread* thread) { _thread = thread; }; + MicroThread* GetOwnerThread() { return _thread; }; }; -typedef TAILQ_HEAD(__KqFdList, KqueuerObj) KqObjList; ///< 高效的双链管理 -typedef struct kevent KqEvent; ///< 重定义一下kqueue event +typedef TAILQ_HEAD(__KqFdList, KqueuerObj) KqObjList; +typedef struct kevent KqEvent; -/** - * @brief EPOLL支持同一FD多个线程侦听, 建立一个引用计数数组, 元素定义 - * @info 引用计数弊大于利, 没有实际意义, 字段保留, 功能移除掉 20150623 - */ class KqFdRef { private: - int _wr_ref; ///< 监听写的引用计数 - int _rd_ref; ///< 监听读的引用计数 - int _events; ///< 当前正在侦听的事件列表 - int _revents; ///< 当前该fd收到的事件信息, 仅在epoll_wait后处理中有效 - KqueuerObj* _kqobj; ///< 单独注册调度器对象,一个fd关联一个对象 + int _wr_ref; + int _rd_ref; + int _events; + int _revents; + KqueuerObj* _kqobj; public: - /** - * @brief 构造与析构函数 - */ KqFdRef() { _wr_ref = 0; _rd_ref = 0; @@ -212,9 +173,6 @@ public: }; ~KqFdRef(){}; - /** - * @brief 监听事件获取与设置接口 - */ void SetListenEvents(int events) { _events = events; }; @@ -222,9 +180,6 @@ public: return _events; }; - /** - * @brief 监听对象获取与设置接口 - */ void SetNotifyObj(KqueuerObj* ntfy) { _kqobj = ntfy; }; @@ -232,9 +187,6 @@ public: return _kqobj; }; - /** - * @brief 监听引用计数的更新 - */ void AttachEvents(int event) { if (event & KQ_EVENT_READ) { _rd_ref++; @@ -260,9 +212,7 @@ public: } }; - /** - * @brief 获取引用计数 - */ + int ReadRefCnt() { return _rd_ref; }; int WriteRefCnt() { return _wr_ref; }; @@ -271,47 +221,47 @@ public: class KqueueProxy { - public: - static const int DEFAULT_MAX_FD_NUM = 100000; + public: + static const int DEFAULT_MAX_FD_NUM = 100000; - private: - int _kqfd; - int _maxfd; - KqEvent* _evtlist; - KqFdRef* _kqrefs; + private: + int _kqfd; + int _maxfd; + KqEvent* _evtlist; + KqFdRef* _kqrefs; - public: - KqueueProxy(); - virtual ~KqueueProxy(){}; + public: + KqueueProxy(); + virtual ~KqueueProxy(){}; - int InitKqueue(int max_num); - void TermKqueue(void); + int InitKqueue(int max_num); + void TermKqueue(void); - virtual int KqueueGetTimeout(void) { return 0; }; - virtual bool KqueueSchedule(KqObjList* fdlist, KqueuerObj* fd, int timeout) { return false; }; - - bool KqueueAdd(KqObjList& fdset); - bool KqueueDel(KqObjList& fdset); - void KqueueDispatch(void); - bool KqueueAddObj(KqueuerObj* obj); - bool KqueueDelObj(KqueuerObj* obj); - bool KqueueCtrlAdd(int fd, int new_events); - bool KqueueCtrlDel(int fd, int new_events); - bool KqueueCtrlDelRef(int fd, int new_events, bool use_ref); + virtual int KqueueGetTimeout(void) { return 0; }; + virtual bool KqueueSchedule(KqObjList* fdlist, KqueuerObj* fd, int timeout) { return false; }; + + bool KqueueAdd(KqObjList& fdset); + bool KqueueDel(KqObjList& fdset); + void KqueueDispatch(void); + bool KqueueAddObj(KqueuerObj* obj); + bool KqueueDelObj(KqueuerObj* obj); + bool KqueueCtrlAdd(int fd, int new_events); + bool KqueueCtrlDel(int fd, int new_events); + bool KqueueCtrlDelRef(int fd, int new_events, bool use_ref); - KqFdRef* KqFdRefGet(int fd) { - return ((fd >= _maxfd) || (fd < 0)) ? (KqFdRef*)NULL : &_kqrefs[fd]; - } + KqFdRef* KqFdRefGet(int fd) { + return ((fd >= _maxfd) || (fd < 0)) ? (KqFdRef*)NULL : &_kqrefs[fd]; + } - void KqueueNtfyReg(int fd, KqueuerObj* obj) { - KqFdRef* ref = KqFdRefGet(fd); - if (ref) { - ref->SetNotifyObj(obj); - } - }; + void KqueueNtfyReg(int fd, KqueuerObj* obj) { + KqFdRef* ref = KqFdRefGet(fd); + if (ref) { + ref->SetNotifyObj(obj); + } + }; - protected: - void KqueueRcvEventList(int evtfdnum); + protected: + void KqueueRcvEventList(int evtfdnum); }; } diff --git a/app/micro_thread/micro_thread.cpp b/app/micro_thread/micro_thread.cpp index b71bef77f..55f494eeb 100644 --- a/app/micro_thread/micro_thread.cpp +++ b/app/micro_thread/micro_thread.cpp @@ -35,29 +35,12 @@ using namespace NS_MICRO_THREAD; #define ASSERT(statement) //#define ASSERT(statement) assert(statement) -/** - * @brief ���ʵ�ֱ��������ĺ��� - * @param jbf jmpbuff����ָ�� - */ extern "C" int save_context(jmp_buf jbf); -/** - * @brief ���ʵ�ָֻ������ĺ��� - * @param jbf jmpbuff����ָ�� - * @param ret �лصķ���ֵ, Ĭ��1 - */ extern "C" void restore_context(jmp_buf jbf, int ret); -/** - * @brief ���ʵ���滻����ջ���� - * @param jbf jmpbuff����ָ�� - * @param esp ��ջָ�� - */ extern "C" void replace_esp(jmp_buf jbf, void* esp); -/** - * @brief ���캯��, Ĭ�ϲ���ջ��С - */ Thread::Thread(int stack_size) { _stack_size = stack_size ? stack_size : ThreadPool::default_stack_size; @@ -68,7 +51,7 @@ Thread::Thread(int stack_size) /** - * @brief LINUX x86/x86_64�µ�ջ����, �����ܹ�����Ҫע����� + * @brief LINUX x86/x86_64's allocated stacks. */ bool Thread::InitStack() { @@ -76,7 +59,7 @@ bool Thread::InitStack() return true; } - ///< ջ������ջ�ڴ����, ��Խ�� + ///< stack index and memory are separated to prevent out of bounds. _stack = (MtStack*)calloc(1, sizeof(MtStack)); if (NULL == _stack) { @@ -102,8 +85,8 @@ bool Thread::InitStack() _stack->_stk_size = _stack_size; _stack->_stk_bottom = _stack->_vaddr + MEM_PAGE_SIZE; _stack->_stk_top = _stack->_stk_bottom + _stack->_stk_size; - // valgrind support: register stack frame - _stack->valgrind_id = VALGRIND_STACK_REGISTER(_stack->_stk_bottom, _stack->_stk_top); + // valgrind support: register stack frame + _stack->valgrind_id = VALGRIND_STACK_REGISTER(_stack->_stk_bottom, _stack->_stk_top); _stack->_esp = _stack->_stk_top - STACK_PAD_SIZE; @@ -114,29 +97,23 @@ bool Thread::InitStack() } -/** - * @brief �ͷŶ�ջ��Ϣ - */ void Thread::FreeStack() { if (!_stack) { return; } munmap(_stack->_vaddr, _stack->_vaddr_size); - // valgrind support: deregister stack frame - VALGRIND_STACK_DEREGISTER(_stack->valgrind_id); + // valgrind support: deregister stack frame + VALGRIND_STACK_DEREGISTER(_stack->valgrind_id); free(_stack); _stack = NULL; } -/** - * @brief ��ʼ��������,���üĴ���,��ջ - */ void Thread::InitContext() { if (save_context(_jmpbuf) != 0) { - ScheduleObj::Instance()->ScheduleStartRun(); // ֱ�ӵ��� this->run? + ScheduleObj::Instance()->ScheduleStartRun(); } if (_stack != NULL) @@ -145,9 +122,6 @@ void Thread::InitContext() } } -/** - * @brief �����л�, ����״̬, �������� - */ void Thread::SwitchContext() { if (save_context(_jmpbuf) == 0) @@ -156,17 +130,12 @@ void Thread::SwitchContext() } } -/** - * @brief �ָ�������, �л��ضϵ�,�������� - */ void Thread::RestoreContext() { restore_context(_jmpbuf, 1); } -/** - * @brief ��ʼ���߳�,���ջ�������ij�ʼ�� - */ + bool Thread::Initial() { if (!InitStack()) @@ -180,18 +149,12 @@ bool Thread::Initial() return true; } -/** - * @brief ��ֹ�߳�,���ջ���������ͷ� - */ void Thread::Destroy() { FreeStack(); memset(&_jmpbuf, 0, sizeof(_jmpbuf)); } -/** - * @brief �߳�״̬����, �ɸ���״̬ - */ void Thread::Reset() { _wakeup_time = 0; @@ -201,10 +164,6 @@ void Thread::Reset() CleanState(); } -/** - * @brief �߳���������˯��, ��λ���� - * @param ms ˯�ߺ����� - */ void Thread::sleep(int ms) { utime64_t now = ScheduleObj::Instance()->ScheduleGetTime(); @@ -216,9 +175,6 @@ void Thread::sleep(int ms) } } -/** - * @brief ��������״̬, �ȴ��������߳̽��� - */ void Thread::Wait() { if (save_context(_jmpbuf) == 0) @@ -227,24 +183,17 @@ void Thread::Wait() } } -/** - * @brief ��ʼ��������,���üĴ���,��ջ - */ bool Thread::CheckStackHealth(char *esp) { - if (!_stack) - return false; + if (!_stack) + return false; - if (esp > _stack->_stk_bottom && esp < _stack->_stk_top) - return true; - else - return false; + if (esp > _stack->_stk_bottom && esp < _stack->_stk_top) + return true; + else + return false; } -/** - * @brief �̹߳���, Ĭ������ͨ�߳� - * @param type ����, Ĭ����ͨ - */ MicroThread::MicroThread(ThreadType type) { memset(&_entry, 0, sizeof(_entry)); @@ -258,9 +207,6 @@ MicroThread::MicroThread(ThreadType type) _parent = NULL; } -/** - * @breif �̸߳���״̬���� - */ void MicroThread::CleanState() { TAILQ_INIT(&_fdset); @@ -273,16 +219,12 @@ void MicroThread::CleanState() _parent = NULL; } -/** - * @brief �̵߳�ʵ�ʹ������� - */ void MicroThread::Run() { if (_start) { _start(_args); } - // �����߳�, �������߳̽��������̬ if (this->IsSubThread()) { this->WakeupParent(); } @@ -291,9 +233,6 @@ void MicroThread::Run() ScheduleObj::Instance()->ScheduleThread(); } -/** - * @brief �������̻߳��Ѹ��̴߳��� - */ void MicroThread::WakeupParent() { MicroThread* parent = this->GetParent(); @@ -311,17 +250,11 @@ void MicroThread::WakeupParent() } } -/** - * @brief �Ƿ��������Ķ������߳� - */ bool MicroThread::HasNoSubThread() { return TAILQ_EMPTY(&_sub_list); } -/** - * @brief ��ָ�����̼߳�������߳��б� - */ void MicroThread::AddSubThread(MicroThread* sub) { ASSERT(!sub->HasFlag(MicroThread::SUB_LIST)); @@ -334,9 +267,6 @@ void MicroThread::AddSubThread(MicroThread* sub) sub->SetFlag(MicroThread::SUB_LIST); } -/** - * @brief ��ָ���߳��Ƴ������߳��б� - */ void MicroThread::RemoveSubThread(MicroThread* sub) { ASSERT(sub->HasFlag(MicroThread::SUB_LIST)); @@ -349,11 +279,7 @@ void MicroThread::RemoveSubThread(MicroThread* sub) sub->UnsetFlag(MicroThread::SUB_LIST); } - -/** - * @brief ��������ʾ����� - */ -ScheduleObj *ScheduleObj::_instance = NULL; ///< ��̬�����ʼ�� +ScheduleObj *ScheduleObj::_instance = NULL; inline ScheduleObj* ScheduleObj::Instance() { if (NULL == _instance) @@ -364,18 +290,12 @@ inline ScheduleObj* ScheduleObj::Instance() return _instance; } -/** - * @brief ���������߳�������, �����ӿ� - */ void ScheduleObj::ScheduleThread() { MtFrame* frame = MtFrame::Instance(); frame->ThreadSchdule(); } -/** - * @brief ��ȡȫ�ֵ�ʱ���, ���뵥λ - */ utime64_t ScheduleObj::ScheduleGetTime() { MtFrame* frame = MtFrame::Instance(); @@ -390,9 +310,6 @@ utime64_t ScheduleObj::ScheduleGetTime() } } -/** - * @brief �̵߳�����������sleep״̬ - */ void ScheduleObj::ScheduleSleep() { MtFrame* frame = MtFrame::Instance(); @@ -406,9 +323,6 @@ void ScheduleObj::ScheduleSleep() frame->ThreadSchdule(); } -/** - * @brief �̵߳�����������pend״̬ - */ void ScheduleObj::SchedulePend() { MtFrame* frame = MtFrame::Instance(); @@ -422,9 +336,6 @@ void ScheduleObj::SchedulePend() frame->ThreadSchdule(); } -/** - * @brief �̵߳���ȡ��pend״̬, �ⲿ����ȡ�� - */ void ScheduleObj::ScheduleUnpend(void* pthread) { MtFrame* frame = MtFrame::Instance(); @@ -438,11 +349,6 @@ void ScheduleObj::ScheduleUnpend(void* pthread) frame->InsertRunable(thread); } - - -/** - * @brief �߳�ִ����Ϻ�, ���մ��� - */ void ScheduleObj::ScheduleReclaim() { MtFrame* frame = MtFrame::Instance(); @@ -455,9 +361,6 @@ void ScheduleObj::ScheduleReclaim() frame->FreeThread(thread); } -/** - * @brief ���������ȳ�ʼִ�� - */ void ScheduleObj::ScheduleStartRun() { MtFrame* frame = MtFrame::Instance(); @@ -471,15 +374,9 @@ void ScheduleObj::ScheduleStartRun() } -/** - * @brief �̳߳�ȫ�ֲ�����ʼ�� - */ -unsigned int ThreadPool::default_thread_num = DEFAULT_THREAD_NUM; ///< Ĭ��2000�̴߳��� -unsigned int ThreadPool::default_stack_size = DEFAULT_STACK_SIZE; ///< Ĭ��128Kջ��С +unsigned int ThreadPool::default_thread_num = DEFAULT_THREAD_NUM; ///< 2000 micro threads. +unsigned int ThreadPool::default_stack_size = DEFAULT_STACK_SIZE; ///< 128k stack. -/** - * @brief �̳߳س�ʼ�� - */ bool ThreadPool::InitialPool(int max_num) { MicroThread *thread = NULL; @@ -509,9 +406,6 @@ bool ThreadPool::InitialPool(int max_num) } } -/** - * @brief �̳߳ط���ʼ�� - */ void ThreadPool::DestroyPool() { MicroThread* thread = NULL; @@ -527,13 +421,9 @@ void ThreadPool::DestroyPool() _use_num = 0; } -/** - * @brief �̷߳���ӿ� - * @return �̶߳��� - */ MicroThread* ThreadPool::AllocThread() { - MT_ATTR_API_SET(492069, _total_num); // �̳߳ش�С + MT_ATTR_API_SET(492069, _total_num); MicroThread* thread = NULL; if (!_freelist.empty()) @@ -569,10 +459,6 @@ MicroThread* ThreadPool::AllocThread() return thread; } -/** - * @brief �߳��ͷŽӿ� - * @param thread �̶߳��� - */ void ThreadPool::FreeThread(MicroThread* thread) { ASSERT(!thread->HasFlag(MicroThread::FREE_LIST)); @@ -581,7 +467,6 @@ void ThreadPool::FreeThread(MicroThread* thread) _freelist.push(thread); thread->SetFlag(MicroThread::FREE_LIST); - ///< ���ж��� > default_thread_num, ���ͷ����ϵ�, �������ͷŵ�ǰ unsigned int free_num = _freelist.size(); if ((free_num > default_thread_num) && (free_num > 1)) { @@ -595,12 +480,9 @@ void ThreadPool::FreeThread(MicroThread* thread) int ThreadPool::GetUsedNum(void) { - return _use_num; + return _use_num; } -/** - * @brief �߳̿����, ȫ��ʵ����ȡ - */ MtFrame *MtFrame::_instance = NULL; inline MtFrame* MtFrame::Instance () { @@ -612,30 +494,20 @@ inline MtFrame* MtFrame::Instance () return _instance; } -/** - * @brief HOOKϵͳapi������ - */ void MtFrame::SetHookFlag() { mt_set_hook_flag(); }; - -/** - * @brief ��ܳ�ʼ��, Ĭ�ϲ�����־���� - */ bool MtFrame::InitFrame(LogAdapter* logadpt, int max_thread_num) { _log_adpt = logadpt; - // ��������������߳���Ŀ, ���Ե���epoll��ص�fd��Ŀ if ((this->InitKqueue(max_thread_num) < 0) || !this->InitialPool(max_thread_num)) { MTLOG_ERROR("Init epoll or thread pool failed"); this->Destroy(); return false; } - - // �������öѴ�С, �Ŵ�Ѹ���Ϊ2�� if (_sleeplist.HeapResize(max_thread_num * 2) < 0) { MTLOG_ERROR("Init heap list failed"); @@ -643,7 +515,6 @@ bool MtFrame::InitFrame(LogAdapter* logadpt, int max_thread_num) return false; } - // ��ʱ��������ʼ��, �Ŵ�Ѹ���Ϊ2�� _timer = new CTimerMng(max_thread_num * 2); if (NULL == _timer) { @@ -652,7 +523,6 @@ bool MtFrame::InitFrame(LogAdapter* logadpt, int max_thread_num) return false; } - // �ػ��̵߳�����ʼ�� _daemon = AllocThread(); if (NULL == _daemon) { @@ -664,7 +534,6 @@ bool MtFrame::InitFrame(LogAdapter* logadpt, int max_thread_num) _daemon->SetState(MicroThread::RUNABLE); _daemon->SetSartFunc(MtFrame::DaemonRun, this); - // �����߳�, ����INIT, ����ʼ��ջ, Ҳ�ص�ע��, ����Ҫͳһ���� _primo = new MicroThread(MicroThread::PRIMORDIAL); if (NULL == _primo) { @@ -675,20 +544,16 @@ bool MtFrame::InitFrame(LogAdapter* logadpt, int max_thread_num) _primo->SetState(MicroThread::RUNNING); SetActiveThread(_primo); - // ��������ʱ��� _last_clock = GetSystemMS(); TAILQ_INIT(&_iolist); TAILQ_INIT(&_pend_list); - //SetHookFlag(); + //SetHookFlag(); return true; } -/** - * @brief ��ܷ���ʼ�� - */ void MtFrame::Destroy(void) { if (NULL == _instance ) @@ -741,20 +606,11 @@ void MtFrame::Destroy(void) _instance = NULL; } -/** - * @brief �߳̿�ܰ汾��ȡ - */ char* MtFrame::Version() { return IMT_VERSION; } -/** - * @brief �̴߳����ӿ� - * @param entry �߳���ں��� - * @param args �߳���ڲ��� - * @return �߳�ָ��, NULL��ʾʧ�� - */ MicroThread* MtFrame::CreateThread(ThreadStart entry, void *args, bool runable) { MtFrame* mtframe = MtFrame::Instance(); @@ -778,39 +634,32 @@ int MtFrame::Loop(void* args) MtFrame* mtframe = MtFrame::Instance(); MicroThread* daemon = mtframe->DaemonThread(); - mtframe->KqueueDispatch(); - mtframe->SetLastClock(mtframe->GetSystemMS()); - mtframe->WakeupTimeout(); - mtframe->CheckExpired(); - daemon->SwitchContext(); + mtframe->KqueueDispatch(); + mtframe->SetLastClock(mtframe->GetSystemMS()); + mtframe->WakeupTimeout(); + mtframe->CheckExpired(); + daemon->SwitchContext(); - return 0; + return 0; } -/** - * @brief �ػ��߳���ں���, ����ָ��Ҫ��static���� - * @param args �߳���ڲ��� - */ void MtFrame::DaemonRun(void* args) { - /* + /* MtFrame* mtframe = MtFrame::Instance(); MicroThread* daemon = mtframe->DaemonThread(); - while (true) { - mtframe->KqueueDispatch(); - mtframe->SetLastClock(mtframe->GetSystemMS()); - mtframe->WakeupTimeout(); - mtframe->CheckExpired(); - daemon->SwitchContext(); - } - */ - ff_run(MtFrame::Loop, NULL); + while (true) { + mtframe->KqueueDispatch(); + mtframe->SetLastClock(mtframe->GetSystemMS()); + mtframe->WakeupTimeout(); + mtframe->CheckExpired(); + daemon->SwitchContext(); + } + */ + ff_run(MtFrame::Loop, NULL); } -/** - * @brief ��ȡ��ǰ�̵߳ĸ��߳� - */ MicroThread *MtFrame::GetRootThread() { if (NULL == _curr_thread) @@ -837,9 +686,6 @@ MicroThread *MtFrame::GetRootThread() return parent; } -/** - * @brief ��ܵ����߳����� - */ void MtFrame::ThreadSchdule() { MicroThread* thread = NULL; @@ -860,9 +706,6 @@ void MtFrame::ThreadSchdule() thread->RestoreContext(); } -/** - * @brief ��ܴ�����ʱ�ص����� - */ void MtFrame::CheckExpired() { static utime64_t check_time = 0; @@ -881,9 +724,6 @@ void MtFrame::CheckExpired() } } -/** - * @brief ��ܼ���ʱ, �������еij�ʱ�߳� - */ void MtFrame::WakeupTimeout() { utime64_t now = GetLastClock(); @@ -891,7 +731,7 @@ void MtFrame::WakeupTimeout() while (thread && (thread->GetWakeupTime() <= now)) { if (thread->HasFlag(MicroThread::IO_LIST)) - { + { RemoveIoWait(thread); } else @@ -905,16 +745,13 @@ void MtFrame::WakeupTimeout() } } -/** - * @brief ��ܵ���epoll waitǰ, �ж��ȴ�ʱ����Ϣ - */ int MtFrame::KqueueGetTimeout() { utime64_t now = GetLastClock(); MicroThread* thread = dynamic_cast<MicroThread*>(_sleeplist.HeapTop()); if (!thread) { - return 10; //Ĭ��10ms epollwait + return 10; //default 10ms epollwait } else if (thread->GetWakeupTime() < now) { @@ -926,10 +763,6 @@ int MtFrame::KqueueGetTimeout() } } -/** - * @brief ��ܹ����̵߳�Ԫ, ��������� - * @param thread �̶߳��� - */ inline void MtFrame::InsertSleep(MicroThread* thread) { ASSERT(!thread->HasFlag(MicroThread::SLEEP_LIST)); @@ -944,10 +777,6 @@ inline void MtFrame::InsertSleep(MicroThread* thread) } } -/** - * @brief ��ܹ����̵߳�Ԫ, �Ƴ������ - * @param thread �̶߳��� - */ inline void MtFrame::RemoveSleep(MicroThread* thread) { ASSERT(thread->HasFlag(MicroThread::SLEEP_LIST)); @@ -961,10 +790,6 @@ inline void MtFrame::RemoveSleep(MicroThread* thread) } } -/** - * @brief ��ܹ����̵߳�Ԫ, ִ��IO�ȴ�״̬ - * @param thread �̶߳��� - */ inline void MtFrame::InsertIoWait(MicroThread* thread) { ASSERT(!thread->HasFlag(MicroThread::IO_LIST)); @@ -973,10 +798,6 @@ inline void MtFrame::InsertIoWait(MicroThread* thread) InsertSleep(thread); } -/** - * @brief ��ܹ����̵߳�Ԫ, �Ƴ�IO�ȴ�״̬ - * @param thread �̶߳��� - */ void MtFrame::RemoveIoWait(MicroThread* thread) { ASSERT(thread->HasFlag(MicroThread::IO_LIST)); @@ -986,10 +807,6 @@ void MtFrame::RemoveIoWait(MicroThread* thread) RemoveSleep(thread); } -/** - * @brief ��ܹ����̵߳�Ԫ, ��������ж��� - * @param thread �̶߳��� - */ void MtFrame::InsertRunable(MicroThread* thread) { ASSERT(!thread->HasFlag(MicroThread::RUN_LIST)); @@ -1000,10 +817,6 @@ void MtFrame::InsertRunable(MicroThread* thread) _waitnum++; } -/** - * @brief ��ܹ����̵߳�Ԫ, �Ƴ������ж��� - * @param thread �̶߳��� - */ inline void MtFrame::RemoveRunable(MicroThread* thread) { ASSERT(thread->HasFlag(MicroThread::RUN_LIST)); @@ -1014,11 +827,6 @@ inline void MtFrame::RemoveRunable(MicroThread* thread) _waitnum--; } - -/** - * @brief ��ܹ����̵߳�Ԫ, ִ��pend�ȴ�״̬ - * @param thread �̶߳��� - */ void MtFrame::InsertPend(MicroThread* thread) { ASSERT(!thread->HasFlag(MicroThread::PEND_LIST)); @@ -1027,10 +835,6 @@ void MtFrame::InsertPend(MicroThread* thread) thread->SetState(MicroThread::PENDING); } -/** - * @brief ��ܹ����̵߳�Ԫ, �Ƴ�PEND�ȴ�״̬ - * @param thread �̶߳��� - */ void MtFrame::RemovePend(MicroThread* thread) { ASSERT(thread->HasFlag(MicroThread::PEND_LIST)); @@ -1038,10 +842,6 @@ void MtFrame::RemovePend(MicroThread* thread) TAILQ_REMOVE(&_pend_list, thread, _entry); } -/** - * @brief �߳������л�, �ȴ������̵߳Ļ��� - * @param timeout ��ȴ�ʱ��, ���� - */ void MtFrame::WaitNotify(utime64_t timeout) { MicroThread* thread = GetActiveThread(); @@ -1051,13 +851,6 @@ void MtFrame::WaitNotify(utime64_t timeout) thread->SwitchContext(); } -/** - * @brief �̴߳����л�����,���óɹ� ���ó�cpu - * @param fdlist ��·������socket�б� - * @param fd ���������fd��Ϣ - * @param timeout ��ȴ�ʱ��, ���� - * @return true �ɹ�, false ʧ�� - */ bool MtFrame::KqueueSchedule(KqObjList* fdlist, KqueuerObj* fd, int timeout) { MicroThread* thread = GetActiveThread(); @@ -1067,7 +860,6 @@ bool MtFrame::KqueueSchedule(KqObjList* fdlist, KqueuerObj* fd, int timeout) return false; } - // 1. ���ϸ��߳���Ҫ���ĵ�epoll���ȶ��� thread->ClearAllFd(); if (fdlist) { @@ -1078,7 +870,6 @@ bool MtFrame::KqueueSchedule(KqObjList* fdlist, KqueuerObj* fd, int timeout) thread->AddFd(fd); } - // 2. ����epoll�����¼�, ������ʱʱ��, �л�IO�ȴ�״̬, �����л� thread->SetWakeupTime(timeout + this->GetLastClock()); if (!this->KqueueAdd(thread->GetFdSet())) { @@ -1088,7 +879,6 @@ bool MtFrame::KqueueSchedule(KqObjList* fdlist, KqueuerObj* fd, int timeout) this->InsertIoWait(thread); thread->SwitchContext(); - // 3. ����OK, �ж���ʱ, epoll ctrl ��ԭ״̬ int rcvnum = 0; KqObjList& rcvfds = thread->GetFdSet(); KqueuerObj* fdata = NULL; @@ -1099,9 +889,9 @@ bool MtFrame::KqueueSchedule(KqObjList* fdlist, KqueuerObj* fd, int timeout) rcvnum++; } } - this->KqueueDel(rcvfds); // ��һ��������ADD, DEL �ջ����� + this->KqueueDel(rcvfds); - if (rcvnum == 0) // ��ʱ����, ���ش��� + if (rcvnum == 0) { errno = ETIME; return false; @@ -1110,16 +900,6 @@ bool MtFrame::KqueueSchedule(KqObjList* fdlist, KqueuerObj* fd, int timeout) return true; } -/** - * @brief �̰߳�����ϵͳIO���� recvfrom - * @param fd ϵͳsocket��Ϣ - * @param buf ������Ϣ������ָ�� - * @param len ������Ϣ���������� - * @param from ��Դ��ַ��ָ�� - * @param fromlen ��Դ��ַ�Ľṹ���� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ճ���, <0 ʧ�� - */ int MtFrame::recvfrom(int fd, void *buf, int len, int flags, struct sockaddr *from, socklen_t *fromlen, int timeout) { MtFrame* mtframe = MtFrame::Instance(); @@ -1129,9 +909,9 @@ int MtFrame::recvfrom(int fd, void *buf, int len, int flags, struct sockaddr *fr if(fd<0 || !buf || len<1) { - errno = EINVAL; - MTLOG_ERROR("recvfrom failed, errno: %d (%m)", errno); - return -10; + errno = EINVAL; + MTLOG_ERROR("recvfrom failed, errno: %d (%m)", errno); + return -10; } if (timeout <= -1) @@ -1180,16 +960,6 @@ int MtFrame::recvfrom(int fd, void *buf, int len, int flags, struct sockaddr *fr } -/** - * @brief �̰߳�����ϵͳIO���� sendto - * @param fd ϵͳsocket��Ϣ - * @param msg �����͵���Ϣָ�� - * @param len �����͵���Ϣ���� - * @param to Ŀ�ĵ�ַ��ָ�� - * @param tolen Ŀ�ĵ�ַ�Ľṹ���� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ͳ���, <0 ʧ�� - */ int MtFrame::sendto(int fd, const void *msg, int len, int flags, const struct sockaddr *to, int tolen, int timeout) { MtFrame* mtframe = MtFrame::Instance(); @@ -1199,9 +969,9 @@ int MtFrame::sendto(int fd, const void *msg, int len, int flags, const struct so if(fd<0 || !msg || len<1) { - errno = EINVAL; - MTLOG_ERROR("sendto failed, errno: %d (%m)", errno); - return -10; + errno = EINVAL; + MTLOG_ERROR("sendto failed, errno: %d (%m)", errno); + return -10; } int n = 0; @@ -1236,14 +1006,6 @@ int MtFrame::sendto(int fd, const void *msg, int len, int flags, const struct so return n; } -/** - * @brief �̰߳�����ϵͳIO���� connect - * @param fd ϵͳsocket��Ϣ - * @param addr ָ��server��Ŀ�ĵ�ַ - * @param addrlen ��ַ�ij��� - * @param timeout ��ȴ�ʱ��, ���� - * @return =0 ���ӳɹ�, <0 ʧ�� - */ int MtFrame::connect(int fd, const struct sockaddr *addr, int addrlen, int timeout) { MtFrame* mtframe = MtFrame::Instance(); @@ -1253,9 +1015,9 @@ int MtFrame::connect(int fd, const struct sockaddr *addr, int addrlen, int timeo if(fd<0 || !addr || addrlen<1) { - errno = EINVAL; - MTLOG_ERROR("connect failed, errno: %d (%m)", errno); - return -10; + errno = EINVAL; + MTLOG_ERROR("connect failed, errno: %d (%m)", errno); + return -10; } int n = 0; @@ -1269,7 +1031,7 @@ int MtFrame::connect(int fd, const struct sockaddr *addr, int addrlen, int timeo return -1; } - if (errno == EISCONN) // ������, ���سɹ� + if (errno == EISCONN) { return 0; } @@ -1295,14 +1057,6 @@ int MtFrame::connect(int fd, const struct sockaddr *addr, int addrlen, int timeo return n; } -/** - * @brief �̰߳�����ϵͳIO���� accept - * @param fd �������� - * @param addr �ͻ��˵�ַ - * @param addrlen ��ַ�ij��� - * @param timeout ��ȴ�ʱ��, ���� - * @return >=0 accept��socket������, <0 ʧ�� - */ int MtFrame::accept(int fd, struct sockaddr *addr, socklen_t *addrlen, int timeout) { MtFrame* mtframe = MtFrame::Instance(); @@ -1312,9 +1066,9 @@ int MtFrame::accept(int fd, struct sockaddr *addr, socklen_t *addrlen, int timeo if(fd<0) { - errno = EINVAL; - MTLOG_ERROR("accept failed, errno: %d (%m)", errno); - return -10; + errno = EINVAL; + MTLOG_ERROR("accept failed, errno: %d (%m)", errno); + return -10; } int acceptfd = 0; @@ -1349,15 +1103,6 @@ int MtFrame::accept(int fd, struct sockaddr *addr, socklen_t *addrlen, int timeo return acceptfd; } - -/** - * @brief �̰߳�����ϵͳIO���� read - * @param fd ϵͳsocket��Ϣ - * @param buf ������Ϣ������ָ�� - * @param nbyte ������Ϣ���������� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ճ���, <0 ʧ�� - */ ssize_t MtFrame::read(int fd, void *buf, size_t nbyte, int timeout) { MtFrame* mtframe = MtFrame::Instance(); @@ -1367,9 +1112,9 @@ ssize_t MtFrame::read(int fd, void *buf, size_t nbyte, int timeout) if(fd<0 || !buf || nbyte<1) { - errno = EINVAL; - MTLOG_ERROR("read failed, errno: %d (%m)", errno); - return -10; + errno = EINVAL; + MTLOG_ERROR("read failed, errno: %d (%m)", errno); + return -10; } ssize_t n = 0; @@ -1404,14 +1149,6 @@ ssize_t MtFrame::read(int fd, void *buf, size_t nbyte, int timeout) return n; } -/** - * @brief �̰߳�����ϵͳIO���� write - * @param fd ϵͳsocket��Ϣ - * @param buf �����͵���Ϣָ�� - * @param nbyte �����͵���Ϣ���� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ͳ���, <0 ʧ�� - */ ssize_t MtFrame::write(int fd, const void *buf, size_t nbyte, int timeout) { MtFrame* mtframe = MtFrame::Instance(); @@ -1421,9 +1158,9 @@ ssize_t MtFrame::write(int fd, const void *buf, size_t nbyte, int timeout) if(fd<0 || !buf || nbyte<1) { - errno = EINVAL; - MTLOG_ERROR("write failed, errno: %d (%m)", errno); - return -10; + errno = EINVAL; + MTLOG_ERROR("write failed, errno: %d (%m)", errno); + return -10; } ssize_t n = 0; @@ -1470,15 +1207,6 @@ ssize_t MtFrame::write(int fd, const void *buf, size_t nbyte, int timeout) return nbyte; } - -/** - * @brief �̰߳�����ϵͳIO���� recv - * @param fd ϵͳsocket��Ϣ - * @param buf ������Ϣ������ָ�� - * @param len ������Ϣ���������� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ճ���, <0 ʧ�� - */ int MtFrame::recv(int fd, void *buf, int len, int flags, int timeout) { MtFrame* mtframe = MtFrame::Instance(); @@ -1488,9 +1216,9 @@ int MtFrame::recv(int fd, void *buf, int len, int flags, int timeout) if(fd<0 || !buf || len<1) { - errno = EINVAL; - MTLOG_ERROR("recv failed, errno: %d (%m)", errno); - return -10; + errno = EINVAL; + MTLOG_ERROR("recv failed, errno: %d (%m)", errno); + return -10; } if (timeout <= -1) @@ -1539,14 +1267,6 @@ int MtFrame::recv(int fd, void *buf, int len, int flags, int timeout) } -/** - * @brief �̰߳�����ϵͳIO���� send - * @param fd ϵͳsocket��Ϣ - * @param buf �����͵���Ϣָ�� - * @param nbyte �����͵���Ϣ���� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ͳ���, <0 ʧ�� - */ ssize_t MtFrame::send(int fd, const void *buf, size_t nbyte, int flags, int timeout) { MtFrame* mtframe = MtFrame::Instance(); @@ -1556,9 +1276,9 @@ ssize_t MtFrame::send(int fd, const void *buf, size_t nbyte, int flags, int time if(fd<0 || !buf || nbyte<1) { - errno = EINVAL; - MTLOG_ERROR("send failed, errno: %d (%m)", errno); - return -10; + errno = EINVAL; + MTLOG_ERROR("send failed, errno: %d (%m)", errno); + return -10; } ssize_t n = 0; @@ -1605,11 +1325,6 @@ ssize_t MtFrame::send(int fd, const void *buf, size_t nbyte, int flags, int time return nbyte; } - - -/** - * @brief �߳�����sleep�ӿ�, ��λms - */ void MtFrame::sleep(int ms) { MtFrame* frame = MtFrame::Instance(); @@ -1620,13 +1335,6 @@ void MtFrame::sleep(int ms) } } -/** - * @brief �̰߳�����ϵͳIO���� recv - * @param fd ϵͳsocket��Ϣ - * @param events �¼����� EPOLLIN or EPOLLOUT - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ճ���, <0 ʧ�� - */ int MtFrame::WaitEvents(int fd, int events, int timeout) { MtFrame* mtframe = MtFrame::Instance(); @@ -1669,5 +1377,3 @@ int MtFrame::WaitEvents(int fd, int events, int timeout) return epfd.GetRcvEvents(); } } - - diff --git a/app/micro_thread/micro_thread.h b/app/micro_thread/micro_thread.h index b0a5fd3c8..e91bdb114 100644 --- a/app/micro_thread/micro_thread.h +++ b/app/micro_thread/micro_thread.h @@ -54,269 +54,159 @@ using std::queue; namespace NS_MICRO_THREAD { -#define STACK_PAD_SIZE 128 ///< ջ���¸�������Ĵ�С -#define MEM_PAGE_SIZE 4096 ///< �ڴ�ҳĬ�ϴ�С -#define DEFAULT_STACK_SIZE 128*1024 ///< Ĭ��ջ��С128K -#define DEFAULT_THREAD_NUM 2000 ///< Ĭ��2000����ʼ�߳� +#define STACK_PAD_SIZE 128 +#define MEM_PAGE_SIZE 4096 +#define DEFAULT_STACK_SIZE 128*1024 +#define DEFAULT_THREAD_NUM 2000 -typedef unsigned long long utime64_t; ///< 64λ��ʱ�䶨�� -typedef void (*ThreadStart)(void*); ///< �߳���ں������� +typedef unsigned long long utime64_t; +typedef void (*ThreadStart)(void*); -/** - * @brief �̵߳��ȵ����������, �������С�ӿڷ�װ - */ class ScheduleObj { public: - /** - * @brief ��������ʾ����� - */ static ScheduleObj* Instance (void); - /** - * @brief ��ȡȫ�ֵ�ʱ���, ���뵥λ - */ utime64_t ScheduleGetTime(void); - /** - * @brief ���������߳������� - */ void ScheduleThread(void); - /** - * @brief �̵߳�����������sleep״̬ - */ void ScheduleSleep(void); - /** - * @brief �̵߳�����������pend״̬ - */ void SchedulePend(void); - /** - * @brief �̵߳���ȡ��pend״̬, �ⲿ����ȡ�� - */ void ScheduleUnpend(void* thread); - /** - * @brief �߳�ִ����Ϻ�, ���մ��� - */ void ScheduleReclaim(void); - /** - * @brief ���������ȳ�ʼִ�� - */ void ScheduleStartRun(void); private: - static ScheduleObj* _instance; // ˽�о�� + static ScheduleObj* _instance; }; - -/** - * @brief �߳�ͨ�õ�ջ֡�ṹ���� - */ struct MtStack { - int _stk_size; ///< ջ�Ĵ�С, ��Чʹ�ÿռ� - int _vaddr_size; ///< �����buff�ܴ�С - char *_vaddr; ///< ������ڴ����ַ - void *_esp; ///< ջ��esp�Ĵ��� - char *_stk_bottom; ///< ջ��͵ĵ�ַ�ռ� - char *_stk_top; ///< ջ��ߵĵ�ַ�ռ� - void *_private; ///< �߳�˽������ - int valgrind_id; ///< valgrind id + int _stk_size; + int _vaddr_size; + char *_vaddr; + void *_esp; + char *_stk_bottom; + char *_stk_top; + void *_private; + int valgrind_id; }; - -/** - * @brief ͨ�õ��߳�ģ�Ͷ��� - */ class Thread : public HeapEntry { public: - /** - * @brief �������������� - */ explicit Thread(int stack_size = 0); virtual ~Thread(){}; - /** - * @brief �̵߳�ʵ�ʹ������� - */ virtual void Run(void){}; - /** - * @brief ��ʼ���߳�,���ջ�������ij�ʼ�� - */ bool Initial(void); - /** - * @brief ��ֹ�߳�,���ջ���������ͷ� - */ void Destroy(void); - /** - * @brief �߳�״̬����, �ɸ���״̬ - */ void Reset(void); - /** - * @brief �߳���������˯��, ��λ���� - * @param ms ˯�ߺ����� - */ void sleep(int ms); - /** - * @brief �߳���������ȴ�, �ö����߳������� - */ void Wait(); - /** - * @brief �����л�, ����״̬, �������� - */ void SwitchContext(void); - /** - * @brief �ָ�������, �л����� - */ void RestoreContext(void); - /** - * @brief ��ȡ�����ʱ�� - * @return �̵߳Ļ���ʱ��� - */ utime64_t GetWakeupTime(void) { return _wakeup_time; }; - /** - * @brief ���������ʱ�� - * @param waketime �̵߳Ļ���ʱ��� - */ void SetWakeupTime(utime64_t waketime) { _wakeup_time = waketime; }; - /** - * @brief �����߳�˽������ - * @param data �߳�˽������ָ�룬ʹ�������Լ������ڴ棬����ֻ����ָ�� - */ void SetPrivate(void *data) { _stack->_private = data; } - /** - * @brief ��ȡ�߳�˽������ - */ void* GetPrivate() { return _stack->_private; } - /** - * @brief ��ʼ��������,���üĴ���,��ջ - */ bool CheckStackHealth(char *esp); protected: - /** - * @brief �����̴߳���״̬, ������ - */ virtual void CleanState(void){}; - /** - * @brief ��ʼ����ջ��Ϣ - */ virtual bool InitStack(void); - /** - * @brief �ͷŶ�ջ��Ϣ - */ virtual void FreeStack(void); - /** - * @brief ��ʼ��������,���üĴ���,��ջ - */ virtual void InitContext(void); private: - MtStack* _stack; ///< ˽��ջָ�� - jmp_buf _jmpbuf; ///< ������jmpbuff - int _stack_size; ///< ջ��С�ֶ� - utime64_t _wakeup_time; ///< ˯����ʱ�� + MtStack* _stack; + jmp_buf _jmpbuf; + int _stack_size; + utime64_t _wakeup_time; }; - -/** - * @brief �߳����ݽṹ���� - */ class MicroThread : public Thread { public: enum ThreadType { - NORMAL = 0, ///< Ĭ����ͨ�߳�, û�ж�̬�����ջ��Ϣ - PRIMORDIAL = 1, ///< ԭ���߳�, main�������� - DAEMON = 2, ///< �ػ��߳�, �ײ�IO EPOLL��������ȴ��� - SUB_THREAD = 3, ///< �����߳�, ��ִ�м��� + NORMAL = 0, ///< normal thread, no dynamic allocated stack infomations. + PRIMORDIAL = 1, ///< primordial thread, created when frame initialized. + DAEMON = 2, ///< daemon thread, IO event management and scheduling trigger. + SUB_THREAD = 3, ///< sub thread, run simple task. }; enum ThreadFlag { - NOT_INLIST = 0x0, ///< ����״̬ - FREE_LIST = 0x1, ///< ���ж����� - IO_LIST = 0x2, ///< IO�ȴ������� - SLEEP_LIST = 0x4, ///< ����SLEEP�� - RUN_LIST = 0x8, ///< �����ж����� - PEND_LIST = 0x10, ///< ���������� - SUB_LIST = 0x20, ///< �����̶߳����� + NOT_INLIST = 0x0, + FREE_LIST = 0x1, + IO_LIST = 0x2, + SLEEP_LIST = 0x4, + RUN_LIST = 0x8, + PEND_LIST = 0x10, + SUB_LIST = 0x20, }; enum ThreadState { - INITIAL = 0, ///< ��ʼ��״̬ - RUNABLE = 1, ///< ������״̬ - RUNNING = 2, ///< ���������� - SLEEPING = 3, ///< IO�ȴ���SLEEP�� - PENDING = 4, ///< ����״̬��, �ȴ����߳�OK�� + INITIAL = 0, + RUNABLE = 1, + RUNNING = 2, + SLEEPING = 3, + PENDING = 4, }; - typedef TAILQ_ENTRY(MicroThread) ThreadLink; ///< �߳����� - typedef TAILQ_HEAD(__ThreadSubTailq, MicroThread) SubThreadList; ///< �̶߳��ж��� + typedef TAILQ_ENTRY(MicroThread) ThreadLink; + typedef TAILQ_HEAD(__ThreadSubTailq, MicroThread) SubThreadList; public: - /** - * @brief �̹߳��������� - */ MicroThread(ThreadType type = NORMAL); ~MicroThread(){}; - ThreadLink _entry; ///< ״̬������� - ThreadLink _sub_entry; ///< ���̶߳������ + ThreadLink _entry; + ThreadLink _sub_entry; - /** - * @brief �̶߳�������ʵ��,������ʱ����絽������ - * @return �̵߳�ʵ�ʻ���ʱ�� - */ virtual utime64_t HeapValue() { return GetWakeupTime(); }; - /** - * @brief �̵߳�ʵ�ʹ������� - */ virtual void Run(void); - - /** - * @breif fd�����������в��� - */ + void ClearAllFd(void) { TAILQ_INIT(&_fdset); }; @@ -330,9 +220,6 @@ public: return _fdset; }; - /** - * @breif �߳����������� - */ void SetType(ThreadType type) { _type = type; }; @@ -340,9 +227,6 @@ public: return _type; }; - /** - * @breif �߳����ͼ��ӿ� - */ bool IsDaemon(void) { return (DAEMON == _type); }; @@ -353,9 +237,6 @@ public: return (SUB_THREAD == _type); }; - /** - * @brief ���߳���������� - */ void SetParent(MicroThread* parent) { _parent = parent; }; @@ -364,16 +245,10 @@ public: }; void WakeupParent(); - /** - * @brief ���̵߳Ĺ��� - */ void AddSubThread(MicroThread* sub); void RemoveSubThread(MicroThread* sub); bool HasNoSubThread(); - /** - * @brief �߳�����״̬���� - */ void SetState(ThreadState state) { _state = state; }; @@ -381,25 +256,19 @@ public: return _state; } - /** - * @breif �̱߳��λ���� - */ void SetFlag(ThreadFlag flag) { - _flag = (ThreadFlag)(_flag | flag); + _flag = (ThreadFlag)(_flag | flag); }; void UnsetFlag(ThreadFlag flag) { _flag = (ThreadFlag)(_flag & ~flag); }; bool HasFlag(ThreadFlag flag) { - return _flag & flag; + return _flag & flag; }; ThreadFlag GetFlag() { return _flag; }; - /** - * @breif �߳���ں�������ע�� - */ void SetSartFunc(ThreadStart func, void* args) { _start = func; _args = args; @@ -411,464 +280,217 @@ public: protected: - /** - * @breif �̸߳���״̬���� - */ virtual void CleanState(void); private: - ThreadState _state; ///< �̵߳�ǰ״̬ - ThreadType _type; ///< �߳����� - ThreadFlag _flag; ///< �̱߳��λ - KqObjList _fdset; ///< �̹߳�ע��socket�б� - SubThreadList _sub_list; ///< �����̵߳Ķ��� - MicroThread* _parent; ///< �����̵߳ĸ��߳� - ThreadStart _start; ///< �߳�ע�ắ�� - void* _args; ///< �߳�ע����� + ThreadState _state; + ThreadType _type; + ThreadFlag _flag; + KqObjList _fdset; + SubThreadList _sub_list; + MicroThread* _parent; + ThreadStart _start; + void* _args; }; -typedef std::set<MicroThread*> ThreadSet; ///< �߳�set�����ṹ -typedef std::queue<MicroThread*> ThreadList; ///< �߳�queue�����ṹ +typedef std::set<MicroThread*> ThreadSet; +typedef std::queue<MicroThread*> ThreadList; -/** - * @brief �߳���־�ӿ�, �ײ��, ��־�ɵ�����ע�� - */ class LogAdapter { public: - /** - * @brief ��־���������� - */ LogAdapter(){}; virtual ~LogAdapter(){}; - /** - * @brief ��־���Ȱ��ȼ�����, ���ٽ��������Ŀ��� - * @return true ���Դ�ӡ�ü���, false ��������ӡ�ü��� - */ virtual bool CheckDebug(){ return true;}; virtual bool CheckTrace(){ return true;}; virtual bool CheckError(){ return true;}; - /** - * @brief ��־�ּ���¼�ӿ� - */ virtual void LogDebug(char* fmt, ...){}; virtual void LogTrace(char* fmt, ...){}; virtual void LogError(char* fmt, ...){}; - /** - * @brief �����ϱ��ӿ� - */ virtual void AttrReportAdd(int attr, int iValue){}; virtual void AttrReportSet(int attr, int iValue){}; }; -/** - * @brief �̳߳ؼ�ʵ�� - */ class ThreadPool { public: - static unsigned int default_thread_num; ///< Ĭ��2000�̴߳��� - static unsigned int default_stack_size; ///< Ĭ��128Kջ��С + static unsigned int default_thread_num; + static unsigned int default_stack_size; - /** - * @brief �����̵߳���С������Ŀ - */ static void SetDefaultThreadNum(unsigned int num) { default_thread_num = num; }; - /** - * @brief �����̵߳�Ĭ��ջ��С, ���ʼ��ǰ���� - */ static void SetDefaultStackSize(unsigned int size) { default_stack_size = (size + MEM_PAGE_SIZE - 1) / MEM_PAGE_SIZE * MEM_PAGE_SIZE; }; - /** - * @brief �̳߳س�ʼ�� - */ bool InitialPool(int max_num); - /** - * @brief �̳߳ط���ʼ�� - */ void DestroyPool (void); - /** - * @brief �̷߳���ӿ� - * @return �̶߳��� - */ MicroThread* AllocThread(void); - /** - * @brief �߳��ͷŽӿ� - * @param thread �̶߳��� - */ void FreeThread(MicroThread* thread); - /** - * @brief ��ȡ��ǰ�߳����� - * @param thread �̶߳��� - */ int GetUsedNum(void); private: - ThreadList _freelist; ///< ���д������̶߳��� - int _total_num; ///< Ŀǰ�ܵ��߳���Ŀ����������������� - int _use_num; ///< ��ǰ����ʹ�õ��߳���Ŀ - int _max_num; ///< ���������, �����ڴ����ʹ�� + ThreadList _freelist; + int _total_num; + int _use_num; + int _max_num; }; -typedef TAILQ_HEAD(__ThreadTailq, MicroThread) ThreadTailq; ///< �̶߳��ж��� +typedef TAILQ_HEAD(__ThreadTailq, MicroThread) ThreadTailq; -/** - * @brief �߳̿����, ȫ�ֵĵ����� - */ class MtFrame : public KqueueProxy, public ThreadPool { private: - static MtFrame* _instance; ///< ����ָ�� - LogAdapter* _log_adpt; ///< ��־�ӿ� - ThreadList _runlist; ///< ������queue, �����ȼ� - ThreadTailq _iolist; ///< �ȴ����У������������� - ThreadTailq _pend_list; ///< �ȴ����У������������� - HeapList _sleeplist; ///< �ȴ���ʱ�Ķ�, ���������, ����ʱ��ȡ��С���� - MicroThread* _daemon; ///< �ػ��߳�, ִ��epoll wait, ��ʱ��� - MicroThread* _primo; ///< ԭ���߳�, ʹ�õ���ԭ����ջ - MicroThread* _curr_thread; ///< ��ǰ�����߳� - utime64_t _last_clock; ///< ȫ��ʱ���, ÿ��idle��ȡһ�� - int _waitnum; ///< �ȴ����е����߳���, �ɵ��ڵ��ȵĽ��� - CTimerMng* _timer; ///< TCP����ר�õ�timer��ʱ�� - int _realtime; /// < ʹ��ʵʱʱ��0, δ���� + static MtFrame* _instance; + LogAdapter* _log_adpt; + ThreadList _runlist; + ThreadTailq _iolist; + ThreadTailq _pend_list; + HeapList _sleeplist; + MicroThread* _daemon; + MicroThread* _primo; + MicroThread* _curr_thread; + utime64_t _last_clock; + int _waitnum; + CTimerMng* _timer; + int _realtime; public: - friend class ScheduleObj; ///< ����������, �ǿ���������ģʽ, ��Ԫ���� - + friend class ScheduleObj; + public: - /** - * @brief �߳̿����, ȫ��ʵ����ȡ - */ static MtFrame* Instance (void); - - /** - * @brief �̰߳�����ϵͳIO���� sendto - * @param fd ϵͳsocket��Ϣ - * @param msg �����͵���Ϣָ�� - * @param len �����͵���Ϣ���� - * @param to Ŀ�ĵ�ַ��ָ�� - * @param tolen Ŀ�ĵ�ַ�Ľṹ���� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ͳ���, <0 ʧ�� - */ + static int sendto(int fd, const void *msg, int len, int flags, const struct sockaddr *to, int tolen, int timeout); - /** - * @brief �̰߳�����ϵͳIO���� recvfrom - * @param fd ϵͳsocket��Ϣ - * @param buf ������Ϣ������ָ�� - * @param len ������Ϣ���������� - * @param from ��Դ��ַ��ָ�� - * @param fromlen ��Դ��ַ�Ľṹ���� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ճ���, <0 ʧ�� - */ static int recvfrom(int fd, void *buf, int len, int flags, struct sockaddr *from, socklen_t *fromlen, int timeout); - /** - * @brief �̰߳�����ϵͳIO���� connect - * @param fd ϵͳsocket��Ϣ - * @param addr ָ��server��Ŀ�ĵ�ַ - * @param addrlen ��ַ�ij��� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ͳ���, <0 ʧ�� - */ static int connect(int fd, const struct sockaddr *addr, int addrlen, int timeout); - /** - * @brief �̰߳�����ϵͳIO���� accept - * @param fd �������� - * @param addr �ͻ��˵�ַ - * @param addrlen ��ַ�ij��� - * @param timeout ��ȴ�ʱ��, ���� - * @return >=0 accept��socket������, <0 ʧ�� - */ static int accept(int fd, struct sockaddr *addr, socklen_t *addrlen, int timeout); - /** - * @brief �̰߳�����ϵͳIO���� read - * @param fd ϵͳsocket��Ϣ - * @param buf ������Ϣ������ָ�� - * @param nbyte ������Ϣ���������� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ճ���, <0 ʧ�� - */ static ssize_t read(int fd, void *buf, size_t nbyte, int timeout); - /** - * @brief �̰߳�����ϵͳIO���� write - * @param fd ϵͳsocket��Ϣ - * @param buf ������Ϣ������ָ�� - * @param nbyte ������Ϣ���������� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ͳ���, <0 ʧ�� - */ static ssize_t write(int fd, const void *buf, size_t nbyte, int timeout); - /** - * @brief �̰߳�����ϵͳIO���� recv - * @param fd ϵͳsocket��Ϣ - * @param buf ������Ϣ������ָ�� - * @param len ������Ϣ���������� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ճ���, <0 ʧ�� - */ static int recv(int fd, void *buf, int len, int flags, int timeout); - /** - * @brief �̰߳�����ϵͳIO���� send - * @param fd ϵͳsocket��Ϣ - * @param buf �����͵���Ϣָ�� - * @param nbyte �����͵���Ϣ���� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ͳ���, <0 ʧ�� - */ static ssize_t send(int fd, const void *buf, size_t nbyte, int flags, int timeout); - - /** - * @brief �߳�����sleep�ӿ�, ��λms - */ static void sleep(int ms); - /** - * @brief �߳̽��ȴ��¼�,��������IJ��� - * @param fd ϵͳsocket��Ϣ - * @param events �¼����� EPOLLIN or EPOLLOUT - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ճ���, <0 ʧ�� - */ static int WaitEvents(int fd, int events, int timeout); - /** - * @brief �̴߳����ӿ� - * @param entry �߳���ں��� - * @param args �߳���ڲ��� - * @return �߳�ָ��, NULL��ʾʧ�� - */ static MicroThread* CreateThread(ThreadStart entry, void *args, bool runable = true); - /** - * @brief �ػ��߳���ں���, ����ָ��Ҫ��static���� - * @param args �߳���ڲ��� - */ static void DaemonRun(void* args); - static int Loop(void* args); + static int Loop(void* args); - /** - * @brief ��ȡ��ǰ�̵߳ĸ��߳� - */ MicroThread *GetRootThread(); - /** - * @brief ��ܳ�ʼ��, Ĭ�ϲ�����־���� - */ bool InitFrame(LogAdapter* logadpt = NULL, int max_thread_num = 50000); - /** - * @brief HOOKϵͳapi������ - */ void SetHookFlag(); - /** - * @brief ��ܷ���ʼ�� - */ void Destroy (void); - /** - * @brief �߳̿�ܰ汾��ȡ - */ char* Version(void); - /** - * @brief ��ܻ�ȡȫ��ʱ��� - */ utime64_t GetLastClock(void) { - if(_realtime) - { - return GetSystemMS(); - } + if(_realtime) + { + return GetSystemMS(); + } return _last_clock; }; - - /** - * @brief ��ܻ�ȡ��ǰ�߳� - */ MicroThread* GetActiveThread(void) { return _curr_thread; }; - /** - * @brief ���ص�ǰ�����е��߳���, ֱ�Ӽ���, Ч�ʸ� - * @return �ȴ��߳��� - */ int RunWaitNum(void) { return _waitnum; }; - /** - * @brief ��ܱ�ע�����־������� - */ LogAdapter* GetLogAdpt(void) { return _log_adpt; }; - /** - * @brief ��ȡ��ܱ��ʱ��ָ�� - */ CTimerMng* GetTimerMng(void) { return _timer; }; - /** - * @brief ��ܵ���epoll waitǰ, �ж��ȴ�ʱ����Ϣ - */ virtual int KqueueGetTimeout(void); - /** - * @brief �̴߳����л�����,���óɹ� ���ó�cpu, �ڲ��ӿ� - * @param fdlist ��·������socket�б� - * @param fd ���������fd��Ϣ - * @param timeout ��ȴ�ʱ��, ���� - * @return true �ɹ�, false ʧ�� - */ virtual bool KqueueSchedule(KqObjList* fdlist, KqueuerObj* fd, int timeout); - - /** - * @brief �߳������л�, �ȴ������̵߳Ļ��� - * @param timeout ��ȴ�ʱ��, ���� - */ void WaitNotify(utime64_t timeout); - /** - * @brief ��ܹ����̵߳�Ԫ, �Ƴ�IO�ȴ�״̬, �ڲ��ӿ� - * @param thread �̶߳��� - */ void RemoveIoWait(MicroThread* thread); - /** - * @brief ��ܹ����̵߳�Ԫ, ��������ж���, �ڲ��ӿ� - * @param thread �̶߳��� - */ void InsertRunable(MicroThread* thread); - /** - * @brief ��ܹ����̵߳�Ԫ, ִ��pend�ȴ�״̬ - * @param thread �̶߳��� - */ void InsertPend(MicroThread* thread); - - /** - * @brief ��ܹ����̵߳�Ԫ, �Ƴ�PEND�ȴ�״̬ - * @param thread �̶߳��� - */ + void RemovePend(MicroThread* thread); - void SetRealTime(int realtime_) - { - _realtime =realtime_; - } + void SetRealTime(int realtime_) + { + _realtime =realtime_; + } private: - /** - * @brief �߳�˽�й��� - */ MtFrame():_realtime(1){ _curr_thread = NULL; }; - /** - * @brief �߳�˽�л�ȡ�ػ��߳� - */ MicroThread* DaemonThread(void){ return _daemon; - }; + }; - /** - * @brief ��ܵ����߳����� - */ void ThreadSchdule(void); - /** - * @brief ��ܴ�����ʱ�ص����� - */ void CheckExpired(); - /** - * @brief ��ܼ���ʱ, �������еij�ʱ�߳� - */ void WakeupTimeout(void); - /** - * @brief ��ܸ���ȫ��ʱ��� - */ void SetLastClock(utime64_t clock) { _last_clock = clock; }; - /** - * @brief ������õ�ǰ�߳� - */ void SetActiveThread(MicroThread* thread) { _curr_thread = thread; - }; + }; - /** - * @brief ��ܵ�ʱ��Դ�ӿ�, ���غ��뼶��ʱ�� - */ utime64_t GetSystemMS(void) { struct timeval tv; gettimeofday(&tv, NULL); return (tv.tv_sec * 1000ULL + tv.tv_usec / 1000ULL); }; - /** - * @brief ��ܹ����̵߳�Ԫ, ִ��IO�ȴ�״̬ - * @param thread �̶߳��� - */ void InsertSleep(MicroThread* thread); - /** - * @brief ��ܹ����̵߳�Ԫ, �Ƴ�IO�ȴ�״̬ - * @param thread �̶߳��� - */ void RemoveSleep(MicroThread* thread); - /** - * @brief ��ܹ����̵߳�Ԫ, ִ��IO�ȴ�״̬ - * @param thread �̶߳��� - */ void InsertIoWait(MicroThread* thread); - /** - * @brief ��ܹ����̵߳�Ԫ, �Ƴ������ж��� - * @param thread �̶߳��� - */ void RemoveRunable(MicroThread* thread); }; -/** - * @brief ��־��Ķ��岿�� - */ #define MTLOG_DEBUG(fmt, args...) \ do { \ register NS_MICRO_THREAD::MtFrame *fm = NS_MICRO_THREAD::MtFrame::Instance(); \ @@ -909,13 +531,13 @@ do { \ } while (0) #define MT_ATTR_API_SET(ATTR, VALUE) \ - do { \ - register NS_MICRO_THREAD::MtFrame *fm = NS_MICRO_THREAD::MtFrame::Instance(); \ - if (fm && fm->GetLogAdpt()) \ - { \ - fm->GetLogAdpt()->AttrReportSet(ATTR, VALUE); \ - } \ - } while (0) + do { \ + register NS_MICRO_THREAD::MtFrame *fm = NS_MICRO_THREAD::MtFrame::Instance(); \ + if (fm && fm->GetLogAdpt()) \ + { \ + fm->GetLogAdpt()->AttrReportSet(ATTR, VALUE); \ + } \ + } while (0) diff --git a/app/micro_thread/mt_action.cpp b/app/micro_thread/mt_action.cpp index 09d47d644..0dc3f6619 100644 --- a/app/micro_thread/mt_action.cpp +++ b/app/micro_thread/mt_action.cpp @@ -17,12 +17,6 @@ */ -/** - * @file mt_action.cpp - * @info �߳�ACTION����ʵ�� - * @time 20130924 - **/ - #include "micro_thread.h" #include "mt_notify.h" #include "mt_connection.h" @@ -32,10 +26,6 @@ using namespace std; using namespace NS_MICRO_THREAD; - -/** - * @brief ��ʼitem״̬ - */ void IMtAction::Init() { _flag = MULTI_FLAG_UNDEF; @@ -50,12 +40,8 @@ void IMtAction::Init() memset(&_addr, 0, sizeof(_addr)); } -/** - * @brief ��������, ����item״̬ - */ void IMtAction::Reset() { - // ������, �����ɹ��Ÿ���, ����ǿ�ƹر� bool force_free = false; if (_errno != ERR_NONE) { force_free = true; @@ -67,9 +53,6 @@ void IMtAction::Reset() } } -/** - * @brief ��ȡ���Ӷ���, ֪ͨ����, ��Ϣ���� - */ KqueuerObj* IMtAction::GetNtfyObj() { IMtConnection* conn = GetIConnection(); if (conn) { @@ -79,10 +62,6 @@ KqueuerObj* IMtAction::GetNtfyObj() { } }; - -/** - * @brief ������ӻ�����ʼ�� - */ int IMtAction::InitConnEnv() { MtFrame* mtframe = MtFrame::Instance(); @@ -96,23 +75,22 @@ int IMtAction::InitConnEnv() return -100; } - // 1. �����ȡconn��� CONN_OBJ_TYPE conn_obj_type = OBJ_CONN_UNDEF; NTFY_OBJ_TYPE ntfy_obj_type = NTFY_OBJ_UNDEF; MULTI_PROTO proto = this->GetProtoType(); MULTI_CONNECT type = this->GetConnType(); - if ((MT_UDP == proto) && (CONN_TYPE_SESSION == type)) // UDP sessionģʽ + if ((MT_UDP == proto) && (CONN_TYPE_SESSION == type)) // UDP session { conn_obj_type = OBJ_UDP_SESSION; ntfy_obj_type = NTFY_OBJ_SESSION; } - else if (MT_UDP == proto) // UDP ����ģʽ + else if (MT_UDP == proto) // UDP { conn_obj_type = OBJ_SHORT_CONN; ntfy_obj_type = NTFY_OBJ_THREAD; } - else // TCP ģʽ + else // TCP { conn_obj_type = OBJ_TCP_KEEP; ntfy_obj_type = NTFY_OBJ_THREAD; @@ -125,7 +103,6 @@ int IMtAction::InitConnEnv() } _conn->SetIMtActon(this); - // 2. ��ȡmsg buff��� int max_len = this->GetMsgBuffSize(); MtMsgBuf* msg_buff = msgmgr->GetMsgBuf(max_len); if (!msg_buff) { @@ -135,7 +112,6 @@ int IMtAction::InitConnEnv() msg_buff->SetBuffType(BUFF_SEND); _conn->SetMtMsgBuff(msg_buff); - // 3. ��ȡ ntfy ������ KqueuerObj* ntfy_obj = ntfymgr->GetNtfyObj(ntfy_obj_type, _ntfy_name); if (!ntfy_obj) { MTLOG_ERROR("Maybe no memory, ntfy type: %d, get failed", ntfy_obj_type); @@ -143,7 +119,6 @@ int IMtAction::InitConnEnv() } _conn->SetNtfyObj(ntfy_obj); - // 4. SESSIONģ��, ����session MicroThread* thread = mtframe->GetActiveThread(); ntfy_obj->SetOwnerThread(thread); this->SetIMsgPtr((IMtMsg*)thread->GetThreadArgs()); @@ -158,10 +133,6 @@ int IMtAction::InitConnEnv() return 0; } - -/** - * @brief �����麯��, �ӿ���ʵ�ֲ��� - */ int IMtAction::DoEncode() { MtMsgBuf* msg_buff = NULL; @@ -185,9 +156,6 @@ int IMtAction::DoEncode() return 0; } -/** - * @brief �����麯��, �ӿ���ʵ�ֲ��� - */ int IMtAction::DoInput() { MtMsgBuf* msg_buff = NULL; @@ -210,7 +178,6 @@ int IMtAction::DoInput() return ret; } - int IMtAction::DoProcess() { MtMsgBuf* msg_buff = NULL; @@ -238,15 +205,11 @@ int IMtAction::DoError() return this->HandleError((int)_errno, _msg); } - - -/** - * @brief �������������� - */ IMtAction::IMtAction() { Init(); } + IMtAction::~IMtAction() { Reset(); diff --git a/app/micro_thread/mt_action.h b/app/micro_thread/mt_action.h index cad198cc3..9a05d9c12 100644 --- a/app/micro_thread/mt_action.h +++ b/app/micro_thread/mt_action.h @@ -16,12 +16,6 @@ * and limitations under the License. */ - -/** - * @file mt_action.h - * @info �߳�ACTION���ඨ�� - **/ - #ifndef __MT_ACTION_H__ #define __MT_ACTION_H__ @@ -34,244 +28,136 @@ namespace NS_MICRO_THREAD { -/** - * @brief ��������״̬��Ƕ��� - */ enum MULTI_STATE { - MULTI_FLAG_UNDEF = 0x0, ///< ��ʼ��, δ���� - MULTI_FLAG_INIT = 0x1, ///< socket�����ѳɹ� - MULTI_FLAG_OPEN = 0x2, ///< socket�����Ѵ� - MULTI_FLAG_SEND = 0x4, ///< �������Ѿ����� - MULTI_FLAG_FIN = 0x8, ///< Ӧ�����Ѿ����յ� + MULTI_FLAG_UNDEF = 0x0, + MULTI_FLAG_INIT = 0x1, + MULTI_FLAG_OPEN = 0x2, + MULTI_FLAG_SEND = 0x4, + MULTI_FLAG_FIN = 0x8, }; -/** - * @brief Э���������Ͷ��� - */ enum MULTI_CONNECT { CONN_UNKNOWN = 0, - CONN_TYPE_SHORT = 0x1, ///< ������, һ�ν�����ر� - CONN_TYPE_LONG = 0x2, ///< �����ӣ�ÿ��ʹ�ú�, �ɻ����ظ�ʹ�� - CONN_TYPE_SESSION = 0x4, ///< �����ӣ���session id ����, ������ + CONN_TYPE_SHORT = 0x1, + CONN_TYPE_LONG = 0x2, + CONN_TYPE_SESSION = 0x4, }; -/** - * @brief �����붨�� - */ enum MULTI_ERROR { ERR_NONE = 0, - ERR_SOCKET_FAIL = -1, ///< ����sockʧ�� - ERR_CONNECT_FAIL = -2, ///< ����ʧ�� - ERR_SEND_FAIL = -3, ///< ���ͱ���ʧ�� - ERR_RECV_FAIL = -4, ///< ����ʧ�� - ERR_RECV_TIMEOUT = -5, ///< ���ճ�ʱ - ERR_KQUEUE_FAIL = -6, ///< epollʧ�� - ERR_FRAME_ERROR = -7, ///< ���ʧ�� - ERR_PEER_CLOSE = -8, ///< �Է��ر� - ERR_PARAM_ERROR = -9, ///< �������� - ERR_MEMORY_ERROR = -10, ///< �ڴ�����ʧ�� - ERR_ENCODE_ERROR = -11, ///< ���ʧ�� - ERR_DST_ADDR_ERROR = -12, ///< Ŀ���ַ��ȡʧ�� + ERR_SOCKET_FAIL = -1, + ERR_CONNECT_FAIL = -2, + ERR_SEND_FAIL = -3, + ERR_RECV_FAIL = -4, + ERR_RECV_TIMEOUT = -5, + ERR_KQUEUE_FAIL = -6, + ERR_FRAME_ERROR = -7, + ERR_PEER_CLOSE = -8, + ERR_PARAM_ERROR = -9, + ERR_MEMORY_ERROR = -10, + ERR_ENCODE_ERROR = -11, + ERR_DST_ADDR_ERROR = -12, }; - - -/** - * @brief �̵߳ĺ�˽���������� - */ class IMtAction : public ISession { public: - /** - * @brief �̲߳�����Ϊ���� - */ IMtAction(); virtual ~IMtAction(); - /** - * @brief ������������Ϣ (��֤�ӿ����������, ��ʹ��inline) - * @param dst -����������͵ĵ�ַ - */ - void SetMsgDstAddr(struct sockaddr_in* dst) { + void SetMsgDstAddr(struct sockaddr_in* dst) { memcpy(&_addr, dst, sizeof(_addr)); - }; - - /** - * @brief ��ȡ��ϢĿ�ĵ�ַ��Ϣ - * @return ע���Ŀ�ĵ�ַ - */ - struct sockaddr_in* GetMsgDstAddr() { - return &_addr; - }; + }; + + struct sockaddr_in* GetMsgDstAddr() { + return &_addr; + }; - /** - * @brief ����buff��С, ����ʵ��ʹ�õ�msgbuff���� - * @return 0�ɹ� - */ void SetMsgBuffSize(int buff_size) { _buff_size = buff_size; }; - /** - * @brief ��ȡԤ�õ�buff��С - * @return ����������Ϣbuff��� - */ int GetMsgBuffSize() { return (_buff_size > 0) ? _buff_size : 65535; - } + } - /** - * @brief ���ó�����session������id - * @return 0�ɹ� - */ void SetSessionName(int name) { _ntfy_name = name; }; - /** - * @brief ��ȡ����session������id - * @return session ע���� - */ int GetSessionName() { return _ntfy_name; - } + } - /** - * @brief ���ñ��δ�����proto��Ϣ - */ void SetProtoType(MULTI_PROTO proto) { _proto = proto; }; - /** - * @brief ��ȡ���δ�����proto��Ϣ - * @return proto type - */ MULTI_PROTO GetProtoType() { return _proto; }; - /** - * @brief ���ñ��δ���������������Ϣ - */ void SetConnType(MULTI_CONNECT type) { _conn_type = type; }; - /** - * @brief ��ȡ���δ���������������Ϣ - * @return conn type - */ MULTI_CONNECT GetConnType() { return _conn_type; }; - /** - * @brief ���ñ��δ�����errno - */ void SetErrno(MULTI_ERROR err) { _errno = err; }; - /** - * @brief ��ȡ���δ�����ERRNO��Ϣ - * @return ERRONO - */ MULTI_ERROR GetErrno() { return _errno; }; - /** - * @brief ���ñ��δ�����timecost - */ void SetCost(int cost) { _time_cost = cost; }; - /** - * @brief ��ȡ���δ�����timecost��Ϣ - * @return timecost - */ int GetCost() { return _time_cost; }; - /** - * @brief ���ô���״̬��Ϣ - * @param flag -��Ϣ����״̬ - */ - void SetMsgFlag(MULTI_STATE flag) { + void SetMsgFlag(MULTI_STATE flag) { _flag = flag; - }; - - /** - * @brief ��ȡ����״̬��Ϣ - * @return flag -��Ϣ����״̬ - */ + }; + MULTI_STATE GetMsgFlag() { return _flag; }; - /** - * @brief �����ڲ���Ϣָ�� - * @return IMtConnָ�� - */ void SetIMsgPtr(IMtMsg* msg ) { _msg = msg; }; - /** - * @brief ��ȡ�ڲ���Ϣָ�� - * @return IMtConnָ�� - */ IMtMsg* GetIMsgPtr() { return _msg; }; - - /** - * @brief �����ڲ�������ָ�� - * @return IMtConnָ�� - */ + void SetIConnection(IMtConnection* conn) { _conn = conn; }; - /** - * @brief ��ȡ�ڲ�������ָ�� - * @return IMtConnָ�� - */ IMtConnection* GetIConnection() { return _conn; }; - /** - * @brief ��ʼ����Ҫ�ֶ���Ϣ - */ void Init(); - /** - * @brief ��������, ����Action״̬ - */ void Reset(); - /** - * @brief ��ȡ���Ӷ���, ֪ͨ����, ��Ϣ���� - */ KqueuerObj* GetNtfyObj(); - /** - * @brief ��ȡ���Ӷ���, ֪ͨ����, ��Ϣ���� - */ int InitConnEnv(); - /** - * @brief �����麯��, �ӿ���ʵ�ֲ��� - */ int DoEncode(); int DoInput(); int DoProcess(); @@ -279,46 +165,28 @@ public: public: - /** - * @brief �������ӵ���Ϣ����ӿ� - * @return >0 -�ɹ�, < 0 ʧ�� - */ virtual int HandleEncode(void* buf, int& len, IMtMsg* msg){return 0;}; - /** - * @brief �������ӵ�CHECK�ӿ�, TCP�ķְ��ӿ� - * @return > 0 �Ѿ��ɹ�����,������������С, =0 �����ȴ�, <0 ����(����-65535 UDP����) - */ virtual int HandleInput(void* buf, int len, IMtMsg* msg){return 0;}; - /** - * @brief �������ӵ�Ӧ�����ӿ�, ����һ�������ֶΰ������ - * @return 0 �ɹ�, ����ʧ�� - */ virtual int HandleProcess(void* buf, int len, IMtMsg* msg){return 0;}; - /** - * @brief �������Ӵ����Ĵ���֪ͨ, ����μ� MULTI_ERROR ö�� - * @info ��handleprocessʧ��, �����쳣�����øýӿ� - * @return 0 �ɹ�, ����ʧ�� - */ virtual int HandleError(int err, IMtMsg* msg){return 0;}; protected: - MULTI_STATE _flag; // �������������Ϣ, ��ǰ״̬��Ϣ - MULTI_PROTO _proto; // Э������ UDP/TCP - MULTI_CONNECT _conn_type; // �������� �������� - MULTI_ERROR _errno; // ��������Ϣ, 0�ɹ��������� - struct sockaddr_in _addr; // ����ʱ��д��ָ�����͵�stAddr - int _time_cost; // ��������Ӧ���ʱ, ���� - int _buff_size; // �����������������Ӧ�� - int _ntfy_name; // ������session ntfy������, sessionģ������ - - IMtMsg* _msg; // ��Ϣָ��, �ϼ�ָ�� - IMtConnection* _conn; // ������ָ��, �¼�ָ��, ���������� + MULTI_STATE _flag; + MULTI_PROTO _proto; + MULTI_CONNECT _conn_type; + MULTI_ERROR _errno; + struct sockaddr_in _addr; + int _time_cost; + int _buff_size; + int _ntfy_name; + IMtMsg* _msg; + IMtConnection* _conn; }; } diff --git a/app/micro_thread/mt_api.cpp b/app/micro_thread/mt_api.cpp index 59c03cc90..474685fd7 100644 --- a/app/micro_thread/mt_api.cpp +++ b/app/micro_thread/mt_api.cpp @@ -19,7 +19,6 @@ /** * @filename mt_sys_call.cpp - * @info �̷߳�װϵͳapi, ͬ�������߳�API��ʵ���첽���� */ #include "kqueue_proxy.h" @@ -31,16 +30,6 @@ namespace NS_MICRO_THREAD { -/** - * @brief ��������˿ڵ�socket�շ��ӿ�, ��socket������������, ҵ������֤������ - * @param dst -�����͵�Ŀ�ĵ�ַ - * @param pkg -�������װ�İ��� - * @param len -�������װ�İ��峤�� - * @param rcv_buf -����Ӧ�����buff - * @param buf_size -modify-����Ӧ�����buff��С, �ɹ�����ʱ, ��ΪӦ������� - * @param timeout -��ʱʱ��, ��λms - * @return 0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��, �ɴ�ӡerrno, -10 ������Ч - */ int mt_udpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, int timeout) { int ret = 0; @@ -53,13 +42,13 @@ int mt_udpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, in { MTLOG_ERROR("mt_udpsendrcv input params invalid, dst[%p], pkg[%p], rcv_buf[%p], len[%d], buf_size[%d]", dst, pkg, rcv_buf, len, buf_size); - return -10; + return -10; } int sock = socket(PF_INET, SOCK_DGRAM, 0); if ((sock < 0) || (ioctl(sock, FIONBIO, &flags) < 0)) { - MT_ATTR_API(320842, 1); // socketʧ�� + MT_ATTR_API(320842, 1); MTLOG_ERROR("mt_udpsendrcv new sock failed, sock: %d, errno: %d (%m)", sock, errno); ret = -1; goto EXIT_LABEL; @@ -68,7 +57,7 @@ int mt_udpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, in rc = MtFrame::sendto(sock, pkg, len, 0, (struct sockaddr*)dst, (int)sizeof(*dst), timeout); if (rc < 0) { - MT_ATTR_API(320844, 1); // ����ʧ�� + MT_ATTR_API(320844, 1); MTLOG_ERROR("mt_udpsendrcv send failed, rc: %d, errno: %d (%m)", rc, errno); ret = -2; goto EXIT_LABEL; @@ -77,7 +66,7 @@ int mt_udpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, in rc = MtFrame::recvfrom(sock, rcv_buf, buf_size, 0, (struct sockaddr*)&from_addr, (socklen_t*)&addr_len, timeout); if (rc < 0) { - MT_ATTR_API(320845, 1); // ����δ��ȫ�ɹ� + MT_ATTR_API(320845, 1); MTLOG_ERROR("mt_udpsendrcv recv failed, rc: %d, errno: %d (%m)", rc, errno); ret = -3; goto EXIT_LABEL; @@ -95,16 +84,11 @@ EXIT_LABEL: return ret; } -/** - * @brief ����TCP���֣�������Ϊ������ - * @return >=0 �ɹ�, <0 ʧ�� - */ int mt_tcp_create_sock(void) { int fd; int flag; - // ����socket fd = ::socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { @@ -112,7 +96,6 @@ int mt_tcp_create_sock(void) return -1; } - // ����socket������ flag = fcntl(fd, F_GETFL, 0); if (flag == -1) { @@ -134,12 +117,8 @@ int mt_tcp_create_sock(void) return fd; } -/** - * @brief TCP��ȡ������֪ͨ������socket - */ static TcpKeepConn* mt_tcp_get_keep_conn(struct sockaddr_in* dst, int& sock) { - // 1. ��ȡ�߳�֪ͨע����� KqueuerObj* ntfy_obj = NtfyObjMgr::Instance()->GetNtfyObj(NTFY_OBJ_THREAD, 0); if (NULL == ntfy_obj) { @@ -147,7 +126,6 @@ static TcpKeepConn* mt_tcp_get_keep_conn(struct sockaddr_in* dst, int& sock) return NULL; } - // 2. ��ȡ���Ӷ���, ����֪ͨ��Ϣ TcpKeepConn* conn = dynamic_cast<TcpKeepConn*>(ConnectionMgr::Instance()->GetConnection(OBJ_TCP_KEEP, dst)); if (NULL == conn) { @@ -157,7 +135,6 @@ static TcpKeepConn* mt_tcp_get_keep_conn(struct sockaddr_in* dst, int& sock) } conn->SetNtfyObj(ntfy_obj); - // 3. ��������socket��� int osfd = conn->CreateSocket(); if (osfd < 0) { @@ -166,15 +143,10 @@ static TcpKeepConn* mt_tcp_get_keep_conn(struct sockaddr_in* dst, int& sock) return NULL; } - // 4. �ɹ��������� sock = osfd; return conn; } -/** - * @brief TCPѭ������, ֱ������OK��ʱ - * [ע��] �����߲�Ҫ�����ĺ�������ֵ����֤��Ҫ��mt_tcpsendrcv�ȵ��ýӿڳ�ͻ [��Ҫ] - */ static int mt_tcp_check_recv(int sock, char* rcv_buf, int &len, int flags, int timeout, MtFuncTcpMsgLen func) { int recv_len = 0; @@ -195,33 +167,32 @@ static int mt_tcp_check_recv(int sock, char* rcv_buf, int &len, int flags, int t MTLOG_ERROR("tcp socket[%d] recv failed ret[%d][%m]", sock, rc); return -3; } - else if (rc == 0) + else if (rc == 0) { - len = recv_len; + len = recv_len; MTLOG_ERROR("tcp socket[%d] remote close", sock); return -7; } recv_len += rc; - /* ��鱨�������� */ rc = func(rcv_buf, recv_len); if (rc < 0) { MTLOG_ERROR("tcp socket[%d] user check pkg error[%d]", sock, rc); return -5; } - else if (rc == 0) // ����δ������ + else if (rc == 0) { - if (len == recv_len) // û�ռ��ٽ�����, ���� + if (len == recv_len) { MTLOG_ERROR("tcp socket[%d] user check pkg not ok, but no more buff", sock); return -6; } continue; } - else // �ɹ����㱨�ij��� + else { - if (rc > recv_len) // ���Ļ�δ��ȫ + if (rc > recv_len) { continue; } @@ -236,20 +207,6 @@ static int mt_tcp_check_recv(int sock, char* rcv_buf, int &len, int flags, int t return 0; } -/** - * @brief TCP��������ӳصķ�ʽ����IP/PORT����, ���ӱ���Ĭ��10���� - * [ע��] tcp���շ���buff, ��������static����, ����������Ĵ��� [��Ҫ] - * [ע��] �Ľӿڣ���ע�ⲻҪ����ķ���ֵ������֤��mt_tcpsendrcv_ex����ֵƥ�� [��Ҫ] - * @param dst -�����͵�Ŀ�ĵ�ַ - * @param pkg -�������װ�İ��� - * @param len -�������װ�İ��峤�� - * @param rcv_buf -����Ӧ�����buff - * @param buf_size -modify-����Ӧ�����buff��С, �ɹ�����ʱ, ��ΪӦ������� - * @param timeout -��ʱʱ��, ��λms - * @param check_func -��ⱨ���Ƿ�ɹ����ﺯ�� - * @return 0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��, - * -4 ����ʧ��, -5 ��ⱨ��ʧ��, -6 ���տռ䲻��, -7 ��������ر����ӣ�-10 ������Ч - */ int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, int timeout, MtFuncTcpMsgLen func) { if (!dst || !pkg || !rcv_buf || !func || len<1 || buf_size<1) @@ -265,7 +222,6 @@ int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, in utime64_t cost_time = 0; int time_left = timeout; - // 1. ��ȡTCP���ӳض���, �ҽ�֪ͨ���� int sock = -1; TcpKeepConn* conn = mt_tcp_get_keep_conn(dst, sock); if ((conn == NULL) || (sock < 0)) @@ -275,7 +231,6 @@ int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, in goto EXIT_LABEL; } - // 2. ���Լ����½����� rc = MtFrame::connect(sock, (struct sockaddr *)dst, addr_len, time_left); if (rc < 0) { @@ -284,7 +239,6 @@ int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, in goto EXIT_LABEL; } - // 3. �������ݴ��� cost_time = MtFrame::Instance()->GetLastClock() - start_ms; time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0; rc = MtFrame::send(sock, pkg, len, 0, time_left); @@ -295,7 +249,6 @@ int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, in goto EXIT_LABEL; } - // 4. �������ݴ��� cost_time = MtFrame::Instance()->GetLastClock() - start_ms; time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0; rc = mt_tcp_check_recv(sock, (char*)rcv_buf, buf_size, 0, time_left, func); @@ -310,7 +263,6 @@ int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, in EXIT_LABEL: - // ʧ����ǿ���ͷ�����, ����ʱ���� if (conn != NULL) { ConnectionMgr::Instance()->FreeConnection(conn, (ret < 0)); @@ -319,20 +271,6 @@ EXIT_LABEL: return ret; } -/** - * @brief TCP�������շ����� - * [ע��] tcp���շ���buff, ��������static����, ����������Ĵ��� [��Ҫ] - * [ע��] �Ľӿڣ���ע�ⲻҪ����ķ���ֵ������֤��mt_tcpsendrcv_ex����ֵƥ�� [��Ҫ] - * @param dst -�����͵�Ŀ�ĵ�ַ - * @param pkg -�������װ�İ��� - * @param len -�������װ�İ��峤�� - * @param rcv_buf -����Ӧ�����buff - * @param buf_size -modify-����Ӧ�����buff��С, �ɹ�����ʱ, ��ΪӦ������� - * @param timeout -��ʱʱ��, ��λms - * @param check_func -��ⱨ���Ƿ�ɹ����ﺯ�� - * @return 0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��, - * -4 ����ʧ��, -5 ��ⱨ��ʧ��, -6 ���տռ䲻��, -7 ��������ر����ӣ�-10 ������Ч - */ int mt_tcpsendrcv_short(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, int timeout, MtFuncTcpMsgLen func) { int ret = 0, rc = 0; @@ -341,7 +279,6 @@ int mt_tcpsendrcv_short(struct sockaddr_in* dst, void* pkg, int len, void* rcv_b utime64_t cost_time = 0; int time_left = timeout; - // 1. ������� if (!dst || !pkg || !rcv_buf || !func || len<1 || buf_size<1) { MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], rcv_buf[%p], fun[%p], len[%d], buf_size[%d]", @@ -349,7 +286,6 @@ int mt_tcpsendrcv_short(struct sockaddr_in* dst, void* pkg, int len, void* rcv_b return -10; } - // 2. ����TCP socket int sock; sock = mt_tcp_create_sock(); if (sock < 0) @@ -358,7 +294,6 @@ int mt_tcpsendrcv_short(struct sockaddr_in* dst, void* pkg, int len, void* rcv_b return -1; } - // 3. ���Լ����½����� rc = MtFrame::connect(sock, (struct sockaddr *)dst, addr_len, time_left); if (rc < 0) { @@ -367,7 +302,6 @@ int mt_tcpsendrcv_short(struct sockaddr_in* dst, void* pkg, int len, void* rcv_b goto EXIT_LABEL; } - // 4. �������ݴ��� cost_time = MtFrame::Instance()->GetLastClock() - start_ms; time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0; rc = MtFrame::send(sock, pkg, len, 0, time_left); @@ -378,7 +312,6 @@ int mt_tcpsendrcv_short(struct sockaddr_in* dst, void* pkg, int len, void* rcv_b goto EXIT_LABEL; } - // 5. �������ݴ��� cost_time = MtFrame::Instance()->GetLastClock() - start_ms; time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0; rc = mt_tcp_check_recv(sock, (char*)rcv_buf, buf_size, 0, time_left, func); @@ -398,17 +331,6 @@ EXIT_LABEL: return ret; } - -/** - * @brief TCP��������ӳصķ�ʽ����IP/PORT����, ���ӱ���Ĭ��10���� - * [ע��] tcp����buff, ��������static����, ����������Ĵ��� [��Ҫ] - * [ע��] �Ľӿڣ���ע�ⲻҪ����ķ���ֵ������֤��mt_tcpsendrcv_ex����ֵƥ�� [��Ҫ] - * @param dst -�����͵�Ŀ�ĵ�ַ - * @param pkg -�������װ�İ��� - * @param len -�������װ�İ��峤�� - * @param timeout -��ʱʱ��, ��λms - * @return 0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -4 ����ʧ��, -10 ������Ч - */ int mt_tcpsend(struct sockaddr_in* dst, void* pkg, int len, int timeout) { if (!dst || !pkg || len<1) @@ -423,7 +345,6 @@ int mt_tcpsend(struct sockaddr_in* dst, void* pkg, int len, int timeout) utime64_t cost_time = 0; int time_left = timeout; - // 1. ��ȡTCP���ӳض���, �ҽ�֪ͨ���� int sock = -1; TcpKeepConn* conn = mt_tcp_get_keep_conn(dst, sock); if ((conn == NULL) || (sock < 0)) @@ -433,7 +354,6 @@ int mt_tcpsend(struct sockaddr_in* dst, void* pkg, int len, int timeout) goto EXIT_LABEL; } - // 2. ���Լ����½����� rc = MtFrame::connect(sock, (struct sockaddr *)dst, addr_len, time_left); if (rc < 0) { @@ -442,7 +362,6 @@ int mt_tcpsend(struct sockaddr_in* dst, void* pkg, int len, int timeout) goto EXIT_LABEL; } - // 3. �������ݴ��� cost_time = MtFrame::Instance()->GetLastClock() - start_ms; time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0; rc = MtFrame::send(sock, pkg, len, 0, time_left); @@ -457,7 +376,6 @@ int mt_tcpsend(struct sockaddr_in* dst, void* pkg, int len, int timeout) EXIT_LABEL: - // ʧ����ǿ���ͷ�����, ����ʱ���� if (conn != NULL) { ConnectionMgr::Instance()->FreeConnection(conn, (ret < 0)); @@ -466,19 +384,8 @@ EXIT_LABEL: return ret; } -/** - * @brief TCP������ֻ�����սӿ� - * [ע��] tcp����buff, ��������static����, ����������Ĵ��� [��Ҫ] - * [ע��] �Ľӿڣ���ע�ⲻҪ����ķ���ֵ������֤��mt_tcpsendrcv_ex����ֵƥ�� [��Ҫ] - * @param dst -�����͵�Ŀ�ĵ�ַ - * @param pkg -�������װ�İ��� - * @param len -�������װ�İ��峤�� - * @param timeout -��ʱʱ��, ��λms - * @return 0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -4 ����ʧ��, -10 ������Ч - */ int mt_tcpsend_short(struct sockaddr_in* dst, void* pkg, int len, int timeout) { - // 1. ��μ�� if (!dst || !pkg || len<1) { MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], len[%d]", dst, pkg, len); @@ -491,7 +398,6 @@ int mt_tcpsend_short(struct sockaddr_in* dst, void* pkg, int len, int timeout) utime64_t cost_time = 0; int time_left = timeout; - // 2. ����TCP socket int sock = -1; sock = mt_tcp_create_sock(); if (sock < 0) @@ -501,7 +407,6 @@ int mt_tcpsend_short(struct sockaddr_in* dst, void* pkg, int len, int timeout) goto EXIT_LABEL; } - // 2. ���Լ����½����� rc = MtFrame::connect(sock, (struct sockaddr *)dst, addr_len, time_left); if (rc < 0) { @@ -510,7 +415,6 @@ int mt_tcpsend_short(struct sockaddr_in* dst, void* pkg, int len, int timeout) goto EXIT_LABEL; } - // 3. �������ݴ��� cost_time = MtFrame::Instance()->GetLastClock() - start_ms; time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0; rc = MtFrame::send(sock, pkg, len, 0, time_left); @@ -531,55 +435,32 @@ EXIT_LABEL: return ret; } - -/** - * @brief TCP�շ��ӿڣ�����ѡ���˱������ӻ��߶����� - * [ע��] tcp���շ���buff, ��������static����, ����������Ĵ��� [��Ҫ] - * @param dst -�����͵�Ŀ�ĵ�ַ - * @param pkg -�������װ�İ��� - * @param len -�������װ�İ��峤�� - * @param rcv_buf -����Ӧ�����buff��ֻ�����տ�������ΪNULL - * @param buf_size -modify-����Ӧ�����buff��С, �ɹ�����ʱ, ��ΪӦ������ȣ�ֻ�����գ�����ΪNULL - * @param timeout -��ʱʱ��, ��λms - * @param check_func -��ⱨ���Ƿ�ɹ����ﺯ�� - * @param type - �������� - * MT_TCP_SHORT: һ��һ�������ӣ� - * MT_TCP_LONG : һ��һ�ճ����ӣ� - * MT_TCP_LONG_SNDONLY : ֻ�����ճ����ӣ� - * MT_TCP_SHORT_SNDONLY: ֻ�����ն����ӣ� - * @return 0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��, - * -4 ����ʧ��, -5 ��ⱨ��ʧ��, -6 ���տռ䲻��, -7 ��������ر�����, -10 ������Ч - */ int mt_tcpsendrcv_ex(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int* buf_size, int timeout, MtFuncTcpMsgLen func, MT_TCP_CONN_TYPE type) { - if(!dst || !pkg || len<1) - { + if(!dst || !pkg || len<1) + { MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], rcv_buf[%p], fun[%p], len[%d], buf_size[%p]type[%d]", dst, pkg, rcv_buf, func, len, buf_size,type); - return -10; - } + return -10; + } switch (type) { - // TCP�����ӵ������� case MT_TCP_LONG: { return mt_tcpsendrcv(dst, pkg, len, rcv_buf, *buf_size, timeout, func); } - // TCP������ֻ������ case MT_TCP_LONG_SNDONLY: { return mt_tcpsend(dst, pkg, len, timeout); } - // TCP�����ӵ������� case MT_TCP_SHORT: { return mt_tcpsendrcv_short(dst, pkg, len, rcv_buf, *buf_size, timeout, func); } - // TCP������ֻ������ case MT_TCP_SHORT_SNDONLY: { return mt_tcpsend_short(dst, pkg, len, timeout); @@ -596,11 +477,6 @@ int mt_tcpsendrcv_ex(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, return 0; } - - -/** - * @brief ������ص��������� - */ static void mt_task_process(void* arg) { int rc = 0; @@ -622,11 +498,6 @@ static void mt_task_process(void* arg) return; }; -/** - * @brief ��·IO�Ĵ���, ��������̹߳��� - * @param req_list - �����б� - * @return 0 �ɹ�, <0ʧ�� - */ int mt_exec_all_task(IMtTaskList& req_list) { MtFrame* mtframe = MtFrame::Instance(); @@ -639,14 +510,12 @@ int mt_exec_all_task(IMtTaskList& req_list) MicroThread::SubThreadList list; TAILQ_INIT(&list); - // ��ֹû��task�������߳�һֱ����ס if (0 == req_list.size()) { MTLOG_DEBUG("no task for execult"); return 0; } - // 1. �����̶߳��� for (IMtTaskList::iterator it = req_list.begin(); it != req_list.end(); ++it) { task = *it; @@ -661,7 +530,6 @@ int mt_exec_all_task(IMtTaskList& req_list) TAILQ_INSERT_TAIL(&list, sub, _sub_entry); } - // 2. ����ִ������ TAILQ_FOREACH_SAFE(sub, &list, _sub_entry, tmp) { TAILQ_REMOVE(&list, sub, _sub_entry); @@ -669,7 +537,6 @@ int mt_exec_all_task(IMtTaskList& req_list) mtframe->InsertRunable(sub); } - // 3. �ȴ����߳�ִ�н��� thread->Wait(); rc = 0; @@ -685,10 +552,6 @@ EXIT_LABEL: } -/** - * @brief ���õ�ǰIMtMsg��˽�б��� - * @info ֻ����ָ�룬�ڴ���Ҫҵ����� - */ void mt_set_msg_private(void *data) { MicroThread *msg_thread = MtFrame::Instance()->GetRootThread(); @@ -696,10 +559,6 @@ void mt_set_msg_private(void *data) msg_thread->SetPrivate(data); } -/** - * @brief ��ȡ��ǰIMtMsg��˽�б��� - * @return ˽�б���ָ�� - */ void* mt_get_msg_private() { MicroThread *msg_thread = MtFrame::Instance()->GetRootThread(); @@ -711,158 +570,71 @@ void* mt_get_msg_private() return msg_thread->GetPrivate(); } -/** - * @brief �߳̿�ܳ�ʼ�� - * @info ҵ��ʹ��spp�������̣߳���Ҫ���øó�ʼ������ - * @return false:��ʼ��ʧ�� true:��ʼ���ɹ� - */ bool mt_init_frame(int argc, char * const argv[]) { - if (argc) { - ff_init(argc, argv); - ff_set_hook_flag(); - } - memset(&g_mt_syscall_tab, 0, sizeof(g_mt_syscall_tab)); + if (argc) { + ff_init(argc, argv); + ff_set_hook_flag(); + } + memset(&g_mt_syscall_tab, 0, sizeof(g_mt_syscall_tab)); return MtFrame::Instance()->InitFrame(); } -/** - * @brief �����̶߳���ջ�ռ��С - * @info �DZ������ã�Ĭ�ϴ�СΪ128K - */ void mt_set_stack_size(unsigned int bytes) { ThreadPool::SetDefaultStackSize(bytes); } -/** - * @brief �̰߳�����ϵͳIO���� recvfrom - * @param fd ϵͳsocket��Ϣ - * @param buf ������Ϣ������ָ�� - * @param len ������Ϣ���������� - * @param from ��Դ��ַ��ָ�� - * @param fromlen ��Դ��ַ�Ľṹ���� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ճ���, <0 ʧ�� - */ int mt_recvfrom(int fd, void *buf, int len, int flags, struct sockaddr *from, socklen_t *fromlen, int timeout) { return MtFrame::recvfrom(fd, buf, len, flags, from, fromlen, timeout); } -/** - * @brief �̰߳�����ϵͳIO���� sendto - * @param fd ϵͳsocket��Ϣ - * @param msg �����͵���Ϣָ�� - * @param len �����͵���Ϣ���� - * @param to Ŀ�ĵ�ַ��ָ�� - * @param tolen Ŀ�ĵ�ַ�Ľṹ���� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ͳ���, <0 ʧ�� - */ int mt_sendto(int fd, const void *msg, int len, int flags, const struct sockaddr *to, int tolen, int timeout) { return MtFrame::sendto(fd, msg, len, flags, to, tolen, timeout); } -/** - * @brief �̰߳�����ϵͳIO���� connect - * @param fd ϵͳsocket��Ϣ - * @param addr ָ��server��Ŀ�ĵ�ַ - * @param addrlen ��ַ�ij��� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ͳ���, <0 ʧ�� - */ int mt_connect(int fd, const struct sockaddr *addr, int addrlen, int timeout) { return MtFrame::connect(fd, addr, addrlen, timeout); } -/** - * @brief �̰߳�����ϵͳIO���� accept - * @param fd �������� - * @param addr �ͻ��˵�ַ - * @param addrlen ��ַ�ij��� - * @param timeout ��ȴ�ʱ��, ���� - * @return >=0 accept��socket������, <0 ʧ�� - */ int mt_accept(int fd, struct sockaddr *addr, socklen_t *addrlen, int timeout) { return MtFrame::accept(fd, addr, addrlen, timeout); } - -/** - * @brief �̰߳�����ϵͳIO���� read - * @param fd ϵͳsocket��Ϣ - * @param buf ������Ϣ������ָ�� - * @param nbyte ������Ϣ���������� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ճ���, <0 ʧ�� - */ ssize_t mt_read(int fd, void *buf, size_t nbyte, int timeout) { return MtFrame::read(fd, buf, nbyte, timeout); } -/** - * @brief �̰߳�����ϵͳIO���� write - * @param fd ϵͳsocket��Ϣ - * @param buf �����͵���Ϣָ�� - * @param nbyte �����͵���Ϣ���� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ͳ���, <0 ʧ�� - */ ssize_t mt_write(int fd, const void *buf, size_t nbyte, int timeout) { return MtFrame::write(fd, buf, nbyte, timeout); } -/** - * @brief �̰߳�����ϵͳIO���� recv - * @param fd ϵͳsocket��Ϣ - * @param buf ������Ϣ������ָ�� - * @param len ������Ϣ���������� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ճ���, <0 ʧ�� - */ ssize_t mt_recv(int fd, void *buf, int len, int flags, int timeout) { return MtFrame::recv(fd, buf, len, flags, timeout); } -/** - * @brief �̰߳�����ϵͳIO���� send - * @param fd ϵͳsocket��Ϣ - * @param buf �����͵���Ϣָ�� - * @param nbyte �����͵���Ϣ���� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ͳ���, <0 ʧ�� - */ ssize_t mt_send(int fd, const void *buf, size_t nbyte, int flags, int timeout) { return MtFrame::send(fd, buf, nbyte, flags, timeout); } -/** - * @brief �߳�����sleep�ӿ�, ��λms - */ void mt_sleep(int ms) { MtFrame::sleep(ms); } -/** - * @brief �̻߳�ȡϵͳʱ�䣬��λms - */ unsigned long long mt_time_ms(void) { return MtFrame::Instance()->GetLastClock(); } -/** - * @brief �̵߳ȴ�epoll�¼��İ������� - */ int mt_wait_events(int fd, int events, int timeout) { return MtFrame::Instance()->WaitEvents(fd, events, timeout); @@ -880,93 +652,89 @@ void* mt_start_thread(void* entry, void* args) class ScopedBuf { public: - ScopedBuf(void*& buf_keeper, bool keep) - :buf_keeper_(buf_keeper),buf_(0),len_(0),len_watermark_(0),keep_(keep) - {} + ScopedBuf(void*& buf_keeper, bool keep) + :buf_keeper_(buf_keeper),buf_(0),len_(0),len_watermark_(0),keep_(keep) + {} - int Alloc(int len) - { - if(len<len_) - { - return -1; // �ռ��СԽ�� - } + int Alloc(int len) + { + if(len<len_) + { + return -1; + } if(len==0) { len = BUF_ALIGNMENT_SIZE; } - if(len_==len) - { - return 0; - } - - len_ = BUF_ALIGN_SIZE(len); - if(len_==0) - { - len_ = BUF_DEFAULT_SIZE; - } - len_watermark_ = len_-BUF_ALIGNMENT_SIZE; - char* tmp = (char*)realloc(buf_, len_); - if(tmp==NULL) - { - return -2; // �����ڴ�ʧ�� - } + if(len_==len) + { + return 0; + } + + len_ = BUF_ALIGN_SIZE(len); + if(len_==0) + { + len_ = BUF_DEFAULT_SIZE; + } + len_watermark_ = len_-BUF_ALIGNMENT_SIZE; + char* tmp = (char*)realloc(buf_, len_); + if(tmp==NULL) + { + return -2; + } - buf_ = tmp; - return 0; - } + buf_ = tmp; + return 0; + } - void reset() - { - if(keep_) - { - buf_keeper_ = (void*)buf_; - buf_ = NULL; - } - } - - ~ScopedBuf() - { - if(buf_!=NULL) - { - free(buf_); - buf_ = NULL; - } - } + void reset() + { + if(keep_) + { + buf_keeper_ = (void*)buf_; + buf_ = NULL; + } + } + + ~ScopedBuf() + { + if(buf_!=NULL) + { + free(buf_); + buf_ = NULL; + } + } public: - void* &buf_keeper_; - char* buf_; - int len_; + void* &buf_keeper_; + char* buf_; + int len_; int len_watermark_; - bool keep_; - + bool keep_; + }; -/** - * @brief TCPѭ������, ֱ������OK��ʱ - * [ע��] �����߲�Ҫ�����ĺ�������ֵ����֤��Ҫ��mt_tcpsendrcv�ȵ��ýӿڳ�ͻ [��Ҫ] - */ static int mt_tcp_check_recv(int sock, void*& rcv_buf, int &len, int flags, int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx, bool keep_rcv_buf) { int recv_len = 0; utime64_t start_ms = MtFrame::Instance()->GetLastClock(); - int rc = 0; - int ret = 0; + int rc = 0; + int ret = 0; int pkg_len = 0; bool msg_len_detected = false; - ScopedBuf sbuf(rcv_buf, keep_rcv_buf); - ret = sbuf.Alloc(len); + ScopedBuf sbuf(rcv_buf, keep_rcv_buf); + ret = sbuf.Alloc(len); - if(ret!=0) - { + if(ret!=0) + { MTLOG_ERROR("tcp socket[%d] recv failed ret[%d], alloc rcv buf failed, [%m]", sock, ret); - return -11; - } - + return -11; + } + do { utime64_t cost_time = MtFrame::Instance()->GetLastClock() - start_ms; @@ -983,29 +751,27 @@ static int mt_tcp_check_recv(int sock, void*& rcv_buf, int &len, int flags, MTLOG_ERROR("tcp socket[%d] recv failed ret[%d][%m]", sock, rc); return -3; } - else if (rc == 0) // Զ�˹ر� + else if (rc == 0) { - if(recv_len==0) // δ�ذ���ֱ�ӷ���Զ�˹ر� - { - MTLOG_ERROR("tcp socket[%d] remote close", sock); - return -7; - } - - /* ��鱨�������� */ - rc = check_func(sbuf.buf_, recv_len, true, msg_ctx, msg_len_detected); + if(recv_len==0) + { + MTLOG_ERROR("tcp socket[%d] remote close", sock); + return -7; + } + + rc = check_func(sbuf.buf_, recv_len, true, msg_ctx, msg_len_detected); - if(rc!=recv_len) // ҵ�����Զ�˹رգ�Ӧ�÷������������ȣ�����<=0,��ʾ���������� - { - MTLOG_ERROR("tcp socket[%d] remote close", sock); - return -7; - } - len = recv_len; - break; + if(rc!=recv_len) + { + MTLOG_ERROR("tcp socket[%d] remote close", sock); + return -7; + } + len = recv_len; + break; } recv_len += rc; - /* ��鱨�������� */ if((!msg_len_detected)||recv_len==pkg_len) { rc = check_func(sbuf.buf_, recv_len, false, msg_ctx,msg_len_detected); @@ -1024,84 +790,65 @@ static int mt_tcp_check_recv(int sock, void*& rcv_buf, int &len, int flags, MTLOG_ERROR("tcp socket[%d] user check pkg error[%d]", sock, rc); return -5; } - else if (rc == 0) // ����δ������,�Ҳ�ȷ����С + else if (rc == 0) { - if(sbuf.len_ > recv_len) - { - continue; - } - // û�ռ��ٽ�����, 2����С��չbuf - ret = sbuf.Alloc(sbuf.len_<<1); - - if(ret!=0) - { - MTLOG_ERROR("tcp socket[%d] recv failed ret[%d], alloc rcv buf failed, [%m]", sock, ret); - return -11; - } - } - else // �ɹ����㱨�ij��� - { - if (rc > recv_len) // ���Ļ�δ��ȫ + if(sbuf.len_ > recv_len) { - if(sbuf.len_ > recv_len) // recv buf���пռ�.��δ����ˮλ - { - continue; - } - - // û�ռ��ٽ�����, ����ҵ��ָʾ��С��չ�ڴ� - ret = sbuf.Alloc(rc); - - if(ret!=0) - { - MTLOG_ERROR("tcp socket[%d] recv failed ret[%d], alloc rcv buf failed, [%m]", sock, ret); - return -11; - } + continue; } - else if(rc==recv_len) // �հ����� + + ret = sbuf.Alloc(sbuf.len_<<1); + + if(ret!=0) + { + MTLOG_ERROR("tcp socket[%d] recv failed ret[%d], alloc rcv buf failed, [%m]", sock, ret); + return -11; + } + } + else + { + if (rc > recv_len) + { + if(sbuf.len_ > recv_len) + { + continue; + } + + ret = sbuf.Alloc(rc); + + if(ret!=0) + { + MTLOG_ERROR("tcp socket[%d] recv failed ret[%d], alloc rcv buf failed, [%m]", sock, ret); + return -11; + } + } + else if(rc==recv_len) { len = rc; break; } - else // �߳����ģʽ�£�������ճ�� + else { - MTLOG_ERROR("tcp socket[%d] user check pkg error, pkg len < recv_len", sock); - return -5; + MTLOG_ERROR("tcp socket[%d] user check pkg error, pkg len < recv_len", sock); + return -5; } } } while (true); - sbuf.reset(); + sbuf.reset(); return 0; } - - -/** - * @brief TCP��������ӳصķ�ʽ����IP/PORT����, ���ӱ���Ĭ��10���� - * [ע��] tcp���շ���buff, ��������static����, ����������Ĵ��� [��Ҫ] - * @param dst -�����͵�Ŀ�ĵ�ַ - * @param pkg -�������װ�İ��� - * @param len -�������װ�İ��峤�� - * @param rcv_buf -���������������ο����� keep_rcv_buf�� - * @param recv_pkg_size -����Ӧ�����buff�ij�ʼ��С����������0ʱ�����ر���ʵ�ʳ��ȡ� - * @param timeout -��ʱʱ��, ��λms - * @param check_func -��ⱨ���Ƿ�ɹ����ﺯ�� - * @param msg_ctx -�������ĵ������ı����� - * @param keep_rcv_buf -true,��ܽ�����rcv_buf��ֵ������������ҵ�����ͷŸ�buf���������ڵ��ý���ǰ�ͷŸ�buf����ע�⡿ - * ҵ����Ҫ�Լ���msg_ctx�б��������Ϣ�������ͨ��malloc�����ڴ桿 - * @return 0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��, - * -4 ����ʧ��, -5 ��ⱨ��ʧ��, -6 ���տռ䲻��, -7 ��������ر�����, -10 ������Ч - */ int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, int& recv_pkg_size, int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx, bool keep_rcv_buf) { - if(!dst || !pkg || len<1) - { + if(!dst || !pkg || len<1) + { MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], len[%d], fun[%p]", dst, pkg, len, check_func); - return -10; - } + return -10; + } int ret = 0, rc = 0; @@ -1110,7 +857,6 @@ int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, i utime64_t cost_time = 0; int time_left = timeout; - // 1. ��ȡTCP���ӳض���, �ҽ�֪ͨ���� int sock = -1; TcpKeepConn* conn = mt_tcp_get_keep_conn(dst, sock); if ((conn == NULL) || (sock < 0)) @@ -1120,7 +866,6 @@ int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, i goto EXIT_LABEL; } - // 2. ���Լ����½����� rc = MtFrame::connect(sock, (struct sockaddr *)dst, addr_len, time_left); if (rc < 0) { @@ -1129,7 +874,6 @@ int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, i goto EXIT_LABEL; } - // 3. �������ݴ��� cost_time = MtFrame::Instance()->GetLastClock() - start_ms; time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0; rc = MtFrame::send(sock, pkg, len, 0, time_left); @@ -1140,7 +884,6 @@ int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, i goto EXIT_LABEL; } - // 4. �������ݴ��� cost_time = MtFrame::Instance()->GetLastClock() - start_ms; time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0; @@ -1155,8 +898,6 @@ int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, i ret = 0; EXIT_LABEL: - - // ʧ����ǿ���ͷ�����, ����ʱ���� if (conn != NULL) { ConnectionMgr::Instance()->FreeConnection(conn, (ret < 0)); @@ -1165,24 +906,6 @@ EXIT_LABEL: return ret; } - -/** - * @brief TCP�������շ����� - * [ע��] tcp���շ���buff, ��������static����, ����������Ĵ��� [��Ҫ] - * [ע��] �Ľӿڣ���ע�ⲻҪ����ķ���ֵ������֤��mt_tcpsendrcv_ex����ֵƥ�� [��Ҫ] - * @param dst -�����͵�Ŀ�ĵ�ַ - * @param pkg -�������װ�İ��� - * @param len -�������װ�İ��峤�� - * @param rcv_buf -���������������ο����� keep_rcv_buf�� - * @param recv_pkg_size -����Ӧ�����buff�ij�ʼ��С����������0ʱ�����ر���ʵ�ʳ��ȡ� - * @param timeout -��ʱʱ��, ��λms - * @param check_func -��ⱨ���Ƿ�ɹ����ﺯ�� - * @param msg_ctx -�������ĵ������ı����� - * @param keep_rcv_buf -true,��ܽ�����rcv_buf��ֵ������������ҵ�����ͷŸ�buf���������ڵ��ý���ǰ�ͷŸ�buf����ע�⡿ - * ҵ����Ҫ�Լ���msg_ctx�б��������Ϣ�������ͨ��malloc�����ڴ桿 - * @return 0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��, - * -4 ����ʧ��, -5 ��ⱨ��ʧ��, -6 ���տռ䲻��, -7 ��������ر�����, -10 ������Ч - */ int mt_tcpsendrcv_short(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, int& recv_pkg_size, int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx, bool keep_rcv_buf) { @@ -1192,15 +915,13 @@ int mt_tcpsendrcv_short(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_ utime64_t cost_time = 0; int time_left = timeout; - // 1. ������� - if(!dst || !pkg || len<1) - { + if(!dst || !pkg || len<1) + { MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], len[%d], fun[%p]", dst, pkg, len, check_func); - return -10; - } + return -10; + } - // 2. ����TCP socket int sock; sock = mt_tcp_create_sock(); if (sock < 0) @@ -1209,7 +930,6 @@ int mt_tcpsendrcv_short(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_ return -1; } - // 3. ���Լ����½����� rc = MtFrame::connect(sock, (struct sockaddr *)dst, addr_len, time_left); if (rc < 0) { @@ -1218,7 +938,6 @@ int mt_tcpsendrcv_short(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_ goto EXIT_LABEL; } - // 4. �������ݴ��� cost_time = MtFrame::Instance()->GetLastClock() - start_ms; time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0; rc = MtFrame::send(sock, pkg, len, 0, time_left); @@ -1229,7 +948,6 @@ int mt_tcpsendrcv_short(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_ goto EXIT_LABEL; } - // 5. �������ݴ��� cost_time = MtFrame::Instance()->GetLastClock() - start_ms; time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0; rc = mt_tcp_check_recv(sock, rcv_buf, recv_pkg_size, 0, time_left, check_func, msg_ctx, keep_rcv_buf); @@ -1250,62 +968,34 @@ EXIT_LABEL: return ret; } - - -/** - * @brief TCP�շ��ӿڣ�����ѡ���˱������ӻ��߶����� - * [ע��] tcp���շ���buff, ��������static����, ����������Ĵ��� [��Ҫ] - * @param dst -�����͵�Ŀ�ĵ�ַ - * @param pkg -�������װ�İ��� - * @param len -�������װ�İ��峤�� - * @param rcv_buf -���������������ο����� keep_rcv_buf�� - * @param recv_pkg_size -����Ӧ�����buff�ij�ʼ��С����������0ʱ�����ر���ʵ�ʳ��ȡ� - * @param timeout -��ʱʱ��, ��λms - * @param check_func -��ⱨ���Ƿ�ɹ����ﺯ�� - * @param msg_ctx -�������ĵ������ı����� - * - * @param type - �������� - * MT_TCP_SHORT: һ��һ�������ӣ� - * MT_TCP_LONG : һ��һ�ճ����ӣ� - * MT_TCP_LONG_SNDONLY : ֻ�����ճ����ӣ� - * MT_TCP_SHORT_SNDONLY: ֻ�����ն����ӣ� - * @param keep_rcv_buf -true,��ܽ�����rcv_buf��ֵ������������ҵ�����ͷŸ�buf���������ڵ��ý���ǰ�ͷŸ�buf����ע�⡿ - * ҵ����Ҫ�Լ���msg_ctx�б��������Ϣ�������ͨ��malloc�����ڴ桿 - * @return 0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��, - * -4 ����ʧ��, -5 ��ⱨ��ʧ��, -6 ���տռ䲻��, -7 ��������ر�����, -10 ������Ч - */ int mt_tcpsendrcv_ex(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, int& rcv_pkg_size, int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx, MT_TCP_CONN_TYPE type, bool keep_rcv_buf) { - if(!dst || !pkg || len<1) - { + if(!dst || !pkg || len<1) + { MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], len[%d], fun[%p], msg_ctx[%p], type[%d]", dst, pkg, len, check_func, msg_ctx, type); - return -10; - } + return -10; + } switch (type) { - // TCP�����ӵ������� case MT_TCP_LONG: { return mt_tcpsendrcv(dst, pkg, len, rcv_buf, rcv_pkg_size, timeout, check_func, msg_ctx, keep_rcv_buf); } - // TCP������ֻ������ case MT_TCP_LONG_SNDONLY: { return mt_tcpsend(dst, pkg, len, timeout); } - // TCP�����ӵ������� case MT_TCP_SHORT: { return mt_tcpsendrcv_short(dst, pkg, len, rcv_buf, rcv_pkg_size, timeout, check_func, msg_ctx, keep_rcv_buf); } - // TCP������ֻ������ case MT_TCP_SHORT_SNDONLY: { return mt_tcpsend_short(dst, pkg, len, timeout); @@ -1313,7 +1003,7 @@ int mt_tcpsendrcv_ex(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf default: { - MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], len[%d], fun[%p], msg_ctx[%p], type[%d]", + MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], len[%d], fun[%p], msg_ctx[%p], type[%d]", dst, pkg, len, check_func, msg_ctx, type); return -10; } @@ -1322,8 +1012,4 @@ int mt_tcpsendrcv_ex(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf return 0; } - - } - - diff --git a/app/micro_thread/mt_api.h b/app/micro_thread/mt_api.h index cdf61c794..fe1e14a57 100644 --- a/app/micro_thread/mt_api.h +++ b/app/micro_thread/mt_api.h @@ -19,7 +19,6 @@ /** * @filename mt_api.h - * @info �̷߳�װϵͳapi, ͬ�������߳�API��ʵ���첽���� */ #ifndef __MT_API_H__ @@ -32,377 +31,109 @@ using std::vector; namespace NS_MICRO_THREAD { -/******************************************************************************/ -/* �߳��û��ӿڶ���: UDP�������շ��ӿ� */ -/******************************************************************************/ - -/** - * @brief ��������˿ڵ�socket�շ��ӿ�, ��socket������������, ҵ������֤������ - * [ע��] UDP����buff, ���Ƽ�static����, ��һ�����յ��������Ĵ���[��Ҫ] - * @param dst -�����͵�Ŀ�ĵ�ַ - * @param pkg -�������װ�İ��� - * @param len -�������װ�İ��峤�� - * @param rcv_buf -����Ӧ�����buff - * @param buf_size -modify-����Ӧ�����buff��С, �ɹ�����ʱ, ��ΪӦ������� - * @param timeout -��ʱʱ��, ��λms - * @return 0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��, �ɴ�ӡerrno - */ int mt_udpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, int timeout); - -/******************************************************************************/ -/* �߳��û��ӿڶ���: TCP���ӳ��շ��ӿ� */ -/******************************************************************************/ - -/** - * @brief TCP��ⱨ���Ƿ���������Ļص��������� - * @param buf ���ı��滺���� - * @param len �Ѿ����յij��� - * @return >0 ʵ�ʵı��ij���; 0 ����Ҫ�ȴ�����; <0 �����쳣 - */ typedef int (*MtFuncTcpMsgLen)(void* buf, int len); -/** - * @brief TCP��������ӳصķ�ʽ����IP/PORT����, ���ӱ���Ĭ��10���� - * [ע��] tcp���շ���buff, ��������static����, ����������Ĵ��� [��Ҫ] - * @param dst -�����͵�Ŀ�ĵ�ַ - * @param pkg -�������װ�İ��� - * @param len -�������װ�İ��峤�� - * @param rcv_buf -����Ӧ�����buff - * @param buf_size -modify-����Ӧ�����buff��С, �ɹ�����ʱ, ��ΪӦ������� - * @param timeout -��ʱʱ��, ��λms - * @param check_func -��ⱨ���Ƿ�ɹ����ﺯ�� - * @return 0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��, - * -4 ����ʧ��, -5 ��ⱨ��ʧ��, -6 ���տռ䲻��, -7 ��������ر����ӣ�-10 ������Ч - */ int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, int timeout, MtFuncTcpMsgLen chek_func); enum MT_TCP_CONN_TYPE { - MT_TCP_SHORT = 1, /// ������ - MT_TCP_LONG = 2, /// ������ - MT_TCP_SHORT_SNDONLY = 3, /// ������ֻ�� - MT_TCP_LONG_SNDONLY = 4, /// ������ֻ�� + MT_TCP_SHORT = 1, + MT_TCP_LONG = 2, + MT_TCP_SHORT_SNDONLY = 3, + MT_TCP_LONG_SNDONLY = 4, MT_TCP_BUTT }; -/** - * @brief TCP�շ��ӿڣ�����ѡ���˱������ӻ��߶����� - * [ע��] tcp���շ���buff, ��������static����, ����������Ĵ��� [��Ҫ] - * @param dst -�����͵�Ŀ�ĵ�ַ - * @param pkg -�������װ�İ��� - * @param len -�������װ�İ��峤�� - * @param rcv_buf -����Ӧ�����buff��ֻ�����տ�������ΪNULL - * @param buf_size -modify-����Ӧ�����buff��С, �ɹ�����ʱ, ��ΪӦ������ȣ�ֻ�����գ�����ΪNULL - * @param timeout -��ʱʱ��, ��λms - * @param check_func -��ⱨ���Ƿ�ɹ����ﺯ�� - * @param type - �������� - * MT_TCP_SHORT: һ��һ�������ӣ� - * MT_TCP_LONG : һ��һ�ճ����ӣ� - * MT_TCP_LONG_SNDONLY : ֻ�����ճ����ӣ� - * MT_TCP_SHORT_SNDONLY: ֻ�����ն����ӣ� - * @return 0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��, - * -4 ����ʧ��, -5 ��ⱨ��ʧ��, -6 ���տռ䲻��, -7 ��������ر�����, -10 ������Ч - */ int mt_tcpsendrcv_ex(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int* buf_size, int timeout, MtFuncTcpMsgLen func, MT_TCP_CONN_TYPE type = MT_TCP_LONG); - /** - * @brief TCP��ⱨ���Ƿ���������Ļص����� - * @param buf ���ı��滺���� - * @param len �Ѿ����յij��� - * @param closed Զ�˹ر����ӣ�����ͨ�����ӹر���ȷ�����ij��ȵ�ҵ�� - * @param msg_ctx �������ĵ������ı�����ͨ����ֵ��ҵ�����ʵ�ֱ��Ľ���״̬���ȣ������ظ���� - * ҵ����mt_tcpsendrcv�д���ò�������ܻص�ʱ���ݸ�ҵ�ñ�������Ϊ����߽ṹ�壬ҵ���Զ��塣 - * ���Ľ�������������ڱ����У�mt_tcpsendrcv���غ�ֱ��ʹ�� - * ��ע�⡿ �ûص����������bufָ����ܱ仯�����Ա��Ľ���״̬�����λ����Ϣ��ʹ��ƫ���� - * @param msg_len_detected �������� ���ҵ��ȷ���˱��ij��ȣ���������Ϊtrue������Ϊfalse������ʱΪfalse�� - * ��ע�⡿�����ֵ����true������Ժ�������ֵ��Ϊ���ij��ȣ�>0�������հ������հ����ǰ�����ٵ��ø�MtFuncTcpMsgChecker���������Ľ�����ɺ���MtFuncTcpMsgChecker�������һ�α��ġ������������հ��������Ч���á� - * @return >0 ����Ľ��ջ����С; 0 ����Ҫ�ȴ�����; <0 �����쳣 - * ��ע�⡿ - * ��������������Է��ش���0: - * 1��ҵ��ȷ�����ij���,���ر��ij��� - * 2��ҵ��ȷ�����ij��ȣ���������buf�Ľ��鳤�ȣ�������ȴ�������buf���ȣ���ܸ��ݸ�ֵrealloc buf�� - * - * ����ֵΪ0����ʾҵ����ȷ�����ij��ȣ���Ҫ�������ա����buf��������ܽ�realloc buf��buf��С����һ�� - * ����ֵС��0����ʾҵ��ȷ�����Ĵ��� - */ typedef int (*MtFuncTcpMsgChecker)(void* buf, int len, bool closed, void* msg_ctx, bool &msg_len_detected); - -/** - * @brief TCP�շ��ӿڣ�����ѡ���˱������ӻ��߶����� - * [ע��] tcp���շ���buff, ��������static����, ����������Ĵ��� [��Ҫ] - * @param dst -�����͵�Ŀ�ĵ�ַ - * @param pkg -�������װ�İ��� - * @param len -�������װ�İ��峤�� - * @param rcv_buf -���������������ο����� keep_rcv_buf��ҵ����Ҫmalloc���ڴ档void* rcv_buf=NULL: ������ܼ��� - * @param recv_pkg_size -����Ӧ�����buff�ij�ʼ��С����������0ʱ�����ر���ʵ�ʳ��ȡ� - * @param timeout -��ʱʱ��, ��λms - * @param check_func -��ⱨ���Ƿ�ɹ����ﺯ�� - * @param msg_ctx -�������ĵ������ı����� - * - * @param type - �������� - * MT_TCP_SHORT: һ��һ�������ӣ� - * MT_TCP_LONG : һ��һ�ճ����ӣ� - * MT_TCP_LONG_SNDONLY : ֻ�����ճ����ӣ� - * MT_TCP_SHORT_SNDONLY: ֻ�����ն����ӣ� - * @param keep_rcv_buf -true,���óɹ���ܽ�����rcv_buf��ֵ������������ҵ�����ͷŸ�buf���������ڵ��ý���ǰ�ͷŸ�buf����ע�⡿ - * ҵ����Ҫ�Լ���msg_ctx�б��������Ϣ�������ͨ��malloc�����ڴ桿 Ĭ�Ͽ���Զ��ͷŸ�buf��ҵ����Ҫ��ctx������Ϣ - * @return 0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��, - * -4 ����ʧ��, -5 ��ⱨ��ʧ��, -6 ���տռ䲻��, -7 ��������ر�����, -10 ������Ч, -11,�������bufʧ�� - */ int mt_tcpsendrcv_ex(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, int& recv_pkg_size, int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx=NULL, MT_TCP_CONN_TYPE type = MT_TCP_LONG, bool keep_rcv_buf=false); -/** - * @brief TCP��������ӳصķ�ʽ����IP/PORT����, ���ӱ���Ĭ��10���� - * [ע��] tcp���շ���buff, ��������static����, ����������Ĵ��� [��Ҫ] - * @param dst -�����͵�Ŀ�ĵ�ַ - * @param pkg -�������װ�İ��� - * @param len -�������װ�İ��峤�� - * @param rcv_buf -���������������ο����� keep_rcv_buf�� - * @param recv_pkg_size -����Ӧ�����buff�ij�ʼ��С����������0ʱ�����ر���ʵ�ʳ��ȡ� - * @param timeout -��ʱʱ��, ��λms - * @param check_func -��ⱨ���Ƿ�ɹ����ﺯ�� - * @param msg_ctx -�������ĵ������ı����� - * @param keep_rcv_buf -true,��ܽ�����rcv_buf��ֵ������������ҵ�����ͷŸ�buf���������ڵ��ý���ǰ�ͷŸ�buf����ע�⡿ - * ҵ����Ҫ�Լ���msg_ctx�б��������Ϣ�������ͨ��malloc�����ڴ桿 - * @return 0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��, - * -4 ����ʧ��, -5 ��ⱨ��ʧ��, -6 ���տռ䲻��, -7 ��������ر�����, -10 ������Ч - */ + int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, int& recv_pkg_size, int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx=NULL, bool keep_rcv_buf=false); -/******************************************************************************/ -/* �߳��û��ӿڶ���: �߳�Task��·����ģ�ͽӿڶ��� */ -/******************************************************************************/ - -/** - * @brief �߳������������ - */ class IMtTask { public: - /** - * @brief �߳�������Ĵ���������ں��� - * @return 0 -�ɹ�, < 0 ʧ�� - */ virtual int Process() { return -1; }; - /** - * @brief ����taskִ�н�� - * @info ��Process����ֵ - */ void SetResult(int rc) { _result = rc; } - /** - * @brief ��ȡtaskִ�н�� - * @info ��Process����ֵ - */ int GetResult(void) { return _result; } - /** - * @brief ����task���� - */ void SetTaskType(int type) { _type = type; } - /** - * @brief ��ȡtask���� - * @info ���ҵ���ж���task������ʹ�ø��ֶ����ֲ�ͬ��task���� - * @return ��ȡtask���� - */ int GetTaskType(void) { return _type; } - /** - * @brief �߳��������������� - */ IMtTask() {}; virtual ~IMtTask() {}; protected: - int _type; // task���ͣ���������task��ҵ������Զ������ͣ�����ӻ���ת�� - int _result; // taskִ�н������Process����ֵ + int _type; + int _result; }; typedef vector<IMtTask*> IMtTaskList; -/** - * @brief ��·IO����, Task-fork-waitģʽ�ӿ� - * @param req_list -task list ��װ����api��task�б� - * @return 0 �ɹ�, -1 �������߳�ʧ�� - */ int mt_exec_all_task(IMtTaskList& req_list); - -/******************************************************************************/ -/* �߳��û��ӿڶ���: �̷߳�װϵͳ�ӿ� */ -/******************************************************************************/ - -/** - * @brief �߳�����sleep�ӿ�, ��λms - * @info ҵ����Ҫ�����ó�CPUʱʹ�� - */ void mt_sleep(int ms); -/** - * @brief �̻߳�ȡϵͳʱ�䣬��λms - */ unsigned long long mt_time_ms(void); -/******************************************************************************/ -/* �߳��û��ӿڶ���: �߳��û�˽�����ݽӿ� */ -/******************************************************************************/ - -/** - * @brief ���õ�ǰIMtMsg��˽�б��� - * @info ֻ����ָ�룬�ڴ���Ҫҵ����� - */ void mt_set_msg_private(void *data); -/** - * @brief ��ȡ��ǰIMtMsg��˽�б��� - * @return ˽�б���ָ�� - */ void* mt_get_msg_private(); - -/******************************************************************************/ -/* �߳��û��ӿڶ���: �̷߳�װϵͳ�ӿ�(���Ƽ�ʹ��) */ -/******************************************************************************/ - -/** - * @brief �߳̿�ܳ�ʼ�� - * @info ҵ��ʹ��spp�������̣߳���Ҫ���øú�����ʼ����ܣ� - * ʹ��spp��ֱ�ӵ���SyncFrame�Ŀ�ܳ�ʼ���������� - * @return false:��ʼ��ʧ�� true:��ʼ���ɹ� - */ bool mt_init_frame(int argc=0, char * const argv[]=NULL); -/** - * @brief �����̶߳���ջ�ռ��С - * @info �DZ������ã�Ĭ�ϴ�СΪ128K - */ void mt_set_stack_size(unsigned int bytes); -/** - * @brief �̰߳�����ϵͳIO���� recvfrom - * @param fd ϵͳsocket��Ϣ - * @param buf ������Ϣ������ָ�� - * @param len ������Ϣ���������� - * @param from ��Դ��ַ��ָ�� - * @param fromlen ��Դ��ַ�Ľṹ���� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ճ���, <0 ʧ�� - */ int mt_recvfrom(int fd, void *buf, int len, int flags, struct sockaddr *from, socklen_t *fromlen, int timeout); -/** - * @brief �̰߳�����ϵͳIO���� sendto - * @param fd ϵͳsocket��Ϣ - * @param msg �����͵���Ϣָ�� - * @param len �����͵���Ϣ���� - * @param to Ŀ�ĵ�ַ��ָ�� - * @param tolen Ŀ�ĵ�ַ�Ľṹ���� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ͳ���, <0 ʧ�� - */ int mt_sendto(int fd, const void *msg, int len, int flags, const struct sockaddr *to, int tolen, int timeout); - -/** - * @brief �̰߳�����ϵͳIO���� connect - * @param fd ϵͳsocket��Ϣ - * @param addr ָ��server��Ŀ�ĵ�ַ - * @param addrlen ��ַ�ij��� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ͳ���, <0 ʧ�� - */ int mt_connect(int fd, const struct sockaddr *addr, int addrlen, int timeout); -/** - * @brief �̰߳�����ϵͳIO���� accept - * @param fd �������� - * @param addr �ͻ��˵�ַ - * @param addrlen ��ַ�ij��� - * @param timeout ��ȴ�ʱ��, ���� - * @return >=0 accept��socket������, <0 ʧ�� - */ int mt_accept(int fd, struct sockaddr *addr, socklen_t *addrlen, int timeout); -/** - * @brief �̰߳�����ϵͳIO���� read - * @param fd ϵͳsocket��Ϣ - * @param buf ������Ϣ������ָ�� - * @param nbyte ������Ϣ���������� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ճ���, <0 ʧ�� - */ ssize_t mt_read(int fd, void *buf, size_t nbyte, int timeout); -/** - * @brief �̰߳�����ϵͳIO���� write - * @param fd ϵͳsocket��Ϣ - * @param buf �����͵���Ϣָ�� - * @param nbyte �����͵���Ϣ���� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ͳ���, <0 ʧ�� - */ ssize_t mt_write(int fd, const void *buf, size_t nbyte, int timeout); -/** - * @brief �̰߳�����ϵͳIO���� recv - * @param fd ϵͳsocket��Ϣ - * @param buf ������Ϣ������ָ�� - * @param len ������Ϣ���������� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ճ���, <0 ʧ�� - */ ssize_t mt_recv(int fd, void *buf, int len, int flags, int timeout); -/** - * @brief �̰߳�����ϵͳIO���� send - * @param fd ϵͳsocket��Ϣ - * @param buf �����͵���Ϣָ�� - * @param nbyte �����͵���Ϣ���� - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 �ɹ����ͳ���, <0 ʧ�� - */ ssize_t mt_send(int fd, const void *buf, size_t nbyte, int flags, int timeout); - -/** - * @brief �̵߳ȴ�epoll�¼��İ������� - * @param fd ϵͳsocket��Ϣ - * @param events �ȴ����¼� IN/OUT - * @param timeout ��ȴ�ʱ��, ���� - * @return >0 ������¼�, <0 ʧ�� - */ int mt_wait_events(int fd, int events, int timeout); - void* mt_start_thread(void* entry, void* args); - } #endif diff --git a/app/micro_thread/mt_cache.cpp b/app/micro_thread/mt_cache.cpp index 398a7e6e7..6a206dffc 100644 --- a/app/micro_thread/mt_cache.cpp +++ b/app/micro_thread/mt_cache.cpp @@ -19,7 +19,6 @@ /** * @filename mt_cache.cpp - * @info TCP����buffer����ʵ�� */ #include <stdlib.h> @@ -37,12 +36,6 @@ namespace NS_MICRO_THREAD { - -/** - * @brief Buffer�������� - * @param size ʵ�ʵ�buff��С - * @return ��NULL block��, ����ʧ�� - */ TSkBuffer* new_sk_buffer(uint32_t size) { uint32_t total = sizeof(TSkBuffer) + size; @@ -65,11 +58,6 @@ TSkBuffer* new_sk_buffer(uint32_t size) return block; } - -/** - * @brief Buffer�ͷŲ��� - * @param block -buff�� - */ void delete_sk_buffer(TSkBuffer* block) { if (NULL == block) { @@ -79,13 +67,6 @@ void delete_sk_buffer(TSkBuffer* block) free(block); } - -/** - * @brief ����������Ϣ(����Դ�ػ���buff,����չ) - * @param buff -���е�buffָ�� - * @param size -��Ҫ��չ�����ճ��ȴ�С - * @return ʵ�ʵ�buff��Ϣ - */ TSkBuffer* reserve_sk_buffer(TSkBuffer* buff, uint32_t size) { if (NULL == buff) { @@ -107,13 +88,6 @@ TSkBuffer* reserve_sk_buffer(TSkBuffer* buff, uint32_t size) return new_buff; } - -/** - * @brief cache �صij�ʼ���ӿ� - * @param mng -�����ص�ָ�� - * @param expired -�����ʱ��, ��λ�� - * @param size -��������Ĭ�����ɵĿ��С - */ void sk_buffer_mng_init(TSkBuffMng* mng, uint32_t expired, uint32_t size) { TAILQ_INIT(&mng->free_list); @@ -122,10 +96,6 @@ void sk_buffer_mng_init(TSkBuffMng* mng, uint32_t expired, uint32_t size) mng->size = size; } -/** - * @brief cache �ص����ٽӿ� - * @param mng -�����ص�ָ�� - */ void sk_buffer_mng_destroy(TSkBuffMng * mng) { TSkBuffer* item = NULL; @@ -138,12 +108,6 @@ void sk_buffer_mng_destroy(TSkBuffMng * mng) mng->count = 0; } - -/** - * @brief �������һ��buff - * @param mng -�����ص�ָ�� - * @return ��NULLΪ�ɹ���ȡ��buff��ָ�� - */ TSkBuffer* alloc_sk_buffer(TSkBuffMng* mng) { if (NULL == mng) { @@ -167,12 +131,6 @@ TSkBuffer* alloc_sk_buffer(TSkBuffMng* mng) return item; } - -/** - * @brief �ͷ�ָ����buff�� - * @param mng -�����ص�ָ�� - * @param buff -���ͷŵ�buffָ�� - */ void free_sk_buffer(TSkBuffMng* mng, TSkBuffer* buff) { if ((NULL == mng) || (NULL == buff)) { @@ -187,12 +145,6 @@ void free_sk_buffer(TSkBuffMng* mng, TSkBuffer* buff) buff->data_len = 0; } - -/** - * @brief ���չ��ڵ�buff�� - * @param mng -�����ص�ָ�� - * @param now -��ǰ��ʱ��, �뼶�� - */ void recycle_sk_buffer(TSkBuffMng* mng, uint32_t now) { TSkBuffer* item = NULL; @@ -210,12 +162,6 @@ void recycle_sk_buffer(TSkBuffMng* mng, uint32_t now) } } - -/** - * @brief Cache��������ʼ�� - * @param cache -������ָ�� - * @param pool -buff��ָ�� - */ void rw_cache_init(TRWCache* cache, TSkBuffMng* pool) { TAILQ_INIT(&cache->list); @@ -224,10 +170,6 @@ void rw_cache_init(TRWCache* cache, TSkBuffMng* pool) cache->pool = pool; } -/** - * @brief Cache���������� - * @param cache -������ָ�� - */ void rw_cache_destroy(TRWCache* cache) { if ((cache == NULL) || (cache->pool == NULL)) { @@ -246,14 +188,6 @@ void rw_cache_destroy(TRWCache* cache) cache->pool = NULL; } - -/** - * @brief Cacheɾ��������ָ���������� - * @param cache -������ָ�� - * @param buff -���buff��ָ�� - * @param len -��ɾ���ij��� - * @return ʵ�ʿ������� - */ uint32_t cache_copy_out(TRWCache* cache, void* buff, uint32_t len) { if ((cache == NULL) || (cache->pool == NULL)) { @@ -266,7 +200,6 @@ uint32_t cache_copy_out(TRWCache* cache, void* buff, uint32_t len) TSkBuffer* tmp = NULL; TAILQ_FOREACH_SAFE(item, &cache->list, entry, tmp) { - // 1. ȷ�Ͽ������ݴ�С skip_len = (item->data_len > left) ? left : item->data_len; if (out_buff != NULL) { @@ -282,21 +215,18 @@ uint32_t cache_copy_out(TRWCache* cache, void* buff, uint32_t len) break; } - // 2. �Ƴ�һ��block if (cache->count > 0) { cache->count--; } TAILQ_REMOVE(&cache->list, item, entry); free_sk_buffer(cache->pool, item); - // 3. ѭ���������� if (left == 0) { break; } } - // ���忼�����ݳ�������, �Ƿ����㹻�������Ƴ� skip_len = len - left; if (cache->len > skip_len) { @@ -310,22 +240,11 @@ uint32_t cache_copy_out(TRWCache* cache, void* buff, uint32_t len) return skip_len; } - -/** - * @brief Cacheɾ����ָ���������� - * @param cache -������ָ�� - * @param len -��ɾ���ij��� - */ void cache_skip_data(TRWCache* cache, uint32_t len) { cache_copy_out(cache, NULL, len); } -/** - * @brief Cache��ָ���������� - * @param cache -������ָ�� - * @param buff -���ӵĿ�ָ�� - */ void cache_append_buffer(TRWCache* cache, TSkBuffer* buff) { if ((NULL == cache) || (NULL == buff)) @@ -338,10 +257,6 @@ void cache_append_buffer(TRWCache* cache, TSkBuffer* buff) cache->count++; } -/** - * @brief Cache�Ƴ���һ���ڴ�, ����free - * @param cache -������ָ�� - */ TSkBuffer* cache_skip_first_buffer(TRWCache* cache) { TSkBuffer* buff = TAILQ_FIRST(&cache->list); @@ -364,13 +279,6 @@ TSkBuffer* cache_skip_first_buffer(TRWCache* cache) return buff; } - -/** - * @brief Cache��ָ���������� - * @param cache -������ָ�� - * @param data -���ӵ�ָ�� - * @param len -���ӵij��� - */ int32_t cache_append_data(TRWCache* cache, const void* data, uint32_t len) { if ((NULL == data) || (NULL == cache) || (NULL == cache->pool)) @@ -386,7 +294,6 @@ int32_t cache_append_data(TRWCache* cache, const void* data, uint32_t len) uint32_t left = len; uint32_t remain = 0; - // 1. β�ռ��Ƚ���append, ��Ϊ��Ҫ�ع�, ǰһ�����Ȳ����� TSkBuffer* tail = TAILQ_LAST(&cache->list, __sk_buff_list); if (tail != NULL) { @@ -403,8 +310,7 @@ int32_t cache_append_data(TRWCache* cache, const void* data, uint32_t len) return (int32_t)len; } } - - // 2. ��ʣ��buff������, ������ʣ���buff, ����β�ڵ� + TRWCache keep_list; rw_cache_init(&keep_list, cache->pool); left -= remain; @@ -429,8 +335,7 @@ int32_t cache_append_data(TRWCache* cache, const void* data, uint32_t len) item->data_len = item->size; left -= item->size; } - - // 3. ���Կ��������buff, ���ﲻ��ع��� + if ((tail != NULL) && (remain > 0)) { memcpy(tail->data + tail->data_len, data, remain); @@ -444,14 +349,6 @@ int32_t cache_append_data(TRWCache* cache, const void* data, uint32_t len) return (int32_t)len; } - -/** - * @brief Cache���ϵ�UDP�ձ��ӿ�, �����ڴ�Ƚ϶�, ������32λʹ�� - * @param cache -������ָ�� - * @param fd - ���ձ���fd��� - * @param remote_addr -�Զ�ip��ַ - * @return ʵ�ʽ��ճ��� - */ int32_t cache_udp_recv(TRWCache* cache, uint32_t fd, struct sockaddr_in* remote_addr) { if (NULL == cache) @@ -494,13 +391,6 @@ int32_t cache_udp_recv(TRWCache* cache, uint32_t fd, struct sockaddr_in* remote_ return total; } - -/** - * @brief Cache���ϵ�TCP�ձ��ӿ� - * @param cache -������ָ�� - * @param fd - ���ձ���fd��� - * @return ʵ�ʽ��ճ��� - */ int32_t cache_tcp_recv(TRWCache* cache, uint32_t fd) { if (NULL == cache) @@ -511,7 +401,6 @@ int32_t cache_tcp_recv(TRWCache* cache, uint32_t fd) int32_t total = 0; for (uint32_t i = 0; i < 100; i++) { - // 1. ÿ�μ��β�ռ�, �ռ������ʼ״̬, �����¿ռ� TSkBuffer* item = TAILQ_LAST(&cache->list, __sk_buff_list); if ((NULL == item) || ((item->data_len + item->data) >= item->end)) @@ -524,7 +413,6 @@ int32_t cache_tcp_recv(TRWCache* cache, uint32_t fd) cache_append_buffer(cache, item); } - // 2. ����������size��С, Ĭ��64K uint8_t* buff = item->data + item->data_len; uint32_t remain = item->end - item->data - item->data_len; mt_hook_syscall(recv); @@ -551,7 +439,7 @@ int32_t cache_tcp_recv(TRWCache* cache, uint32_t fd) item->data_len += recvd_len; cache->len += recvd_len; total += recvd_len; - if (recvd_len < (int32_t)remain) // �ղ���, ����Ϊ����OK + if (recvd_len < (int32_t)remain) { return total; } @@ -561,12 +449,6 @@ int32_t cache_tcp_recv(TRWCache* cache, uint32_t fd) return total; } -/** - * @brief Cache���ϵ�TCP���ͽӿ� - * @param cache -������ָ�� - * @param fd - ��������fd��� - * @return ʵ�ʷ��ͳ��� - */ int32_t cache_tcp_send(TRWCache* cache, uint32_t fd) { if ((NULL == cache) || (NULL == cache->pool)) @@ -578,8 +460,7 @@ int32_t cache_tcp_send(TRWCache* cache, uint32_t fd) { return 0; } - - + int32_t ret = 0, total = 0; TSkBuffer* item = NULL; TSkBuffer* tmp = NULL; @@ -612,15 +493,6 @@ int32_t cache_tcp_send(TRWCache* cache, uint32_t fd) return total; } - -/** - * @brief Cache���ϵ�TCP���ͽӿ�, δʹ��IOVEC - * @param cache -������ָ�� - * @param fd - ��������fd��� - * @param data -������cache��, �������͵�buff - * @param len -�������͵�buff���� - * @return ʵ�ʷ��ͳ��� - */ int32_t cache_tcp_send_buff(TRWCache* cache, uint32_t fd, const void* data, uint32_t len) { if ((NULL == cache) || (NULL == data)) @@ -628,7 +500,6 @@ int32_t cache_tcp_send_buff(TRWCache* cache, uint32_t fd, const void* data, uint return -1; } - // 1. ���ȷ���CACHE���� int32_t ret = cache_tcp_send(cache, fd); if (ret < 0) { @@ -636,7 +507,6 @@ int32_t cache_tcp_send_buff(TRWCache* cache, uint32_t fd, const void* data, uint return ret; } - // 2. CACHE�Ѿ������� int32_t send_len = 0; if (cache->len == 0) { @@ -666,12 +536,6 @@ int32_t cache_tcp_send_buff(TRWCache* cache, uint32_t fd, const void* data, uint return send_len; } - -/** - * @brief ��ȡcache��Ч�����ܳ��� - * @param multi -������ָ�� - * @return ʵ����Ч���ݳ��� - */ uint32_t get_data_len(TBuffVecPtr multi) { TRWCache* cache = (TRWCache*)multi; @@ -682,11 +546,6 @@ uint32_t get_data_len(TBuffVecPtr multi) } } -/** - * @brief ��ȡcache��Ч���ݿ���� - * @param multi -������ָ�� - * @return ʵ����Ч���ݿ���� - */ uint32_t get_block_count(TBuffVecPtr multi) { TRWCache* cache = (TRWCache*)multi; @@ -697,11 +556,6 @@ uint32_t get_block_count(TBuffVecPtr multi) } } -/** - * @brief ��ȡcache�ĵ�һ������ָ�� - * @param multi -������ָ�� - * @return ��һ������ָ�� - */ TBuffBlockPtr get_first_block(TBuffVecPtr multi) { TRWCache* cache = (TRWCache*)multi; @@ -712,12 +566,6 @@ TBuffBlockPtr get_first_block(TBuffVecPtr multi) } } -/** - * @brief ��ȡcache����һ������ָ�� - * @param multi -������ָ�� - * @param block -��ǰ��ָ�� - * @return ��һ������ָ�� - */ TBuffBlockPtr get_next_block(TBuffVecPtr multi, TBuffBlockPtr block) { TRWCache* cache = (TRWCache*)multi; @@ -731,12 +579,6 @@ TBuffBlockPtr get_next_block(TBuffVecPtr multi, TBuffBlockPtr block) } -/** - * @brief ��ȡ���ݿ��ָ�������ݳ��� - * @param block -��ǰ��ָ�� - * @param data -����ָ��-modify���� - * @param len -����ָ�� modify���� - */ void get_block_data(TBuffBlockPtr block, const void** data, int32_t* len) { TSkBuffer* item = (TSkBuffer*)block; @@ -756,14 +598,6 @@ void get_block_data(TBuffBlockPtr block, const void** data, int32_t* len) } } - -/** - * @brief ��ȡ���ݿ��ָ�������ݳ��� - * @param multi -������ָ�� - * @param data -����д������ָ�� - * @param len -���� - * @return ���ݶ�ȡ�����ݳ��� - */ uint32_t read_cache_data(TBuffVecPtr multi, void* data, uint32_t len) { TRWCache* cache = (TRWCache*)multi; @@ -803,15 +637,6 @@ uint32_t read_cache_data(TBuffVecPtr multi, void* data, uint32_t len) return offset; } - - -/** - * @brief ��ȡ���ݿ��ָ�������ݳ��� - * @param multi -������ָ�� - * @param data -����д������ָ�� - * @param len -���� - * @return ���ݶ�ȡ�����ݳ��� - */ uint32_t read_cache_begin(TBuffVecPtr multi, uint32_t begin, void* data, uint32_t len) { TRWCache* cache = (TRWCache*)multi; @@ -829,7 +654,6 @@ uint32_t read_cache_begin(TBuffVecPtr multi, uint32_t begin, void* data, uint32_ TSkBuffer* item = NULL; TAILQ_FOREACH(item, &cache->list, entry) { - // 1. ��ʼλ����ʣ��, �������ò��� uint8_t* start_ptr = item->data; uint32_t real_left = item->data_len; if (pos_left > 0) @@ -840,13 +664,11 @@ uint32_t read_cache_begin(TBuffVecPtr multi, uint32_t begin, void* data, uint32_ start_ptr += skip_len; } - // 2. ����������ʣ��, ��ȴ���һ�� if (real_left == 0) { continue; } - // 3. ��ʣ��, ����������� uint32_t copy_len = copy_left > real_left ? real_left : copy_left; if (data != NULL) { @@ -863,7 +685,4 @@ uint32_t read_cache_begin(TBuffVecPtr multi, uint32_t begin, void* data, uint32_ return offset; } - - }; - diff --git a/app/micro_thread/mt_cache.h b/app/micro_thread/mt_cache.h index f71a55d76..9e4d8c23a 100644 --- a/app/micro_thread/mt_cache.h +++ b/app/micro_thread/mt_cache.h @@ -19,7 +19,6 @@ /** * @filename mt_cache.h - * @info TCP����buffer�������� */ #ifndef ___MT_BUFFER_CACHE_H @@ -31,268 +30,99 @@ namespace NS_MICRO_THREAD { - -// Ĭ�ϵ�buff��С #define SK_DFLT_BUFF_SIZE 64*1024 #define SK_DFLT_ALIGN_SIZE 8 #define SK_ERR_NEED_CLOSE 10000 -/** - * @brief �û�̬ buffer �ṹ���� - */ + typedef struct _sk_buffer_tag { - TAILQ_ENTRY(_sk_buffer_tag) entry; // list entry buffer LRU�� - uint32_t last_time; // �ϴ�ʹ��ʱ��� - uint32_t size; // buffer�ڵ�Ŀռ��С - uint8_t* head; // buff������ͷָ�� - uint8_t* end; // buff����������ָ�� - uint8_t* data; // ��Ч���ݵ�ͷָ�� - uint32_t data_len; // ��Ч�����ݳ��� - uint8_t buff[0]; // ԭʼָ������ + TAILQ_ENTRY(_sk_buffer_tag) entry; + uint32_t last_time; + uint32_t size; + uint8_t* head; + uint8_t* end; + uint8_t* data; + uint32_t data_len; + uint8_t buff[0]; } TSkBuffer; -typedef TAILQ_HEAD(__sk_buff_list, _sk_buffer_tag) TSkBuffList; // multi ����������� +typedef TAILQ_HEAD(__sk_buff_list, _sk_buffer_tag) TSkBuffList; -/** - * @brief ����ָ����С��buff�� - * @param size ��Ч��������С - * @return ��NULLΪ�ɹ����ص�buffָ�� - */ TSkBuffer* new_sk_buffer(uint32_t size = SK_DFLT_BUFF_SIZE); -/** - * @brief �ͷ�ָ����buff�� - * @param ���ͷŵ�buffָ�� - */ void delete_sk_buffer(TSkBuffer* buff); - -/** - * @brief ����������Ϣ(����Դ�ػ���buff,����չ) - * @param buff -���е�buffָ�� - * @param size -��Ҫ��չ�����ճ��ȴ�С - * @return ʵ�ʵ�buff��Ϣ - */ TSkBuffer* reserve_sk_buffer(TSkBuffer* buff, uint32_t size); - -/** - * @brief buffer cache ���� - */ typedef struct _sk_buff_mng_tag { - TSkBuffList free_list; // buff���� - uint32_t expired; // ��ʱʱ�� - uint32_t size; // buff��С - uint32_t count; // ����� + TSkBuffList free_list; + uint32_t expired; + uint32_t size; + uint32_t count; } TSkBuffMng; - -/** - * @brief cache �صij�ʼ���ӿ� - * @param mng -�����ص�ָ�� - * @param expired -�����ʱ��, ��λ�� - * @param size -��������Ĭ�����ɵĿ��С - */ void sk_buffer_mng_init(TSkBuffMng* mng, uint32_t expired, uint32_t size = SK_DFLT_BUFF_SIZE); -/** - * @brief cache �ص����ٽӿ� - * @param mng -�����ص�ָ�� - */ void sk_buffer_mng_destroy(TSkBuffMng * mng); - -/** - * @brief �������һ��buff - * @param mng -�����ص�ָ�� - * @return ��NULLΪ�ɹ���ȡ��buff��ָ�� - */ TSkBuffer* alloc_sk_buffer(TSkBuffMng* mng); -/** - * @brief �ͷ�ָ����buff�� - * @param mng -�����ص�ָ�� - * @param buff -���ͷŵ�buffָ�� - */ void free_sk_buffer(TSkBuffMng* mng, TSkBuffer* buff); -/** - * @brief ���չ��ڵ�buff�� - * @param mng -�����ص�ָ�� - * @param now -��ǰ��ʱ��, �뼶�� - */ void recycle_sk_buffer(TSkBuffMng* mng, uint32_t now); - -/** - * @brief ԭʼ�� buffer cache ���� - */ typedef struct _sk_rw_cache_tag { - TSkBuffList list; // buff���� - uint32_t len; // ���ݳ��� - uint32_t count; // ����� - TSkBuffMng *pool; // ȫ��buff��ָ�� + TSkBuffList list; + uint32_t len; + uint32_t count; + TSkBuffMng *pool; } TRWCache; - -/** - * @brief Cache��������ʼ�� - * @param cache -������ָ�� - * @param pool -buff��ָ�� - */ void rw_cache_init(TRWCache* cache, TSkBuffMng* pool); -/** - * @brief Cache���������� - * @param cache -������ָ�� - */ void rw_cache_destroy(TRWCache* cache); -/** - * @brief Cacheɾ����ָ���������� - * @param cache -������ָ�� - * @param len -��ɾ���ij��� - */ void cache_skip_data(TRWCache* cache, uint32_t len); -/** - * @brief Cache�Ƴ���һ���ڴ� - * @param cache -������ָ�� - */ TSkBuffer* cache_skip_first_buffer(TRWCache* cache); - -/** - * @brief Cache��ָ���������� - * @param cache -������ָ�� - * @param data -���ӵ�ָ�� - * @param len -���ӵij��� - */ int32_t cache_append_data(TRWCache* cache, const void* data, uint32_t len); -/** - * @brief Cache��ָ���������� - * @param cache -������ָ�� - * @param buff -���ӵĿ�ָ�� - */ void cache_append_buffer(TRWCache* cache, TSkBuffer* buff); -/** - * @brief Cacheɾ��������ָ���������� - * @param cache -������ָ�� - * @param buff -���buff��ָ�� - * @param len -��ɾ���ij��� - * @return ʵ�ʿ������� - */ uint32_t cache_copy_out(TRWCache* cache, void* buff, uint32_t len); - -/** - * @brief Cache���ϵ�UDP�ձ��ӿ�, �����ڴ�Ƚ϶�, ������32λʹ�� - * @param cache -������ָ�� - * @param fd - ���ձ���fd��� - * @param remote_addr -�Զ�ip��ַ - * @return ʵ�ʽ��ճ��� - */ int32_t cache_udp_recv(TRWCache* cache, uint32_t fd, struct sockaddr_in* remote_addr); -/** - * @brief Cache���ϵ�TCP�ձ��ӿ� - * @param cache -������ָ�� - * @param fd - ���ձ���fd��� - * @return ʵ�ʽ��ճ��� - */ int32_t cache_tcp_recv(TRWCache* cache, uint32_t fd); -/** - * @brief Cache���ϵ�TCP���ͽӿ� - * @param cache -������ָ�� - * @param fd - ��������fd��� - * @return ʵ�ʷ��ͳ��� - */ int32_t cache_tcp_send(TRWCache* cache, uint32_t fd); -/** - * @brief Cache���ϵ�TCP���ͽӿ�, δʹ��IOVEC - * @param cache -������ָ�� - * @param fd - ��������fd��� - * @param data -������cache��, �������͵�buff - * @param len -�������͵�buff���� - * @return ʵ�ʷ��ͳ��� - */ int32_t cache_tcp_send_buff(TRWCache* cache, uint32_t fd, const void* data, uint32_t len); - - - // interface -typedef void* TBuffVecPtr; ///< ���block��cache����ָ���� -typedef void* TBuffBlockPtr; ///< ����������ָ���� +typedef void* TBuffVecPtr; +typedef void* TBuffBlockPtr; - -/** - * @brief ��ȡcache��Ч�����ܳ��� - * @param multi -������ָ�� - * @return ʵ����Ч���ݳ��� - */ uint32_t get_data_len(TBuffVecPtr multi); -/** - * @brief ��ȡcache��Ч���ݿ���� - * @param multi -������ָ�� - * @return ʵ����Ч���ݿ���� - */ uint32_t get_block_count(TBuffVecPtr multi); -/** - * @brief ��ȡcache�ĵ�һ������ָ�� - * @param multi -������ָ�� - * @return ��һ������ָ�� - */ TBuffBlockPtr get_first_block(TBuffVecPtr multi); -/** - * @brief ��ȡcache����һ������ָ�� - * @param multi -������ָ�� - * @param block -��ǰ��ָ�� - * @return ��һ������ָ�� - */ TBuffBlockPtr get_next_block(TBuffVecPtr multi, TBuffBlockPtr block); -/** - * @brief ��ȡ���ݿ��ָ�������ݳ��� - * @param block -��ǰ��ָ�� - * @param data -����ָ��-modify���� - * @param len -����ָ�� modify���� - */ void get_block_data(TBuffBlockPtr block, const void** data, int32_t* len); - -/** - * @brief ��ȡ���ݿ��ָ�������ݳ��� - * @param multi -������ָ�� - * @param data -����д������ָ�� - * @param len -���� - * @return ���ݶ�ȡ�����ݳ��� - */ uint32_t read_cache_data(TBuffVecPtr multi, void* data, uint32_t len); - -/** - * @brief ��ȡ���ݿ��ָ�������ݳ��� - * @param multi -������ָ�� - * @param data -����д������ָ�� - * @param len -���� - * @return ���ݶ�ȡ�����ݳ��� - */ uint32_t read_cache_begin(TBuffVecPtr multi, uint32_t begin, void* data, uint32_t len); - }; #endif diff --git a/app/micro_thread/mt_concurrent.cpp b/app/micro_thread/mt_concurrent.cpp index 3e62d6194..1e02d15c1 100644 --- a/app/micro_thread/mt_concurrent.cpp +++ b/app/micro_thread/mt_concurrent.cpp @@ -19,7 +19,6 @@ /** * @file mt_concurrent.c - * @info ��·������ģ����չ * @time 20130924 **/ @@ -32,14 +31,6 @@ using namespace std; using namespace NS_MICRO_THREAD; - -/** - * @brief ��·IO�Ĵ����Ż�, �첽���ȵȴ����� - * @param req_list - �����б� - * @param how - EPOLLIN EPOLLOUT - * @param timeout - ��ʱʱ�� ���뵥λ - * @return 0 �ɹ�, <0ʧ�� -3 ������ʱ - */ int NS_MICRO_THREAD::mt_multi_netfd_poll(IMtActList& req_list, int how, int timeout) { KqObjList fdlist; @@ -99,11 +90,6 @@ int NS_MICRO_THREAD::mt_multi_netfd_poll(IMtActList& req_list, int how, int time return 0; } -/** - * @brief Ϊÿ��ITEM���������ĵ�socket - * @param req_list - �����б� - * @return 0 �ɹ�, <0ʧ�� - */ int NS_MICRO_THREAD::mt_multi_newsock(IMtActList& req_list) { int sock = -1, has_ok = 0; @@ -161,13 +147,6 @@ int NS_MICRO_THREAD::mt_multi_newsock(IMtActList& req_list) } } - -/** - * @brief ��·IO�Ĵ���, ������ - * @param req_list - �����б� - * @param timeout - ��ʱʱ�� ���뵥λ - * @return 0 �ɹ�, <0ʧ�� - */ int NS_MICRO_THREAD::mt_multi_open(IMtActList& req_list, int timeout) { utime64_t start_ms = MtFrame::Instance()->GetLastClock(); @@ -244,13 +223,6 @@ int NS_MICRO_THREAD::mt_multi_open(IMtActList& req_list, int timeout) } - -/** - * @brief ��·IO�Ĵ���, �������� - * @param req_list - �����б� - * @param timeout - ��ʱʱ�� ���뵥λ - * @return 0 �ɹ�, <0ʧ�� - */ int NS_MICRO_THREAD::mt_multi_sendto(IMtActList& req_list, int timeout) { utime64_t start_ms = MtFrame::Instance()->GetLastClock(); @@ -284,7 +256,6 @@ int NS_MICRO_THREAD::mt_multi_sendto(IMtActList& req_list, int timeout) return -2; } - // 0 -��Ҫ��������; -1 ֹͣ����; > 0 ����OK ret = net_handler->SendData(); if (ret == -1) { @@ -335,11 +306,6 @@ int NS_MICRO_THREAD::mt_multi_sendto(IMtActList& req_list, int timeout) return 0; } - - -/** - * @brief ��·IO�������մ��� - */ int NS_MICRO_THREAD::mt_multi_recvfrom(IMtActList& req_list, int timeout) { utime64_t start_ms = MtFrame::Instance()->GetLastClock(); @@ -360,7 +326,7 @@ int NS_MICRO_THREAD::mt_multi_recvfrom(IMtActList& req_list, int timeout) continue; } - if (MULTI_FLAG_FIN == action->GetMsgFlag()) ///< �Ѵ������ + if (MULTI_FLAG_FIN == action->GetMsgFlag()) { continue; } @@ -373,7 +339,6 @@ int NS_MICRO_THREAD::mt_multi_recvfrom(IMtActList& req_list, int timeout) return -2; } - // <0 ʧ��, 0 ������, >0 �ɹ� ret = net_handler->RecvData(); if (ret < 0) { @@ -415,18 +380,15 @@ int NS_MICRO_THREAD::mt_multi_recvfrom(IMtActList& req_list, int timeout) } } -/** - * @brief ��·IO�������մ��� - */ int NS_MICRO_THREAD::mt_multi_sendrcv_ex(IMtActList& req_list, int timeout) { utime64_t start_ms = MtFrame::Instance()->GetLastClock(); utime64_t curr_ms = 0; - int rc = mt_multi_newsock(req_list); // TODO, ����ȡconnect��ʱʱ��� + int rc = mt_multi_newsock(req_list); if (rc < 0) { - MT_ATTR_API(320842, 1); // socketʧ�� + MT_ATTR_API(320842, 1); MTLOG_ERROR("mt_multi_sendrcv new sock failed, ret: %d", rc); return -1; } @@ -434,7 +396,7 @@ int NS_MICRO_THREAD::mt_multi_sendrcv_ex(IMtActList& req_list, int timeout) rc = mt_multi_open(req_list, timeout); if (rc < 0) { - MT_ATTR_API(320843, 1); // connectʧ�� + MT_ATTR_API(320843, 1); MTLOG_ERROR("mt_multi_sendrcv open failed, ret: %d", rc); return -2; } @@ -443,7 +405,7 @@ int NS_MICRO_THREAD::mt_multi_sendrcv_ex(IMtActList& req_list, int timeout) rc = mt_multi_sendto(req_list, timeout - (curr_ms - start_ms)); if (rc < 0) { - MT_ATTR_API(320844, 1); // ����ʧ�� + MT_ATTR_API(320844, 1); MTLOG_ERROR("mt_multi_sendrcv send failed, ret: %d", rc); return -3; } @@ -452,7 +414,7 @@ int NS_MICRO_THREAD::mt_multi_sendrcv_ex(IMtActList& req_list, int timeout) rc = mt_multi_recvfrom(req_list, timeout - (curr_ms - start_ms)); if (rc < 0) { - MT_ATTR_API(320845, 1); // ����δ��ȫ�ɹ� + MT_ATTR_API(320845, 1); MTLOG_ERROR("mt_multi_sendrcv recv failed, ret: %d", rc); return -4; } @@ -460,18 +422,10 @@ int NS_MICRO_THREAD::mt_multi_sendrcv_ex(IMtActList& req_list, int timeout) return 0; } - -/** - * @brief ��·IO�������մ����ӿ�, ��װACTON�ӿ�ģ��, �ڲ�����msg - * @param req_list -action list ʵ�ַ�װ�����ӿ� - * @param timeout -��ʱʱ��, ��λms - * @return 0 �ɹ�, -1 ��ʼ������ʧ��, �����ɹ��ֳɹ� - */ int NS_MICRO_THREAD::mt_msg_sendrcv(IMtActList& req_list, int timeout) { int iRet = 0; - - // ��һ��, ��ʼ��action����, ��װ������ + for (IMtActList::iterator it = req_list.begin(); it != req_list.end(); ++it) { IMtAction* pAction = *it; @@ -491,10 +445,8 @@ int NS_MICRO_THREAD::mt_msg_sendrcv(IMtActList& req_list, int timeout) } - // �ڶ���, ͬ���շ���Ϣ, ʧ��Ҳ��Ҫ֪ͨ���� mt_multi_sendrcv_ex(req_list, timeout); - // ������, ͬ��֪ͨ������� for (IMtActList::iterator it = req_list.begin(); it != req_list.end(); ++it) { IMtAction* pAction = *it; @@ -514,7 +466,6 @@ int NS_MICRO_THREAD::mt_msg_sendrcv(IMtActList& req_list, int timeout) } } - // ���IJ�, ��������ڲ���Դ, ���ݸ����÷� for (IMtActList::iterator it = req_list.begin(); it != req_list.end(); ++it) { IMtAction* pAction = *it; @@ -523,6 +474,3 @@ int NS_MICRO_THREAD::mt_msg_sendrcv(IMtActList& req_list, int timeout) return 0; } - - - diff --git a/app/micro_thread/mt_concurrent.h b/app/micro_thread/mt_concurrent.h index 781b6f552..af7f085fc 100644 --- a/app/micro_thread/mt_concurrent.h +++ b/app/micro_thread/mt_concurrent.h @@ -19,7 +19,6 @@ /** * @file mt_concurrent.h - * @info ��չ״̬�̵߳Ĵ���ģ�� * @time 20130515 **/ @@ -36,62 +35,19 @@ using std::vector; class IMtAction; typedef vector<IMtAction*> IMtActList; -/******************************************************************************/ -/* �߳��û��ӿڶ���: �߳�Action��·����ģ�ͽӿڶ��� */ -/******************************************************************************/ -/** - * @brief ��·IO�������մ����ӿ�, ��װACTON�ӿ�ģ��, �ڲ�����msg - * @param req_list -action list ʵ�ַ�װ�����ӿ� - * @param timeout -��ʱʱ��, ��λms - * @return 0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -100 ����Ӧ��ʧ��, �ɴ�ӡerrno - */ int mt_msg_sendrcv(IMtActList& req_list, int timeout); -/******************************************************************************/ -/* �ڲ�ʵ�ֶ��岿�� */ -/******************************************************************************/ - -/** - * @brief ��·IO�Ĵ����Ż�, �첽���ȵȴ����� - * @param req_list - �����б� - * @param how - EPOLLIN EPOLLOUT - * @param timeout - ��ʱʱ�� ���뵥λ - * @return 0 �ɹ�, <0ʧ�� -3 ������ʱ - */ int mt_multi_netfd_poll(IMtActList& req_list, int how, int timeout); -/** - * @brief Ϊÿ��ITEM���������ĵ�socket - * @param req_list - �����б� - * @return 0 �ɹ�, <0ʧ�� - */ int mt_multi_newsock(IMtActList& req_list); -/** - * @brief ��·IO�Ĵ���, ������ - * @param req_list - �����б� - * @param timeout - ��ʱʱ�� ���뵥λ - * @return 0 �ɹ�, <0ʧ�� - */ int mt_multi_open(IMtActList& req_list, int timeout); -/** - * @brief ��·IO�Ĵ���, �������� - * @param req_list - �����б� - * @param timeout - ��ʱʱ�� ���뵥λ - * @return 0 �ɹ�, <0ʧ�� - */ int mt_multi_sendto(IMtActList& req_list, int timeout); -/** - * @brief ��·IO�������մ��� - */ int mt_multi_recvfrom(IMtActList& req_list, int timeout); -/** - * @brief ��·IO�������մ��� - */ int mt_multi_sendrcv_ex(IMtActList& req_list, int timeout); } diff --git a/app/micro_thread/mt_connection.cpp b/app/micro_thread/mt_connection.cpp index 89077f765..399852ae1 100644 --- a/app/micro_thread/mt_connection.cpp +++ b/app/micro_thread/mt_connection.cpp @@ -19,7 +19,6 @@ /** * @file mt_connection.cpp - * @info �߳���Ϣ�������ӹ���ʵ�� * @time 20130924 **/ #include <fcntl.h> @@ -38,10 +37,6 @@ using namespace std; using namespace NS_MICRO_THREAD; - -/** - * @brief �߳����ӻ���������� - */ IMtConnection::IMtConnection() { _type = OBJ_CONN_UNDEF; @@ -62,10 +57,6 @@ IMtConnection::~IMtConnection() } } - -/** - * @brief ���ӻ��ո����������� - */ void IMtConnection::Reset() { if (_ntfy_obj) { @@ -83,22 +74,15 @@ void IMtConnection::Reset() _msg_buff = NULL; } - -/** - * @brief ���ӵ�socket����, �������ӵ�Э�����͵� - * @return >0 -�ɹ�, ����ϵͳfd, < 0 ʧ�� - */ int UdpShortConn::CreateSocket() { - // 1. UDP������, ÿ���´�SOCKET _osfd = socket(AF_INET, SOCK_DGRAM, 0); if (_osfd < 0) { MTLOG_ERROR("socket create failed, errno %d(%s)", errno, strerror(errno)); return -1; } - - // 2. ���������� + int flags = 1; if (ioctl(_osfd, FIONBIO, &flags) < 0) { @@ -108,7 +92,6 @@ int UdpShortConn::CreateSocket() return -2; } - // 3. ���¹�����Ϣ if (_ntfy_obj) { _ntfy_obj->SetOsfd(_osfd); } @@ -116,9 +99,6 @@ int UdpShortConn::CreateSocket() return _osfd; } -/** - * @brief �ر�ϵͳsocket, ����һЩ״̬ - */ int UdpShortConn::CloseSocket() { if (_osfd < 0) @@ -132,11 +112,6 @@ int UdpShortConn::CloseSocket() return 0; } - -/** - * @brief ���Է�������, ������һ�� - * @return 0 ���ͱ��ж�, ������. <0 ϵͳʧ��. >0 ���η��ͳɹ� - */ int UdpShortConn::SendData() { if (!_action || !_msg_buff) { @@ -167,11 +142,6 @@ int UdpShortConn::SendData() } } -/** - * @brief ���Խ�������, ������һ�� - * @param buff ���ջ�����ָ�� - * @return -1 �Զ˹ر�. -2 ϵͳʧ��. >0 ���ν��ճɹ� - */ int UdpShortConn::RecvData() { if (!_action || !_msg_buff) { @@ -194,19 +164,18 @@ int UdpShortConn::RecvData() { MTLOG_ERROR("socket recv failed, fd %d, errno %d(%s)", _osfd, errno, strerror(errno)); - return -2; // ϵͳ���� + return -2; } } else if (ret == 0) { - return -1; // �Զ˹ر� + return -1; } else { _msg_buff->SetHaveRcvLen(ret); } - // �����ļ��, >0 �հ�����; =0 �����ȴ�; <0(-65535����)�����쳣 ret = _action->DoInput(); if (ret > 0) { @@ -228,20 +197,12 @@ int UdpShortConn::RecvData() } } -/** - * @brief ���ӻ��ո����������� - */ void UdpShortConn::Reset() { CloseSocket(); this->IMtConnection::Reset(); } - -/** - * @brief ���Ӵ���Զ�˻Ựͨ��, ��TCP��connect�� - * @return 0 -�ɹ�, < 0 ʧ�� - */ int TcpKeepConn::OpenCnnect() { if (!_action || !_msg_buff) { @@ -279,12 +240,9 @@ int TcpKeepConn::OpenCnnect() } } -/** - * @brief ����sock��TCP�������� - */ int TcpKeepConn::CreateSocket() { - if (_osfd > 0) // ��������ʱ, ��������������; ������������ntfyfd + if (_osfd > 0) { if (_ntfy_obj) { _ntfy_obj->SetOsfd(_osfd); @@ -293,15 +251,13 @@ int TcpKeepConn::CreateSocket() return _osfd; } - // ��һ�ν���ʱ, ����socket _osfd = socket(AF_INET, SOCK_STREAM, 0); if (_osfd < 0) { MTLOG_ERROR("create tcp socket failed, error: %d", errno); return -1; } - - // ���������� + int flags = 1; if (ioctl(_osfd, FIONBIO, &flags) < 0) { @@ -311,7 +267,6 @@ int TcpKeepConn::CreateSocket() return -2; } - // ���¹�����Ϣ _keep_ntfy.SetOsfd(_osfd); _keep_ntfy.DisableOutput(); _keep_ntfy.EnableInput(); @@ -323,13 +278,6 @@ int TcpKeepConn::CreateSocket() return _osfd; } -/** - * @brief ���Է�������, ������һ�� - * @param dst ����Ŀ�ĵ�ַ - * @param buff ���ͻ�����ָ�� - * @param size �����͵���� - * @return 0 ���ͱ��ж�, ������. <0 ϵͳʧ��. >0 ���η��ͳɹ� - */ int TcpKeepConn::SendData() { if (!_action || !_msg_buff) { @@ -360,7 +308,6 @@ int TcpKeepConn::SendData() _msg_buff->SetHaveSndLen(have_send_len); } - // ȫ���������, ���سɹ�, ��������ȴ� if (have_send_len >= msg_len) { return msg_len; @@ -371,11 +318,6 @@ int TcpKeepConn::SendData() } } -/** - * @brief ���Խ�������, ������һ�� - * @param buff ���ջ�����ָ�� - * @return -1 �Զ˹ر�. -2 ϵͳʧ��. >0 ���ν��ճɹ� - */ int TcpKeepConn::RecvData() { if (!_action || !_msg_buff) { @@ -397,14 +339,14 @@ int TcpKeepConn::RecvData() else { MTLOG_ERROR("recv tcp socket failed, error: %d", errno); - return -2; // ϵͳ���� + return -2; } } else if (ret == 0) { MTLOG_ERROR("tcp remote close, address: %s[%d]", inet_ntoa(_dst_addr.sin_addr), ntohs(_dst_addr.sin_port)); - return -1; // �Զ˹ر� + return -1; } else { @@ -412,7 +354,6 @@ int TcpKeepConn::RecvData() _msg_buff->SetHaveRcvLen(have_rcv_len); } - // �����ļ��, >0 �հ�����; =0 �����ȴ�; <0(-65535����)�����쳣 ret = _action->DoInput(); if (ret > 0) { @@ -429,9 +370,6 @@ int TcpKeepConn::RecvData() } } -/** - * @brief �ر�ϵͳsocket, ����һЩ״̬ - */ int TcpKeepConn::CloseSocket() { if (_osfd < 0) @@ -446,9 +384,6 @@ int TcpKeepConn::CloseSocket() return 0; } -/** - * @brief ���ӻ��ո����������� - */ void TcpKeepConn::Reset() { memset(&_dst_addr, 0 ,sizeof(_dst_addr)); @@ -456,17 +391,11 @@ void TcpKeepConn::Reset() this->IMtConnection::Reset(); } -/** - * @brief ���ӻ��ո����������� - */ void TcpKeepConn::ConnReuseClean() { this->IMtConnection::Reset(); } -/** - * @brief Idle���洦��, epoll ����Զ�˹رյ� - */ bool TcpKeepConn::IdleAttach() { if (_osfd < 0) { @@ -482,7 +411,6 @@ bool TcpKeepConn::IdleAttach() _keep_ntfy.DisableOutput(); _keep_ntfy.EnableInput(); - // ���ʱ������ CTimerMng* timer = MtFrame::Instance()->GetTimerMng(); if ((NULL == timer) || !timer->start_timer(this, _keep_time)) { @@ -502,9 +430,6 @@ bool TcpKeepConn::IdleAttach() } } -/** - * @brief Idleȡ�����洦��, �����ɿ����߳�����Զ�˹ر� - */ bool TcpKeepConn::IdleDetach() { if (_osfd < 0) { @@ -520,7 +445,6 @@ bool TcpKeepConn::IdleDetach() _keep_ntfy.DisableOutput(); _keep_ntfy.EnableInput(); - // ���ʱ��ɾ�� CTimerMng* timer = MtFrame::Instance()->GetTimerMng(); if (NULL != timer) { @@ -539,19 +463,12 @@ bool TcpKeepConn::IdleDetach() } } - -/** - * @brief ��ʱ֪ͨ����, ����ʵ���� - */ void TcpKeepConn::timer_notify() { MTLOG_DEBUG("keep timeout[%u], fd %d, close connection", _keep_time, _osfd); ConnectionMgr::Instance()->CloseIdleTcpKeep(this); } -/** - * @brief �������������� - */ TcpKeepMgr::TcpKeepMgr() { _keep_hash = new HashList(10000); @@ -574,10 +491,6 @@ TcpKeepMgr::~TcpKeepMgr() _keep_hash = NULL; } - -/** - * @brief ��IP��ַ��ȡTCP�ı������� - */ TcpKeepConn* TcpKeepMgr::GetTcpKeepConn(struct sockaddr_in* dst) { TcpKeepConn* conn = NULL; @@ -606,9 +519,6 @@ TcpKeepConn* TcpKeepMgr::GetTcpKeepConn(struct sockaddr_in* dst) return conn; } -/** - * @brief ��IP��ַ����TCP�ı������� - */ bool TcpKeepMgr::RemoveTcpKeepConn(TcpKeepConn* conn) { struct sockaddr_in* dst = conn->GetDestAddr(); @@ -633,10 +543,6 @@ bool TcpKeepMgr::RemoveTcpKeepConn(TcpKeepConn* conn) } - -/** - * @brief ��IP��ַ����TCP�ı������� - */ bool TcpKeepMgr::CacheTcpKeepConn(TcpKeepConn* conn) { struct sockaddr_in* dst = conn->GetDestAddr(); @@ -672,9 +578,6 @@ bool TcpKeepMgr::CacheTcpKeepConn(TcpKeepConn* conn) } -/** - * @brief �رջ�tcp������ - */ void TcpKeepMgr::FreeTcpKeepConn(TcpKeepConn* conn, bool force_free) { if (force_free) @@ -693,16 +596,9 @@ void TcpKeepMgr::FreeTcpKeepConn(TcpKeepConn* conn, bool force_free) } } } - - -/** - * @brief ���ӵ�socket����, �������ӵ�Э�����͵� - * @return >0 -�ɹ�, ����ϵͳfd, < 0 ʧ�� - */ int UdpSessionConn::CreateSocket() { - // 1. session������, ��֪ͨ��������fd if (!_action || !_ntfy_obj) { MTLOG_ERROR("conn not set action %p, or _ntfy_obj %p, error", _action, _ntfy_obj); return -100; @@ -718,7 +614,6 @@ int UdpSessionConn::CreateSocket() return -300; } - // 2. ί�ɴ���, ���¾����Ϣ int osfd = real_ntfy->GetOsfd(); if (osfd <= 0) { @@ -733,19 +628,11 @@ int UdpSessionConn::CreateSocket() return osfd; } -/** - * @brief �ر�ϵͳsocket, ����һЩ״̬ - */ int UdpSessionConn::CloseSocket() { return 0; } - -/** - * @brief ���Է�������, ������һ�� - * @return 0 ���ͱ��ж�, ������. <0 ϵͳʧ��. >0 ���η��ͳɹ� - */ int UdpSessionConn::SendData() { if (!_action || !_msg_buff || !_ntfy_obj) { @@ -776,11 +663,6 @@ int UdpSessionConn::SendData() } } -/** - * @brief ���Խ�������, ������һ�� - * @param buff ���ջ�����ָ�� - * @return 0 -�����ȴ�����; >0 ���ճɹ�; < 0 ʧ�� - */ int UdpSessionConn::RecvData() { if (!_ntfy_obj || !_msg_buff) { @@ -793,7 +675,6 @@ int UdpSessionConn::RecvData() return 0; } - // UDP Session ֪ͨ���滻msg buff, ͨ��type�ж� int msg_len = _msg_buff->GetMsgLen(); if (BUFF_RECV == _msg_buff->GetBuffType()) { @@ -806,11 +687,6 @@ int UdpSessionConn::RecvData() } } - -/** - * @brief sessionȫ�ֹ������ - * @return ȫ�־��ָ�� - */ ConnectionMgr* ConnectionMgr::_instance = NULL; ConnectionMgr* ConnectionMgr::Instance (void) { @@ -822,9 +698,6 @@ ConnectionMgr* ConnectionMgr::Instance (void) return _instance; } -/** - * @brief session����ȫ�ֵ����ٽӿ� - */ void ConnectionMgr::Destroy() { if( _instance != NULL ) @@ -834,23 +707,14 @@ void ConnectionMgr::Destroy() } } -/** - * @brief ��Ϣbuff�Ĺ��캯�� - */ ConnectionMgr::ConnectionMgr() { } -/** - * @brief ��������, ��������Դ, ������������ - */ ConnectionMgr::~ConnectionMgr() { } -/** - * @brief ��ȡ�ӿ� - */ IMtConnection* ConnectionMgr::GetConnection(CONN_OBJ_TYPE type, struct sockaddr_in* dst) { switch (type) @@ -874,9 +738,6 @@ IMtConnection* ConnectionMgr::GetConnection(CONN_OBJ_TYPE type, struct sockaddr_ } -/** - * @brief ���սӿ� - */ void ConnectionMgr::FreeConnection(IMtConnection* conn, bool force_free) { if (!conn) { @@ -908,14 +769,8 @@ void ConnectionMgr::FreeConnection(IMtConnection* conn, bool force_free) return; } - -/** - * @brief �ر�idle��tcp������ - */ void ConnectionMgr::CloseIdleTcpKeep(TcpKeepConn* conn) { _tcp_keep_mgr.RemoveTcpKeepConn(conn); _tcp_keep_mgr.FreeTcpKeepConn(conn, true); } - - diff --git a/app/micro_thread/mt_connection.h b/app/micro_thread/mt_connection.h index f395f42c3..f266cf91c 100644 --- a/app/micro_thread/mt_connection.h +++ b/app/micro_thread/mt_connection.h @@ -19,7 +19,6 @@ /** * @file mt_connection.h - * @info �߳����ӹ������岿�� * @time 20130924 **/ @@ -36,140 +35,75 @@ namespace NS_MICRO_THREAD { using std::queue; -/** - * @brief ���Ӷ������� - */ enum CONN_OBJ_TYPE { - OBJ_CONN_UNDEF = 0, ///< δ��������Ӷ��� - OBJ_SHORT_CONN = 1, ///< �����Ӷ���, fd�����Ự, ÿ������CLOSE - OBJ_TCP_KEEP = 2, ///< TCP�ĸ���ģ��, ÿ��ÿ����ʹ�ø�fd, ����ɸ��� - OBJ_UDP_SESSION = 3, ///< UDP��sessionģ��, ÿ���ӿɹ������߳�ʹ�� + OBJ_CONN_UNDEF = 0, + OBJ_SHORT_CONN = 1, + OBJ_TCP_KEEP = 2, + OBJ_UDP_SESSION = 3, }; -/** - * @brief �߳�һ���������, ӳ��һ�����Ӷ��� - */ class IMtConnection { public: - /** - * @brief �߳����ӻ���������� - */ IMtConnection(); virtual ~IMtConnection(); - /** - * @brief ���ӻ��ո����������� - */ virtual void Reset(); - - /** - * @brief ��ȡ���Ӷ����������Ϣ - */ + CONN_OBJ_TYPE GetConnType() { return _type; }; - - /** - * @brief �����ڲ�ACTIONָ�� - * @return IMtConnָ�� - */ + void SetIMtActon(IMtAction* action ) { _action = action; }; - /** - * @brief ��ȡ�ڲ�ACTIONָ�� - * @return IMtConnָ�� - */ IMtAction* GetIMtActon() { return _action; }; - /** - * @brief �����ڲ�ACTIONָ�� - * @return IMtConnָ�� - */ void SetNtfyObj(KqueuerObj* obj ) { _ntfy_obj = obj; }; - /** - * @brief ��ȡ�ڲ�ACTIONָ�� - * @return IMtConnָ�� - */ KqueuerObj* GetNtfyObj() { return _ntfy_obj; }; - /** - * @brief �����ڲ�msgbuffָ�� - * @return IMtConnָ�� - */ void SetMtMsgBuff(MtMsgBuf* msg_buf) { _msg_buff = msg_buf; }; - /** - * @brief ��ȡ�ڲ�msgbuffָ�� - * @return IMtConnָ�� - */ MtMsgBuf* GetMtMsgBuff() { return _msg_buff; }; public: - - /** - * @brief ���ӵ�socket����, �������ӵ�Э�����͵� - * @return >0 -�ɹ�, ����ϵͳfd, < 0 ʧ�� - */ + virtual int CreateSocket() {return 0;}; - - /** - * @brief ���Ӵ���Զ�˻Ựͨ��, ��TCP��connect�� - * @return 0 -�ɹ�, < 0 ʧ�� - */ + virtual int OpenCnnect() {return 0;}; - /** - * @brief ���ӷ������� - * @return >0 -�ɹ�, ����ʵ�ʷ��ͳ���, < 0 ʧ�� - */ virtual int SendData() {return 0;}; - /** - * @brief ���ӽ������� - * @return >0 -�ɹ�, ���ر��ν��ճ���, < 0 ʧ��(-1 �Զ˹ر�; -2 ���մ���) - */ virtual int RecvData() {return 0;}; - /** - * @brief �ر�socket�˿� - * @return >0 -�ɹ�, ����ϵͳfd, < 0 ʧ�� - */ virtual int CloseSocket() {return 0;}; protected: - CONN_OBJ_TYPE _type; // Ԥ�õ�type, �ɰ�type���������� - IMtAction* _action; // ������actionָ��, �ϼ�ָ��, ��������Դ������ - KqueuerObj* _ntfy_obj; // EPOLL֪ͨ����, �¼�ָ��, ���������� - MtMsgBuf* _msg_buff; // ��̬������buff�ֶ�, �¼�ָ��, ���������� + CONN_OBJ_TYPE _type; + IMtAction* _action; + KqueuerObj* _ntfy_obj; + MtMsgBuf* _msg_buff; }; -/** - * @brief ����sock�Ķ��������� - */ class UdpShortConn : public IMtConnection { public: - /** - * @brief ����socket�Ķ����ӵĹ��������� - */ UdpShortConn() { _osfd = -1; _type = OBJ_SHORT_CONN; @@ -178,37 +112,18 @@ public: CloseSocket(); }; - /** - * @brief ���ӻ��ո����������� - */ virtual void Reset(); - /** - * @brief ���ӵ�socket����, �������ӵ�Э�����͵� - * @return >0 -�ɹ�, ����ϵͳfd, < 0 ʧ�� - */ virtual int CreateSocket(); - /** - * @brief ���ӷ������� - * @return >0 -�ɹ�, ����ʵ�ʷ��ͳ���, < 0 ʧ�� - */ virtual int SendData(); - /** - * @brief ���ӽ������� - * @return >0 -�ɹ�, ���ر��ν��ճ���, < 0 ʧ��(-1 �Զ˹ر�; -2 ���մ���) - */ virtual int RecvData(); - /** - * @brief �ر�socket�˿� - * @return >0 -�ɹ�, ����ϵͳfd, < 0 ʧ�� - */ virtual int CloseSocket(); protected: - int _osfd; // ÿ�����ӵ�������socket + int _osfd; }; @@ -218,64 +133,36 @@ enum TcpKeepFlag TCP_KEEP_IN_KQUEUE = 0x2, }; -/** - * @brief ����session��UDP�������� - */ class UdpSessionConn : public IMtConnection { public: - /** - * @brief ����socket�Ķ����ӵĹ��������� - */ UdpSessionConn() { _type = OBJ_UDP_SESSION; }; virtual ~UdpSessionConn() { }; - /** - * @brief ���ӵ�socket����, �������ӵ�Э�����͵� - * @return >0 -�ɹ�, ����ϵͳfd, < 0 ʧ�� - */ virtual int CreateSocket(); - /** - * @brief ���ӷ������� - * @return >0 -�ɹ�, ����ʵ�ʷ��ͳ���, < 0 ʧ�� - */ virtual int SendData(); - /** - * @brief ���ӽ������� - * @return >0 -�ɹ�, ���ر��ν��ճ���, < 0 ʧ��(-1 �Զ˹ر�; -2 ���մ���) - */ virtual int RecvData(); - /** - * @brief �ر�socket�˿� - * @return >0 -�ɹ�, ����ϵͳfd, < 0 ʧ�� - */ virtual int CloseSocket(); }; -/** - * @brief ����sock��TCP�������� - */ typedef TAILQ_ENTRY(TcpKeepConn) KeepConnLink; typedef TAILQ_HEAD(__KeepConnTailq, TcpKeepConn) KeepConnList; class TcpKeepConn : public IMtConnection, public CTimerNotify { public: - int _keep_flag; // ����״̬��� - KeepConnLink _keep_entry; // ���й������ + int _keep_flag; + KeepConnLink _keep_entry; - /** - * @brief ����socket�Ķ����ӵĹ��������� - */ TcpKeepConn() { _osfd = -1; - _keep_time = 10*60*1000; // Ĭ��10����, ��������� + _keep_time = 10*60*1000; _keep_flag = 0; _type = OBJ_TCP_KEEP; _keep_ntfy.SetKeepNtfyObj(this); @@ -284,102 +171,51 @@ public: CloseSocket(); }; - /** - * @brief ���ӻ��ո����������� - */ virtual void Reset(); - - /** - * @brief ���Ӵ���Զ�˻Ựͨ��, ��TCP��connect�� - * @return 0 -�ɹ�, < 0 ʧ�� - */ + virtual int OpenCnnect(); - /** - * @brief ���ӵ�socket����, �������ӵ�Э�����͵� - * @return >0 -�ɹ�, ����ϵͳfd, < 0 ʧ�� - */ virtual int CreateSocket(); - /** - * @brief ���ӷ������� - * @return >0 -�ɹ�, ����ʵ�ʷ��ͳ���, < 0 ʧ�� - */ virtual int SendData(); - /** - * @brief ���ӽ������� - * @return >0 -�ɹ�, ���ر��ν��ճ���, < 0 ʧ��(-1 �Զ˹ر�; -2 ���մ���) - */ virtual int RecvData(); - /** - * @brief �ر�socket�˿� - * @return >0 -�ɹ�, ����ϵͳfd, < 0 ʧ�� - */ virtual int CloseSocket(); - /** - * @brief ���ӱ��ָ��� - */ void ConnReuseClean(); - /** - * @brief Idle���洦��, epoll ����Զ�˹رյ� - */ bool IdleAttach(); - /** - * @brief Idleȡ�����洦��, �����ɿ����߳�����Զ�˹ر� - */ bool IdleDetach(); - /** - * @brief �洢Ŀ�ĵ�ַ��Ϣ, ���ڸ��� - */ void SetDestAddr(struct sockaddr_in* dst) { memcpy(&_dst_addr, dst, sizeof(_dst_addr)); } - /** - * @brief ��ȡĿ�ĵ�ַ��Ϣ - */ struct sockaddr_in* GetDestAddr() { return &_dst_addr; } - /** - * @brief ��ʱ֪ͨ����, ����ʵ���� - */ + virtual void timer_notify(); - /** - * @brief ���ó�ʱʱ��, ���뵥λ - */ void SetKeepTime(unsigned int time) { _keep_time = time; }; protected: - int _osfd; // ÿ�����ӵ�������socket - unsigned int _keep_time; // ���ñ����ʱ�� - TcpKeepNtfy _keep_ntfy; // ����һ���������Ӷ��� - struct sockaddr_in _dst_addr; // Զ�˵�ַ��Ϣ + int _osfd; + unsigned int _keep_time; + TcpKeepNtfy _keep_ntfy; + struct sockaddr_in _dst_addr; }; - - -/** - * @brief ����ַhash���泤���� - */ class TcpKeepKey : public HashKey { public: - /** - * @brief �������������� - */ TcpKeepKey() { _addr_ipv4 = 0; _net_port = 0; @@ -394,25 +230,14 @@ public: this->SetDataPtr(this); }; - /** - * @brief �����ݲ�����conn - */ ~TcpKeepKey() { TAILQ_INIT(&_keep_list); }; - /** - * @brief �ڵ�Ԫ�ص�hash�㷨, ��ȡkey��hashֵ - * @return �ڵ�Ԫ�ص�hashֵ - */ virtual uint32_t HashValue(){ return _addr_ipv4 ^ ((_net_port << 16) | _net_port); }; - /** - * @brief �ڵ�Ԫ�ص�cmp����, ͬһͰID��, ��key�Ƚ� - * @return �ڵ�Ԫ�ص�hashֵ - */ virtual int HashCmp(HashKey* rhs){ TcpKeepKey* data = dynamic_cast<TcpKeepKey*>(rhs); if (!data) { @@ -427,10 +252,6 @@ public: return 0; }; - - /** - * @brief ���Ӷ������ - */ void InsertConn(TcpKeepConn* conn) { if (conn->_keep_flag & TCP_KEEP_IN_LIST) { return; @@ -452,60 +273,36 @@ public: }; private: - uint32_t _addr_ipv4; ///< ip��ַ - uint16_t _net_port; ///< port �������� - KeepConnList _keep_list; ///< ʵ�ʵĿ��ж��� + uint32_t _addr_ipv4; + uint16_t _net_port; + KeepConnList _keep_list; }; - -/** - * @brief TCP�����ӵ����Ӷ���������ڴ�cache - */ class TcpKeepMgr { public: - typedef CPtrPool<TcpKeepConn> TcpKeepQueue; ///< �ڴ滺��� + typedef CPtrPool<TcpKeepConn> TcpKeepQueue; - /** - * @brief �������������� - */ TcpKeepMgr(); ~TcpKeepMgr(); - - /** - * @brief ��IP��ַ��ȡTCP�ı������� - */ TcpKeepConn* GetTcpKeepConn(struct sockaddr_in* dst); - - /** - * @brief ��IP��ַ����TCP�ı������� - */ + bool CacheTcpKeepConn(TcpKeepConn* conn); - /** - * @brief ��IP��ַ����TCP�ı�������, ȥ��CACHE - */ bool RemoveTcpKeepConn(TcpKeepConn* conn); - /** - * @brief �رջ�tcp������ - */ void FreeTcpKeepConn(TcpKeepConn* conn, bool force_free); private: - HashList* _keep_hash; ///< hash��, �洢��IP���������Ӷ��� - TcpKeepQueue _mem_queue; ///< mem����, ����conn�ڴ�� + HashList* _keep_hash; + TcpKeepQueue _mem_queue; }; - -/** - * @brief ���ӹ�������ģ�� - */ class ConnectionMgr { public: @@ -513,48 +310,26 @@ public: typedef CPtrPool<UdpShortConn> UdpShortQueue; typedef CPtrPool<UdpSessionConn> UdpSessionQueue; - /** - * @brief ��Ϣbuff��ȫ�ֹ�������ӿ� - * @return ȫ�־��ָ�� - */ static ConnectionMgr* Instance (void); - /** - * @brief ��Ϣ�����ӿ� - */ static void Destroy(void); - /** - * @brief ��ȡ�ӿ� - */ IMtConnection* GetConnection(CONN_OBJ_TYPE type, struct sockaddr_in* dst); - - /** - * @brief ���սӿ� - */ + void FreeConnection(IMtConnection* conn, bool force_free); - /** - * @brief �ر�idle��tcp������ - */ void CloseIdleTcpKeep(TcpKeepConn* conn); - /** - * @brief ��Ϣbuff���������� - */ ~ConnectionMgr(); private: - /** - * @brief ��Ϣbuff�Ĺ��캯�� - */ ConnectionMgr(); - static ConnectionMgr * _instance; ///< �������� + static ConnectionMgr * _instance; - UdpShortQueue _udp_short_queue; ///< �����ӵĶ��г� - UdpSessionQueue _udp_session_queue; ///< udp session ���ӳ� - TcpKeepMgr _tcp_keep_mgr; ///< tcp keep ������ + UdpShortQueue _udp_short_queue; + UdpSessionQueue _udp_session_queue; + TcpKeepMgr _tcp_keep_mgr; }; } diff --git a/app/micro_thread/mt_incl.h b/app/micro_thread/mt_incl.h index c830457c7..11e8dab30 100644 --- a/app/micro_thread/mt_incl.h +++ b/app/micro_thread/mt_incl.h @@ -19,7 +19,6 @@ /** * @file mt_incl.h - * @info �߳��ṩ��spp_plugin��ͷ�ļ� * @time 20130924 */ diff --git a/app/micro_thread/mt_mbuf_pool.cpp b/app/micro_thread/mt_mbuf_pool.cpp index 104c911a8..e47a0963a 100644 --- a/app/micro_thread/mt_mbuf_pool.cpp +++ b/app/micro_thread/mt_mbuf_pool.cpp @@ -19,7 +19,6 @@ /** * @file mt_mbuf_pool.cpp - * @info �߳���Ϣbuf�ع���ʵ�� * @time 20130924 **/ @@ -29,11 +28,6 @@ using namespace std; using namespace NS_MICRO_THREAD; - -/** - * @brief ���ӹ���ȫ�ַ��ʽӿ� - * @return ȫ�־��ָ�� - */ MsgBuffPool* MsgBuffPool::_instance = NULL; MsgBuffPool* MsgBuffPool::Instance (void) { @@ -45,9 +39,6 @@ MsgBuffPool* MsgBuffPool::Instance (void) return _instance; } -/** - * @brief ���ӹ���ȫ�ֵ����ٽӿ� - */ void MsgBuffPool::Destroy() { if( _instance != NULL ) @@ -57,19 +48,12 @@ void MsgBuffPool::Destroy() } } - -/** - * @brief ��Ϣbuff�Ĺ��캯�� - */ MsgBuffPool::MsgBuffPool(int max_free) { _max_free = max_free; _hash_map = new HashList(10000); } -/** - * @brief ��Ϣbuff���������� - */ MsgBuffPool::~MsgBuffPool() { if (!_hash_map) { @@ -91,10 +75,6 @@ MsgBuffPool::~MsgBuffPool() _hash_map = NULL; } -/** - * @brief ��ȡ��ϢbuffԪ�� - * @return msgbufָ��, ʧ��ΪNULL - */ MtMsgBuf* MsgBuffPool::GetMsgBuf(int max_size) { if (!_hash_map) { @@ -127,10 +107,6 @@ MtMsgBuf* MsgBuffPool::GetMsgBuf(int max_size) } } -/** - * @brief ��ȡ��ϢbuffԪ�� - * @return msgbufָ��, ʧ��ΪNULL - */ void MsgBuffPool::FreeMsgBuf(MtMsgBuf* msg_buf) { if (!_hash_map || !msg_buf) { diff --git a/app/micro_thread/mt_mbuf_pool.h b/app/micro_thread/mt_mbuf_pool.h index 2281b665f..96af522e5 100644 --- a/app/micro_thread/mt_mbuf_pool.h +++ b/app/micro_thread/mt_mbuf_pool.h @@ -19,7 +19,6 @@ /** * @file mt_mbuf_pool.h - * @info �߳�ͬ����Ϣbuf�� **/ #ifndef __MT_MBUF_POOL_H__ @@ -35,33 +34,27 @@ using std::queue; enum BUFF_TYPE { - BUFF_UNDEF = 0, ///< δ�������� - BUFF_RECV = 1, ///< ����buff - BUFF_SEND = 2, ///< ����buff + BUFF_UNDEF = 0, + BUFF_RECV = 1, + BUFF_SEND = 2, }; -/** - * @brief ��ϢͶ�ݵ�buffer�� - */ typedef TAILQ_ENTRY(MtMsgBuf) MsgBufLink; typedef TAILQ_HEAD(__MtbuffTailq, MtMsgBuf) MsgBufQueue; class MtMsgBuf { private: - int _max_len; // ���Ŀռ䳤�� - int _msg_len; // ʵ�ʵ���Ϣ���� - int _buf_type; // buff�Ƿ��ͻ��ǽ��� - int _recv_len; // �ѽ��յ���Ϣ���� - int _send_len; // �ѷ��͵���Ϣ���� - void* _msg_buff; // buffer ʵ��ͷָ�� + int _max_len; + int _msg_len; + int _buf_type; + int _recv_len; + int _send_len; + void* _msg_buff; public: MsgBufLink _entry; - /** - * @brief ���캯��, ָ�����buff���� - */ MtMsgBuf(int max_len) { _max_len = max_len; _msg_len = 0; @@ -78,9 +71,6 @@ public: } }; - /** - * @brief ��Ϣ���͵��������ȡ - */ void SetBuffType(BUFF_TYPE type) { _buf_type = (int)type; }; @@ -88,9 +78,6 @@ public: return (BUFF_TYPE)_buf_type; }; - /** - * @brief ���ýӿ�, �ָ���ʼ״̬ - */ void Reset() { _msg_len = 0; _recv_len = 0; @@ -98,9 +85,6 @@ public: _buf_type = BUFF_UNDEF; }; - /** - * @brief ��Ϣ���ȵ��������ȡ - */ void SetMsgLen(int msg_len) { _msg_len = msg_len; }; @@ -108,9 +92,6 @@ public: return _msg_len; }; - /** - * @brief �����bufferָ���ȡ - */ int GetMaxLen() { return _max_len; }; @@ -118,9 +99,6 @@ public: return _msg_buff; }; - /** - * @brief �м�״̬��ȡ����� - */ int GetHaveSndLen() { return _send_len; }; @@ -128,9 +106,7 @@ public: _send_len = snd_len; }; - /** - * @brief �м�״̬��ȡ����� - */ + int GetHaveRcvLen() { return _recv_len; }; @@ -139,18 +115,10 @@ public: }; }; -/** - * @brief ָ����С��buffer, �����ӳ��ɿ��ж��� - */ class MsgBufMap : public HashKey { public: - /** - * @brief ��Ϣbuff�����Ĺ��� - * @param buff_size ��mapԪ���ϵ�����buff, �����buff�ռ��Сֵ - * @param max_free �ö��й���Ԫ��, ��ֵ�free��Ŀ - */ MsgBufMap(int buff_size, int max_free) { _max_buf_size = buff_size; _max_free = max_free; @@ -159,18 +127,11 @@ public: TAILQ_INIT(&_msg_queue); }; - /** - * @brief ��Ϣbuff�����Ĺ���, ����, ������key��Ϣ - * @param buff_size ��mapԪ���ϵ�����buff, �����buff�ռ��Сֵ - */ explicit MsgBufMap(int buff_size) { _max_buf_size = buff_size; TAILQ_INIT(&_msg_queue); }; - /** - * @brief ��Ϣbuff�������������� - */ ~MsgBufMap() { MtMsgBuf* ptr = NULL; MtMsgBuf* tmp = NULL; @@ -183,11 +144,7 @@ public: TAILQ_INIT(&_msg_queue); }; - - /** - * @brief ��ȡ��ϢbuffԪ�� - * @return msgbufָ��, ʧ��ΪNULL - */ + MtMsgBuf* GetMsgBuf(){ MtMsgBuf* ptr = NULL; if (!TAILQ_EMPTY(&_msg_queue)) { @@ -201,10 +158,6 @@ public: return ptr; }; - /** - * @brief �ͷ���ϢbuffԪ�� - * @param msgbufָ�� - */ void FreeMsgBuf(MtMsgBuf* ptr){ if (_queue_num >= _max_free) { delete ptr; @@ -215,88 +168,49 @@ public: } }; - /** - * @brief �ڵ�Ԫ�ص�hash�㷨, ��ȡkey��hashֵ - * @return �ڵ�Ԫ�ص�hashֵ - */ virtual uint32_t HashValue(){ return _max_buf_size; }; - /** - * @brief �ڵ�Ԫ�ص�cmp����, ͬһͰID��, ��key�Ƚ� - * @return �ڵ�Ԫ�ص�hashֵ - */ virtual int HashCmp(HashKey* rhs){ return this->_max_buf_size - (int)rhs->HashValue(); }; private: - int _max_free; ///< �����б������� - int _max_buf_size; ///< ����������buffsize - int _queue_num; ///< ���ж��и��� - MsgBufQueue _msg_queue; ///< ʵ�ʵĿ��ж��� + int _max_free; + int _max_buf_size; + int _queue_num; + MsgBufQueue _msg_queue; }; - -/** - * @brief ȫ�ֵ�buffer�ض���, ͳһ���������buffer - */ class MsgBuffPool { public: - /** - * @brief ��Ϣbuff��ȫ�ֹ�������ӿ� - * @return ȫ�־��ָ�� - */ static MsgBuffPool* Instance (void); - /** - * @brief ��Ϣ�����ӿ� - */ static void Destroy(void); - /** - * @brief ��Ϣbuff��ȫ�ֹ�������Ĭ�����Ŀ��и��� - * @param max_free �����б�����Ŀ, ��Ҫ�ڷ���Ԫ��ǰ���� - */ void SetMaxFreeNum(int max_free) { _max_free = max_free; }; - /** - * @brief ��ȡ��ϢbuffԪ�� - * @return msgbufָ��, ʧ��ΪNULL - */ MtMsgBuf* GetMsgBuf(int max_size); - /** - * @brief �ͷ���ϢbuffԪ�� - * @param msgbufָ�� - */ void FreeMsgBuf(MtMsgBuf* msg_buf); - /** - * @brief ��Ϣbuff��ȫ������������ - */ ~MsgBuffPool(); private: - /** - * @brief ��Ϣbuff�Ĺ��캯�� - */ explicit MsgBuffPool(int max_free = 300); - static MsgBuffPool * _instance; ///< �������� - int _max_free; ///< �����������Ŀ - HashList* _hash_map; ///< ��size hashmap ������ж��� + static MsgBuffPool * _instance; + int _max_free; + HashList* _hash_map; }; - - } #endif diff --git a/app/micro_thread/mt_msg.h b/app/micro_thread/mt_msg.h index c67b00c5e..4edb819d1 100644 --- a/app/micro_thread/mt_msg.h +++ b/app/micro_thread/mt_msg.h @@ -19,7 +19,6 @@ /** * @file mt_msg.h - * @info �߳�ͬ����Ϣ�Ļ��� **/ #ifndef __MT_MSG_H__ @@ -27,22 +26,12 @@ namespace NS_MICRO_THREAD { -/** - * @brief �߳���Ϣ�������� - */ class IMtMsg { public: - /** - * @brief �߳���Ϣ��Ĵ���������ں��� - * @return 0 -�ɹ�, < 0 ʧ�� - */ virtual int HandleProcess() { return -1; }; - - /** - * @brief �߳���Ϣ����������� - */ + IMtMsg() {}; virtual ~IMtMsg() {}; }; diff --git a/app/micro_thread/mt_net.cpp b/app/micro_thread/mt_net.cpp index e1b639275..40ccbb08b 100644 --- a/app/micro_thread/mt_net.cpp +++ b/app/micro_thread/mt_net.cpp @@ -19,7 +19,6 @@ /** * @file mt_mbuf_pool.cpp - * @info �߳���Ϣbuf�ع���ʵ�� * @time 20130924 **/ @@ -34,7 +33,6 @@ using namespace std; using namespace NS_MICRO_THREAD; -// �������鹹 CNetHelper::CNetHelper() { handler = (void*)CNetMgr::Instance()->AllocNetItem(); @@ -51,7 +49,6 @@ CNetHelper::~CNetHelper() } } -// ͬ���շ��ӿ� int32_t CNetHelper::SendRecv(void* data, uint32_t len, uint32_t timeout) { if (handler != NULL) { @@ -62,7 +59,6 @@ int32_t CNetHelper::SendRecv(void* data, uint32_t len, uint32_t timeout) } } -// ��ȡ����buff��Ϣ, ��Ч��ֱ��helper���� void* CNetHelper::GetRspBuff() { if (handler != NULL) { @@ -73,7 +69,6 @@ void* CNetHelper::GetRspBuff() } } -// ��ȡ����buff��Ϣ, ��Ч��ֱ��helper���� uint32_t CNetHelper::GetRspLen() { if (handler != NULL) { @@ -84,8 +79,6 @@ uint32_t CNetHelper::GetRspLen() } } - -// ת����������Ϣ, �����ȡ char* CNetHelper::GetErrMsg(int32_t result) { static const char* errmsg = "unknown error type"; @@ -151,7 +144,6 @@ char* CNetHelper::GetErrMsg(int32_t result) return (char*)errmsg; } -// ����Э�������, Ĭ��UDP void CNetHelper::SetProtoType(MT_PROTO_TYPE type) { if (handler != NULL) { @@ -160,7 +152,6 @@ void CNetHelper::SetProtoType(MT_PROTO_TYPE type) } } -// ����Ŀ��IP��ַ void CNetHelper::SetDestAddress(struct sockaddr_in* dst) { if (handler != NULL) { @@ -169,7 +160,6 @@ void CNetHelper::SetDestAddress(struct sockaddr_in* dst) } } -// ����session����session id��Ϣ, �����0 void CNetHelper::SetSessionId(uint64_t sid) { if (handler != NULL) { @@ -178,7 +168,6 @@ void CNetHelper::SetSessionId(uint64_t sid) } } -// ����session�����ص����� void CNetHelper::SetSessionCallback(CHECK_SESSION_CALLBACK function) { if (handler != NULL) { @@ -187,22 +176,16 @@ void CNetHelper::SetSessionCallback(CHECK_SESSION_CALLBACK function) } } - - -// ����һ��net handler, ���ڸ��� void CNetHandler::Reset() { - // ȥ��session��connection������� this->Unlink(); this->UnRegistSession(); - // ��̬�ڴ����� if (_rsp_buff != NULL) { delete_sk_buffer(_rsp_buff); _rsp_buff = NULL; } - // �ֶγ�ʼ������ _thread = NULL; _proto_type = NET_PROTO_TCP; _conn_type = TYPE_CONN_SESSION; @@ -219,7 +202,6 @@ void CNetHandler::Reset() } -// ���캯�� CNetHandler::CNetHandler() { _state_flags = 0; @@ -228,23 +210,19 @@ CNetHandler::CNetHandler() this->Reset(); } -// �������� CNetHandler::~CNetHandler() { this->Reset(); } -// ����Ҫ�IJ�����Ϣ int32_t CNetHandler::CheckParams() { - // 1. ��������Ч��� if ((NULL == _req_data) || (_req_len == 0)) { MTLOG_ERROR("param invalid, data[%p], len[%u]", _req_data, _req_len); return RC_INVALID_PARAM; } - // 2. Ŀ�ĵ�ַ��Ч��� if ((_dest_ipv4.sin_addr.s_addr == 0) || (_dest_ipv4.sin_port == 0)) { MTLOG_ERROR("param invalid, ip[%u], port[%u]", _dest_ipv4.sin_addr.s_addr, @@ -252,7 +230,6 @@ int32_t CNetHandler::CheckParams() return RC_INVALID_PARAM; } - // 3. session ���ͼ����ȷ�� if (_conn_type == TYPE_CONN_SESSION) { if ((_callback == NULL) || (_session_id == 0)) @@ -261,7 +238,6 @@ int32_t CNetHandler::CheckParams() return RC_INVALID_PARAM; } - // �����ע��session��Ϣ if (!this->RegistSession()) { MTLOG_ERROR("param invalid, session_id[%llu] regist failed", _session_id); @@ -269,11 +245,9 @@ int32_t CNetHandler::CheckParams() } } - // 4. ����ִ�� return 0; } -// ��ȡ����, ͬʱ�������ȴ����ӵĶ����� int32_t CNetHandler::GetConnLink() { CDestLinks key; @@ -298,8 +272,6 @@ int32_t CNetHandler::GetConnLink() return 0; } - -// ����Ҫ�IJ�����Ϣ int32_t CNetHandler::WaitConnect(uint64_t timeout) { CSockLink* conn = (CSockLink*)this->_conn_ptr; @@ -322,14 +294,11 @@ int32_t CNetHandler::WaitConnect(uint64_t timeout) return RC_SUCCESS; } - // ����ȴ�connect���� this->SwitchToConn(); - // �ȴ������� MtFrame* mtframe = MtFrame::Instance(); mtframe->WaitNotify(timeout); - // ɾ����connect����, ��ʱ��Ҫ�����, �������� this->SwitchToIdle(); if (_err_no != 0) @@ -338,7 +307,6 @@ int32_t CNetHandler::WaitConnect(uint64_t timeout) return _err_no; } - // ��ʱ��������ȷ�� if (conn->Connected()) { MTLOG_DEBUG("connect ok"); @@ -351,8 +319,6 @@ int32_t CNetHandler::WaitConnect(uint64_t timeout) } } - -// ����Ҫ�IJ�����Ϣ int32_t CNetHandler::WaitSend(uint64_t timeout) { CSockLink* conn = (CSockLink*)this->_conn_ptr; @@ -376,24 +342,19 @@ int32_t CNetHandler::WaitSend(uint64_t timeout) return RC_SUCCESS; } - // �ȴ���������, �л��ѷ��͵���Ϣ this->SwitchToSend(); - // �ȴ������� MtFrame* mtframe = MtFrame::Instance(); mtframe->WaitNotify(timeout); - // ɾ����connect���� this->SwitchToIdle(); - // �쳣��� if (_err_no != 0) { MTLOG_ERROR("send get out errno %d", _err_no); return _err_no; } - // ��ʱ��� if (_req_len == 0) { MTLOG_DEBUG("send req ok, len %u", _send_pos); @@ -406,7 +367,6 @@ int32_t CNetHandler::WaitSend(uint64_t timeout) } } -// ����Ҫ�IJ�����Ϣ int32_t CNetHandler::WaitRecv(uint64_t timeout) { CSockLink* conn = (CSockLink*)this->_conn_ptr; @@ -422,17 +382,13 @@ int32_t CNetHandler::WaitRecv(uint64_t timeout) return 0; } - // �л����ȴ����� this->SwitchToRecv(); - // �ȴ������� MtFrame* mtframe = MtFrame::Instance(); mtframe->WaitNotify(timeout); - // ɾ����connect���� this->SwitchToIdle(); - // ��ʱ��� if ((_rsp_buff != NULL) && (_rsp_buff->data_len > 0)) { MTLOG_DEBUG("recv get rsp, len %d", _rsp_buff->data_len); @@ -445,8 +401,6 @@ int32_t CNetHandler::WaitRecv(uint64_t timeout) } } - -// ͬ���շ��ӿ�, ��Ϊsessionר�� int32_t CNetHandler::SendRecv(void* data, uint32_t len, uint32_t timeout) { utime64_t start_ms = MtFrame::Instance()->GetLastClock(); @@ -455,7 +409,6 @@ int32_t CNetHandler::SendRecv(void* data, uint32_t len, uint32_t timeout) this->_req_data = data; this->_req_len = len; - // 0. ����Ҫ��������Ϣ int32_t ret = this->CheckParams(); if (ret < 0) { @@ -463,7 +416,6 @@ int32_t CNetHandler::SendRecv(void* data, uint32_t len, uint32_t timeout) goto EXIT_LABEL; } - // 1. ��ȡ������·ָ�� ret = this->GetConnLink(); if (ret < 0) { @@ -471,7 +423,6 @@ int32_t CNetHandler::SendRecv(void* data, uint32_t len, uint32_t timeout) goto EXIT_LABEL; } - // 2. �ȴ����ӳɹ� ret = this->WaitConnect(time_left); if (ret < 0) { @@ -479,7 +430,6 @@ int32_t CNetHandler::SendRecv(void* data, uint32_t len, uint32_t timeout) goto EXIT_LABEL; } - // 3. �ȴ����ͳɹ� cost_time = MtFrame::Instance()->GetLastClock() - start_ms; time_left = (timeout > (uint32_t)cost_time) ? (timeout - (uint32_t)cost_time) : 0; ret = this->WaitSend(time_left); @@ -488,8 +438,7 @@ int32_t CNetHandler::SendRecv(void* data, uint32_t len, uint32_t timeout) MTLOG_ERROR("sock send failed, ret: %d", ret); goto EXIT_LABEL; } - - // 4. �ȴ����ճɹ� + cost_time = MtFrame::Instance()->GetLastClock() - start_ms; time_left = (timeout > (uint32_t)cost_time) ? (timeout - (uint32_t)cost_time) : 0; ret = this->WaitRecv(time_left); @@ -499,22 +448,17 @@ int32_t CNetHandler::SendRecv(void* data, uint32_t len, uint32_t timeout) goto EXIT_LABEL; } - // 5. �ɹ����� ret = 0; EXIT_LABEL: - // ���η������, ��ҪUNLINK, ����NETHANDLER֧�� this->Unlink(); - - // �ɹ�ʧ��, ��Ҫȥ��sessionע�� + this->UnRegistSession(); return ret; } - -// �������͵����� uint32_t CNetHandler::SkipSendPos(uint32_t len) { uint32_t skip_len = (len >= _req_len) ? _req_len : len; @@ -525,15 +469,12 @@ uint32_t CNetHandler::SkipSendPos(uint32_t len) return skip_len; } -// �������Ӷ��� void CNetHandler::Link(CSockLink* conn) { this->_conn_ptr = conn; this->SwitchToIdle(); } - -// �������Ӷ��� void CNetHandler::Unlink() { if (this->_state_flags != 0) @@ -543,7 +484,6 @@ void CNetHandler::Unlink() this->_conn_ptr = NULL; } -// �����ڵȴ����Ӷ��� void CNetHandler::SwitchToConn() { CSockLink* conn = (CSockLink*)this->_conn_ptr; @@ -559,7 +499,6 @@ void CNetHandler::SwitchToConn() conn->AppendToList(CSockLink::LINK_CONN_LIST, this); } -// �л������Ͷ��� void CNetHandler::SwitchToSend() { CSockLink* conn = (CSockLink*)this->_conn_ptr; @@ -575,8 +514,6 @@ void CNetHandler::SwitchToSend() conn->AppendToList(CSockLink::LINK_SEND_LIST, this); } - -// �л������ն��� void CNetHandler::SwitchToRecv() { CSockLink* conn = (CSockLink*)this->_conn_ptr; @@ -592,7 +529,6 @@ void CNetHandler::SwitchToRecv() conn->AppendToList(CSockLink::LINK_RECV_LIST, this); } -// �л�������״̬ void CNetHandler::SwitchToIdle() { CSockLink* conn = (CSockLink*)this->_conn_ptr; @@ -608,8 +544,6 @@ void CNetHandler::SwitchToIdle() conn->AppendToList(CSockLink::LINK_IDLE_LIST, this); } - -// ��������״̬���� void CNetHandler::DetachConn() { CSockLink* conn = (CSockLink*)this->_conn_ptr; @@ -619,7 +553,7 @@ void CNetHandler::DetachConn() return; } - if (_state_flags == 0) // ��ʱ���¼�����, 2����֧, ����Ҫ������ + if (_state_flags == 0) { return; } @@ -649,13 +583,6 @@ void CNetHandler::DetachConn() } } - - - -/** - * @brief �ڵ�Ԫ�ص�hash�㷨, ��ȡkey��hashֵ - * @return �ڵ�Ԫ�ص�hashֵ - */ uint32_t CNetHandler::HashValue() { uint32_t ip = _dest_ipv4.sin_addr.s_addr; @@ -668,10 +595,6 @@ uint32_t CNetHandler::HashValue() return hash; } -/** - * @brief �ڵ�Ԫ�ص�cmp����, ͬһͰID��, ��key�Ƚ� - * @return �ڵ�Ԫ�ص�hashֵ - */ int32_t CNetHandler::HashCmp(HashKey* rhs) { CNetHandler* data = (CNetHandler*)(rhs); @@ -699,8 +622,6 @@ int32_t CNetHandler::HashCmp(HashKey* rhs) return 0; }; - -// ע��session���� bool CNetHandler::RegistSession() { if (CNetMgr::Instance()->FindNetItem(this) != NULL) @@ -716,7 +637,6 @@ bool CNetHandler::RegistSession() return true; } -// ȡ��ע��session void CNetHandler::UnRegistSession() { if (this->_state_flags & STATE_IN_SESSION) @@ -726,8 +646,6 @@ void CNetHandler::UnRegistSession() } } - -// ��ȡ�������� TNetItemList* CSockLink::GetItemList(int32_t type) { TNetItemList* list = NULL; @@ -756,8 +674,6 @@ TNetItemList* CSockLink::GetItemList(int32_t type) return list; } - -// ���������Ϣ void CSockLink::AppendToList(int32_t type, CNetHandler* item) { TNetItemList* list = this->GetItemList(type); @@ -770,7 +686,6 @@ void CSockLink::AppendToList(int32_t type, CNetHandler* item) TAILQ_INSERT_TAIL(list, item, _link_entry); } -// ���������Ϣ void CSockLink::RemoveFromList(int32_t type, CNetHandler* item) { TNetItemList* list = this->GetItemList(type); @@ -783,22 +698,18 @@ void CSockLink::RemoveFromList(int32_t type, CNetHandler* item) TAILQ_REMOVE(list, item, _link_entry); } - -// ֪ͨ�����߳� void CSockLink::NotifyThread(CNetHandler* item, int32_t result) { static MtFrame* frame = NULL; if (frame == NULL) { frame = MtFrame::Instance(); } - - // ���÷�������Ϣ + if (result != RC_SUCCESS) { item->SetErrNo(result); } - // ���ÿ����� MicroThread* thread = item->GetThread(); if ((thread != NULL) && (thread->HasFlag(MicroThread::IO_LIST))) { @@ -807,8 +718,6 @@ void CSockLink::NotifyThread(CNetHandler* item, int32_t result) } } - -// ֪ͨ�����߳� void CSockLink::NotifyAll(int32_t result) { CNetHandler* item = NULL; @@ -839,15 +748,11 @@ void CSockLink::NotifyAll(int32_t result) } } - -// �����ó�ʼ���� void CSockLink::Reset() { - // �ر�fd, ֪ͨ�������߳� this->Close(); this->NotifyAll(_errno); - // ����cache���� rw_cache_destroy(&_recv_cache); if (_rsp_buff != NULL) { @@ -855,7 +760,6 @@ void CSockLink::Reset() _rsp_buff = NULL; } - // �����ȴ�����, ���ѵȴ��߳� TAILQ_INIT(&_wait_connect); TAILQ_INIT(&_wait_send); TAILQ_INIT(&_wait_recv); @@ -867,11 +771,9 @@ void CSockLink::Reset() _last_access = mt_time_ms(); _parents = NULL; - // ����������� this->KqueuerObj::Reset(); } -// �������������� CSockLink::CSockLink() { rw_cache_init(&_recv_cache, NULL); @@ -889,39 +791,30 @@ CSockLink::CSockLink() _parents = NULL; } -// �������������� CSockLink::~CSockLink() { this->Reset(); } - -// ����Э������, ����buff�ص�ָ�� void CSockLink::SetProtoType(MT_PROTO_TYPE type) { _proto_type = type; _recv_cache.pool = CNetMgr::Instance()->GetSkBuffMng(type); } -// �ر���·�ľ�� void CSockLink::Close() { - // 1. ���δ��ʼ��, ֱ�ӷ��� if (_fd < 0) { return; } - // 2. ��������ͷ� MtFrame::Instance()->KqueueDelObj(this); - // 3. �رվ�� close(_fd); _fd = -1; } - -// �쳣��ֹ�Ĵ������� void CSockLink::Destroy() { CDestLinks* dstlink = (CDestLinks*)_parents; @@ -937,16 +830,13 @@ void CSockLink::Destroy() } } - -// ����socket��� int32_t CSockLink::CreateSock() { - if (_fd > 0) // ��������ʱ, ��������������; + if (_fd > 0) { return _fd; } - // ����� if (NET_PROTO_TCP == _proto_type) { _fd = socket(AF_INET, SOCK_STREAM, 0); @@ -962,7 +852,6 @@ int32_t CSockLink::CreateSock() return -1; } - // ���÷����� int flags = 1; if (ioctl(_fd, FIONBIO, &flags) < 0) { @@ -972,14 +861,12 @@ int32_t CSockLink::CreateSock() return -2; } - // ѡ������, NODELAY if (NET_PROTO_TCP == _proto_type) { setsockopt(_fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags)); - this->EnableOutput(); // TCP �ȴ�connect����, ʡһ��epollctrl + this->EnableOutput(); } - // ����epoll��� this->EnableInput(); if (!MtFrame::Instance()->KqueueAddObj(this)) { @@ -992,7 +879,6 @@ int32_t CSockLink::CreateSock() return _fd; } -// ��ȡĿ��ip��Ϣ struct sockaddr_in* CSockLink::GetDestAddr(struct sockaddr_in* addr) { CDestLinks* dstlink = (CDestLinks*)_parents; @@ -1011,31 +897,25 @@ struct sockaddr_in* CSockLink::GetDestAddr(struct sockaddr_in* addr) return addr; } - -// �������ӹ��� bool CSockLink::Connect() { this->_last_access = mt_time_ms(); - // 1. UDP�����ӹ��� if (_proto_type == NET_PROTO_UDP) { _state |= LINK_CONNECTED; } - // 2. ������״̬, �ɹ����� if (_state & LINK_CONNECTED) { return true; } - // 3. ����������, �˳��ȴ� if (_state & LINK_CONNECTING) { return false; } - // 4. ������, �״����ӳ��� struct sockaddr_in addr = {0}; mt_hook_syscall(connect); @@ -1070,7 +950,6 @@ bool CSockLink::Connect() } } -// ��UDP�ķ�ʽ���͵ȴ�������, һ��ֱ�Ӿͷ���OK int32_t CSockLink::SendCacheUdp(void* data, uint32_t len) { mt_hook_syscall(sendto); @@ -1081,7 +960,6 @@ int32_t CSockLink::SendCacheUdp(void* data, uint32_t len) CNetHandler* tmp = NULL; struct sockaddr_in dst = {0}; - // 1. ���Է��͵ȴ�����, ����ָ�����߳� TAILQ_FOREACH_SAFE(item, &_wait_send, _link_entry, tmp) { item->GetSendData(buff, buff_len); @@ -1113,7 +991,6 @@ int32_t CSockLink::SendCacheUdp(void* data, uint32_t len) item->SwitchToIdle(); } - // 2. ������OK��, �ٷ��ͱ�������, û�д��������������� if ((data == NULL) || (len == 0)) { return 0; @@ -1140,8 +1017,6 @@ int32_t CSockLink::SendCacheUdp(void* data, uint32_t len) } } - -// TCP�Ļ��巢�ʹ��� int32_t CSockLink::SendCacheTcp(void* data, uint32_t len) { void* buff = NULL; @@ -1151,7 +1026,6 @@ int32_t CSockLink::SendCacheTcp(void* data, uint32_t len) CNetHandler* item = NULL; CNetHandler* tmp = NULL; - // 1. ���Է��͵ȴ�����, ����ָ�����߳� TAILQ_FOREACH_SAFE(item, &_wait_send, _link_entry, tmp) { item->GetSendData(buff, buff_len); @@ -1185,7 +1059,6 @@ int32_t CSockLink::SendCacheTcp(void* data, uint32_t len) } } - // 2. ���Է��͵ȴ�����, ����ָ�����߳� uint32_t send_left = (uint32_t)bytes; TAILQ_FOREACH_SAFE(item, &_wait_send, _link_entry, tmp) { @@ -1206,8 +1079,6 @@ int32_t CSockLink::SendCacheTcp(void* data, uint32_t len) return send_left; } - -// �������ӹ��� int32_t CSockLink::SendData(void* data, uint32_t len) { int32_t ret = 0; @@ -1215,7 +1086,6 @@ int32_t CSockLink::SendData(void* data, uint32_t len) this->_last_access = mt_time_ms(); - // 1. ���Է�������, �ȷ����Ŷӵ����� if (_proto_type == NET_PROTO_UDP) { ret = SendCacheUdp(data, len); @@ -1225,7 +1095,6 @@ int32_t CSockLink::SendData(void* data, uint32_t len) ret = SendCacheTcp(data, len); } - // 2. ��ǰ�����Ƿ������, �����, ���Բ�������OUT if (ret < (int32_t)len) { this->EnableOutput(); @@ -1237,7 +1106,6 @@ int32_t CSockLink::SendData(void* data, uint32_t len) rc = MtFrame::Instance()->KqueueCtrlDel(_fd, KQ_EVENT_WRITE); } - // 3. ����ˢ�¾��epollע�� if (!rc) { MTLOG_ERROR("socket epoll mng failed[%m], wait timeout"); @@ -1246,7 +1114,6 @@ int32_t CSockLink::SendData(void* data, uint32_t len) return ret; } -// ���ݷַ��������� int32_t CSockLink::RecvDispath() { if (_proto_type == NET_PROTO_UDP) @@ -1259,14 +1126,10 @@ int32_t CSockLink::RecvDispath() } } -// ���Խ��ո�������ݵ���ʱbuff void CSockLink::ExtendRecvRsp() { if (NULL == _rsp_buff) { - // buff���η���, ���̫��, �ᵼ���ظ��Ŀ���; ���̫С, �ᵼ��2��check��� - // Ȩ��һ��, �ݶ�500�ֽ�����, �ֳ���, ������ȫ, �˷Ѳ���, С���ֳ��� - // ��Ҫrealloc, ��500�ֽڿ�������Ҳ���� _rsp_buff = new_sk_buffer(512); if (NULL == _rsp_buff) { @@ -1279,12 +1142,10 @@ void CSockLink::ExtendRecvRsp() _rsp_buff->data + _rsp_buff->data_len , _rsp_buff->size - _rsp_buff->data_len); } -// ���ص�����, ���ȴ��Ŷӵȴ��л�ȡ, ���ݴӸ��ڵ��ȡ CHECK_SESSION_CALLBACK CSockLink::GetSessionCallback() { CHECK_SESSION_CALLBACK check_session = NULL; - - // 1. �Ŷӻ�ȡ�ص����� + CNetHandler* item = TAILQ_FIRST(&_wait_recv); if (NULL == item) { @@ -1301,14 +1162,12 @@ CHECK_SESSION_CALLBACK CSockLink::GetSessionCallback() EXIT_LABEL: - // 2. ������ڵ�ΪNULL, ֱ�ӷ��ض��н�� CDestLinks* dstlink = (CDestLinks*)_parents; if (NULL == dstlink) { return check_session; } - // 3. ������н��Ϊ��, ����·Ĭ�ϻ����func; ��Ϊ��, ���� if (check_session != NULL) { dstlink->SetDefaultCallback(check_session); @@ -1321,11 +1180,8 @@ EXIT_LABEL: return check_session; } - -// TCP����������������ַ� int32_t CSockLink::DispathTcp() { - // 1. TCP�ȴ�����, ������ʽ, �������β, ֻ�ܹر� CHECK_SESSION_CALLBACK check_session = this->GetSessionCallback(); if (NULL == check_session) { @@ -1333,7 +1189,6 @@ int32_t CSockLink::DispathTcp() return -1; } - // 2. ����ͬһIP/PORT��Э��, ��������������ͨ�� uint32_t need_len = 0; uint64_t sid = 0; int32_t ret = 0; @@ -1359,21 +1214,18 @@ int32_t CSockLink::DispathTcp() if (ret == 0) { - // 1. �û������ָ������, Ĭ��2����չ, ���ܻ���Ӱ�� if ((need_len == 0) && (_rsp_buff->data_len == _rsp_buff->size)) { MTLOG_DEBUG("recv default buff full[%u], but user no set need length", _rsp_buff->size); need_len = _rsp_buff->size * 2; } - - // 2. ����ʣ��ռ�, ������ȴ�����; �����ռ䳬��, ������� + if ((need_len <= _rsp_buff->size) || (need_len > 100*1024*1024)) { MTLOG_DEBUG("maybe need wait more data: %u", need_len); return 0; } - // 3. ��չbuff����, ���ٳ���һ�� _rsp_buff = reserve_sk_buffer(_rsp_buff, need_len); if (NULL == _rsp_buff) { @@ -1382,25 +1234,21 @@ int32_t CSockLink::DispathTcp() return -3; } - // 4. �Ѿ������������Ϣ, �ȴ������հ� if (_rsp_buff->data_len >= _recv_cache.len) { MTLOG_DEBUG("maybe need wait more data, now %u", _recv_cache.len); return 0; } - // 5. ������������, ���Խ��� continue; } - // �����쳣����, ����ʵ��δ������, �����ȴ� if (ret > (int32_t)_recv_cache.len) { MTLOG_DEBUG("maybe pkg not all ok, wait more"); return 0; } - // ��ѯ��session�Ķ��� CNetHandler* session = this->FindSession(sid); if (NULL == session) { @@ -1415,7 +1263,7 @@ int32_t CSockLink::DispathTcp() cache_skip_data(&_recv_cache, ret); this->NotifyThread(session, 0); session->SwitchToIdle(); - _rsp_buff->data_len = ret; //TCPʵ����Ч�ı��ij��ȱ�� + _rsp_buff->data_len = ret; session->SetRespBuff(_rsp_buff); _rsp_buff = NULL; } @@ -1425,17 +1273,13 @@ int32_t CSockLink::DispathTcp() } - -// UDP����������������ַ� int32_t CSockLink::DispathUdp() { - // 1. UDP���ݴ�����, ������Ҳ���Զ��������еı��� CHECK_SESSION_CALLBACK check_session = NULL; CNetHandler* item = TAILQ_FIRST(&_wait_recv); if (NULL == item) { MTLOG_DEBUG("recv data with no wait item, maybe wrong pkg recv"); - // �˴����˳�, ��Ϊ���Զ���UDPӦ��(��ʱ��\������) } else { @@ -1446,7 +1290,6 @@ int32_t CSockLink::DispathUdp() } } - // 2. ����ÿ����, �ҵ�session�ʹ���, ������ uint64_t sid = 0; uint32_t need_len = 0; int32_t ret = 0; @@ -1468,8 +1311,7 @@ int32_t CSockLink::DispathUdp() cache_skip_data(&_recv_cache, block->data_len); continue; } - - // ��ѯ��session�Ķ��� + CNetHandler* session = this->FindSession(sid); if (NULL == session) { @@ -1489,8 +1331,6 @@ int32_t CSockLink::DispathUdp() return 0; } - -// ��ѯ����sessionid������session��Ϣ CNetHandler* CSockLink::FindSession(uint64_t sid) { CNetHandler key; @@ -1509,19 +1349,12 @@ CNetHandler* CSockLink::FindSession(uint64_t sid) return CNetMgr::Instance()->FindNetItem(&key); } - - -/** - * @brief �ɶ��¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ int CSockLink::InputNotify() { int32_t ret = 0; this->_last_access = mt_time_ms(); - // 1. ��������, �������� if (_proto_type == NET_PROTO_UDP) { ret = cache_udp_recv(&_recv_cache, _fd, NULL); @@ -1530,8 +1363,7 @@ int CSockLink::InputNotify() { ret = cache_tcp_recv(&_recv_cache, _fd); } - - // 2. ����ʧ������������ + if (ret < 0) { if (ret == -SK_ERR_NEED_CLOSE) @@ -1549,7 +1381,6 @@ int CSockLink::InputNotify() return -1; } - // 3. �ַ�����, ������TCP ret = this->RecvDispath(); if (ret < 0) { @@ -1558,22 +1389,16 @@ int CSockLink::InputNotify() return -2; } - // 4. �ɹ����� return 0; } -/** - * @brief ��д�¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ int CSockLink::OutputNotify() { int32_t ret = 0; this->_last_access = mt_time_ms(); - // 1. ���ӵȴ�����������л� if (_state & LINK_CONNECTING) { _state &= ~LINK_CONNECTING; @@ -1588,7 +1413,6 @@ int CSockLink::OutputNotify() } } - // 2. ���Է������� if (_proto_type == NET_PROTO_UDP) { ret = SendCacheUdp(NULL, 0); @@ -1598,7 +1422,6 @@ int CSockLink::OutputNotify() ret = SendCacheTcp(NULL, 0); } - // 3. ����ʧ������������ if (ret < 0) { MTLOG_ERROR("Send on link failed, close it, ret %d[%m]", ret); @@ -1607,7 +1430,6 @@ int CSockLink::OutputNotify() return ret; } - // 4. ��������, �����������epoll if (TAILQ_EMPTY(&_wait_send)) { this->DisableOutput(); @@ -1620,11 +1442,6 @@ int CSockLink::OutputNotify() return 0; } - -/** - * @brief �쳣֪ͨ�ӿ� - * @return ���Է���ֵ, ���������¼����� - */ int CSockLink::HangupNotify() { MTLOG_ERROR("socket epoll error, fd %d", _fd); @@ -1634,28 +1451,23 @@ int CSockLink::HangupNotify() return -1; } - -// ���캯�� CDestLinks::CDestLinks() { - _timeout = 5*60*1000; // Ĭ��5���� + _timeout = 5*60*1000; _addr_ipv4 = 0; _net_port = 0; _proto_type = NET_PROTO_UNDEF; _conn_type = TYPE_CONN_SESSION; - _max_links = 3; // Ĭ��3�� + _max_links = 3; // 默认3个 _curr_link = 0; _dflt_callback = NULL; TAILQ_INIT(&_sock_list); } - -// ���ø��õĽӿں��� void CDestLinks::Reset() { - // �������Ӷ��� CSockLink* item = NULL; CSockLink* temp = NULL; TAILQ_FOREACH_SAFE(item, &_sock_list, _link_entry, temp) @@ -1664,31 +1476,27 @@ void CDestLinks::Reset() } TAILQ_INIT(&_sock_list); - // ��ʱ��ɾ�� CTimerMng* timer = MtFrame::Instance()->GetTimerMng(); if (NULL != timer) { timer->stop_timer(this); } - // ����Ĭ���ֶ���Ϣ - _timeout = 5*60*1000; // Ĭ��5���� + _timeout = 5*60*1000; _addr_ipv4 = 0; _net_port = 0; _proto_type = NET_PROTO_UNDEF; _conn_type = TYPE_CONN_SESSION; - _max_links = 3; // Ĭ��3�� + _max_links = 3; _curr_link = 0; } -// ���캯�� CDestLinks::~CDestLinks() { this->Reset(); } -// ������ʱ�� void CDestLinks::StartTimer() { CTimerMng* timer = MtFrame::Instance()->GetTimerMng(); @@ -1698,8 +1506,6 @@ void CDestLinks::StartTimer() } } - -// �ͷ�һ������link void CDestLinks::FreeSockLink(CSockLink* sock) { if ((sock == NULL) || (sock->GetParentsPtr() != (void*)this)) @@ -1717,8 +1523,6 @@ void CDestLinks::FreeSockLink(CSockLink* sock) CNetMgr::Instance()->FreeSockLink(sock); } - -// ��ȡһ������link, ĿǰΪ��ѯ CSockLink* CDestLinks::GetSockLink() { CSockLink* link = NULL; @@ -1745,12 +1549,8 @@ CSockLink* CDestLinks::GetSockLink() return link; } -/** - * @brief ��ʱ֪ͨ����, ��������·, ���������·���� - */ void CDestLinks::timer_notify() { - // 1. ����Ƿ��п��е���·, ɾ���� uint64_t now = mt_time_ms(); CSockLink* item = NULL; CSockLink* temp = NULL; @@ -1763,7 +1563,6 @@ void CDestLinks::timer_notify() } } - // 2. ����Ƿ�����Ч����·, û����ע��Ŀ���� item = TAILQ_FIRST(&_sock_list); if (NULL == item) { @@ -1772,17 +1571,11 @@ void CDestLinks::timer_notify() return; } - // 3. ���¼��붨ʱ������ this->StartTimer(); return; } - -/** - * @brief sessionȫ�ֹ������ - * @return ȫ�־��ָ�� - */ CNetMgr* CNetMgr::_instance = NULL; CNetMgr* CNetMgr::Instance (void) { @@ -1794,9 +1587,6 @@ CNetMgr* CNetMgr::Instance (void) return _instance; } -/** - * @brief session����ȫ�ֵ����ٽӿ� - */ void CNetMgr::Destroy() { if( _instance != NULL ) @@ -1806,7 +1596,6 @@ void CNetMgr::Destroy() } } -// ��ѯ�Ƿ��Ѿ�����ͬһ��sid�Ķ��� CNetHandler* CNetMgr::FindNetItem(CNetHandler* key) { if (NULL == this->_session_hash) @@ -1817,7 +1606,6 @@ CNetHandler* CNetMgr::FindNetItem(CNetHandler* key) return (CNetHandler*)_session_hash->HashFind(key); } -// ע��һ��item, �Ȳ�ѯ�����, ��֤��ͻ void CNetMgr::InsertNetItem(CNetHandler* item) { if (NULL == this->_session_hash) @@ -1834,7 +1622,6 @@ void CNetMgr::InsertNetItem(CNetHandler* item) return; } -// �Ƴ�һ��item���� void CNetMgr::RemoveNetItem(CNetHandler* item) { CNetHandler* handler = this->FindNetItem(item); @@ -1846,7 +1633,6 @@ void CNetMgr::RemoveNetItem(CNetHandler* item) _session_hash->HashRemove(handler); } -// ��ѯ�Ƿ��Ѿ�����ͬһ��sid�Ķ��� CDestLinks* CNetMgr::FindDestLink(CDestLinks* key) { if (NULL == this->_ip_hash) @@ -1857,7 +1643,6 @@ CDestLinks* CNetMgr::FindDestLink(CDestLinks* key) return (CDestLinks*)_ip_hash->HashFind(key); } -// ע��һ��item, �Ȳ�ѯ�����, ��֤��ͻ void CNetMgr::InsertDestLink(CDestLinks* item) { if (NULL == this->_ip_hash) @@ -1874,7 +1659,6 @@ void CNetMgr::InsertDestLink(CDestLinks* item) return; } -// �Ƴ�һ��item���� void CNetMgr::RemoveDestLink(CDestLinks* item) { CDestLinks* handler = this->FindDestLink(item); @@ -1886,8 +1670,6 @@ void CNetMgr::RemoveDestLink(CDestLinks* item) _ip_hash->HashRemove(handler); } - -// ��ѯ��һ��Ŀ��ip��links�ڵ� CDestLinks* CNetMgr::FindCreateDest(CDestLinks* key) { CDestLinks* dest = this->FindDestLink(key); @@ -1911,17 +1693,13 @@ CDestLinks* CNetMgr::FindCreateDest(CDestLinks* key) return dest; } - -// ��ѯ��һ��Ŀ��ip��links�ڵ� void CNetMgr::DeleteDestLink(CDestLinks* dst) { this->RemoveDestLink(dst); - dst->Reset(); // ֱ��freeǰ���� reset + dst->Reset(); this->FreeDestLink(dst); } - -// ���캯��ʵ�� CNetMgr::CNetMgr() { sk_buffer_mng_init(&_tcp_pool, 60, 4096); @@ -1931,11 +1709,8 @@ CNetMgr::CNetMgr() _session_hash = new HashList(100000); } - -// ��������ʵ�� CNetMgr::~CNetMgr() { - // �������е�dest��Դ if (_ip_hash != NULL) { HashKey* hash_item = _ip_hash->HashGetFirst(); @@ -1949,7 +1724,6 @@ CNetMgr::~CNetMgr() _ip_hash = NULL; } - // �������е� netitem ��Դ if (_session_hash != NULL) { HashKey* hash_item = _session_hash->HashGetFirst(); @@ -1963,15 +1737,10 @@ CNetMgr::~CNetMgr() _session_hash = NULL; } - // ����buff����Դ sk_buffer_mng_destroy(&_tcp_pool); sk_buffer_mng_destroy(&_udp_pool); } - -/** - * @brief ������Դ��Ϣ - */ void CNetMgr::RecycleObjs(uint64_t now) { uint32_t now_s = (uint32_t)(now / 1000); @@ -1983,5 +1752,3 @@ void CNetMgr::RecycleObjs(uint64_t now) _sock_link_pool.RecycleItem(now); _dest_ip_pool.RecycleItem(now); } - - diff --git a/app/micro_thread/mt_net.h b/app/micro_thread/mt_net.h index d20f1df6c..368316dab 100644 --- a/app/micro_thread/mt_net.h +++ b/app/micro_thread/mt_net.h @@ -19,7 +19,6 @@ /** * @file mt_net.h - * @info �̷߳�װ������ӿ��� **/ #ifndef __MT_NET_H__ @@ -33,41 +32,28 @@ namespace NS_MICRO_THREAD { -/** - * @brief �������Ͷ��� - */ enum MT_CONN_TYPE { TYPE_CONN_UNKNOWN = 0, - TYPE_CONN_SHORT = 0x1, ///< ������, һ�ν�����ر� - TYPE_CONN_POOL = 0x2, ///< �����ӣ�ÿ��ʹ�ú�, �ɻ����ظ�ʹ�� - TYPE_CONN_SESSION = 0x4, ///< �����ӣ���session id ����, ������ - TYPE_CONN_SENDONLY = 0x8, ///< ֻ������ + TYPE_CONN_SHORT = 0x1, + TYPE_CONN_POOL = 0x2, + TYPE_CONN_SESSION = 0x4, + TYPE_CONN_SENDONLY = 0x8, }; - -/******************************************************************************/ -/* �ڲ�ʵ�ֲ��� */ -/******************************************************************************/ class CSockLink; -/** - * @brief ��ʱ���յĶ����ģ��ʵ�� - * @info List������tailq, Type ��Ҫ��reset����, releasetime, linkentry�ֶ� - */ template <typename List, typename Type> class CRecyclePool { public: - // ���캯��, Ĭ��60s��ʱ CRecyclePool() { _expired = 60 * 1000; _count = 0; TAILQ_INIT(&_free_list); }; - // ��������, ɾ������Ԫ�� ~CRecyclePool() { Type* item = NULL; Type* tmp = NULL; @@ -79,7 +65,6 @@ public: _count = 0; }; - // ���û��´������� Type* AllocItem() { Type* item = TAILQ_FIRST(&_free_list); if (item != NULL) @@ -98,16 +83,13 @@ public: return item; }; - // �ͷŹ������� void FreeItem(Type* obj) { //obj->Reset(); TAILQ_INSERT_TAIL(&_free_list, obj, _link_entry); obj->_release_time = mt_time_ms(); _count++; }; - - // ���վ�� void RecycleItem(uint64_t now) { Type* item = NULL; Type* tmp = NULL; @@ -123,28 +105,21 @@ public: } }; - // �����Զ���ij�ʱʱ�� void SetExpiredTime(uint64_t expired) { _expired = expired; }; private: - List _free_list; ///< �������� - uint64_t _expired; ///< ��ʱʱ�� - uint32_t _count; ///< Ԫ�ؼ��� + List _free_list; + uint64_t _expired; + uint32_t _count; }; - - -/** - * @brief ÿ��IO����һ��������� - */ class CNetHandler : public HashKey { public: - // ���״̬���� enum { STATE_IN_SESSION = 0x1, STATE_IN_CONNECT = 0x2, @@ -152,23 +127,13 @@ public: STATE_IN_RECV = 0x8, STATE_IN_IDLE = 0x10, }; - - /** - * @brief �ڵ�Ԫ�ص�hash�㷨, ��ȡkey��hashֵ - * @return �ڵ�Ԫ�ص�hashֵ - */ + virtual uint32_t HashValue(); - /** - * @brief �ڵ�Ԫ�ص�cmp����, ͬһͰID��, ��key�Ƚ� - * @return �ڵ�Ԫ�ص�hashֵ - */ virtual int HashCmp(HashKey* rhs); - // ͬ���շ��ӿ� int32_t SendRecv(void* data, uint32_t len, uint32_t timeout); - // ��ȡ����buff��Ϣ, ��Ч��ֱ��helper���� void* GetRspBuff() { if (_rsp_buff != NULL) { return _rsp_buff->data; @@ -177,7 +142,6 @@ public: } }; - // ��ȡ����buff��Ϣ, ��Ч��ֱ��helper���� uint32_t GetRspLen() { if (_rsp_buff != NULL) { return _rsp_buff->data_len; @@ -185,8 +149,7 @@ public: return 0; } }; - - // ����rsp��Ϣ + void SetRespBuff(TSkBuffer* buff) { if (_rsp_buff != NULL) { delete_sk_buffer(_rsp_buff); @@ -196,34 +159,28 @@ public: _rsp_buff = buff; }; - // ����Э�������, Ĭ��UDP void SetProtoType(MT_PROTO_TYPE type) { _proto_type = type; }; - // ������������, Ĭ�ϳ����� void SetConnType(MT_CONN_TYPE type) { _conn_type = type; }; - // ����Ŀ��IP��ַ void SetDestAddress(struct sockaddr_in* dst) { if (dst != NULL) { memcpy(&_dest_ipv4, dst, sizeof(*dst)); } }; - // ����session����session id��Ϣ, �����0 void SetSessionId(uint64_t sid) { _session_id = sid; }; - // ����session�����ص����� void SetSessionCallback(CHECK_SESSION_CALLBACK function) { _callback = function; }; - // ��ȡ�ص�������Ϣ CHECK_SESSION_CALLBACK GetSessionCallback() { return _callback; }; @@ -231,113 +188,86 @@ public: public: - // �������Ӷ��� void Link(CSockLink* conn); - // �������Ӷ��� void Unlink(); - - // ����Ҫ�IJ�����Ϣ + int32_t CheckParams(); - // ��ȡ����, ͬʱ�������ȴ����ӵĶ����� int32_t GetConnLink(); - // ����Ҫ�IJ�����Ϣ int32_t WaitConnect(uint64_t timeout); - // ����Ҫ�IJ�����Ϣ int32_t WaitSend(uint64_t timeout); - - // ����Ҫ�IJ�����Ϣ + int32_t WaitRecv(uint64_t timeout); - // �����ڵȴ����Ӷ��� void SwitchToConn(); - // �л������Ͷ��� void SwitchToSend(); - // �л������ն��� void SwitchToRecv(); - - // �л�������״̬ + void SwitchToIdle(); - // �������Ӷ��� void DetachConn(); - - // ע��session���� + bool RegistSession(); - // ȡ��ע��session void UnRegistSession(); - // �������͵����� uint32_t SkipSendPos(uint32_t len); - // ���÷����� void SetErrNo(int32_t err) { _err_no = err; }; - // ��ȡ�������߳���Ϣ MicroThread* GetThread() { return _thread; }; - // ��ȡ�����͵�ָ�������ݳ��� void GetSendData(void*& data, uint32_t& len) { data = _req_data; len = _req_len; }; - - // ���ýӿ� + void Reset(); - // ���������� CNetHandler(); ~CNetHandler(); - // ���п�ݷ��ʵĺ궨�� TAILQ_ENTRY(CNetHandler) _link_entry; uint64_t _release_time; protected: - MicroThread* _thread; ///< �����߳�ָ����� - MT_PROTO_TYPE _proto_type; ///< Э������ - MT_CONN_TYPE _conn_type; ///< �������� - struct sockaddr_in _dest_ipv4; ///< ipv4Ŀ�ĵ�ַ - uint64_t _session_id; ///< �ỰID - CHECK_SESSION_CALLBACK _callback; ///< �Ự��ȡ�ص����� - uint32_t _state_flags; ///< �ڲ�״̬�ֶ� - int32_t _err_no; ///< ��������Ϣ - void* _conn_ptr; ///< socket ��·ָ�� - uint32_t _send_pos; ///< �ѷ��͵�posλ�� - uint32_t _req_len; ///< ��������� - void* _req_data; ///< �����ָ�� - TSkBuffer* _rsp_buff; ///< Ӧ��buff��Ϣ + MicroThread* _thread; + MT_PROTO_TYPE _proto_type; + MT_CONN_TYPE _conn_type; + struct sockaddr_in _dest_ipv4; + uint64_t _session_id; + CHECK_SESSION_CALLBACK _callback; + uint32_t _state_flags; + int32_t _err_no; + void* _conn_ptr; + uint32_t _send_pos; + uint32_t _req_len; + void* _req_data; + TSkBuffer* _rsp_buff; }; -typedef TAILQ_HEAD(__NetHandlerList, CNetHandler) TNetItemList; ///< ��Ч��˫������ -typedef CRecyclePool<TNetItemList, CNetHandler> TNetItemPool; ///< ��ʱ���յĶ���� +typedef TAILQ_HEAD(__NetHandlerList, CNetHandler) TNetItemList; +typedef CRecyclePool<TNetItemList, CNetHandler> TNetItemPool; - -/** - * @brief ��������·���� - */ class CSockLink : public KqueuerObj { public: - // ���״̬���� enum { LINK_CONNECTING = 0x1, LINK_CONNECTED = 0x2, }; - // ״̬���ж��� enum { LINK_IDLE_LIST = 1, LINK_CONN_LIST = 2, @@ -345,115 +275,74 @@ public: LINK_RECV_LIST = 4, }; - // ����socket��� int32_t CreateSock(); - // �ر���·�ľ�� void Close(); - // �������ӹ��� bool Connect(); bool Connected() { return (_state & LINK_CONNECTED); } - // �쳣��ֹ�Ĵ������� void Destroy(); - // ��ȡ�������� TNetItemList* GetItemList(int32_t type); - // ���������Ϣ void AppendToList(int32_t type, CNetHandler* item); - // ���������Ϣ void RemoveFromList(int32_t type, CNetHandler* item); - // ��ȡĿ��ip��Ϣ struct sockaddr_in* GetDestAddr(struct sockaddr_in* addr); - // �������ӹ��� int32_t SendData(void* data, uint32_t len); - // udp�������� int32_t SendCacheUdp(void* data, uint32_t len); - // tcp�������� int32_t SendCacheTcp(void* data, uint32_t len); - // ���Խ��ո�������ݵ���ʱbuff void ExtendRecvRsp(); - // ���ݷַ��������� int32_t RecvDispath(); - // ���ص�����, ���ȴ��Ŷӵȴ��л�ȡ, ���ݴӸ��ڵ��ȡ CHECK_SESSION_CALLBACK GetSessionCallback(); - // TCP����������������ַ� int32_t DispathTcp(); - // UDP����������������ַ� int32_t DispathUdp(); - // ��ѯ����sessionid������session��Ϣ CNetHandler* FindSession(uint64_t sid); - /** - * @brief �ɶ��¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ virtual int InputNotify(); - /** - * @brief ��д�¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ virtual int OutputNotify(); - - /** - * @brief �쳣֪ͨ�ӿ� - * @return ���Է���ֵ, ���������¼����� - */ + virtual int HangupNotify(); - - // �������������� CSockLink(); - ~CSockLink(); - - // �����ó�ʼ���� + ~CSockLink(); + void Reset(); - - // ֪ͨ�����߳� + void NotifyThread(CNetHandler* item, int32_t result); - // ֪ͨ�����߳� void NotifyAll(int32_t result); - // ����Э������, ����buff�ص�ָ�� void SetProtoType(MT_PROTO_TYPE type); - // �����ϼ�ָ����Ϣ void SetParentsPtr(void* ptr) { _parents = ptr; }; - // ��ȡ�ϼ��ڵ�ָ�� void* GetParentsPtr() { return _parents; }; - // ��ȡ�ϴεķ���ʱ�� uint64_t GetLastAccess() { return _last_access; }; - - public: - // ���п�ݷ��ʵĺ궨�� TAILQ_ENTRY(CSockLink) _link_entry; uint64_t _release_time; @@ -471,44 +360,32 @@ private: TSkBuffer* _rsp_buff; void* _parents; }; -typedef TAILQ_HEAD(__SocklinkList, CSockLink) TLinkList; ///< ��Ч��˫������ -typedef CRecyclePool<TLinkList, CSockLink> TLinkPool; ///< ��ʱ���յĶ���� - +typedef TAILQ_HEAD(__SocklinkList, CSockLink) TLinkList; +typedef CRecyclePool<TLinkList, CSockLink> TLinkPool; class CDestLinks : public CTimerNotify, public HashKey { public: - // ���캯�� CDestLinks(); - - // �������� ~CDestLinks(); - // ���ø��õĽӿں��� void Reset(); - // ������ʱ�� void StartTimer(); - // ��ȡһ������link, ��ʱ����ѯ CSockLink* GetSockLink(); - // �ͷ�һ������link void FreeSockLink(CSockLink* sock); - // ��ȡЭ������ MT_PROTO_TYPE GetProtoType() { return _proto_type; }; - // ��ȡ�������� MT_CONN_TYPE GetConnType() { return _conn_type; }; - - // ���ùؼ���Ϣ void SetKeyInfo(uint32_t ipv4, uint16_t port, MT_PROTO_TYPE proto, MT_CONN_TYPE conn) { _addr_ipv4 = ipv4; _net_port = port; @@ -516,37 +393,24 @@ public: _conn_type = conn; }; - // ����KEY��Ϣ void CopyKeyInfo(CDestLinks* key) { _addr_ipv4 = key->_addr_ipv4; _net_port = key->_net_port; _proto_type = key->_proto_type; _conn_type = key->_conn_type; }; - - // ��ȡIP port��Ϣ + void GetDestIP(uint32_t& ip, uint16_t& port) { ip = _addr_ipv4; port = _net_port; }; - /** - * @brief ��ʱ֪ͨ����, ��������·, ���������·���� - */ virtual void timer_notify(); - - /** - * @brief �ڵ�Ԫ�ص�hash�㷨, ��ȡkey��hashֵ - * @return �ڵ�Ԫ�ص�hashֵ - */ + virtual uint32_t HashValue() { return _addr_ipv4 ^ (((uint32_t)_net_port << 16) | (_proto_type << 8) | _conn_type); }; - /** - * @brief �ڵ�Ԫ�ص�cmp����, ͬһͰID��, ��key�Ƚ� - * @return �ڵ�Ԫ�ص�hashֵ - */ virtual int HashCmp(HashKey* rhs) { CDestLinks* data = (CDestLinks*)(rhs); if (!data) { @@ -568,120 +432,86 @@ public: return 0; }; - // ����session�����ص����� void SetDefaultCallback(CHECK_SESSION_CALLBACK function) { _dflt_callback = function; }; - // ��ȡ�ص�������Ϣ CHECK_SESSION_CALLBACK GetDefaultCallback() { return _dflt_callback; }; - // ���п�ݷ��ʵĺ궨�� TAILQ_ENTRY(CDestLinks) _link_entry; uint64_t _release_time; private: - uint32_t _timeout; ///< idle�ij�ʱʱ�� - uint32_t _addr_ipv4; ///< ip��ַ - uint16_t _net_port; ///< port �������� - MT_PROTO_TYPE _proto_type; ///< Э������ - MT_CONN_TYPE _conn_type; ///< �������� + uint32_t _timeout; + uint32_t _addr_ipv4; + uint16_t _net_port; + MT_PROTO_TYPE _proto_type; + MT_CONN_TYPE _conn_type; - uint32_t _max_links; ///< ��������� - uint32_t _curr_link; ///< ��ǰ������ - TLinkList _sock_list; ///< �������� - CHECK_SESSION_CALLBACK _dflt_callback; ///< Ĭ�ϵ�check���� + uint32_t _max_links; + uint32_t _curr_link; + TLinkList _sock_list; + CHECK_SESSION_CALLBACK _dflt_callback; }; -typedef TAILQ_HEAD(__DestlinkList, CDestLinks) TDestList; ///< ��Ч��˫������ -typedef CRecyclePool<TDestList, CDestLinks> TDestPool; ///< ��ʱ���յĶ���� +typedef TAILQ_HEAD(__DestlinkList, CDestLinks) TDestList; +typedef CRecyclePool<TDestList, CDestLinks> TDestPool; -/** - * @brief ���ӹ�������ģ�� - */ class CNetMgr { public: - /** - * @brief ��Ϣbuff��ȫ�ֹ�������ӿ� - * @return ȫ�־��ָ�� - */ static CNetMgr* Instance (void); - /** - * @brief ��Ϣ�����ӿ� - */ static void Destroy(void); - // ��ѯ�Ƿ��Ѿ�����ͬһ��sid�Ķ��� CNetHandler* FindNetItem(CNetHandler* key); - // ע��һ��item, �Ȳ�ѯ�����, ��֤��ͻ void InsertNetItem(CNetHandler* item); - // �Ƴ�һ��item���� void RemoveNetItem(CNetHandler* item); - // ��ѯ��һ��Ŀ��ip��links�ڵ� CDestLinks* FindCreateDest(CDestLinks* key); - // ɾ�������е�Ŀ����·��Ϣ void DeleteDestLink(CDestLinks* dst); - - // ��ѯ�Ƿ��Ѿ�����ͬһ��sid�Ķ��� + CDestLinks* FindDestLink(CDestLinks* key); - // ע��һ��item, �Ȳ�ѯ�����, ��֤��ͻ void InsertDestLink(CDestLinks* item); - - // �Ƴ�һ��item���� + void RemoveDestLink(CDestLinks* item); - /** - * @brief ��Ϣbuff���������� - */ ~CNetMgr(); - /** - * @brief ������Դ��Ϣ - */ void RecycleObjs(uint64_t now); - // ����һ������������ CNetHandler* AllocNetItem() { return _net_item_pool.AllocItem(); }; - // �ͷ�һ������������ void FreeNetItem(CNetHandler* item) { return _net_item_pool.FreeItem(item); }; - // ����һ��SOCK������· CSockLink* AllocSockLink() { return _sock_link_pool.AllocItem(); }; - // �ͷ�һ��SOCK������· void FreeSockLink(CSockLink* item) { return _sock_link_pool.FreeItem(item); }; - // ����һ��SOCK������· CDestLinks* AllocDestLink() { return _dest_ip_pool.AllocItem(); }; - // �ͷ�һ��SOCK������· void FreeDestLink(CDestLinks* item) { return _dest_ip_pool.FreeItem(item); }; - // ��ȡudp��buff����Ϣ TSkBuffMng* GetSkBuffMng(MT_PROTO_TYPE type) { if (type == NET_PROTO_TCP) { return &_tcp_pool; @@ -692,25 +522,18 @@ public: private: - /** - * @brief ��Ϣbuff�Ĺ��캯�� - */ CNetMgr(); - static CNetMgr * _instance; ///< �������� - HashList* _ip_hash; ///< Ŀ�ĵ�ַhash - HashList* _session_hash; ///< session id��hash - TSkBuffMng _udp_pool; ///< udp pool, 64K - TSkBuffMng _tcp_pool; ///< tcp pool, 4K - TDestPool _dest_ip_pool; ///< Ŀ��ip����� - TLinkPool _sock_link_pool; ///< socket pool - TNetItemPool _net_item_pool; ///< net handle pool + static CNetMgr * _instance; + HashList* _ip_hash; + HashList* _session_hash; + TSkBuffMng _udp_pool; + TSkBuffMng _tcp_pool; + TDestPool _dest_ip_pool; + TLinkPool _sock_link_pool; + TNetItemPool _net_item_pool; }; - - - - } #endif diff --git a/app/micro_thread/mt_net_api.h b/app/micro_thread/mt_net_api.h index 86971bb01..dbb940f23 100644 --- a/app/micro_thread/mt_net_api.h +++ b/app/micro_thread/mt_net_api.h @@ -19,7 +19,6 @@ /** * @file mt_net_api.h - * @info �̷߳�װ������ӿ��� **/ #ifndef __MT_NET_API_H__ @@ -29,90 +28,60 @@ namespace NS_MICRO_THREAD { -/** - * @brief Э�����Ͷ��� - */ enum MT_PROTO_TYPE { NET_PROTO_UNDEF = 0, - NET_PROTO_UDP = 0x1, ///< �������� UDP - NET_PROTO_TCP = 0x2 ///< �������� TCP + NET_PROTO_UDP = 0x1, + NET_PROTO_TCP = 0x2 }; -/** - * @brief �������Ͷ��� - */ enum MT_RC_TYPE { RC_SUCCESS = 0, - RC_ERR_SOCKET = -1, ///< ����socketʧ�� - RC_SEND_FAIL = -2, ///< ����ʧ�� - RC_RECV_FAIL = -3, ///< ����ʧ�� - RC_CONNECT_FAIL = -4, ///< ����ʧ�� - RC_CHECK_PKG_FAIL = -5, ///< ���ļ��ʧ�� - RC_NO_MORE_BUFF = -6, ///< �ռ䳬������ - RC_REMOTE_CLOSED = -7, ///< ��˹ر����� + RC_ERR_SOCKET = -1, + RC_SEND_FAIL = -2, + RC_RECV_FAIL = -3, + RC_CONNECT_FAIL = -4, + RC_CHECK_PKG_FAIL = -5, + RC_NO_MORE_BUFF = -6, + RC_REMOTE_CLOSED = -7, - RC_INVALID_PARAM = -10, ///< ��Ч���� - RC_INVALID_HANDLER = -11, ///< ��Ч�ľ�� - RC_MEM_ERROR = -12, ///< �ڴ��쳣 - RC_CONFLICT_SID = -13, ///< SESSION ID��ͻ - RC_KQUEUE_ERROR = -14, ///< rst�źŵ� + RC_INVALID_PARAM = -10, + RC_INVALID_HANDLER = -11, + RC_MEM_ERROR = -12, + RC_CONFLICT_SID = -13, + RC_KQUEUE_ERROR = -14, }; - -/** - * @brief ��鱨���Ƿ�����, ����ȡsession�Ļص����� - * @info �ṩneed_len������ԭ��, ������ȷ�ϱ��ij���ʱ, ����ÿ����չϣ������ - * �����������ֵ����������, ��������������� - * @param data -ʵ�ʽ��յ�����ָ�� - * @param len -�Ѿ����ջ����ij��� - * @param session_id -�ɹ�������sessionid��Ϣ - * @param need_len -ϣ����չһ��buff, Ŀǰ���100M - * @return >0 �ɹ���������ʵ�ʵİ�����; =0 ���IJ�����, �������ո�������; <0 ����ʧ�� - */ typedef int32_t (*CHECK_SESSION_CALLBACK)(const void* data, uint32_t len, uint64_t* session_id, uint32_t* need_len); - -/** - * @brief ����ӿ��ඨ�� - */ class CNetHelper { public: - // ת����������Ϣ, �����ȡ static char* GetErrMsg(int32_t result); - // ͬ���շ��ӿ� int32_t SendRecv(void* data, uint32_t len, uint32_t timeout); - // ��ȡ����buff��Ϣ, ��Ч��ֱ��helper���� void* GetRspBuff(); - // ��ȡ���ذ��ij��� uint32_t GetRspLen(); - // ����Э�������, Ĭ��UDP void SetProtoType(MT_PROTO_TYPE type); - // ����Ŀ��IP��ַ - void SetDestAddress(struct sockaddr_in* dst); + void SetDestAddress(struct sockaddr_in* dst); - // ����session����session id��Ϣ, �����0 - void SetSessionId(uint64_t sid); + void SetSessionId(uint64_t sid); - // ����session�����ص����� void SetSessionCallback(CHECK_SESSION_CALLBACK function); - // �������鹹 CNetHelper(); ~CNetHelper(); private: - void* handler; // ˽�о��, ������չ + void* handler; }; diff --git a/app/micro_thread/mt_notify.cpp b/app/micro_thread/mt_notify.cpp index 035b6c913..6f007455b 100644 --- a/app/micro_thread/mt_notify.cpp +++ b/app/micro_thread/mt_notify.cpp @@ -19,7 +19,6 @@ /** * @file mt_notify.cpp - * @info �̵߳���ע��������ʵ�� * @time 20130924 **/ #include <fcntl.h> @@ -39,11 +38,6 @@ using namespace std; using namespace NS_MICRO_THREAD; - -/** - * @brief ֪ͨ��������ȴ�״̬, ����ȴ������� - * @param proxy ������sessionģ�� - */ void ISessionNtfy::InsertWriteWait(SessionProxy* proxy) { if (!proxy->_flag) { @@ -52,10 +46,6 @@ void ISessionNtfy::InsertWriteWait(SessionProxy* proxy) } } -/** - * @brief ֪ͨ�����Ƴ��ȴ�״̬ - * @param proxy ������sessionģ�� - */ void ISessionNtfy::RemoveWriteWait(SessionProxy* proxy) { if (proxy->_flag) { @@ -64,10 +54,6 @@ void ISessionNtfy::RemoveWriteWait(SessionProxy* proxy) } } -/** - * @brief �۲���ģʽ, ֪ͨд�ȴ��߳� - * @info UDP����֪ͨÿ���߳�ִ��д����, TCP��Ҫ�Ŷ�д - */ void UdpSessionNtfy::NotifyWriteWait() { MtFrame* frame = MtFrame::Instance(); @@ -86,21 +72,15 @@ void UdpSessionNtfy::NotifyWriteWait() } } -/** - * @brief ����socket, �����ɶ��¼� - * @return fd�ľ��, <0 ʧ�� - */ int UdpSessionNtfy::CreateSocket() { - // 1. UDP������, ÿ���´�SOCKET int osfd = socket(AF_INET, SOCK_DGRAM, 0); if (osfd < 0) { MTLOG_ERROR("socket create failed, errno %d(%s)", errno, strerror(errno)); return -1; } - - // 2. ���������� + int flags = 1; if (ioctl(osfd, FIONBIO, &flags) < 0) { @@ -110,7 +90,6 @@ int UdpSessionNtfy::CreateSocket() return -2; } - // ��ѡbindִ��, ���ñ���port��ִ�� if (_local_addr.sin_port != 0) { int ret = bind(osfd, (struct sockaddr *)&_local_addr, sizeof(_local_addr)); @@ -124,7 +103,6 @@ int UdpSessionNtfy::CreateSocket() } } - // 3. ���¹�����Ϣ, Ĭ��udp session ���� epollin this->SetOsfd(osfd); this->EnableInput(); MtFrame* frame = MtFrame::Instance(); @@ -134,10 +112,6 @@ int UdpSessionNtfy::CreateSocket() return osfd; } - -/** - * @brief �ر�socket, ֹͣ�����ɶ��¼� - */ void UdpSessionNtfy::CloseSocket() { int osfd = this->GetOsfd(); @@ -152,11 +126,6 @@ void UdpSessionNtfy::CloseSocket() } } - -/** - * @brief �ɶ��¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ int UdpSessionNtfy::InputNotify() { while (1) @@ -164,7 +133,6 @@ int UdpSessionNtfy::InputNotify() int ret = 0; int have_rcv_len = 0; - // 1. ��ȡ�հ�������, ����ѡ��δ�����������buff if (!_msg_buff) { _msg_buff = MsgBuffPool::Instance()->GetMsgBuf(this->GetMsgBuffSize()); if (NULL == _msg_buff) { @@ -175,7 +143,6 @@ int UdpSessionNtfy::InputNotify() } char* buff = (char*)_msg_buff->GetMsgBuff(); - // 2. ��ȡsocket, �հ����� int osfd = this->GetOsfd(); struct sockaddr_in from; socklen_t fromlen = sizeof(from); @@ -191,13 +158,13 @@ int UdpSessionNtfy::InputNotify() else { MTLOG_ERROR("recv error, fd %d", osfd); - return 0; // ϵͳ����, UDP �ݲ��ر� + return 0; } } else if (ret == 0) { MTLOG_DEBUG("remote close connection, fd %d", osfd); - return 0; // �Զ˹ر�, UDP �ݲ��ر� + return 0; } else { @@ -206,7 +173,6 @@ int UdpSessionNtfy::InputNotify() _msg_buff->SetMsgLen(have_rcv_len); } - // 3. �����Ϣ��������, ��ȡsessionid int sessionid = 0; ret = this->GetSessionId(buff, have_rcv_len, sessionid); if (ret <= 0) @@ -218,18 +184,16 @@ int UdpSessionNtfy::InputNotify() return 0; } - // 4. ӳ���ѯthread���, ����handle���, ���ö��¼�����, �ҽ�msgbuff ISession* session = SessionMgr::Instance()->FindSession(sessionid); if (NULL == session) { - MT_ATTR_API(350403, 1); // session �����ѳ�ʱ + MT_ATTR_API(350403, 1); MTLOG_DEBUG("session %d, not find, maybe timeout, drop pkg", sessionid); MsgBuffPool::Instance()->FreeMsgBuf(_msg_buff); _msg_buff = NULL; return 0; } - // 5. �ҽ�recvbuff, �����߳� IMtConnection* conn = session->GetSessionConn(); MicroThread* thread = session->GetOwnerThread(); if (!thread || !conn || !conn->GetNtfyObj()) @@ -259,43 +223,26 @@ int UdpSessionNtfy::InputNotify() return 0; } -/** - * @brief ��д�¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ int UdpSessionNtfy::OutputNotify() { NotifyWriteWait(); return 0; } -/** - * @brief �쳣֪ͨ�ӿ�, �ر�fd����, thread�ȴ�������ʱ - * @return ���Է���ֵ, ���������¼����� - */ int UdpSessionNtfy::HangupNotify() { - // 1. ����epoll ctl�����¼� MtFrame* frame = MtFrame::Instance(); frame->KqueueCtrlDel(this->GetOsfd(), this->GetEvents()); MTLOG_ERROR("sesson obj %p, recv error event. fd %d", this, this->GetOsfd()); - - // 2. ���´�socket + CloseSocket(); - // 3. �ؼ���epoll listen CreateSocket(); return 0; } -/** - * @brief ����epoll�����¼��Ļص��ӿ�, ������ʼ��EPOLLIN, ż��EPOLLOUT - * @param args fd���ö����ָ�� - * @return 0 �ɹ�, < 0 ʧ��, Ҫ������ع�������ǰ״̬ - * @info Ĭ���Ǽ����ɶ��¼���, ����ֻ������д�¼��ļ���ɾ�� - */ int UdpSessionNtfy::KqueueCtlAdd(void* args) { MtFrame* frame = MtFrame::Instance(); @@ -304,7 +251,6 @@ int UdpSessionNtfy::KqueueCtlAdd(void* args) int osfd = this->GetOsfd(); - // ֪ͨ������Ҫ����, FD֪ͨ���������ϲ��Ḵ��, ��������ͻ���, �쳣log��¼ KqueuerObj* old_obj = fd_ref->GetNotifyObj(); if ((old_obj != NULL) && (old_obj != this)) { @@ -312,7 +258,6 @@ int UdpSessionNtfy::KqueueCtlAdd(void* args) return -1; } - // ���ÿ�ܵ�epoll ctl�ӿ�, ����epoll ctrlϸ�� if (!frame->KqueueCtrlAdd(osfd, KQ_EVENT_WRITE)) { MTLOG_ERROR("epfd ref add failed, log"); @@ -323,11 +268,6 @@ int UdpSessionNtfy::KqueueCtlAdd(void* args) return 0; } -/** - * @brief ����epoll�����¼��Ļص��ӿ�, ������ʼ��EPOLLIN, ż��EPOLLOUT - * @param args fd���ö����ָ�� - * @return 0 �ɹ�, < 0 ʧ��, Ҫ������ع�������ǰ״̬ - */ int UdpSessionNtfy::KqueueCtlDel(void* args) { MtFrame* frame = MtFrame::Instance(); @@ -335,8 +275,7 @@ int UdpSessionNtfy::KqueueCtlDel(void* args) //ASSERT(fd_ref != NULL); int osfd = this->GetOsfd(); - - // ֪ͨ������Ҫ����, FD֪ͨ���������ϲ��Ḵ��, ��������ͻ���, �쳣log��¼ + KqueuerObj* old_obj = fd_ref->GetNotifyObj(); if (old_obj != this) { @@ -344,7 +283,6 @@ int UdpSessionNtfy::KqueueCtlDel(void* args) return -1; } - // ���ÿ�ܵ�epoll ctl�ӿ�, ����epoll ctrlϸ�� if (!frame->KqueueCtrlDel(osfd, KQ_EVENT_WRITE)) { MTLOG_ERROR("epfd ref del failed, log"); @@ -356,41 +294,24 @@ int UdpSessionNtfy::KqueueCtlDel(void* args) } - -/** - * @brief �ɶ��¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ int TcpKeepNtfy::InputNotify() { KeepaliveClose(); return -1; } - -/** - * @brief ��д�¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ + int TcpKeepNtfy::OutputNotify() { KeepaliveClose(); return -1; } - -/** - * @brief �쳣֪ͨ�ӿ� - * @return ���Է���ֵ, ���������¼����� - */ + int TcpKeepNtfy::HangupNotify() { KeepaliveClose(); return -1; } - -/** - * @brief ����ʵ�����ӹرղ��� - */ void TcpKeepNtfy::KeepaliveClose() { if (_keep_conn) { @@ -400,12 +321,7 @@ void TcpKeepNtfy::KeepaliveClose() MTLOG_ERROR("_keep_conn ptr null, error"); } } - -/** - * @brief sessionȫ�ֹ������ - * @return ȫ�־��ָ�� - */ NtfyObjMgr* NtfyObjMgr::_instance = NULL; NtfyObjMgr* NtfyObjMgr::Instance (void) { @@ -417,9 +333,6 @@ NtfyObjMgr* NtfyObjMgr::Instance (void) return _instance; } -/** - * @brief session����ȫ�ֵ����ٽӿ� - */ void NtfyObjMgr::Destroy() { if( _instance != NULL ) @@ -429,26 +342,14 @@ void NtfyObjMgr::Destroy() } } -/** - * @brief ��Ϣbuff�Ĺ��캯�� - */ NtfyObjMgr::NtfyObjMgr() { } -/** - * @brief ��������, ��������Դ, ������������ - */ NtfyObjMgr::~NtfyObjMgr() { } -/** - * @brief ע�᳤����session��Ϣ - * @param session_name �����ӵı�ʶ, ÿ�����Ӵ���һ��session��װ��ʽ - * @param session �����Ӷ���ָ��, ������������ - * @return 0 �ɹ�, < 0 ʧ�� - */ int NtfyObjMgr::RegisterSession(int session_name, ISessionNtfy* session) { if (session_name <= 0 || NULL == session) { @@ -468,11 +369,6 @@ int NtfyObjMgr::RegisterSession(int session_name, ISessionNtfy* session) return 0; } -/** - * @brief ��ȡע�᳤����session��Ϣ - * @param session_name �����ӵı�ʶ, ÿ�����Ӵ���һ��session��װ��ʽ - * @return ������ָ��, ʧ��ΪNULL - */ ISessionNtfy* NtfyObjMgr::GetNameSession(int session_name) { SessionMap::iterator it = _session_map.find(session_name); @@ -486,12 +382,6 @@ ISessionNtfy* NtfyObjMgr::GetNameSession(int session_name) } } -/** - * @brief ��ȡͨ��֪ͨ����, ���߳�֪ͨ������session֪ͨ�������� - * @param type ����, �߳�֪ͨ���ͣ�UDP/TCP SESSION֪ͨ�� - * @param session_name proxyģ��,һ����ȡsession���� - * @return ֪ͨ�����ָ��, ʧ��ΪNULL - */ KqueuerObj* NtfyObjMgr::GetNtfyObj(int type, int session_name) { KqueuerObj* obj = NULL; @@ -515,7 +405,6 @@ KqueuerObj* NtfyObjMgr::GetNtfyObj(int type, int session_name) break; } - // ��ȡ�ײ�ij����Ӷ���, ����������ʵ�ʵ�֪ͨ���� if (proxy) { ISessionNtfy* ntfy = this->GetNameSession(session_name); if (!ntfy) { @@ -531,10 +420,6 @@ KqueuerObj* NtfyObjMgr::GetNtfyObj(int type, int session_name) } -/** - * @brief �ͷ�֪ͨ����ָ�� - * @param obj ֪ͨ���� - */ void NtfyObjMgr::FreeNtfyObj(KqueuerObj* obj) { SessionProxy* proxy = NULL; @@ -566,6 +451,3 @@ void NtfyObjMgr::FreeNtfyObj(KqueuerObj* obj) delete obj; return; } - - - diff --git a/app/micro_thread/mt_notify.h b/app/micro_thread/mt_notify.h index b4072019c..80acf1475 100644 --- a/app/micro_thread/mt_notify.h +++ b/app/micro_thread/mt_notify.h @@ -19,7 +19,6 @@ /** * @file mt_notify.h - * @info �߳�ע���֪ͨ����������� * @time 20130926 **/ @@ -39,91 +38,43 @@ using std::map; class SessionProxy; class TcpKeepConn; -/** - * @brief ֪ͨ�������� - */ enum NTFY_OBJ_TYPE { - NTFY_OBJ_UNDEF = 0, ///< δ��������Ӷ��� - NTFY_OBJ_THREAD = 1, ///< �����Ӷ���, һ��fd��Ӧһ��thread - NTFY_OBJ_KEEPALIVE = 2, ///< TCP�������ֵ�notify����, ������ thread - NTFY_OBJ_SESSION = 3, ///< UDP��sessionģ��, �����ij����Ӷ��� + NTFY_OBJ_UNDEF = 0, + NTFY_OBJ_THREAD = 1, + NTFY_OBJ_KEEPALIVE = 2, + NTFY_OBJ_SESSION = 3, }; -/** - * @brief Э�����Ͷ��� - */ enum MULTI_PROTO { MT_UNKNOWN = 0, - MT_UDP = 0x1, ///< �������� UDP - MT_TCP = 0x2 ///< �������� TCP + MT_UDP = 0x1, + MT_TCP = 0x2 }; -/** - * @brief ������sessionģ��, �����շ����ȹ����ӿ� - */ typedef TAILQ_ENTRY(SessionProxy) NtfyEntry; typedef TAILQ_HEAD(__NtfyList, SessionProxy) NtfyList; class ISessionNtfy : public KqueuerObj { public: - /** - * @brief ��鱨��������, ͬʱ��ȡsessionid��Ϣ - * @param pkg ����ָ�� - * @param len �����ѽ��ճ��� - * @param session ������sessionid, ������� - * @return <=0 ʧ��, >0 ʵ�ʱ��ij��� - */ virtual int GetSessionId(void* pkg, int len, int& session) { return 0;}; - /** - * @brief ����socket, �����ɶ��¼� - * @return fd�ľ��, <0 ʧ�� - */ virtual int CreateSocket(){return -1;}; - /** - * @brief �ر�socket, ֹͣ�����ɶ��¼� - */ virtual void CloseSocket(){}; - /** - * @brief �ɶ��¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ virtual int InputNotify(){return 0;}; - /** - * @brief ��д�¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ virtual int OutputNotify(){return 0;}; - - /** - * @brief �쳣֪ͨ�ӿ� - * @return ���Է���ֵ, ���������¼����� - */ + virtual int HangupNotify(){return 0;}; - /** - * @brief ����epoll�����¼��Ļص��ӿ�, ������ʼ��EPOLLIN, ż��EPOLLOUT - * @param args fd���ö����ָ�� - * @return 0 �ɹ�, < 0 ʧ��, Ҫ������ع�������ǰ״̬ - */ virtual int KqueueCtlAdd(void* args){return 0;}; - /** - * @brief ����epoll�����¼��Ļص��ӿ�, ������ʼ��EPOLLIN, ż��EPOLLOUT - * @param args fd���ö����ָ�� - * @return 0 �ɹ�, < 0 ʧ��, Ҫ������ع�������ǰ״̬ - */ virtual int KqueueCtlDel(void* args){return 0;}; - /** - * @brief ���캯���������� - */ ISessionNtfy(): KqueuerObj(0) { _proto = MT_UDP; _buff_size = 0; @@ -132,85 +83,46 @@ public: } virtual ~ISessionNtfy() { }; - /** - * @brief ���ñ��δ�����proto��Ϣ - */ void SetProtoType(MULTI_PROTO proto) { _proto = proto; }; - /** - * @brief ��ȡ���δ�����proto��Ϣ - * @return proto type - */ MULTI_PROTO GetProtoType() { return _proto; }; - - /** - * @brief ����buff��С, ����ʵ��ʹ�õ�msgbuff���� - * @return 0�ɹ� - */ + void SetMsgBuffSize(int buff_size) { _buff_size = buff_size; }; - /** - * @brief ��ȡԤ�õ�buff��С, ��������, ����65535 - * @return ����������Ϣbuff��� - */ int GetMsgBuffSize() { return (_buff_size > 0) ? _buff_size : 65535; } - /** - * @brief ֪ͨ��������ȴ�״̬ - */ void InsertWriteWait(SessionProxy* proxy); - /** - * @brief ֪ͨ����ȡ���ȴ�״̬ - */ void RemoveWriteWait(SessionProxy* proxy); - /** - * @brief �۲���ģʽ, ֪ͨд�ȴ��߳� - * @info UDP����֪ͨÿ���߳�ִ��д����, TCP��Ҫ�Ŷ�д - */ + virtual void NotifyWriteWait(){}; protected: - MULTI_PROTO _proto; // Э������ UDP/TCP - int _buff_size; // �����Ϣ���� - NtfyList _write_list; // ��д�ȴ����� - MtMsgBuf* _msg_buff; // ��ʱ�հ���Ż����� + MULTI_PROTO _proto; + int _buff_size; + NtfyList _write_list; + MtMsgBuf* _msg_buff; }; -/** - * @brief UDP������sessionģ�͵Ļ���ӿ� - * @info ҵ��session��Ҫ�̳иýӿ�, ��������, ʵ�ֻ�ȡGetSessionId���� - * @info ������չ, ��ָ�����ض˿ڵ� - */ class UdpSessionNtfy : public ISessionNtfy { public: - - /** - * @brief ��鱨��������, ͬʱ��ȡsessionid��Ϣ, �ɼ̳���ʵ���� - * @param pkg ����ָ�� - * @param len �����ѽ��ճ��� - * @param session ������sessionid, ������� - * @return <=0 ʧ��, >0 ʵ�ʱ��ij��� - */ + virtual int GetSessionId(void* pkg, int len, int& session) { return 0;}; public: - /** - * @brief �������������� - */ UdpSessionNtfy() : ISessionNtfy(){ ISessionNtfy::SetProtoType(MT_UDP); @@ -220,61 +132,24 @@ public: } virtual ~UdpSessionNtfy() { }; - /** - * @brief �۲���ģʽ, ֪ͨд�ȴ��߳� - * @info UDP����֪ͨÿ���߳�ִ��д����, TCP��Ҫ�Ŷ�д - */ virtual void NotifyWriteWait(); - /** - * @brief ����socket, �����ɶ��¼� - * @return fd�ľ��, <0 ʧ�� - */ virtual int CreateSocket(); - /** - * @brief �ر�socket, ֹͣ�����ɶ��¼� - */ virtual void CloseSocket(); - /** - * @brief �ɶ��¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ virtual int InputNotify(); - - /** - * @brief ��д�¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ + virtual int OutputNotify(); - - /** - * @brief �쳣֪ͨ�ӿ� - * @return ���Է���ֵ, ���������¼����� - */ + virtual int HangupNotify(); - /** - * @brief ����epoll�����¼��Ļص��ӿ�, ������ʼ��EPOLLIN, ż��EPOLLOUT - * @param args fd���ö����ָ�� - * @return 0 �ɹ�, < 0 ʧ��, Ҫ������ع�������ǰ״̬ - */ virtual int KqueueCtlAdd(void* args); - /** - * @brief ����epoll�����¼��Ļص��ӿ�, ������ʼ��EPOLLIN, ż��EPOLLOUT - * @param args fd���ö����ָ�� - * @return 0 �ɹ�, < 0 ʧ��, Ҫ������ع�������ǰ״̬ - */ virtual int KqueueCtlDel(void* args); public: - /** - * @brief ����udp���صı���bind��ַ, �����bind���ͻ, ��ʱͣ�� - * ��������, �ܱ�֤ÿ����Ψһport��ʹ�� - */ void SetLocalAddr(struct sockaddr_in* local_addr) { memcpy(&_local_addr, local_addr, sizeof(_local_addr)); }; @@ -285,47 +160,28 @@ protected: }; - -/** - * @brief UDPģʽsessionģ�͵Ĵ���֪ͨ����, �������ӳ�䵽ijһ��session notify - * @info session proxy ��������epollע��, �������¼�֪ͨ, ����Ҫ���ij�ʱ�� - */ class SessionProxy : public KqueuerObj { public: - int _flag; ///< 0-���ڶ�����, 1-�ڵȴ����� - NtfyEntry _write_entry; ///< ������д�ȴ����еĹ������ + int _flag; + NtfyEntry _write_entry; - /** - * @brief ���ô�������, ����������fd��� - */ void SetRealNtfyObj(ISessionNtfy* obj) { _real_ntfy = obj; this->SetOsfd(obj->GetOsfd()); }; - - /** - * @brief ��ȡ��������ָ�� - */ + ISessionNtfy* GetRealNtfyObj() { return _real_ntfy; }; public: - /** - * @brief ���մ���, ������������ - */ virtual void Reset() { _real_ntfy = NULL; this->KqueuerObj::Reset(); }; - /** - * @brief ����epoll�����¼��Ļص��ӿ�, ������ʼ��EPOLLIN, ż��EPOLLOUT - * @param args fd���ö����ָ�� - * @return 0 �ɹ�, < 0 ʧ��, Ҫ������ع�������ǰ״̬ - */ virtual int KqueueCtlAdd(void* args) { if (!_real_ntfy) { return -1; @@ -344,11 +200,6 @@ public: return 0; }; - /** - * @brief ����epoll�����¼��Ļص��ӿ�, ������ʼ��EPOLLIN, ż��EPOLLOUT - * @param args fd���ö����ָ�� - * @return 0 �ɹ�, < 0 ʧ��, Ҫ������ع�������ǰ״̬ - */ virtual int KqueueCtlDel(void* args) { if (!_real_ntfy) { return -1; @@ -364,86 +215,48 @@ public: }; private: - ISessionNtfy* _real_ntfy; // ʵ�ʵ�ִ���� + ISessionNtfy* _real_ntfy; }; -/** - * @brief TCPģʽ��keepalive֪ͨ����, �������Ŀɶ��¼�, ȷ���Ƿ�Զ˹ر� - */ class TcpKeepNtfy: public KqueuerObj { public: - /** - * @brief ���캯�� - */ TcpKeepNtfy() : _keep_conn(NULL){}; - /** - * @brief �ɶ��¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ virtual int InputNotify(); - - /** - * @brief ��д�¼�֪ͨ�ӿ�, ����֪ͨ�������ܻ��ƻ�����, ���÷���ֵ���� - * @return 0 ��fd�ɼ������������¼�; !=0 ��fd�������ص����� - */ + virtual int OutputNotify(); - - /** - * @brief �쳣֪ͨ�ӿ� - * @return ���Է���ֵ, ���������¼����� - */ + virtual int HangupNotify(); - /** - * @brief ���ô������� - */ void SetKeepNtfyObj(TcpKeepConn* obj) { _keep_conn = obj; }; - /** - * @brief ��ȡ��������ָ�� - */ TcpKeepConn* GetKeepNtfyObj() { return _keep_conn; }; - - /** - * @brief ����ʵ�����ӹرղ��� - */ + void KeepaliveClose(); private: - TcpKeepConn* _keep_conn; // ʵ�ʵ����������� + TcpKeepConn* _keep_conn; }; - -/** - * @brief ��̬�ڴ��ģ����, ���ڷ���new/delete�Ķ������, ��һ���̶���������� - */ template<typename ValueType> class CPtrPool { public: - typedef typename std::queue<ValueType*> PtrQueue; ///< �ڴ�ָ����� + typedef typename std::queue<ValueType*> PtrQueue; public: - /** - * @brief ��̬�ڴ�ع��캯�� - * @param max �����ж��б����ָ��Ԫ��, Ĭ��500 - */ explicit CPtrPool(int max = 500) : _max_free(max), _total(0){}; - - /** - * @brief ��̬�ڴ����������, ����������freelist - */ + ~CPtrPool() { ValueType* ptr = NULL; while (!_ptr_list.empty()) { @@ -453,10 +266,6 @@ public: } }; - /** - * @brief �����ڴ�ָ��, ���ȴӻ����ȡ, ���п�����̬ new ���� - * @return ģ�����͵�ָ��Ԫ��, �ձ�ʾ�ڴ�����ʧ�� - */ ValueType* AllocPtr() { ValueType* ptr = NULL; if (!_ptr_list.empty()) { @@ -470,9 +279,6 @@ public: return ptr; }; - /** - * @brief �ͷ��ڴ�ָ��, �����ж��г������, ��ֱ���ͷ�, ������л��� - */ void FreePtr(ValueType* ptr) { if ((int)_ptr_list.size() >= _max_free) { delete ptr; @@ -483,15 +289,11 @@ public: }; protected: - PtrQueue _ptr_list; ///< ���ж��� - int _max_free; ///< ������Ԫ�� - int _total; ///< ����new�Ķ������ͳ�� + PtrQueue _ptr_list; + int _max_free; + int _total; }; - -/** - * @brief ֪ͨ����ȫ�ֹ����� - */ class NtfyObjMgr { public: @@ -499,70 +301,31 @@ public: typedef std::map<int, ISessionNtfy*> SessionMap; typedef CPtrPool<KqueuerObj> NtfyThreadQueue; typedef CPtrPool<SessionProxy> NtfySessionQueue; - - /** - * @brief �Ự�����ĵ�ȫ�ֹ�������ӿ� - * @return ȫ�־��ָ�� - */ + static NtfyObjMgr* Instance (void); - /** - * @brief �����ӿ� - */ static void Destroy(void); - /** - * @brief ע�᳤����session��Ϣ - * @param session_name �����ӵı�ʶ, ÿ�����Ӵ���һ��session��װ��ʽ - * @param session �����Ӷ���ָ��, ������������ - * @return 0 �ɹ�, < 0 ʧ�� - */ int RegisterSession(int session_name, ISessionNtfy* session); - /** - * @brief ��ȡע�᳤����session��Ϣ - * @param session_name �����ӵı�ʶ, ÿ�����Ӵ���һ��session��װ��ʽ - * @return ������ָ��, ʧ��ΪNULL - */ ISessionNtfy* GetNameSession(int session_name); - /** - * @brief ��ȡͨ��֪ͨ����, ���߳�֪ͨ������session֪ͨ�������� - * @param type ����, �߳�֪ͨ���ͣ�UDP/TCP SESSION֪ͨ�� - * @param session_name proxyģ��,һ����ȡsession���� - * @return ֪ͨ�����ָ��, ʧ��ΪNULL - */ KqueuerObj* GetNtfyObj(int type, int session_name = 0); - - /** - * @brief �ͷ�֪ͨ����ָ�� - * @param obj ֪ͨ���� - */ void FreeNtfyObj(KqueuerObj* obj); - /** - * @brief �������� - */ ~NtfyObjMgr(); private: - /** - * @brief ��Ϣbuff�Ĺ��캯�� - */ NtfyObjMgr(); - static NtfyObjMgr * _instance; ///< �������� - SessionMap _session_map; ///< ȫ�ֵ�ע��session���� - NtfyThreadQueue _fd_ntfy_pool; ///< fd֪ͨ���� - NtfySessionQueue _udp_proxy_pool; ///< fd֪ͨ���� + static NtfyObjMgr * _instance; + SessionMap _session_map; + NtfyThreadQueue _fd_ntfy_pool; + NtfySessionQueue _udp_proxy_pool; }; - - } #endif - - diff --git a/app/micro_thread/mt_self_echo.cpp b/app/micro_thread/mt_self_echo.cpp deleted file mode 100644 index b8ae08059..000000000 --- a/app/micro_thread/mt_self_echo.cpp +++ /dev/null @@ -1,251 +0,0 @@ - -/** - * Tencent is pleased to support the open source community by making MSEC available. - * - * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the GNU General Public License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. You may - * obtain a copy of the License at - * - * https://opensource.org/licenses/GPL-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the - * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, - * either express or implied. See the License for the specific language governing permissions - * and limitations under the License. - */ - - -#include <sys/types.h> -#include <sys/socket.h> -#include <unistd.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <time.h> -#include <string.h> -#include <stdio.h> -#include <stdlib.h> -#include "mt_incl.h" -#include "micro_thread.h" - -using namespace NS_MICRO_THREAD; - -static bool run = true; - -static struct sockaddr_in addr; - -int mt_tcp_create_sock(void) -{ - int fd; - //int flag; - - // ����socket - fd = socket(AF_INET, SOCK_STREAM, 0); - if (fd < 0) - { - printf("create tcp socket failed, error: %m\n"); - return -1; - } - - // ����socket������ - - /* - flag = fcntl(fd, F_GETFL, 0); - if (flag == -1) - { - ::close(fd); - printf("get fd flags failed, error: %m\n"); - return -2; - } - - if (flag & O_NONBLOCK) - return fd; - - if (fcntl(fd, F_SETFL, flag | O_NONBLOCK | O_NDELAY) == -1) - { - ::close(fd); - printf("set fd flags failed, error: %m\n"); - return -3; - } - */ - - - int nb = 1; - ioctl(fd, FIONBIO, &nb); - - return fd; -} - -void echo(void* arg) -{ - char buf[1024]; - int ret = 0; - int *p = (int *)arg; - int clt_fd = *p; - printf("start to echo with client: %d\n", clt_fd); - while (1) { - ret = mt_recv(clt_fd, (void*)buf,1024,0,-1); - if(ret<0) - { - printf("recv client data failed[%m]\n"); - mt_sleep(1); - break; - } - - ret = mt_send(clt_fd, (void*)buf, ret, 0, 1000); - if (ret < 0) { - printf("send client data failed[%m]\n"); - mt_sleep(1); - break; - } - } - if(clt_fd>0) close(clt_fd); - delete p; -} - -void server(void* arg) -{ - int fd = mt_tcp_create_sock(); - if(fd<0) - { - run = false; - printf("create listen socket failed\n"); - return; - } - - - int optval = 1; - unsigned optlen = sizeof(optval); - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, optlen); - - if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) - { - close(fd); - printf("bind failed [%m]\n"); - return ; - } - - if (listen(fd, 1024) < 0) - { - close(fd); - printf("listen failed[%m]\n"); - return ; - } - - int clt_fd = 0; - int *p; - while(run) - { - struct sockaddr_in client_addr; - int addr_len = sizeof(client_addr);; - - clt_fd = mt_accept(fd, (struct sockaddr*)&client_addr, (socklen_t*)&addr_len, -1); - if(clt_fd<0) - { - mt_sleep(1); - continue; - } - int nb = 1; - ioctl(clt_fd, FIONBIO, &nb); - p = new int(clt_fd); - printf("start a new micro thread to echo with client: %d\n", clt_fd); - mt_start_thread((void*)echo, (void *)p); - mt_sleep(1); - } - printf("server exit\n"); - - -} - -struct MsgCtx -{ - int check_count; - int msg_id; -}; - -int TcpMsgChecker(void* buf, int len, bool closed, void* msg_ctx, bool& msg_len_detected) -{ - - struct MsgCtx* ctx = (struct MsgCtx*)msg_ctx; - - ctx->check_count++; - printf("#%d msg check msg times #%d, buf=%p, len=%d, closed=%d\n", ctx->msg_id, ctx->check_count, buf,len,closed); - - if(len<4) - { - return 0; - } - - - int r_len=ntohl(*(uint32_t*)buf); - //if(r_len!=len) - // { - // return 0; - //} - msg_len_detected = true; - - return r_len; -} - - -/* -void client(void* arg) -{ - //char buf[1024]; - - struct MsgCtx ctx; - void* rcv_buf = NULL; - int rcv_len = 0; - bool keep_rcv_buf = true; - int ret = 0; - char snd_ch = 1; - int count=0; - while(true) - { - rcv_buf = NULL; - rcv_len = 1; - keep_rcv_buf = (((++count)%2) == 0); - ctx.check_count = 0; - ctx.msg_id = count; - ret = mt_tcpsendrcv_ex((struct sockaddr_in*)&addr, (void*)&snd_ch, 1, rcv_buf, rcv_len,20000, &TcpMsgChecker, (void*)&ctx, MT_TCP_SHORT,keep_rcv_buf); - if(ret<0) - { - printf("client send rcv failed[%m]\n"); - continue; - } - printf("#%d client tcp finished: rcv_len=%d, rcv_buf=%p, keep_rcv_buf=%d\n",count, rcv_len, rcv_buf,keep_rcv_buf); - - if(keep_rcv_buf) - { - if(rcv_buf==NULL) - { - printf("client should hold rcvbuf, something wrong\n"); - continue; - } - free(rcv_buf); - } - } - - printf("client exit!"); -} -*/ - -int main(int argc, char* argv[]) -{ - - memset((void*)&addr,0,sizeof(addr)); - addr.sin_family=AF_INET; - addr.sin_addr.s_addr = inet_addr("112.90.143.29"); - addr.sin_port = htons(19999); - - mt_init_frame(argc, argv); - - mt_start_thread((void*)server,NULL); - - while (run) { - mt_sleep(10); - } - - printf("main exit"); -} diff --git a/app/micro_thread/mt_session.cpp b/app/micro_thread/mt_session.cpp index 123e55870..c53d3da16 100644 --- a/app/micro_thread/mt_session.cpp +++ b/app/micro_thread/mt_session.cpp @@ -19,7 +19,6 @@ /** * @file mt_session.cpp - * @info �̺߳�����ӻỰ����ʵ�ֲ��� * @time 20130924 **/ @@ -29,23 +28,15 @@ using namespace std; using namespace NS_MICRO_THREAD; -/** - * @brief session�ӿ���Դ�Ի��մ��� - */ ISession::~ISession() { if (_session_flg) { SessionMgr* sessionmgr = SessionMgr::Instance(); sessionmgr->RemoveSession(_session_id); - _session_flg = (int)SESSION_IDLE; // �����, ��remove�����ڴ�����Ӵ��� + _session_flg = (int)SESSION_IDLE; } } - -/** - * @brief sessionȫ�ֹ������ - * @return ȫ�־��ָ�� - */ SessionMgr* SessionMgr::_instance = NULL; SessionMgr* SessionMgr::Instance (void) { @@ -57,9 +48,6 @@ SessionMgr* SessionMgr::Instance (void) return _instance; } -/** - * @brief session����ȫ�ֵ����ٽӿ� - */ void SessionMgr::Destroy() { if( _instance != NULL ) @@ -69,18 +57,12 @@ void SessionMgr::Destroy() } } -/** - * @brief ��Ϣbuff�Ĺ��캯�� - */ SessionMgr::SessionMgr() { _curr_session = 0; _hash_map = new HashList(100000); } -/** - * @brief ��������, ��������Դ, ������������ - */ SessionMgr::~SessionMgr() { if (_hash_map) { @@ -89,9 +71,6 @@ SessionMgr::~SessionMgr() } } -/** - * @brief Session���ݴ洢 - */ int SessionMgr::InsertSession(ISession* session) { if (!_hash_map || !session) { @@ -109,9 +88,6 @@ int SessionMgr::InsertSession(ISession* session) return _hash_map->HashInsert(session); } -/** - * @brief ��ѯsession���� - */ ISession* SessionMgr::FindSession(int session_id) { if (!_hash_map) { @@ -124,9 +100,6 @@ ISession* SessionMgr::FindSession(int session_id) return dynamic_cast<ISession*>(_hash_map->HashFind(&key)); } -/** - * @brief ɾ��session���� - */ void SessionMgr::RemoveSession(int session_id) { if (!_hash_map) { @@ -138,5 +111,3 @@ void SessionMgr::RemoveSession(int session_id) key.SetSessionId(session_id); return _hash_map->HashRemove(&key); } - - diff --git a/app/micro_thread/mt_session.h b/app/micro_thread/mt_session.h index d434aae33..77bc5a43f 100644 --- a/app/micro_thread/mt_session.h +++ b/app/micro_thread/mt_session.h @@ -19,7 +19,6 @@ /** * @file mt_session.h - * @info �̵߳��¼��Ự��������, ÿ��������ӹ���һsession��Ϣ * @time 20130924 **/ @@ -35,28 +34,19 @@ class IMtConnection; enum SESSION_FLAG { - SESSION_IDLE = 0, ///< SESSION δ����hash���� - SESSION_INUSE = 1, ///< SESSION �������״̬ + SESSION_IDLE = 0, + SESSION_INUSE = 1, }; -/** - * @brief ��������session�ӿڶ���, ����session��ӳ���thread,action�� - */ class ISession : public HashKey { public: - /** - * @brief �������������� - */ ISession() : _session_id(0), _session_flg(0), _thread(NULL), _connection(NULL) {}; virtual ~ISession(); public: - /** - * @brief �ỰID���������ȡ - */ void SetSessionId(int id) { _session_id = id; }; @@ -64,9 +54,6 @@ public: return _session_id; }; - /** - * @brief �����̵߳��������ȡ - */ MicroThread* GetOwnerThread(){ return _thread; }; @@ -74,9 +61,6 @@ public: _thread = thread; }; - /** - * @brief �������ӵ��������ȡ - */ IMtConnection* GetSessionConn(){ return _connection; }; @@ -84,9 +68,6 @@ public: _connection = conn; }; - /** - * @brief �Ựflag���������ȡ - */ void SetSessionFlag(int flag) { _session_flg = flag; }; @@ -94,52 +75,30 @@ public: return _session_flg; }; - /** - * @brief �ڵ�Ԫ�ص�hash�㷨, ��ȡkey��hashֵ - * @return �ڵ�Ԫ�ص�hashֵ - */ virtual uint32_t HashValue(){ return _session_id; }; - /** - * @brief �ڵ�Ԫ�ص�cmp����, ͬһͰID��, ��key�Ƚ� - * @return �ڵ�Ԫ�ص�hashֵ - */ virtual int HashCmp(HashKey* rhs){ return this->_session_id - (int)rhs->HashValue(); }; protected: - int _session_id; // �Ựid��Ϣ - int _session_flg; // ��¼session״̬ 0 -����hash��, 1 -hash������ - MicroThread* _thread; // �Ự������session - IMtConnection* _connection; // �Ự���������� + int _session_id; + int _session_flg; + MicroThread* _thread; + IMtConnection* _connection; }; -/** - * @brief ȫ�ֵ�session�����ṹ - */ class SessionMgr { public: - /** - * @brief �Ự�����ĵ�ȫ�ֹ�������ӿ� - * @return ȫ�־��ָ�� - */ static SessionMgr* Instance (void); - /** - * @brief ȫ�ֵ�ɾ���ӿ� - */ static void Destroy(); - /** - * @brief ��ȡsessionid - * @return ȫ�־��ָ�� - */ int GetSessionId(void) { _curr_session++; if (!_curr_session) { @@ -148,40 +107,23 @@ public: return _curr_session; }; - /** - * @brief Session���ݴ洢 - */ int InsertSession(ISession* session); - /** - * @brief ��ѯsession���� - */ ISession* FindSession(int session_id); - /** - * @brief ɾ��session���� - */ void RemoveSession(int session_id); - /** - * @brief �������� - */ ~SessionMgr(); private: - /** - * @brief ��Ϣbuff�Ĺ��캯�� - */ SessionMgr(); - static SessionMgr * _instance; ///< �������� - int _curr_session; ///< session���� - HashList* _hash_map; ///< ��sessionid hash�洢 + static SessionMgr * _instance; + int _curr_session; + HashList* _hash_map; }; } #endif - - diff --git a/app/micro_thread/mt_sys_hook.cpp b/app/micro_thread/mt_sys_hook.cpp index dd70f4109..737941c22 100644 --- a/app/micro_thread/mt_sys_hook.cpp +++ b/app/micro_thread/mt_sys_hook.cpp @@ -19,8 +19,6 @@ /** * @filename mt_sys_hook.cpp - * @info �߳�hookϵͳapi, �Բ��ö�����������, תͬ��Ϊ�첽�� - * ֻhook socket��ص�API, HOOK ����, �ο�pth��libcoʵ�� */ #include <stdio.h> @@ -44,25 +42,18 @@ using namespace NS_MICRO_THREAD; #define MT_FD_FLG_INUSE 0x1 #define MT_FD_FLG_UNBLOCK 0x2 - -/** - * @brief ÿsockt����һ�������ṹ, ����Ƿ���ҪHOOK, ��ʱʱ��� - */ typedef struct socket_hook_info { - int sock_flag; // �Ƿ�ʹ��HOOK, �Ƿ��û�����UNBLOCK - int read_timeout; // socket��ȡ��ʱʱ��, ms��λ - int write_timeout; // socketд�볬ʱʱ��, ms��λ + int sock_flag; + int read_timeout; + int write_timeout; }MtHookFd; -MtSyscallFuncTab g_mt_syscall_tab; // ȫ�ַ��ű� -int g_mt_hook_flag; // ȫ�ֿ��Ʊ�� -int g_ff_hook_flag; // ȫ�ֿ��Ʊ�� -static MtHookFd g_mt_hook_fd_tab[MT_HOOK_MAX_FD]; // ȫ��fd���� +MtSyscallFuncTab g_mt_syscall_tab; +int g_mt_hook_flag; +int g_ff_hook_flag; +static MtHookFd g_mt_hook_fd_tab[MT_HOOK_MAX_FD]; -/** - * @brief �ڲ��ӿ�, ��ȡhook fd��ص���Ϣ, socket Ĭ��hook, open Ĭ��no hook - */ MtHookFd* mt_hook_find_fd(int fd) { if ((fd < 0) || (fd >= MT_HOOK_MAX_FD)) { @@ -77,9 +68,6 @@ MtHookFd* mt_hook_find_fd(int fd) } } -/** - * @brief �ڲ��ӿ�, ����socket����hook, ֻ����socket, �������ļ�IO - */ void mt_hook_new_fd(int fd) { if ((fd < 0) || (fd >= MT_HOOK_MAX_FD)) { @@ -92,9 +80,6 @@ void mt_hook_new_fd(int fd) fd_info->write_timeout = 500; } -/** - * @brief �ڲ��ӿ�, �ر�hook socket - */ void mt_hook_free_fd(int fd) { if ((fd < 0) || (fd >= MT_HOOK_MAX_FD)) { @@ -107,9 +92,6 @@ void mt_hook_free_fd(int fd) fd_info->write_timeout = 0; } -/** - * @brief HOOK�ӿ�, ��ʼ��mt���, �ӹ�ϵͳapi, �ػ��û����õķ�������� - */ #ifdef __cplusplus extern "C" { #endif @@ -135,17 +117,14 @@ int ioctl(int fd, unsigned long cmd, ...) } } - return ff_hook_ioctl(fd, cmd, arg); + return ff_hook_ioctl(fd, cmd, arg); } -/** - * @brief HOOK�ӿ�, ��ʼ��mt���, �ӹ�ϵͳapi, Ĭ������unblock - */ int socket(int domain, int type, int protocol) { mt_hook_syscall(socket); - - if (!ff_hook_active()) + + if (!ff_hook_active()) { return mt_real_func(socket)(domain, type, protocol); } @@ -159,36 +138,29 @@ int socket(int domain, int type, int protocol) mt_hook_new_fd(fd); mt_hook_syscall(ioctl); - int nb = 1; - ff_hook_ioctl(fd, FIONBIO, &nb); + int nb = 1; + ff_hook_ioctl(fd, FIONBIO, &nb); return fd; } -/** - * @brief HOOK�ӿ�, ��ʼ��mt���, �ӹ�ϵͳapi - */ int close(int fd) { mt_hook_syscall(close); - if (!ff_hook_active()) + if (!ff_hook_active()) { return mt_real_func(close)(fd); } mt_hook_free_fd(fd); - return ff_hook_close(fd); + return ff_hook_close(fd); } - -/** - * @brief HOOK�ӿ�, ��ʼ��mt���, �ӹ�ϵͳapi - */ int connect(int fd, const struct sockaddr *address, socklen_t address_len) { mt_hook_syscall(connect); MtHookFd* hook_fd = mt_hook_find_fd(fd); - if (!mt_hook_active() || !hook_fd || !ff_hook_active()) + if (!mt_hook_active() || !hook_fd || !ff_hook_active()) { return mt_real_func(connect)(fd, address, address_len); } @@ -201,9 +173,6 @@ int connect(int fd, const struct sockaddr *address, socklen_t address_len) return MtFrame::connect(fd, address, (int)address_len, hook_fd->write_timeout); } -/** - * @brief HOOK�ӿ�, ��ʼ��mt���, �ӹ�ϵͳapi, �û��Ѿ�����unblock, ���� - */ ssize_t read(int fd, void *buf, size_t nbyte) { mt_hook_syscall(read); @@ -221,9 +190,6 @@ ssize_t read(int fd, void *buf, size_t nbyte) return MtFrame::read(fd, buf, nbyte, hook_fd->read_timeout); } -/** - * @brief HOOK�ӿ�, ��ʼ��mt���, �ӹ�ϵͳapi, �û��Ѿ�����unblock, ���� - */ ssize_t write(int fd, const void *buf, size_t nbyte) { mt_hook_syscall(write); @@ -241,9 +207,6 @@ ssize_t write(int fd, const void *buf, size_t nbyte) return MtFrame::write(fd, buf, nbyte, hook_fd->write_timeout); } -/** - * @brief HOOK�ӿ�, ��ʼ��mt���, �ӹ�ϵͳapi, �û��Ѿ�����unblock, ���� - */ ssize_t sendto(int fd, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len) { @@ -263,9 +226,6 @@ ssize_t sendto(int fd, const void *message, size_t length, int flags, dest_addr, dest_len, hook_fd->write_timeout); } -/** - * @brief HOOK�ӿ�, ��ʼ��mt���, �ӹ�ϵͳapi, �û��Ѿ�����unblock, ���� - */ ssize_t recvfrom(int fd, void *buffer, size_t length, int flags, struct sockaddr *address, socklen_t *address_len) { @@ -285,9 +245,6 @@ ssize_t recvfrom(int fd, void *buffer, size_t length, int flags, } -/** - * @brief HOOK�ӿ�, ��ʼ��mt���, �ӹ�ϵͳapi, �û��Ѿ�����unblock, ���� - */ ssize_t recv(int fd, void *buffer, size_t length, int flags) { mt_hook_syscall(recv); @@ -305,9 +262,6 @@ ssize_t recv(int fd, void *buffer, size_t length, int flags) return MtFrame::recv(fd, buffer, length, flags, hook_fd->read_timeout); } -/** - * @brief HOOK�ӿ�, ��ʼ��mt���, �ӹ�ϵͳapi, �û��Ѿ�����unblock, ���� - */ ssize_t send(int fd, const void *buf, size_t nbyte, int flags) { mt_hook_syscall(send); @@ -325,10 +279,6 @@ ssize_t send(int fd, const void *buf, size_t nbyte, int flags) return MtFrame::send(fd, buf, nbyte, flags, hook_fd->write_timeout); } - -/** - * @brief HOOK�ӿ�, ��ʼ��mt���, �ӹ�ϵͳapi, �ػ��û����õij�ʱʱ����Ϣ - */ int setsockopt(int fd, int level, int option_name, const void *option_value, socklen_t option_len) { mt_hook_syscall(setsockopt); @@ -351,14 +301,9 @@ int setsockopt(int fd, int level, int option_name, const void *option_value, soc } } - return ff_hook_setsockopt(fd, level, option_name, option_value, option_len); + return ff_hook_setsockopt(fd, level, option_name, option_value, option_len); } - - -/** - * @brief HOOK�ӿ�, ��ʼ��mt���, �ӹ�ϵͳapi, �ػ��û����õķ�������� - */ int fcntl(int fd, int cmd, ...) { va_list ap; @@ -388,7 +333,6 @@ int fcntl(int fd, int cmd, ...) return ff_hook_fcntl(fd, cmd, arg); } - int listen(int sockfd, int backlog) { mt_hook_syscall(listen); @@ -397,7 +341,7 @@ int listen(int sockfd, int backlog) return mt_real_func(listen)(sockfd, backlog); } - return ff_hook_listen(sockfd, backlog); + return ff_hook_listen(sockfd, backlog); } int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) @@ -408,7 +352,7 @@ int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) return mt_real_func(bind)(sockfd, addr, addrlen); } - return ff_hook_bind(sockfd, addr, addrlen); + return ff_hook_bind(sockfd, addr, addrlen); } int accept(int fd, struct sockaddr *addr, socklen_t *addrlen) @@ -419,7 +363,7 @@ int accept(int fd, struct sockaddr *addr, socklen_t *addrlen) return mt_real_func(accept)(fd, addr, addrlen); } - return ff_hook_accept(fd, addr, addrlen); + return ff_hook_accept(fd, addr, addrlen); } #ifdef __cplusplus diff --git a/app/micro_thread/mt_sys_hook.h b/app/micro_thread/mt_sys_hook.h index 45372ac85..489b92df9 100644 --- a/app/micro_thread/mt_sys_hook.h +++ b/app/micro_thread/mt_sys_hook.h @@ -19,8 +19,6 @@ /** * @filename mt_sys_hook.h - * @info �߳�hookϵͳapi, �Բ��ö�����������, תͬ��Ϊ�첽�� - * HOOK ����, �ο�pth��libcoʵ�� */ #ifndef _MT_SYS_HOOK___ @@ -35,10 +33,6 @@ extern "C" { #endif -/******************************************************************************/ -/* 1. HOOK �ĺ������岿�� */ -/******************************************************************************/ - typedef int (*func_socket)(int domain, int type, int protocol); typedef int (*func_bind)(int sockfd, const struct sockaddr *addr, socklen_t addrlen); typedef int (*func_listen)(int sockfd, int backlog); @@ -63,14 +57,6 @@ typedef int (*func_fcntl)(int fd, int cmd, ...); typedef unsigned int (*func_sleep)(unsigned int seconds); - -/******************************************************************************/ -/* 2. ȫ�ֵ�hook�����ṹ */ -/******************************************************************************/ - -/** - * @brief Hook��ԭʼ�������й�������, ֧�ֶ�̬�������� - */ typedef struct mt_syscall_func_tab { func_socket real_socket; @@ -88,25 +74,21 @@ typedef struct mt_syscall_func_tab func_fcntl real_fcntl; func_ioctl real_ioctl; - func_sleep real_sleep; // �ݲ�֧�֣���Ϊû����fd����, ��ֹ���� - func_select real_select; // �ݲ�֧��, 1024�������� - func_poll real_poll; // �ݲ�֧��, ȷ�������ʵʩ + func_sleep real_sleep; + func_select real_select; + func_poll real_poll; func_accept real_accept; -}MtSyscallFuncTab; +} MtSyscallFuncTab; - -/******************************************************************************/ -/* 3. ֱ�ӵ���ԭʼϵͳapi�Ľӿ� */ -/******************************************************************************/ -extern MtSyscallFuncTab g_mt_syscall_tab; // ȫ�ַ��ű� -extern int g_mt_hook_flag; // ȫ�ֿ��Ʊ�� -extern int g_ff_hook_flag; // ȫ�ֿ��Ʊ�� +extern MtSyscallFuncTab g_mt_syscall_tab; +extern int g_mt_hook_flag; +extern int g_ff_hook_flag; #define mt_hook_syscall(name) \ do { \ if (!g_mt_syscall_tab.real_##name) { \ - g_mt_syscall_tab.real_##name = (func_##name)dlsym(RTLD_NEXT, #name);\ + g_mt_syscall_tab.real_##name = (func_##name)dlsym(RTLD_NEXT, #name);\ } \ } while (0) diff --git a/app/micro_thread/mt_version.h b/app/micro_thread/mt_version.h index ed2e91143..c037b4088 100644 --- a/app/micro_thread/mt_version.h +++ b/app/micro_thread/mt_version.h @@ -17,17 +17,6 @@ */ -/** - * @file mt_version.h - * @info �̰߳汾�������¼��Ϣ�ļ� - * @time 20131018 - * ------------------------------------------------------------------------ - * @brief v0.2.0 - ֧��UDP��sessionģʽӳ�������� - * v0.2.1 - ֧��TCP��sendrcv�ӿ� - * v0.2.3 - ʵʱʱ��� - * v0.2.4 - ֧��TCP��sendrcv�ӿڶ�̬��չ����buf - */ - #ifndef _MT_VERSION_EX__ #define _MT_VERSION_EX__