mirror of https://github.com/F-Stack/f-stack.git
564 lines
12 KiB
C
564 lines
12 KiB
C
|
|
|||
|
/**
|
|||
|
* 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.
|
|||
|
*/
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @file mt_connection.h
|
|||
|
* @info <EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>岿<EFBFBD><EFBFBD>
|
|||
|
* @time 20130924
|
|||
|
**/
|
|||
|
|
|||
|
#ifndef __MT_CONNECTION_H__
|
|||
|
#define __MT_CONNECTION_H__
|
|||
|
|
|||
|
#include <netinet/in.h>
|
|||
|
#include <queue>
|
|||
|
#include "mt_mbuf_pool.h"
|
|||
|
#include "hash_list.h"
|
|||
|
#include "mt_action.h"
|
|||
|
|
|||
|
namespace NS_MICRO_THREAD {
|
|||
|
|
|||
|
using std::queue;
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
enum CONN_OBJ_TYPE
|
|||
|
{
|
|||
|
OBJ_CONN_UNDEF = 0, ///< δ<><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>
|
|||
|
OBJ_SHORT_CONN = 1, ///< <20><><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>, fd<66><64><EFBFBD><EFBFBD><EFBFBD>Ự, ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>CLOSE
|
|||
|
OBJ_TCP_KEEP = 2, ///< TCP<43>ĸ<EFBFBD><C4B8><EFBFBD>ģ<EFBFBD><C4A3>, ÿ<><C3BF>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD>ʹ<EFBFBD>ø<EFBFBD>fd, <20><><EFBFBD><EFBFBD><EFBFBD>ɸ<EFBFBD><C9B8><EFBFBD>
|
|||
|
OBJ_UDP_SESSION = 3, ///< UDP<44><50>sessionģ<6E><C4A3>, ÿ<><C3BF><EFBFBD>ӿɹ<D3BF><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>ʹ<EFBFBD><CAB9>
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>߳<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ӳ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
class IMtConnection
|
|||
|
{
|
|||
|
public:
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
IMtConnection();
|
|||
|
virtual ~IMtConnection();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD><EFBFBD>ո<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual void Reset();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|||
|
*/
|
|||
|
CONN_OBJ_TYPE GetConnType() {
|
|||
|
return _type;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>ACTIONָ<EFBFBD><EFBFBD>
|
|||
|
* @return IMtConnָ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void SetIMtActon(IMtAction* action ) {
|
|||
|
_action = action;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD>ڲ<EFBFBD>ACTIONָ<EFBFBD><EFBFBD>
|
|||
|
* @return IMtConnָ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
IMtAction* GetIMtActon() {
|
|||
|
return _action;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>ACTIONָ<EFBFBD><EFBFBD>
|
|||
|
* @return IMtConnָ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void SetNtfyObj(KqueuerObj* obj ) {
|
|||
|
_ntfy_obj = obj;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD>ڲ<EFBFBD>ACTIONָ<EFBFBD><EFBFBD>
|
|||
|
* @return IMtConnָ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
KqueuerObj* GetNtfyObj() {
|
|||
|
return _ntfy_obj;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>msgbuffָ<EFBFBD><EFBFBD>
|
|||
|
* @return IMtConnָ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void SetMtMsgBuff(MtMsgBuf* msg_buf) {
|
|||
|
_msg_buff = msg_buf;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD>ڲ<EFBFBD>msgbuffָ<EFBFBD><EFBFBD>
|
|||
|
* @return IMtConnָ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
MtMsgBuf* GetMtMsgBuff() {
|
|||
|
return _msg_buff;
|
|||
|
};
|
|||
|
|
|||
|
public:
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>socket<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳfd, < 0 ʧ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual int CreateSocket() {return 0;};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>˻Ựͨ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>TCP<EFBFBD><EFBFBD>connect<EFBFBD><EFBFBD>
|
|||
|
* @return 0 -<EFBFBD>ɹ<EFBFBD>, < 0 ʧ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual int OpenCnnect() {return 0;};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ʷ<EFBFBD><EFBFBD>ͳ<EFBFBD><EFBFBD><EFBFBD>, < 0 ʧ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual int SendData() {return 0;};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD>ν<EFBFBD><EFBFBD>ճ<EFBFBD><EFBFBD><EFBFBD>, < 0 ʧ<EFBFBD><EFBFBD>(-1 <EFBFBD>Զ˹ر<EFBFBD>; -2 <EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
*/
|
|||
|
virtual int RecvData() {return 0;};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ر<EFBFBD>socket<EFBFBD>˿<EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳfd, < 0 ʧ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual int CloseSocket() {return 0;};
|
|||
|
|
|||
|
protected:
|
|||
|
|
|||
|
CONN_OBJ_TYPE _type; // Ԥ<>õ<EFBFBD>type, <20>ɰ<EFBFBD>type<70><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
IMtAction* _action; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>actionָ<6E><D6B8>, <20>ϼ<EFBFBD>ָ<EFBFBD><D6B8>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
KqueuerObj* _ntfy_obj; // EPOLL֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD>, <20>¼<EFBFBD>ָ<EFBFBD><D6B8>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
MtMsgBuf* _msg_buff; // <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff<66>ֶ<EFBFBD>, <20>¼<EFBFBD>ָ<EFBFBD><D6B8>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>sock<EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
class UdpShortConn : public IMtConnection
|
|||
|
{
|
|||
|
public:
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>socket<EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵĹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
UdpShortConn() {
|
|||
|
_osfd = -1;
|
|||
|
_type = OBJ_SHORT_CONN;
|
|||
|
};
|
|||
|
virtual ~UdpShortConn() {
|
|||
|
CloseSocket();
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD><EFBFBD>ո<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual void Reset();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>socket<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳfd, < 0 ʧ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual int CreateSocket();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ʷ<EFBFBD><EFBFBD>ͳ<EFBFBD><EFBFBD><EFBFBD>, < 0 ʧ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual int SendData();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD>ν<EFBFBD><EFBFBD>ճ<EFBFBD><EFBFBD><EFBFBD>, < 0 ʧ<EFBFBD><EFBFBD>(-1 <EFBFBD>Զ˹ر<EFBFBD>; -2 <EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
*/
|
|||
|
virtual int RecvData();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ر<EFBFBD>socket<EFBFBD>˿<EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳfd, < 0 ʧ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual int CloseSocket();
|
|||
|
|
|||
|
protected:
|
|||
|
int _osfd; // ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>socket
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
enum TcpKeepFlag
|
|||
|
{
|
|||
|
TCP_KEEP_IN_LIST = 0x1,
|
|||
|
TCP_KEEP_IN_KQUEUE = 0x2,
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>session<EFBFBD><EFBFBD>UDP<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
class UdpSessionConn : public IMtConnection
|
|||
|
{
|
|||
|
public:
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>socket<EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵĹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
UdpSessionConn() {
|
|||
|
_type = OBJ_UDP_SESSION;
|
|||
|
};
|
|||
|
virtual ~UdpSessionConn() { };
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>socket<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳfd, < 0 ʧ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual int CreateSocket();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ʷ<EFBFBD><EFBFBD>ͳ<EFBFBD><EFBFBD><EFBFBD>, < 0 ʧ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual int SendData();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD>ν<EFBFBD><EFBFBD>ճ<EFBFBD><EFBFBD><EFBFBD>, < 0 ʧ<EFBFBD><EFBFBD>(-1 <EFBFBD>Զ˹ر<EFBFBD>; -2 <EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
*/
|
|||
|
virtual int RecvData();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ر<EFBFBD>socket<EFBFBD>˿<EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳfd, < 0 ʧ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual int CloseSocket();
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>sock<EFBFBD><EFBFBD>TCP<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
typedef TAILQ_ENTRY(TcpKeepConn) KeepConnLink;
|
|||
|
typedef TAILQ_HEAD(__KeepConnTailq, TcpKeepConn) KeepConnList;
|
|||
|
class TcpKeepConn : public IMtConnection, public CTimerNotify
|
|||
|
{
|
|||
|
public:
|
|||
|
|
|||
|
int _keep_flag; // <20><><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD>
|
|||
|
KeepConnLink _keep_entry; // <20><><EFBFBD>й<EFBFBD><D0B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>socket<EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵĹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
TcpKeepConn() {
|
|||
|
_osfd = -1;
|
|||
|
_keep_time = 10*60*1000; // Ĭ<><C4AC>10<31><30><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><D4B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
_keep_flag = 0;
|
|||
|
_type = OBJ_TCP_KEEP;
|
|||
|
_keep_ntfy.SetKeepNtfyObj(this);
|
|||
|
};
|
|||
|
virtual ~TcpKeepConn() {
|
|||
|
CloseSocket();
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD><EFBFBD>ո<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual void Reset();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>˻Ựͨ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>TCP<EFBFBD><EFBFBD>connect<EFBFBD><EFBFBD>
|
|||
|
* @return 0 -<EFBFBD>ɹ<EFBFBD>, < 0 ʧ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual int OpenCnnect();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>socket<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳfd, < 0 ʧ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual int CreateSocket();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ʷ<EFBFBD><EFBFBD>ͳ<EFBFBD><EFBFBD><EFBFBD>, < 0 ʧ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual int SendData();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD>ν<EFBFBD><EFBFBD>ճ<EFBFBD><EFBFBD><EFBFBD>, < 0 ʧ<EFBFBD><EFBFBD>(-1 <EFBFBD>Զ˹ر<EFBFBD>; -2 <EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
*/
|
|||
|
virtual int RecvData();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ر<EFBFBD>socket<EFBFBD>˿<EFBFBD>
|
|||
|
* @return >0 -<EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳfd, < 0 ʧ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual int CloseSocket();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӱ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void ConnReuseClean();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief Idle<EFBFBD><EFBFBD><EFBFBD>洦<EFBFBD><EFBFBD>, epoll <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>˹رյ<EFBFBD>
|
|||
|
*/
|
|||
|
bool IdleAttach();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief Idleȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>洦<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>˹ر<EFBFBD>
|
|||
|
*/
|
|||
|
bool IdleDetach();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>洢Ŀ<EFBFBD>ĵ<EFBFBD>ַ<EFBFBD><EFBFBD>Ϣ, <EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void SetDestAddr(struct sockaddr_in* dst) {
|
|||
|
memcpy(&_dst_addr, dst, sizeof(_dst_addr));
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>ȡĿ<EFBFBD>ĵ<EFBFBD>ַ<EFBFBD><EFBFBD>Ϣ
|
|||
|
*/
|
|||
|
struct sockaddr_in* GetDestAddr() {
|
|||
|
return &_dst_addr;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>ʱ֪ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual void timer_notify();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ó<EFBFBD>ʱʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>뵥λ
|
|||
|
*/
|
|||
|
void SetKeepTime(unsigned int time) {
|
|||
|
_keep_time = time;
|
|||
|
};
|
|||
|
|
|||
|
protected:
|
|||
|
int _osfd; // ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>socket
|
|||
|
unsigned int _keep_time; // <20><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
TcpKeepNtfy _keep_ntfy; // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>
|
|||
|
struct sockaddr_in _dst_addr; // Զ<>˵<EFBFBD>ַ<EFBFBD><D6B7>Ϣ
|
|||
|
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַhash<EFBFBD><EFBFBD><EFBFBD>泤<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
class TcpKeepKey : public HashKey
|
|||
|
{
|
|||
|
public:
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
TcpKeepKey() {
|
|||
|
_addr_ipv4 = 0;
|
|||
|
_net_port = 0;
|
|||
|
TAILQ_INIT(&_keep_list);
|
|||
|
this->SetDataPtr(this);
|
|||
|
};
|
|||
|
|
|||
|
TcpKeepKey(struct sockaddr_in * dst) {
|
|||
|
_addr_ipv4 = dst->sin_addr.s_addr;
|
|||
|
_net_port = dst->sin_port;
|
|||
|
TAILQ_INIT(&_keep_list);
|
|||
|
this->SetDataPtr(this);
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>conn
|
|||
|
*/
|
|||
|
~TcpKeepKey() {
|
|||
|
TAILQ_INIT(&_keep_list);
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ڵ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>hash<EFBFBD>㷨, <EFBFBD><EFBFBD>ȡkey<EFBFBD><EFBFBD>hashֵ
|
|||
|
* @return <EFBFBD>ڵ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>hashֵ
|
|||
|
*/
|
|||
|
virtual uint32_t HashValue(){
|
|||
|
return _addr_ipv4 ^ ((_net_port << 16) | _net_port);
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ڵ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>cmp<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ͬһͰID<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>key<EFBFBD>Ƚ<EFBFBD>
|
|||
|
* @return <EFBFBD>ڵ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>hashֵ
|
|||
|
*/
|
|||
|
virtual int HashCmp(HashKey* rhs){
|
|||
|
TcpKeepKey* data = dynamic_cast<TcpKeepKey*>(rhs);
|
|||
|
if (!data) {
|
|||
|
return -1;
|
|||
|
}
|
|||
|
if (this->_addr_ipv4 != data->_addr_ipv4) {
|
|||
|
return this->_addr_ipv4 - data->_addr_ipv4;
|
|||
|
}
|
|||
|
if (this->_net_port != data->_net_port) {
|
|||
|
return this->_net_port - data->_net_port;
|
|||
|
}
|
|||
|
return 0;
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void InsertConn(TcpKeepConn* conn) {
|
|||
|
if (conn->_keep_flag & TCP_KEEP_IN_LIST) {
|
|||
|
return;
|
|||
|
}
|
|||
|
TAILQ_INSERT_TAIL(&_keep_list, conn, _keep_entry);
|
|||
|
conn->_keep_flag |= TCP_KEEP_IN_LIST;
|
|||
|
};
|
|||
|
|
|||
|
void RemoveConn(TcpKeepConn* conn) {
|
|||
|
if (!(conn->_keep_flag & TCP_KEEP_IN_LIST)) {
|
|||
|
return;
|
|||
|
}
|
|||
|
TAILQ_REMOVE(&_keep_list, conn, _keep_entry);
|
|||
|
conn->_keep_flag &= ~TCP_KEEP_IN_LIST;
|
|||
|
};
|
|||
|
|
|||
|
TcpKeepConn* GetFirstConn() {
|
|||
|
return TAILQ_FIRST(&_keep_list);
|
|||
|
};
|
|||
|
|
|||
|
private:
|
|||
|
uint32_t _addr_ipv4; ///< ip<69><70>ַ
|
|||
|
uint16_t _net_port; ///< port <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
KeepConnList _keep_list; ///< ʵ<>ʵĿ<CAB5><C4BF>ж<EFBFBD><D0B6><EFBFBD>
|
|||
|
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @brief TCP<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>cache
|
|||
|
*/
|
|||
|
class TcpKeepMgr
|
|||
|
{
|
|||
|
public:
|
|||
|
|
|||
|
typedef CPtrPool<TcpKeepConn> TcpKeepQueue; ///< <20>ڴ滺<DAB4><E6BBBA><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
TcpKeepMgr();
|
|||
|
|
|||
|
~TcpKeepMgr();
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>IP<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ȡTCP<EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
TcpKeepConn* GetTcpKeepConn(struct sockaddr_in* dst);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>IP<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>TCP<EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
bool CacheTcpKeepConn(TcpKeepConn* conn);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>IP<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>TCP<EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ȥ<EFBFBD><EFBFBD>CACHE
|
|||
|
*/
|
|||
|
bool RemoveTcpKeepConn(TcpKeepConn* conn);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>رջ<EFBFBD>tcp<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void FreeTcpKeepConn(TcpKeepConn* conn, bool force_free);
|
|||
|
|
|||
|
private:
|
|||
|
|
|||
|
HashList* _keep_hash; ///< hash<73><68>, <20>洢<EFBFBD><E6B4A2>IP<49><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>
|
|||
|
TcpKeepQueue _mem_queue; ///< mem<65><6D><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>conn<6E>ڴ<EFBFBD><DAB4><EFBFBD>
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ӹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
class ConnectionMgr
|
|||
|
{
|
|||
|
public:
|
|||
|
|
|||
|
typedef CPtrPool<UdpShortConn> UdpShortQueue;
|
|||
|
typedef CPtrPool<UdpSessionConn> UdpSessionQueue;
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ϣbuff<EFBFBD><EFBFBD>ȫ<EFBFBD>ֹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
|
|||
|
* @return ȫ<EFBFBD>־<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
static ConnectionMgr* Instance (void);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
|
|||
|
*/
|
|||
|
static void Destroy(void);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD>ӿ<EFBFBD>
|
|||
|
*/
|
|||
|
IMtConnection* GetConnection(CONN_OBJ_TYPE type, struct sockaddr_in* dst);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>սӿ<EFBFBD>
|
|||
|
*/
|
|||
|
void FreeConnection(IMtConnection* conn, bool force_free);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ر<EFBFBD>idle<EFBFBD><EFBFBD>tcp<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void CloseIdleTcpKeep(TcpKeepConn* conn);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ϣbuff<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
~ConnectionMgr();
|
|||
|
|
|||
|
private:
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ϣbuff<EFBFBD>Ĺ<EFBFBD><EFBFBD>캯<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
ConnectionMgr();
|
|||
|
|
|||
|
static ConnectionMgr * _instance; ///< <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
UdpShortQueue _udp_short_queue; ///< <20><><EFBFBD><EFBFBD><EFBFBD>ӵĶ<D3B5><C4B6>г<EFBFBD>
|
|||
|
UdpSessionQueue _udp_session_queue; ///< udp session <20><><EFBFBD>ӳ<EFBFBD>
|
|||
|
TcpKeepMgr _tcp_keep_mgr; ///< tcp keep <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
};
|
|||
|
|
|||
|
}
|
|||
|
#endif
|
|||
|
|
|||
|
|