/app/micro_thread/mt_action.h

https://github.com/F-Stack/f-stack · C Header · 195 lines · 131 code · 48 blank · 16 comment · 0 complexity · 9f5a34feac32662490b4436fb286c135 MD5 · raw file

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