helper.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #pragma once
  2. #include "SocketInterface.h"
  3. #include "GeneralHelper.h"
  4. #define USER_INFO_MSG (WM_USER + 100)
  5. #define MAX_LOG_RECORD_LENGTH 1000
  6. #define EVT_ON_SEND _T("OnSend")
  7. #define EVT_ON_RECEIVE _T("OnReceive")
  8. #define EVT_ON_CLOSE _T("OnClose")
  9. #define EVT_ON_ERROR _T("OnError")
  10. #define EVT_ON_PREPARE_CONNECT _T("OnPrepareConnect")
  11. #define EVT_ON_PREPARE_LISTEN _T("OnPrepareListen")
  12. #define EVT_ON_ACCEPT _T("OnAccept")
  13. #define EVT_ON_CONNECT _T("OnConnect")
  14. #define EVT_ON_HAND_SHAKE _T("OnHandShake")
  15. #define EVT_ON_SHUTDOWN _T("OnShutdown")
  16. #define EVT_ON_END_TEST _T("END TEST")
  17. #define EVT_ON_MESSAGE_BEGIN _T("OnMessageBegin")
  18. #define EVT_ON_REQUEST_LINE _T("OnRequestLine")
  19. #define EVT_ON_STATUS_LINE _T("OnStatusLine")
  20. #define EVT_ON_HEADER _T("OnHeader")
  21. #define EVT_ON_HEADERS_COMPLETE _T("OnHeadersComplete")
  22. #define EVT_ON_BODY _T("OnBody")
  23. #define EVT_ON_CHUNK_HEADER _T("OnChunkHeader")
  24. #define EVT_ON_CHUNK_COMPLETE _T("OnChunkComplete")
  25. #define EVT_ON_MESSAGE_COMPLETE _T("OnMessageComplete")
  26. #define EVT_ON_UPGRADE _T("OnUpgrade")
  27. #define EVT_ON_PARSE_ERROR _T("OnParseError")
  28. #define MAX_STUDIOID_LENGTH 37
  29. #define MAX_ACCOUNT_LENGTH 17
  30. #define MAX_PASSWORD_LENGTH 17
  31. enum EnAppState
  32. {
  33. ST_STARTING, ST_STARTED, ST_CONNECTING, ST_CONNECTED, ST_STOPPING, ST_STOPPED
  34. };
  35. enum ProtocolCmd
  36. {
  37. //----------------------------:common comand.type: 1~999----------------------------------------------------
  38. CMD_HEART = 1, // 心跳包;
  39. CMD_TOCHAT = 2, // 文字聊天;
  40. CMD_FILEINFO_TRANSFER = 3, // 文件传输-文件信息;
  41. CMD_FILECONT_TRANSFER = 4, // 文件传输-文件内容;
  42. CMD_UPDATA = 5, // 有升级信息(用户终端每次登录时,请求检测是否有新版本可升级的信息);
  43. CMD_DATABASEINFO = 6, // 向请求端返回数据库信息;
  44. CMD_OHTER_INFO = 999, // 其他信息;
  45. //------------------------C2L:consumer terminal to lyfz.service: 1000~1999------------------------------------
  46. C2LCMD_REQ_LOGIN = 1000, // 用户终端请求登录利亚方舟服务端(外网ip+登录信息);
  47. //C2LCMD_GET_DOG_DATA = 1001, // 利亚方舟获取用户终端机器加密狗数据信息;
  48. C2LCMD_DOGINFO = 1001, // 用户终端向利亚方舟发送加密狗信息;
  49. C2LCMD_GET_VAR = 1002, // 请求用户终端的版本号;
  50. C2LCMD_DEAL_ILLEGAL = 1003, // 处理盗版、过期用户:停止用户终端工作、启用定时关机、程序文件自动删除等等操作命令;
  51. C2LCMD_DEAL_SMS = 1004, // 利亚方舟服务端处理用户终端的短信发送工作;
  52. C2LCMD_GET_DBINFO = 1005, // 利亚方舟服务端获取用户终端电脑数据库信息;
  53. // .预留,若SQL数据库引擎未启用TCP/IP作为远程连接方式,需定义读写表的具体命令;(Jeff.100%的不建议使用这种方式获取数据,占用太多带宽和资源)
  54. // .若要使用SQL,则只允许SQL的updata、insert、delete、drop……等命令,不允许使用select来返回查询到数据;
  55. C2LCMD_OTHER_INFO = 1999, // 读其它信息;
  56. //------------------------C2C:consumer client to consumer service: 2000~2999--------------------------------
  57. C2CCMD_REQ_LOGIN = 2000, // 客户端请求登录服务器;
  58. C2CCMD_REQ_LOGOUT = 2001, // 客户端注销;
  59. C2CCMD_REQ_DOG_DATA = 2002, // 读软件狗数据;
  60. C2CCMD_AUTO_UPDATA = 2003, // 服务端向所有客户发送自动数据更新命令;
  61. C2CCMD_REQ_SERVER_VAR = 2004, // 请求服务端的版本号;
  62. C2CCMD_NEW_DEPARTMENT = 2005, // 利亚方舟服务端获取用户终端电脑数据库信息;
  63. C2CCMD_DEL_DEPARTMENT = 2006, // 客户端端向备份程序发送订单相片重新备份命令(导片与备份存在写备份无法锁定现象,need to Notify);
  64. C2CCMD_MOD_DEPARTMENT = 2007,
  65. C2CCMD_QRY_DEPARTMENT = 2008
  66. };
  67. enum PackageFlag
  68. {
  69. PACKAGE_ONESELF = 0, // 协议包-独立;
  70. PACKAGE_MULTIPLE = 1, // 协议包-多个;
  71. };
  72. enum UserLogInStatus
  73. {
  74. USER_LOGIN = 1, // 登录成功;
  75. USER_PW_MISTAKE = 2, // 密码错误;
  76. USER_HAVE_LOGIN = 3, // 用户已经登录;
  77. USER_NULL = 0 // 没有用户;
  78. };
  79. typedef struct __LogInUser__
  80. {
  81. TCHAR szStudioId[MAX_STUDIOID_LENGTH]; // 影楼Guid;
  82. TCHAR szAccount[MAX_ACCOUNT_LENGTH]; // 账号;
  83. TCHAR szPassword[MAX_PASSWORD_LENGTH]; // 密码;
  84. }LogInUser,*pLogInUser;
  85. #pragma pack(push,1)
  86. // 协议头-勿更改;
  87. typedef struct __THEPROPACKAGE__
  88. {
  89. byte flag; // 协议包类型标识;
  90. short nCmd; // 协议携带的命令标识;
  91. unsigned int nDataLen; // 数据长度,除了头之外的数据;
  92. unsigned int nSubDataLen; // 分包大小;
  93. TCHAR szAccount[MAX_ACCOUNT_LENGTH]; // 账号;
  94. DWORD dwReserve;
  95. BYTE byBody[2]; // 包数据;
  96. __THEPROPACKAGE__(){
  97. flag = PACKAGE_ONESELF;
  98. nCmd = 0;
  99. nDataLen = 0;
  100. nSubDataLen = 0;
  101. dwReserve = 0;
  102. memset(byBody, 0, 2);
  103. memset(szAccount, 0, sizeof(TCHAR)*MAX_ACCOUNT_LENGTH);
  104. }
  105. }TheProPackage,*pTheProPackage;
  106. #pragma pack(pop)
  107. struct info_msg
  108. {
  109. LPCTSTR name;
  110. CONNID connID;
  111. LPCTSTR evt;
  112. int contentLength;
  113. LPCTSTR content;
  114. static info_msg* Construct(CONNID dwConnID, LPCTSTR lpszEvent, int iContentLength = 0, LPCTSTR lpszContent = nullptr, LPCTSTR lpszName = nullptr);
  115. static void Destruct(info_msg* pMsg);
  116. private:
  117. info_msg(CONNID dwConnID, LPCTSTR lpszEvent, int iContentLength = 0, LPCTSTR lpszContent = nullptr, LPCTSTR lpszName = nullptr);
  118. ~info_msg();
  119. };
  120. struct TPkgHeader
  121. {
  122. DWORD seq;
  123. int body_len;
  124. };
  125. struct TPkgBody
  126. {
  127. char name[30];
  128. short age;
  129. char desc[1];
  130. };
  131. struct TPkgInfo
  132. {
  133. bool is_header;
  134. int length;
  135. TPkgInfo(bool header = true, int len = sizeof(TPkgHeader)) : is_header(header), length(len) {}
  136. void Reset() {is_header = true, length = sizeof(TPkgHeader);}
  137. ~TPkgInfo() {}
  138. };
  139. CBufferPtr* GeneratePkgBuffer(DWORD seq, LPCTSTR lpszName, short age, LPCTSTR lpszDesc);
  140. CBufferPtr* GeneratePkgBuffer(const TPkgHeader& header, const TPkgBody& body);
  141. void SetMainWnd(CWnd* pWnd);
  142. void SetInfoList(CListBox* pInfoList);
  143. void LogServerStart(LPCTSTR lpszAddress, USHORT port, LPCTSTR lpszName = nullptr);
  144. void LogServerStartFail(DWORD code, LPCTSTR lpszDesc, LPCTSTR lpszName = nullptr);
  145. void LogServerStop(LPCTSTR lpszName = nullptr);
  146. void LogServerStopFail(DWORD code, LPCTSTR lpszDesc, LPCTSTR lpszName = nullptr);
  147. void LogClientStart(LPCTSTR lpszAddress, USHORT port, LPCTSTR lpszName = nullptr);
  148. void LogClientStarting(LPCTSTR lpszAddress, USHORT port, LPCTSTR lpszName = nullptr);
  149. void LogClientStartFail(DWORD code, LPCTSTR lpszDesc, LPCTSTR lpszName = nullptr);
  150. void LogClientStopping(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  151. void LogClientStopFail(DWORD code, LPCTSTR lpszDesc, LPCTSTR lpszName = nullptr);
  152. void LogSend(CONNID dwConnID, LPCTSTR lpszContent, LPCTSTR lpszName = nullptr);
  153. void LogClientSendFail(int iSequence, int iSocketIndex, DWORD code, LPCTSTR lpszDesc, LPCTSTR lpszName = nullptr);
  154. void LogSendFail(CONNID dwConnID, DWORD code, LPCTSTR lpszDesc, LPCTSTR lpszName = nullptr);
  155. void LogDisconnect(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  156. void LogDisconnectFail(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  157. void LogRelease(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  158. void LogReleaseFail(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  159. void LogDetect(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  160. void LogDetectFail(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  161. void LogOnConnect(CONNID dwConnID, const CString& strAddress, USHORT usPort);
  162. void LogOnConnect2(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  163. void LogOnHandShake2(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  164. void PostOnSend(CONNID dwConnID, const BYTE* pData, int iLength, LPCTSTR lpszName = nullptr);
  165. void PostOnReceive(CONNID dwConnID, const BYTE* pData, int iLength, LPCTSTR lpszName = nullptr);
  166. void PostOnReceiveCast(CONNID dwConnID, LPCTSTR lpszAddress, USHORT usPort, const BYTE* pData, int iLength, LPCTSTR lpszName = nullptr);
  167. void PostOnClose(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  168. void PostOnError(CONNID dwConnID, int enOperation, int iErrorCode, LPCTSTR lpszName = nullptr);
  169. void PostOnAccept(CONNID dwConnID, LPCTSTR lpszAddress, USHORT usPort, BOOL bPass, LPCTSTR lpszName = nullptr);
  170. void PostOnAccept2(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  171. void PostOnHandShake(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  172. void PostOnPrepareListen(LPCTSTR lpszAddress, USHORT usPort, LPCTSTR lpszName = nullptr);
  173. void PostOnPrepareConnect(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  174. void PostOnConnect(CONNID dwConnID, LPCTSTR lpszAddress, USHORT usPort, LPCTSTR lpszName = nullptr);
  175. void PostOnConnect2(CONNID dwConnID, LPCTSTR lpszAddress, USHORT usPort, LPCTSTR lpszName = nullptr);
  176. void PostOnConnect3(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  177. void PostOnShutdown(LPCTSTR lpszName = nullptr);
  178. void PostServerStatics(const LONGLONG& llTotalSent, const LONGLONG& llTotalReceived, LPCTSTR lpszName = nullptr);
  179. void PostTimeConsuming(DWORD dwTickCount, LPCTSTR lpszName = nullptr);
  180. void PostOnMessageBegin(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  181. void PostOnRequestLine(CONNID dwConnID, LPCTSTR lpszMethod, USHORT usUrlFieldSet, LPCTSTR lpszUrl, LPCTSTR lpszName = nullptr);
  182. void PostOnStatusLine(CONNID dwConnID, USHORT usStatusCode, LPCTSTR lpszDesc, LPCTSTR lpszName = nullptr);
  183. void PostOnHeader(CONNID dwConnID, LPCTSTR lpszHeaderName, LPCTSTR lpszHeaderValue, LPCTSTR lpszName = nullptr);
  184. void PostOnHeadersComplete(CONNID dwConnID, LPCTSTR lpszSummary, LPCTSTR lpszName = nullptr);
  185. void PostOnBody(CONNID dwConnID, const BYTE* pData, int iLength, LPCTSTR lpszName = nullptr);
  186. void PostOnChunkHeader(CONNID dwConnID, int iLength, LPCTSTR lpszName = nullptr);
  187. void PostOnChunkComplete(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  188. void PostOnMessageComplete(CONNID dwConnID, LPCTSTR lpszName = nullptr);
  189. void PostOnUpgrade(CONNID dwConnID, EnHttpUpgradeType enUpgradeType, LPCTSTR lpszName = nullptr);
  190. void PostOnParseError(CONNID dwConnID, int iErrorCode, LPCTSTR lpszErrorDesc, LPCTSTR lpszName = nullptr);
  191. void PostInfoMsg(info_msg* msg);
  192. void LogInfoMsg(info_msg* pInfoMsg);
  193. void LogMsg(const CString& msg);
  194. #ifdef _SSL_SUPPORT
  195. extern int g_c_iVerifyMode;
  196. extern BOOL g_c_bNeedClientVerification;
  197. extern LPCTSTR g_c_lpszCAPemCertFileOrPath;
  198. extern LPCTSTR g_c_lpszPemCertFile;
  199. extern LPCTSTR g_c_lpszPemKeyFile;
  200. extern LPCTSTR g_c_lpszKeyPasswod;
  201. extern int g_s_iVerifyMode;
  202. extern BOOL g_s_bNeedClientVerification;
  203. extern LPCTSTR g_s_lpszCAPemCertFileOrPath;
  204. extern LPCTSTR g_s_lpszPemCertFile;
  205. extern LPCTSTR g_s_lpszPemKeyFile;
  206. extern LPCTSTR g_s_lpszKeyPasswod;
  207. #endif