helper.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. #include "stdafx.h"
  2. #include "helper.h"
  3. CWnd* g_pMainWnd;
  4. CListBox* g_pInfoList;
  5. info_msg* info_msg::Construct(CONNID dwConnID, LPCTSTR lpszEvent, int iContentLength, LPCTSTR lpszContent, LPCTSTR lpszName)
  6. {
  7. return new info_msg(dwConnID, lpszEvent, iContentLength, lpszContent, lpszName);
  8. }
  9. void info_msg::Destruct(info_msg* pMsg)
  10. {
  11. delete pMsg;
  12. }
  13. info_msg::info_msg(CONNID dwConnID, LPCTSTR lpszEvent, int iContentLength, LPCTSTR lpszContent, LPCTSTR lpszName)
  14. : connID(dwConnID), evt(lpszEvent), contentLength(iContentLength), content(lpszContent), name(nullptr)
  15. {
  16. if(lpszName)
  17. {
  18. int len = lstrlen(lpszName);
  19. if(len > 0)
  20. {
  21. name = new TCHAR[len + 1];
  22. memcpy((LPSTR)name, lpszName, (len + 1) * sizeof(TCHAR));
  23. }
  24. }
  25. }
  26. info_msg::~info_msg()
  27. {
  28. if(name)
  29. delete[] name;
  30. if(contentLength > 0)
  31. delete[] content;
  32. }
  33. void SetMainWnd(CWnd* pWnd)
  34. {
  35. g_pMainWnd = pWnd;
  36. }
  37. void SetInfoList(CListBox* pInfoList)
  38. {
  39. g_pInfoList = pInfoList;
  40. }
  41. inline CString SafeString(LPCTSTR lpszName)
  42. {
  43. CString str(lpszName);
  44. if(lpszName) str.AppendChar(' ');
  45. return str;
  46. }
  47. inline CString SafeString2(LPCTSTR lpszName)
  48. {
  49. CString str(lpszName);
  50. if(lpszName) str.Append(_T(" #"));
  51. return str;
  52. }
  53. void LogServerStart(LPCTSTR lpszAddress, USHORT port, LPCTSTR lpszName)
  54. {
  55. CString msg;
  56. msg.Format(_T("$ %sServer Start OK --> (%s : %d)"), SafeString(lpszName), lpszAddress, port);
  57. LogMsg(msg);
  58. }
  59. void LogServerStartFail(DWORD code, LPCTSTR lpszDesc, LPCTSTR lpszName)
  60. {
  61. CString msg;
  62. msg.Format(_T("$ %sServer Start Fail --> %s (%d)"), SafeString(lpszName), lpszDesc, code);
  63. LogMsg(msg);
  64. }
  65. void LogServerStop(LPCTSTR lpszName)
  66. {
  67. CString msg;
  68. msg.Format(_T("$ %sServer Stop"), SafeString(lpszName));
  69. LogMsg(msg);
  70. }
  71. void LogServerStopFail(DWORD code, LPCTSTR lpszDesc, LPCTSTR lpszName)
  72. {
  73. CString msg;
  74. msg.Format(_T("$ %sServer Stop Fail --> %s (%d)"), SafeString(lpszName), lpszDesc, code);
  75. LogMsg(msg);
  76. }
  77. void LogClientStart(LPCTSTR lpszAddress, USHORT port, LPCTSTR lpszName)
  78. {
  79. CString msg;
  80. msg.Format(_T("$ %sClient Start OK --> (%s : %d)"), SafeString(lpszName), lpszAddress, port);
  81. LogMsg(msg);
  82. }
  83. void LogClientStarting(LPCTSTR lpszAddress, USHORT port, LPCTSTR lpszName)
  84. {
  85. CString msg;
  86. msg.Format(_T("$ %sClient Starting ... --> (%s : %d)"), SafeString(lpszName), lpszAddress, port);
  87. LogMsg(msg);
  88. }
  89. void LogClientStartFail(DWORD code, LPCTSTR lpszDesc, LPCTSTR lpszName)
  90. {
  91. CString msg;
  92. msg.Format(_T("$ %sClient Start Fail --> %s (%d)"), SafeString(lpszName), lpszDesc, code);
  93. LogMsg(msg);
  94. }
  95. void LogClientStopping(CONNID dwConnID, LPCTSTR lpszName)
  96. {
  97. CString msg;
  98. msg.Format(_T("$ %sClient Stopping ... --> (%Iu)"), SafeString(lpszName), dwConnID);
  99. LogMsg(msg);
  100. }
  101. void LogClientStopFail(DWORD code, LPCTSTR lpszDesc, LPCTSTR lpszName)
  102. {
  103. CString msg;
  104. msg.Format(_T("$ %sClient Stop Fail --> %s (%d)"), SafeString(lpszName), lpszDesc, code);
  105. LogMsg(msg);
  106. }
  107. void LogClientSendFail(int iSequence, int iSocketIndex, DWORD code, LPCTSTR lpszDesc, LPCTSTR lpszName)
  108. {
  109. CString msg;
  110. msg.Format(_T("$ %sClient Send Fail [SOCK: %d, SEQ: %d] --> %s (%d)"), SafeString(lpszName), iSocketIndex, iSequence, lpszDesc, code);
  111. LogMsg(msg);
  112. }
  113. void LogSend(CONNID dwConnID, LPCTSTR lpszContent, LPCTSTR lpszName)
  114. {
  115. CString msg;
  116. msg.Format(_T("$ %s(%Iu) Send OK --> %s"), SafeString2(lpszName), dwConnID, lpszContent);
  117. LogMsg(msg);
  118. }
  119. void LogSendFail(CONNID dwConnID, DWORD code, LPCTSTR lpszDesc, LPCTSTR lpszName)
  120. {
  121. CString msg;
  122. msg.Format(_T("$ %s(%Iu) Send Fail --> %s (%d)"), SafeString2(lpszName), dwConnID, lpszDesc, code, lpszName);
  123. LogMsg(msg);
  124. }
  125. void LogDisconnect(CONNID dwConnID, LPCTSTR lpszName)
  126. {
  127. CString msg;
  128. msg.Format(_T("$ %s(%Iu) Disconnect OK"), SafeString2(lpszName), dwConnID);
  129. LogMsg(msg);
  130. }
  131. void LogDisconnectFail(CONNID dwConnID, LPCTSTR lpszName)
  132. {
  133. CString msg;
  134. msg.Format(_T("$ %s(%Iu) Disconnect Fail"), SafeString2(lpszName), dwConnID, lpszName);
  135. LogMsg(msg);
  136. }
  137. void LogRelease(CONNID dwConnID, LPCTSTR lpszName)
  138. {
  139. CString msg;
  140. msg.Format(_T("$ %s(%Iu) Release OK"), SafeString2(lpszName), dwConnID);
  141. LogMsg(msg);
  142. }
  143. void LogReleaseFail(CONNID dwConnID, LPCTSTR lpszName)
  144. {
  145. CString msg;
  146. msg.Format(_T("$ %s(%Iu) Release Fail"), SafeString2(lpszName), dwConnID);
  147. LogMsg(msg);
  148. }
  149. void LogDetect(CONNID dwConnID, LPCTSTR lpszName)
  150. {
  151. CString msg;
  152. msg.Format(_T("$ %s(%Iu) Detect Connection OK"), SafeString2(lpszName), dwConnID);
  153. LogMsg(msg);
  154. }
  155. void LogDetectFail(CONNID dwConnID, LPCTSTR lpszName)
  156. {
  157. CString msg;
  158. msg.Format(_T("$ %s(%Iu) Detect Connection Fail"), SafeString2(lpszName), dwConnID);
  159. LogMsg(msg);
  160. }
  161. void LogOnConnect(CONNID dwConnID, const CString& strAddress, USHORT usPort, LPCTSTR lpszName)
  162. {
  163. LPTSTR lpszContent = new TCHAR[100];
  164. wsprintf(lpszContent, _T("local address: %s:%d"), strAddress, usPort);
  165. int content_len = lstrlen(lpszContent);
  166. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_CONNECT, content_len, lpszContent, lpszName);
  167. LogInfoMsg(msg);
  168. }
  169. void LogOnConnect2(CONNID dwConnID, LPCTSTR lpszName)
  170. {
  171. CString msg;
  172. msg.Format(_T(" > [ %s%Iu, %s ]"), SafeString2(lpszName), dwConnID, EVT_ON_CONNECT);
  173. LogMsg(msg);
  174. }
  175. void LogOnHandShake2(CONNID dwConnID, LPCTSTR lpszName)
  176. {
  177. CString msg;
  178. msg.Format(_T(" > [ %s%Iu, %s ]"), SafeString2(lpszName), dwConnID, EVT_ON_HAND_SHAKE);
  179. LogMsg(msg);
  180. }
  181. void PostOnSend(CONNID dwConnID, const BYTE* pData, int iLength, LPCTSTR lpszName)
  182. {
  183. LPTSTR lpszContent = new TCHAR[20];
  184. wsprintf(lpszContent, _T("(%d bytes)"), iLength);
  185. int content_len = lstrlen(lpszContent);
  186. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_SEND, content_len, lpszContent, lpszName);
  187. PostInfoMsg(msg);
  188. }
  189. void PostOnReceive(CONNID dwConnID, const BYTE* pData, int iLength, LPCTSTR lpszName)
  190. {
  191. LPTSTR lpszContent = new TCHAR[20];
  192. wsprintf(lpszContent, _T("(%d bytes)"), iLength);
  193. int content_len = lstrlen(lpszContent);
  194. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_RECEIVE, content_len, lpszContent, lpszName);
  195. PostInfoMsg(msg);
  196. }
  197. void PostOnReceiveCast(CONNID dwConnID, LPCTSTR lpszAddress, USHORT usPort, const BYTE* pData, int iLength, LPCTSTR lpszName)
  198. {
  199. LPTSTR lpszContent = new TCHAR[100];
  200. wsprintf(lpszContent, _T("<%s:%d> (%d bytes)"), lpszAddress, usPort, iLength);
  201. int content_len = lstrlen(lpszContent);
  202. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_RECEIVE, content_len, lpszContent, lpszName);
  203. PostInfoMsg(msg);
  204. }
  205. void PostOnClose(CONNID dwConnID, LPCTSTR lpszName)
  206. {
  207. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_CLOSE, 0, nullptr, lpszName);
  208. PostInfoMsg(msg);
  209. }
  210. void PostOnError(CONNID dwConnID, int enOperation, int iErrorCode, LPCTSTR lpszName)
  211. {
  212. LPTSTR lpszContent = new TCHAR[100];
  213. wsprintf(lpszContent, _T("OP: %d, CODE: %d"), enOperation, iErrorCode);
  214. int content_len = lstrlen(lpszContent);
  215. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_ERROR, content_len, lpszContent, lpszName);
  216. PostInfoMsg(msg);
  217. }
  218. void PostOnAccept(CONNID dwConnID, LPCTSTR lpszAddress, USHORT usPort, BOOL bPass, LPCTSTR lpszName)
  219. {
  220. LPTSTR lpszContent = new TCHAR[100];
  221. wsprintf(lpszContent, _T("%s (%s:%d)"), bPass ? _T("PASS") : _T("REJECT"), lpszAddress, usPort);
  222. int content_len = lstrlen(lpszContent);
  223. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_ACCEPT, content_len, lpszContent, lpszName);
  224. PostInfoMsg(msg);
  225. }
  226. void PostOnAccept2(CONNID dwConnID, LPCTSTR lpszName)
  227. {
  228. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_ACCEPT, 0, nullptr, lpszName);
  229. PostInfoMsg(msg);
  230. }
  231. void PostOnHandShake(CONNID dwConnID, LPCTSTR lpszName)
  232. {
  233. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_HAND_SHAKE, 0, nullptr, lpszName);
  234. PostInfoMsg(msg);
  235. }
  236. void PostOnPrepareListen(LPCTSTR lpszAddress, USHORT usPort, LPCTSTR lpszName)
  237. {
  238. LPTSTR lpszContent = new TCHAR[100];
  239. wsprintf(lpszContent, _T("bind address: %s:%d"), lpszAddress, usPort);
  240. int content_len = lstrlen(lpszContent);
  241. info_msg* msg = info_msg::Construct(0, EVT_ON_PREPARE_LISTEN, content_len, lpszContent, lpszName);
  242. LogInfoMsg(msg);
  243. }
  244. void PostOnPrepareConnect(CONNID dwConnID, LPCTSTR lpszName)
  245. {
  246. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_PREPARE_CONNECT, 0, nullptr, lpszName);
  247. LogInfoMsg(msg);
  248. }
  249. void PostOnConnect(CONNID dwConnID, LPCTSTR lpszAddress, USHORT usPort, LPCTSTR lpszName)
  250. {
  251. LPTSTR lpszContent = new TCHAR[100];
  252. wsprintf(lpszContent, _T("local address: %s:%d"), lpszAddress, usPort);
  253. int content_len = lstrlen(lpszContent);
  254. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_CONNECT, content_len, lpszContent, lpszName);
  255. PostInfoMsg(msg);
  256. }
  257. void PostOnConnect2(CONNID dwConnID, LPCTSTR lpszAddress, USHORT usPort, LPCTSTR lpszName)
  258. {
  259. LPTSTR lpszContent = new TCHAR[100];
  260. wsprintf(lpszContent, _T("remote address: %s:%d"), lpszAddress, usPort);
  261. int content_len = lstrlen(lpszContent);
  262. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_CONNECT, content_len, lpszContent, lpszName);
  263. PostInfoMsg(msg);
  264. }
  265. void PostOnConnect3(CONNID dwConnID, LPCTSTR lpszName)
  266. {
  267. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_CONNECT, 0, nullptr, lpszName);
  268. PostInfoMsg(msg);
  269. }
  270. void PostOnShutdown(LPCTSTR lpszName)
  271. {
  272. info_msg* msg = info_msg::Construct(0, EVT_ON_SHUTDOWN, 0, nullptr, lpszName);
  273. PostInfoMsg(msg);
  274. }
  275. void PostServerStatics(const LONGLONG& llTotalSent, const LONGLONG& llTotalReceived, LPCTSTR lpszName)
  276. {
  277. LPTSTR lpszContent = new TCHAR[100];
  278. wsprintf(lpszContent, _T(" *** Summary: send - %I64d, recv - %I64d"), llTotalSent, llTotalReceived);
  279. int content_len = lstrlen(lpszContent);
  280. info_msg* msg = info_msg::Construct(0, EVT_ON_END_TEST, content_len, lpszContent, lpszName);
  281. PostInfoMsg(msg);
  282. }
  283. void PostTimeConsuming(DWORD dwTickCount, LPCTSTR lpszName)
  284. {
  285. LPTSTR lpszContent = new TCHAR[100];
  286. wsprintf(lpszContent, _T("Total Time Consuming: %u"), dwTickCount);
  287. int content_len = lstrlen(lpszContent);
  288. info_msg* msg = info_msg::Construct(0, EVT_ON_END_TEST, content_len, lpszContent, lpszName);
  289. PostInfoMsg(msg);
  290. }
  291. void PostOnMessageBegin(CONNID dwConnID, LPCTSTR lpszName)
  292. {
  293. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_MESSAGE_BEGIN, 0, nullptr, lpszName);
  294. PostInfoMsg(msg);
  295. }
  296. void PostOnRequestLine(CONNID dwConnID, LPCSTR lpszMethod, USHORT usUrlFieldSet, LPCSTR lpszUrl, LPCTSTR lpszName)
  297. {
  298. USES_CONVERSION;
  299. int content_len = (int)(strlen(lpszMethod) + strlen(lpszUrl) + 20);
  300. LPTSTR lpszContent = new TCHAR[content_len];
  301. wsprintf(lpszContent, _T("[%s/0x%02X] : %s"), A2T(lpszMethod), usUrlFieldSet, A2T(lpszUrl));
  302. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_REQUEST_LINE, content_len, lpszContent, lpszName);
  303. PostInfoMsg(msg);
  304. }
  305. void PostOnStatusLine(CONNID dwConnID, USHORT usStatusCode, LPCSTR lpszDesc, LPCTSTR lpszName)
  306. {
  307. USES_CONVERSION;
  308. int content_len = (int)(strlen(lpszDesc) + 10);
  309. LPTSTR lpszContent = new TCHAR[content_len];
  310. wsprintf(lpszContent, _T("(%u) : %s"), usStatusCode, A2T(lpszDesc));
  311. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_STATUS_LINE, content_len, lpszContent, lpszName);
  312. PostInfoMsg(msg);
  313. }
  314. void PostOnHeader(CONNID dwConnID, LPCSTR lpszHeaderName, LPCSTR lpszHeaderValue, LPCTSTR lpszName)
  315. {
  316. USES_CONVERSION;
  317. int content_len = (int)(strlen(lpszHeaderName) + strlen(lpszHeaderValue) + 10);
  318. LPTSTR lpszContent = new TCHAR[content_len];
  319. wsprintf(lpszContent, _T("%s: %s"), A2T(lpszHeaderName), A2T(lpszHeaderValue));
  320. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_HEADER, content_len, lpszContent, lpszName);
  321. PostInfoMsg(msg);
  322. }
  323. void PostOnHeadersComplete(CONNID dwConnID, LPCSTR lpszSummary, LPCTSTR lpszName)
  324. {
  325. USES_CONVERSION;
  326. static LPCTSTR PREFIX = _T("* * * * * * * * * Summary * * * * * * * * *\r\n");
  327. static int PREFIX_LEN = lstrlen(PREFIX);
  328. LPCTSTR lpszSummaryT = A2CT(lpszSummary);
  329. int content_len = lstrlen(lpszSummaryT) + PREFIX_LEN + 1;
  330. LPTSTR lpszContent = new TCHAR[content_len];
  331. memcpy(lpszContent, PREFIX, PREFIX_LEN * sizeof(TCHAR));
  332. memcpy(lpszContent + PREFIX_LEN, lpszSummaryT, (content_len - PREFIX_LEN) * sizeof(TCHAR));
  333. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_HEADERS_COMPLETE, content_len, lpszContent, lpszName);
  334. PostInfoMsg(msg);
  335. }
  336. void PostOnBody(CONNID dwConnID, const BYTE* pData, int iLength, LPCTSTR lpszName)
  337. {
  338. LPTSTR lpszContent = new TCHAR[20];
  339. wsprintf(lpszContent, _T("(%d bytes)"), iLength);
  340. int content_len = lstrlen(lpszContent);
  341. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_BODY, content_len, lpszContent, lpszName);
  342. PostInfoMsg(msg);
  343. }
  344. void PostOnChunkHeader(CONNID dwConnID, int iLength, LPCTSTR lpszName)
  345. {
  346. LPTSTR lpszContent = new TCHAR[20];
  347. wsprintf(lpszContent, _T("(%d bytes)"), iLength);
  348. int content_len = lstrlen(lpszContent);
  349. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_CHUNK_HEADER, content_len, lpszContent, lpszName);
  350. PostInfoMsg(msg);
  351. }
  352. void PostOnChunkComplete(CONNID dwConnID, LPCTSTR lpszName)
  353. {
  354. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_CHUNK_COMPLETE, 0, nullptr, lpszName);
  355. PostInfoMsg(msg);
  356. }
  357. void PostOnMessageComplete(CONNID dwConnID, LPCTSTR lpszName)
  358. {
  359. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_MESSAGE_COMPLETE, 0, nullptr, lpszName);
  360. PostInfoMsg(msg);
  361. }
  362. void PostOnUpgrade(CONNID dwConnID, EnHttpUpgradeType enUpgradeType, LPCTSTR lpszName)
  363. {
  364. LPTSTR lpszContent = new TCHAR[20];
  365. wsprintf(lpszContent, _T("(type: %d)"), enUpgradeType);
  366. int content_len = lstrlen(lpszContent);
  367. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_UPGRADE, content_len, lpszContent, lpszName);
  368. PostInfoMsg(msg);
  369. }
  370. void PostOnParseError(CONNID dwConnID, int iErrorCode, LPCSTR lpszErrorDesc, LPCTSTR lpszName)
  371. {
  372. USES_CONVERSION;
  373. int content_len = (int)(strlen(lpszErrorDesc) + 10);
  374. LPTSTR lpszContent = new TCHAR[content_len];
  375. wsprintf(lpszContent, _T("(%i) : %s"), iErrorCode, A2T(lpszErrorDesc));
  376. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_PARSE_ERROR, content_len, lpszContent, lpszName);
  377. PostInfoMsg(msg);
  378. }
  379. void PostInfoMsg(info_msg* msg)
  380. {
  381. if( g_pMainWnd == nullptr ||
  382. g_pMainWnd->GetSafeHwnd() == nullptr ||
  383. !g_pMainWnd->PostMessage(USER_INFO_MSG, (WPARAM)msg ))
  384. info_msg::Destruct(msg);
  385. }
  386. void LogInfoMsg(info_msg* pInfoMsg)
  387. {
  388. CString msg;
  389. if(pInfoMsg->name)
  390. {
  391. if(pInfoMsg->connID > 0)
  392. {
  393. if(pInfoMsg->contentLength > 0)
  394. msg.Format(_T(" > [ %s #%Iu, %s ] -> %s"), pInfoMsg->name, pInfoMsg->connID, pInfoMsg->evt, pInfoMsg->content);
  395. else
  396. msg.Format(_T(" > [ %s #%Iu, %s ]"), pInfoMsg->name, pInfoMsg->connID, pInfoMsg->evt);
  397. }
  398. else
  399. {
  400. if(pInfoMsg->contentLength > 0)
  401. msg.Format(_T(" > [ %s - %s ] -> %s"), pInfoMsg->name, pInfoMsg->evt, pInfoMsg->content);
  402. else
  403. msg.Format(_T(" > [ %s - %s ]"), pInfoMsg->name, pInfoMsg->evt);
  404. }
  405. }
  406. else
  407. {
  408. if(pInfoMsg->connID > 0)
  409. {
  410. if(pInfoMsg->contentLength > 0)
  411. msg.Format(_T(" > [ %Iu, %s ] -> %s"), pInfoMsg->connID, pInfoMsg->evt, pInfoMsg->content);
  412. else
  413. msg.Format(_T(" > [ %Iu, %s ]"), pInfoMsg->connID, pInfoMsg->evt);
  414. }
  415. else
  416. {
  417. if(pInfoMsg->contentLength > 0)
  418. msg.Format(_T(" > [ %s ] -> %s"), pInfoMsg->evt, pInfoMsg->content);
  419. else
  420. msg.Format(_T(" > [ %s ]"), pInfoMsg->evt);
  421. }
  422. }
  423. LogMsg(msg);
  424. info_msg::Destruct(pInfoMsg);
  425. }
  426. void LogMsg(const CString& msg)
  427. {
  428. if(!g_pInfoList || !g_pInfoList->GetSafeHwnd())
  429. return;
  430. g_pInfoList->SetRedraw(FALSE);
  431. int iCurIndex = g_pInfoList->GetCurSel();
  432. BOOL bFirst = TRUE;
  433. int iStart = 0;
  434. int iAdd = 0;
  435. while(true)
  436. {
  437. CString item = msg.Tokenize(_T("\r\n"), iStart);
  438. if(iStart == -1)
  439. break;
  440. if(bFirst)
  441. bFirst = FALSE;
  442. else
  443. item.Insert(0, _T(" | "));
  444. g_pInfoList->AddString(item);
  445. ++iAdd;
  446. }
  447. int iCount = g_pInfoList->GetCount();
  448. BOOL bCurLast = (iCurIndex == LB_ERR || iCurIndex + iAdd == iCount - 1);
  449. int iDec = 0;
  450. while(g_pInfoList->GetCount() > MAX_LOG_RECORD_LENGTH)
  451. {
  452. g_pInfoList->DeleteString(0);
  453. ++iDec;
  454. }
  455. if(bCurLast)
  456. {
  457. iCurIndex = iCount - iDec - 1;
  458. g_pInfoList->SetCurSel(iCurIndex);
  459. }
  460. else if(iCurIndex < iDec)
  461. {
  462. iCurIndex = 0;
  463. g_pInfoList->SetCurSel(iCurIndex);
  464. }
  465. else
  466. iCurIndex = g_pInfoList->GetCurSel();
  467. g_pInfoList->SetAnchorIndex(iCurIndex);
  468. g_pInfoList->SetRedraw(TRUE);
  469. }
  470. CBufferPtr* GeneratePkgBuffer(DWORD seq, LPCTSTR lpszName, short age, LPCTSTR lpszDesc)
  471. {
  472. USES_CONVERSION;
  473. LPCSTR name = T2A((LPTSTR)lpszName);
  474. LPCSTR desc = T2A((LPTSTR)lpszDesc);
  475. int desc_len = (int)strlen(desc) + 1;
  476. int body_len = offsetof(TPkgBody, desc) + desc_len;
  477. TPkgBody* pBody = (TPkgBody*)_alloca(body_len);
  478. memset(pBody, 0, body_len);
  479. pBody->age = age;
  480. strcpy(pBody->name, name);
  481. strcpy(pBody->desc, desc);
  482. TPkgHeader header;
  483. header.seq = seq;
  484. header.body_len = body_len;
  485. return GeneratePkgBuffer(header, *pBody);
  486. }
  487. CBufferPtr* GeneratePkgBuffer(const TPkgHeader& header, const TPkgBody& body)
  488. {
  489. int header_len = sizeof(TPkgHeader);
  490. int body_len = header.body_len;
  491. CBufferPtr* pBuffer = new CBufferPtr(header_len + body_len);
  492. memcpy(pBuffer->Ptr(), (BYTE*)&header, header_len);
  493. memcpy(pBuffer->Ptr() + header_len, (BYTE*)&body, body_len);
  494. return pBuffer;
  495. }
  496. #ifdef _SSL_SUPPORT
  497. #include "../../../Common/Src/FuncHelper.h"
  498. CString g_c_strCAPemCertFileOrPath;
  499. CString g_c_strPemCertFile;
  500. CString g_c_strPemKeyFile;
  501. CString g_s_strCAPemCertFileOrPath;
  502. CString g_s_strPemCertFile;
  503. CString g_s_strPemKeyFile;
  504. int g_c_iVerifyMode = SSL_VM_PEER;
  505. LPCTSTR g_c_lpszCAPemCertFileOrPath = _T("ssl-cert\\ca.crt");
  506. LPCTSTR g_c_lpszPemCertFile = _T("ssl-cert\\client.cer");
  507. LPCTSTR g_c_lpszPemKeyFile = _T("ssl-cert\\client.key");
  508. LPCTSTR g_c_lpszKeyPasswod = _T("123456");
  509. int g_s_iVerifyMode = SSL_VM_PEER | SSL_VM_FAIL_IF_NO_PEER_CERT;
  510. LPCTSTR g_s_lpszCAPemCertFileOrPath = _T("ssl-cert\\ca.crt");
  511. LPCTSTR g_s_lpszPemCertFile = _T("ssl-cert\\server.cer");
  512. LPCTSTR g_s_lpszPemKeyFile = _T("ssl-cert\\server.key");
  513. LPCTSTR g_s_lpszKeyPasswod = _T("123456");
  514. BOOL InitSSLParams();
  515. BOOL g_SSLParams = InitSSLParams();
  516. BOOL InitSSLParams()
  517. {
  518. ::SetCurrentPathToModulePath();
  519. CString strPath;
  520. ::GetCurrentDirectory(MAX_PATH, strPath.GetBuffer(MAX_PATH));
  521. strPath.ReleaseBuffer();
  522. strPath += PATH_SEPARATOR_CHAR;
  523. if(g_c_lpszPemCertFile)
  524. {
  525. g_c_strPemCertFile = strPath + g_c_lpszPemCertFile;
  526. g_c_lpszPemCertFile = g_c_strPemCertFile;
  527. }
  528. if(g_c_lpszPemKeyFile)
  529. {
  530. g_c_strPemKeyFile = strPath + g_c_lpszPemKeyFile;
  531. g_c_lpszPemKeyFile = g_c_strPemKeyFile;
  532. }
  533. if(g_c_lpszCAPemCertFileOrPath)
  534. {
  535. g_c_strCAPemCertFileOrPath = strPath + g_c_lpszCAPemCertFileOrPath;
  536. g_c_lpszCAPemCertFileOrPath = g_c_strCAPemCertFileOrPath;
  537. }
  538. if(g_s_lpszPemCertFile)
  539. {
  540. g_s_strPemCertFile = strPath + g_s_lpszPemCertFile;
  541. g_s_lpszPemCertFile = g_s_strPemCertFile;
  542. }
  543. if(g_s_lpszPemKeyFile)
  544. {
  545. g_s_strPemKeyFile = strPath + g_s_lpszPemKeyFile;
  546. g_s_lpszPemKeyFile = g_s_strPemKeyFile;
  547. }
  548. if(g_s_lpszCAPemCertFileOrPath)
  549. {
  550. g_s_strCAPemCertFileOrPath = strPath + g_s_lpszCAPemCertFileOrPath;
  551. g_s_lpszCAPemCertFileOrPath = g_s_strCAPemCertFileOrPath;
  552. }
  553. return TRUE;
  554. }
  555. #endif