xref: /f-stack/app/micro_thread/mt_action.h (revision 35a81399)
1 
2 /**
3  * Tencent is pleased to support the open source community by making MSEC available.
4  *
5  * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
6  *
7  * Licensed under the GNU General Public License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License. You may
9  * obtain a copy of the License at
10  *
11  *     https://opensource.org/licenses/GPL-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software distributed under the
14  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language governing permissions
16  * and limitations under the License.
17  */
18 
19 #ifndef __MT_ACTION_H__
20 #define __MT_ACTION_H__
21 
22 #include <netinet/in.h>
23 #include <queue>
24 #include "mt_msg.h"
25 #include "mt_session.h"
26 #include "mt_notify.h"
27 
28 namespace NS_MICRO_THREAD {
29 
30 
31 enum MULTI_STATE
32 {
33     MULTI_FLAG_UNDEF   = 0x0,
34     MULTI_FLAG_INIT    = 0x1,
35     MULTI_FLAG_OPEN    = 0x2,
36     MULTI_FLAG_SEND    = 0x4,
37     MULTI_FLAG_FIN     = 0x8,
38 };
39 
40 enum MULTI_CONNECT
41 {
42     CONN_UNKNOWN        = 0,
43     CONN_TYPE_SHORT     = 0x1,
44     CONN_TYPE_LONG      = 0x2,
45     CONN_TYPE_SESSION   = 0x4,
46 };
47 
48 enum MULTI_ERROR
49 {
50     ERR_NONE            =  0,
51     ERR_SOCKET_FAIL     = -1,
52     ERR_CONNECT_FAIL    = -2,
53     ERR_SEND_FAIL       = -3,
54     ERR_RECV_FAIL       = -4,
55     ERR_RECV_TIMEOUT    = -5,
56     ERR_KQUEUE_FAIL     = -6,
57     ERR_FRAME_ERROR     = -7,
58     ERR_PEER_CLOSE      = -8,
59     ERR_PARAM_ERROR     = -9,
60     ERR_MEMORY_ERROR    = -10,
61     ERR_ENCODE_ERROR    = -11,
62     ERR_DST_ADDR_ERROR  = -12,
63 };
64 
65 
66 class IMtAction : public ISession
67 {
68 public:
69 
70     IMtAction();
71     virtual ~IMtAction();
72 
SetMsgDstAddr(struct sockaddr_in * dst)73     void SetMsgDstAddr(struct sockaddr_in* dst) {
74         memcpy(&_addr, dst, sizeof(_addr));
75     };
76 
GetMsgDstAddr()77     struct sockaddr_in* GetMsgDstAddr() {
78         return &_addr;
79     };
80 
SetMsgBuffSize(int buff_size)81     void SetMsgBuffSize(int buff_size) {
82         _buff_size = buff_size;
83     };
84 
GetMsgBuffSize()85     int GetMsgBuffSize()     {
86         return (_buff_size > 0) ? _buff_size : 65535;
87     }
88 
SetSessionName(int name)89     void SetSessionName(int name) {
90         _ntfy_name = name;
91     };
92 
GetSessionName()93     int GetSessionName()     {
94         return _ntfy_name;
95     }
96 
SetProtoType(MULTI_PROTO proto)97     void SetProtoType(MULTI_PROTO proto) {
98         _proto = proto;
99     };
100 
GetProtoType()101     MULTI_PROTO GetProtoType() {
102         return _proto;
103     };
104 
SetConnType(MULTI_CONNECT type)105     void SetConnType(MULTI_CONNECT type) {
106         _conn_type = type;
107     };
108 
GetConnType()109     MULTI_CONNECT GetConnType() {
110         return _conn_type;
111     };
112 
SetErrno(MULTI_ERROR err)113     void SetErrno(MULTI_ERROR err) {
114         _errno = err;
115     };
116 
GetErrno()117     MULTI_ERROR GetErrno() {
118         return _errno;
119     };
120 
SetCost(int cost)121     void SetCost(int cost) {
122         _time_cost = cost;
123     };
124 
GetCost()125     int GetCost() {
126         return _time_cost;
127     };
128 
SetMsgFlag(MULTI_STATE flag)129     void SetMsgFlag(MULTI_STATE flag) {
130         _flag = flag;
131     };
132 
GetMsgFlag()133     MULTI_STATE GetMsgFlag() {
134         return _flag;
135     };
136 
SetIMsgPtr(IMtMsg * msg)137     void SetIMsgPtr(IMtMsg* msg  ) {
138         _msg = msg;
139     };
140 
GetIMsgPtr()141     IMtMsg* GetIMsgPtr() {
142         return _msg;
143     };
144 
SetIConnection(IMtConnection * conn)145     void SetIConnection(IMtConnection* conn) {
146         _conn = conn;
147     };
148 
GetIConnection()149     IMtConnection* GetIConnection() {
150         return _conn;
151     };
152 
153     void Init();
154 
155     void Reset();
156 
157     KqueuerObj* GetNtfyObj();
158 
159     int InitConnEnv();
160 
161     int DoEncode();
162     int DoInput();
163     int DoProcess();
164     int DoError();
165 
166 public:
167 
HandleEncode(void * buf,int & len,IMtMsg * msg)168     virtual int HandleEncode(void* buf, int& len, IMtMsg* msg){return 0;};
169 
HandleInput(void * buf,int len,IMtMsg * msg)170     virtual int HandleInput(void* buf, int len, IMtMsg* msg){return 0;};
171 
HandleProcess(void * buf,int len,IMtMsg * msg)172     virtual int HandleProcess(void* buf, int len, IMtMsg* msg){return 0;};
173 
HandleError(int err,IMtMsg * msg)174     virtual int HandleError(int err, IMtMsg* msg){return 0;};
175 
176 
177 protected:
178 
179     MULTI_STATE         _flag;
180     MULTI_PROTO         _proto;
181     MULTI_CONNECT       _conn_type;
182     MULTI_ERROR         _errno;
183     struct sockaddr_in  _addr;
184     int                 _time_cost;
185     int                 _buff_size;
186     int                 _ntfy_name;
187 
188     IMtMsg*             _msg;
189     IMtConnection*      _conn;
190 };
191 
192 }
193 
194 #endif
195 
196