helper.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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, LPCTSTR lpszMethod, USHORT usUrlFieldSet, LPCTSTR lpszUrl, LPCTSTR lpszName)
  297. {
  298. int content_len = (int)(_tclen(lpszMethod) + _tclen(lpszUrl) + 20);
  299. LPTSTR lpszContent = new TCHAR[content_len];
  300. wsprintf(lpszContent, _T("[%s/0x%02X] : %s"), lpszMethod, usUrlFieldSet, lpszUrl);
  301. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_REQUEST_LINE, content_len, lpszContent, lpszName);
  302. PostInfoMsg(msg);
  303. }
  304. void PostOnStatusLine(CONNID dwConnID, USHORT usStatusCode, LPCTSTR lpszDesc, LPCTSTR lpszName)
  305. {
  306. int content_len = (int)(_tclen(lpszDesc) + 10);
  307. LPTSTR lpszContent = new TCHAR[content_len];
  308. wsprintf(lpszContent, _T("(%u) : %s"), usStatusCode, lpszDesc);
  309. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_STATUS_LINE, content_len, lpszContent, lpszName);
  310. PostInfoMsg(msg);
  311. }
  312. void PostOnHeader(CONNID dwConnID, LPCTSTR lpszHeaderName, LPCTSTR lpszHeaderValue, LPCTSTR lpszName)
  313. {
  314. int content_len = (int)(_tclen(lpszHeaderName) + _tclen(lpszHeaderValue)+10);
  315. LPTSTR lpszContent = new TCHAR[content_len];
  316. wsprintf(lpszContent, _T("%s: %s"), lpszHeaderName, lpszHeaderValue);
  317. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_HEADER, content_len, lpszContent, lpszName);
  318. PostInfoMsg(msg);
  319. }
  320. void PostOnHeadersComplete(CONNID dwConnID, LPCTSTR lpszSummary, LPCTSTR lpszName)
  321. {
  322. static LPCTSTR PREFIX = _T("* * * * * * * * * Summary * * * * * * * * *\r\n");
  323. static int PREFIX_LEN = lstrlen(PREFIX);
  324. LPCTSTR lpszSummaryT = A2CT(lpszSummary);
  325. int content_len = lstrlen(lpszSummaryT) + PREFIX_LEN + 1;
  326. LPTSTR lpszContent = new TCHAR[content_len];
  327. memcpy(lpszContent, PREFIX, PREFIX_LEN * sizeof(TCHAR));
  328. memcpy(lpszContent + PREFIX_LEN, lpszSummaryT, (content_len - PREFIX_LEN) * sizeof(TCHAR));
  329. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_HEADERS_COMPLETE, content_len, lpszContent, lpszName);
  330. PostInfoMsg(msg);
  331. }
  332. void PostOnBody(CONNID dwConnID, const BYTE* pData, int iLength, LPCTSTR lpszName)
  333. {
  334. LPTSTR lpszContent = new TCHAR[20];
  335. wsprintf(lpszContent, _T("(%d bytes)"), iLength);
  336. int content_len = lstrlen(lpszContent);
  337. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_BODY, content_len, lpszContent, lpszName);
  338. PostInfoMsg(msg);
  339. }
  340. void PostOnChunkHeader(CONNID dwConnID, int iLength, LPCTSTR lpszName)
  341. {
  342. LPTSTR lpszContent = new TCHAR[20];
  343. wsprintf(lpszContent, _T("(%d bytes)"), iLength);
  344. int content_len = lstrlen(lpszContent);
  345. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_CHUNK_HEADER, content_len, lpszContent, lpszName);
  346. PostInfoMsg(msg);
  347. }
  348. void PostOnChunkComplete(CONNID dwConnID, LPCTSTR lpszName)
  349. {
  350. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_CHUNK_COMPLETE, 0, nullptr, lpszName);
  351. PostInfoMsg(msg);
  352. }
  353. void PostOnMessageComplete(CONNID dwConnID, LPCTSTR lpszName)
  354. {
  355. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_MESSAGE_COMPLETE, 0, nullptr, lpszName);
  356. PostInfoMsg(msg);
  357. }
  358. void PostOnUpgrade(CONNID dwConnID, EnHttpUpgradeType enUpgradeType, LPCTSTR lpszName)
  359. {
  360. LPTSTR lpszContent = new TCHAR[20];
  361. wsprintf(lpszContent, _T("(type: %d)"), enUpgradeType);
  362. int content_len = lstrlen(lpszContent);
  363. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_UPGRADE, content_len, lpszContent, lpszName);
  364. PostInfoMsg(msg);
  365. }
  366. void PostOnParseError(CONNID dwConnID, int iErrorCode, LPCTSTR lpszErrorDesc, LPCTSTR lpszName)
  367. {
  368. int content_len = (int)(_tclen(lpszErrorDesc) + 10);
  369. LPTSTR lpszContent = new TCHAR[content_len];
  370. wsprintf(lpszContent, _T("(%i) : %s"), iErrorCode, lpszErrorDesc);
  371. info_msg* msg = info_msg::Construct(dwConnID, EVT_ON_PARSE_ERROR, content_len, lpszContent, lpszName);
  372. PostInfoMsg(msg);
  373. }
  374. void PostInfoMsg(info_msg* msg)
  375. {
  376. if( g_pMainWnd == nullptr ||
  377. g_pMainWnd->GetSafeHwnd() == nullptr ||
  378. !g_pMainWnd->PostMessage(USER_INFO_MSG, (WPARAM)msg ))
  379. info_msg::Destruct(msg);
  380. }
  381. void LogInfoMsg(info_msg* pInfoMsg)
  382. {
  383. CString msg;
  384. if(pInfoMsg->name)
  385. {
  386. if(pInfoMsg->connID > 0)
  387. {
  388. if(pInfoMsg->contentLength > 0)
  389. msg.Format(_T(" > [ %s #%Iu, %s ] -> %s"), pInfoMsg->name, pInfoMsg->connID, pInfoMsg->evt, pInfoMsg->content);
  390. else
  391. msg.Format(_T(" > [ %s #%Iu, %s ]"), pInfoMsg->name, pInfoMsg->connID, pInfoMsg->evt);
  392. }
  393. else
  394. {
  395. if(pInfoMsg->contentLength > 0)
  396. msg.Format(_T(" > [ %s - %s ] -> %s"), pInfoMsg->name, pInfoMsg->evt, pInfoMsg->content);
  397. else
  398. msg.Format(_T(" > [ %s - %s ]"), pInfoMsg->name, pInfoMsg->evt);
  399. }
  400. }
  401. else
  402. {
  403. if(pInfoMsg->connID > 0)
  404. {
  405. if(pInfoMsg->contentLength > 0)
  406. msg.Format(_T(" > [ %Iu, %s ] -> %s"), pInfoMsg->connID, pInfoMsg->evt, pInfoMsg->content);
  407. else
  408. msg.Format(_T(" > [ %Iu, %s ]"), pInfoMsg->connID, pInfoMsg->evt);
  409. }
  410. else
  411. {
  412. if(pInfoMsg->contentLength > 0)
  413. msg.Format(_T(" > [ %s ] -> %s"), pInfoMsg->evt, pInfoMsg->content);
  414. else
  415. msg.Format(_T(" > [ %s ]"), pInfoMsg->evt);
  416. }
  417. }
  418. LogMsg(msg);
  419. info_msg::Destruct(pInfoMsg);
  420. }
  421. void LogMsg(const CString& msg)
  422. {
  423. if(!g_pInfoList || !g_pInfoList->GetSafeHwnd())
  424. return;
  425. g_pInfoList->SetRedraw(FALSE);
  426. int iCurIndex = g_pInfoList->GetCurSel();
  427. BOOL bFirst = TRUE;
  428. int iStart = 0;
  429. int iAdd = 0;
  430. while(true)
  431. {
  432. CString item = msg.Tokenize(_T("\r\n"), iStart);
  433. if(iStart == -1)
  434. break;
  435. if(bFirst)
  436. bFirst = FALSE;
  437. else
  438. item.Insert(0, _T(" | "));
  439. g_pInfoList->AddString(item);
  440. ++iAdd;
  441. }
  442. int iCount = g_pInfoList->GetCount();
  443. BOOL bCurLast = (iCurIndex == LB_ERR || iCurIndex + iAdd == iCount - 1);
  444. int iDec = 0;
  445. while(g_pInfoList->GetCount() > MAX_LOG_RECORD_LENGTH)
  446. {
  447. g_pInfoList->DeleteString(0);
  448. ++iDec;
  449. }
  450. if(bCurLast)
  451. {
  452. iCurIndex = iCount - iDec - 1;
  453. g_pInfoList->SetCurSel(iCurIndex);
  454. }
  455. else if(iCurIndex < iDec)
  456. {
  457. iCurIndex = 0;
  458. g_pInfoList->SetCurSel(iCurIndex);
  459. }
  460. else
  461. iCurIndex = g_pInfoList->GetCurSel();
  462. g_pInfoList->SetAnchorIndex(iCurIndex);
  463. g_pInfoList->SetRedraw(TRUE);
  464. }
  465. CBufferPtr* GeneratePkgBuffer(DWORD seq, LPCTSTR lpszName, short age, LPCTSTR lpszDesc)
  466. {
  467. USES_CONVERSION;
  468. LPCSTR name = T2A((LPTSTR)lpszName);
  469. LPCSTR desc = T2A((LPTSTR)lpszDesc);
  470. int desc_len = (int)strlen(desc) + 1;
  471. int body_len = offsetof(TPkgBody, desc) + desc_len;
  472. TPkgBody* pBody = (TPkgBody*)_alloca(body_len);
  473. memset(pBody, 0, body_len);
  474. pBody->age = age;
  475. strcpy(pBody->name, name);
  476. strcpy(pBody->desc, desc);
  477. TPkgHeader header;
  478. header.seq = seq;
  479. header.body_len = body_len;
  480. return GeneratePkgBuffer(header, *pBody);
  481. }
  482. CBufferPtr* GeneratePkgBuffer(const TPkgHeader& header, const TPkgBody& body)
  483. {
  484. int header_len = sizeof(TPkgHeader);
  485. int body_len = header.body_len;
  486. CBufferPtr* pBuffer = new CBufferPtr(header_len + body_len);
  487. memcpy(pBuffer->Ptr(), (BYTE*)&header, header_len);
  488. memcpy(pBuffer->Ptr() + header_len, (BYTE*)&body, body_len);
  489. return pBuffer;
  490. }
  491. #ifdef _SSL_SUPPORT
  492. #include "../../../Common/Src/FuncHelper.h"
  493. CString g_c_strCAPemCertFileOrPath;
  494. CString g_c_strPemCertFile;
  495. CString g_c_strPemKeyFile;
  496. CString g_s_strCAPemCertFileOrPath;
  497. CString g_s_strPemCertFile;
  498. CString g_s_strPemKeyFile;
  499. int g_c_iVerifyMode = SSL_VM_PEER;
  500. LPCTSTR g_c_lpszCAPemCertFileOrPath = _T("ssl-cert\\ca.crt");
  501. LPCTSTR g_c_lpszPemCertFile = _T("ssl-cert\\client.cer");
  502. LPCTSTR g_c_lpszPemKeyFile = _T("ssl-cert\\client.key");
  503. LPCTSTR g_c_lpszKeyPasswod = _T("123456");
  504. int g_s_iVerifyMode = SSL_VM_PEER | SSL_VM_FAIL_IF_NO_PEER_CERT;
  505. LPCTSTR g_s_lpszCAPemCertFileOrPath = _T("ssl-cert\\ca.crt");
  506. LPCTSTR g_s_lpszPemCertFile = _T("ssl-cert\\server.cer");
  507. LPCTSTR g_s_lpszPemKeyFile = _T("ssl-cert\\server.key");
  508. LPCTSTR g_s_lpszKeyPasswod = _T("123456");
  509. BOOL InitSSLParams();
  510. BOOL g_SSLParams = InitSSLParams();
  511. BOOL InitSSLParams()
  512. {
  513. ::SetCurrentPathToModulePath();
  514. CString strPath;
  515. ::GetCurrentDirectory(MAX_PATH, strPath.GetBuffer(MAX_PATH));
  516. strPath.ReleaseBuffer();
  517. strPath += PATH_SEPARATOR_CHAR;
  518. if(g_c_lpszPemCertFile)
  519. {
  520. g_c_strPemCertFile = strPath + g_c_lpszPemCertFile;
  521. g_c_lpszPemCertFile = g_c_strPemCertFile;
  522. }
  523. if(g_c_lpszPemKeyFile)
  524. {
  525. g_c_strPemKeyFile = strPath + g_c_lpszPemKeyFile;
  526. g_c_lpszPemKeyFile = g_c_strPemKeyFile;
  527. }
  528. if(g_c_lpszCAPemCertFileOrPath)
  529. {
  530. g_c_strCAPemCertFileOrPath = strPath + g_c_lpszCAPemCertFileOrPath;
  531. g_c_lpszCAPemCertFileOrPath = g_c_strCAPemCertFileOrPath;
  532. }
  533. if(g_s_lpszPemCertFile)
  534. {
  535. g_s_strPemCertFile = strPath + g_s_lpszPemCertFile;
  536. g_s_lpszPemCertFile = g_s_strPemCertFile;
  537. }
  538. if(g_s_lpszPemKeyFile)
  539. {
  540. g_s_strPemKeyFile = strPath + g_s_lpszPemKeyFile;
  541. g_s_lpszPemKeyFile = g_s_strPemKeyFile;
  542. }
  543. if(g_s_lpszCAPemCertFileOrPath)
  544. {
  545. g_s_strCAPemCertFileOrPath = strPath + g_s_lpszCAPemCertFileOrPath;
  546. g_s_lpszCAPemCertFileOrPath = g_s_strCAPemCertFileOrPath;
  547. }
  548. return TRUE;
  549. }
  550. #endif