开源的socket服务端客户端,支持C# C++

helper.cpp 23KB

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