f-stack/app/micro_thread/mt_action.h

328 lines
7.3 KiB
C
Raw Normal View History

2017-04-21 10:43:26 +00:00
/**
* 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_action.h
* @info ΢<EFBFBD>߳<EFBFBD>ACTION<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**/
#ifndef __MT_ACTION_H__
#define __MT_ACTION_H__
#include <netinet/in.h>
#include <queue>
#include "mt_msg.h"
#include "mt_session.h"
#include "mt_notify.h"
namespace NS_MICRO_THREAD {
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD><EFBFBD><EFBFBD>
*/
enum MULTI_STATE
{
MULTI_FLAG_UNDEF = 0x0, ///< <20><>ʼ<EFBFBD><CABC>, δ<><CEB4><EFBFBD><EFBFBD>
MULTI_FLAG_INIT = 0x1, ///< socket<65><74><EFBFBD><EFBFBD><EFBFBD>ѳɹ<D1B3>
MULTI_FLAG_OPEN = 0x2, ///< socket<65><74><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD><D1B4><EFBFBD>
MULTI_FLAG_SEND = 0x4, ///< <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD>
MULTI_FLAG_FIN = 0x8, ///< Ӧ<><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD>յ<EFBFBD>
};
/**
* @brief Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD><EFBFBD><EFBFBD>
*/
enum MULTI_CONNECT
{
CONN_UNKNOWN = 0,
CONN_TYPE_SHORT = 0x1, ///< <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, һ<>ν<EFBFBD><CEBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>
CONN_TYPE_LONG = 0x2, ///< <20><><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>ÿ<EFBFBD><C3BF>ʹ<EFBFBD>ú<EFBFBD>, <20>ɻ<EFBFBD><C9BB><EFBFBD><EFBFBD>ظ<EFBFBD>ʹ<EFBFBD><CAB9>
CONN_TYPE_SESSION = 0x4, ///< <20><><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD>session id <20><><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
};
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
enum MULTI_ERROR
{
ERR_NONE = 0,
ERR_SOCKET_FAIL = -1, ///< <20><><EFBFBD><EFBFBD>sockʧ<6B><CAA7>
ERR_CONNECT_FAIL = -2, ///< <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
ERR_SEND_FAIL = -3, ///< <20><><EFBFBD>ͱ<EFBFBD><CDB1><EFBFBD>ʧ<EFBFBD><CAA7>
ERR_RECV_FAIL = -4, ///< <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
ERR_RECV_TIMEOUT = -5, ///< <20><><EFBFBD>ճ<EFBFBD>ʱ
ERR_KQUEUE_FAIL = -6, ///< epollʧ<6C><CAA7>
ERR_FRAME_ERROR = -7, ///< <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
ERR_PEER_CLOSE = -8, ///< <20>Է<EFBFBD><D4B7>ر<EFBFBD>
ERR_PARAM_ERROR = -9, ///< <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ERR_MEMORY_ERROR = -10, ///< <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
ERR_ENCODE_ERROR = -11, ///< <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
ERR_DST_ADDR_ERROR = -12, ///< Ŀ<><C4BF><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>ȡʧ<C8A1><CAA7>
};
/**
* @brief ΢<EFBFBD>̵߳ĺ<EFBFBD><EFBFBD>˽<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
class IMtAction : public ISession
{
public:
/**
* @brief ΢<EFBFBD>̲߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
IMtAction();
virtual ~IMtAction();
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ (<EFBFBD><EFBFBD>֤<EFBFBD>ӿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>inline)
* @param dst -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵ĵ<EFBFBD>ַ
*/
void SetMsgDstAddr(struct sockaddr_in* dst) {
memcpy(&_addr, dst, sizeof(_addr));
};
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ϢĿ<EFBFBD>ĵ<EFBFBD>ַ<EFBFBD><EFBFBD>Ϣ
* @return ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD>ĵ<EFBFBD>ַ
*/
struct sockaddr_in* GetMsgDstAddr() {
return &_addr;
};
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff<EFBFBD><EFBFBD>С, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>ʹ<EFBFBD>õ<EFBFBD>msgbuff<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return 0<EFBFBD>ɹ<EFBFBD>
*/
void SetMsgBuffSize(int buff_size) {
_buff_size = buff_size;
};
/**
* @brief <EFBFBD><EFBFBD>ȡԤ<EFBFBD>õ<EFBFBD>buff<EFBFBD><EFBFBD>С
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣbuff<EFBFBD><EFBFBD><EFBFBD>󳤶<EFBFBD>
*/
int GetMsgBuffSize() {
return (_buff_size > 0) ? _buff_size : 65535;
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ó<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>session<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>id
* @return 0<EFBFBD>ɹ<EFBFBD>
*/
void SetSessionName(int name) {
_ntfy_name = name;
};
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>session<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>id
* @return session ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
int GetSessionName() {
return _ntfy_name;
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>proto<EFBFBD><EFBFBD>Ϣ
*/
void SetProtoType(MULTI_PROTO proto) {
_proto = proto;
};
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>proto<EFBFBD><EFBFBD>Ϣ
* @return proto type
*/
MULTI_PROTO GetProtoType() {
return _proto;
};
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
*/
void SetConnType(MULTI_CONNECT type) {
_conn_type = type;
};
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
* @return conn type
*/
MULTI_CONNECT GetConnType() {
return _conn_type;
};
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>errno
*/
void SetErrno(MULTI_ERROR err) {
_errno = err;
};
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ERRNO<EFBFBD><EFBFBD>Ϣ
* @return ERRONO
*/
MULTI_ERROR GetErrno() {
return _errno;
};
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>timecost
*/
void SetCost(int cost) {
_time_cost = cost;
};
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>timecost<EFBFBD><EFBFBD>Ϣ
* @return timecost
*/
int GetCost() {
return _time_cost;
};
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD>Ϣ
* @param flag -<EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
*/
void SetMsgFlag(MULTI_STATE flag) {
_flag = flag;
};
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD>Ϣ
* @return flag -<EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
*/
MULTI_STATE GetMsgFlag() {
return _flag;
};
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD>Ϣָ<EFBFBD><EFBFBD>
* @return IMtConnָ<EFBFBD><EFBFBD>
*/
void SetIMsgPtr(IMtMsg* msg ) {
_msg = msg;
};
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD>Ϣָ<EFBFBD><EFBFBD>
* @return IMtConnָ<EFBFBD><EFBFBD>
*/
IMtMsg* GetIMsgPtr() {
return _msg;
};
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
* @return IMtConnָ<EFBFBD><EFBFBD>
*/
void SetIConnection(IMtConnection* conn) {
_conn = conn;
};
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
* @return IMtConnָ<EFBFBD><EFBFBD>
*/
IMtConnection* GetIConnection() {
return _conn;
};
/**
* @brief <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ֶ<EFBFBD><EFBFBD><EFBFBD>Ϣ
*/
void Init();
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Action״̬
*/
void Reset();
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD>, ֪ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
KqueuerObj* GetNtfyObj();
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD>, ֪ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
int InitConnEnv();
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD>򻯽ӿ<EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֲ<EFBFBD><EFBFBD><EFBFBD>
*/
int DoEncode();
int DoInput();
int DoProcess();
int DoError();
public:
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
* @return >0 -<EFBFBD>ɹ<EFBFBD>, < 0 ʧ<EFBFBD><EFBFBD>
*/
virtual int HandleEncode(void* buf, int& len, IMtMsg* msg){return 0;};
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>CHECK<EFBFBD>ӿ<EFBFBD>, TCP<EFBFBD>ķְ<EFBFBD><EFBFBD>ӿ<EFBFBD>
* @return > 0 <EFBFBD>Ѿ<EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С, =0 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD>, <0 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD>-65535 UDP<EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
*/
virtual int HandleInput(void* buf, int len, IMtMsg* msg){return 0;};
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>, <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>ʧ<EFBFBD><EFBFBD>
*/
virtual int HandleProcess(void* buf, int len, IMtMsg* msg){return 0;};
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>֪ͨ, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>μ<EFBFBD> MULTI_ERROR ö<EFBFBD><EFBFBD>
* @info <EFBFBD><EFBFBD>handleprocessʧ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>øýӿ<EFBFBD>
* @return 0 <EFBFBD>ɹ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>
*/
virtual int HandleError(int err, IMtMsg* msg){return 0;};
protected:
MULTI_STATE _flag; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ, <20><>ǰ״̬<D7B4><CCAC>Ϣ
MULTI_PROTO _proto; // Э<><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> UDP/TCP
MULTI_CONNECT _conn_type; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
MULTI_ERROR _errno; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ, 0<>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
struct sockaddr_in _addr; // <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>д<EFBFBD><D0B4>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>stAddr
int _time_cost; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>ʱ, <20><><EFBFBD><EFBFBD>
int _buff_size; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>𳤶<EFBFBD>
int _ntfy_name; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>session ntfy<66><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, sessionģ<6E><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
IMtMsg* _msg; // <20><>Ϣָ<CFA2><D6B8>, <20>ϼ<EFBFBD>ָ<EFBFBD><D6B8>
IMtConnection* _conn; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>, <20>¼<EFBFBD>ָ<EFBFBD><D6B8>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
};
}
#endif