ConnectSocket.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  1. /********************************************************************/
  2. /* */
  3. /* CONNECTSOCKET.CPP */
  4. /* */
  5. /* Implementation of the Connect Socket. */
  6. /* This class is a part of the CConnectThread which handles */
  7. /* socket connections. Incomming data is processed in OnReceive */
  8. /* */
  9. /* Programmed by LYFZ van der Meer */
  10. /* Based partially on and inspired by FileZilla Server. */
  11. /* */
  12. /* http://www.LYFZvandermeer.nl */
  13. /* */
  14. /* Last updated: 15 july 2002 */
  15. /* */
  16. /********************************************************************/
  17. //在ConnectSocket.cpp文件中实现连接套接字的创建,该类是CConnectThread类的
  18. //一部分,收到数据由函数OnReceive()完成处理。 net_Send
  19. #include "stdafx.h"
  20. #include "LYFZReceiveMsgApp.h"
  21. #include "LYFZReceiveMsg.h"
  22. #include "ConnectSocket.h"
  23. #include "ConnectThread.h"
  24. #include "ApplicationDlg.h"
  25. #include "DataSocket.h"
  26. #include "mysqldata.h"
  27. #include "./include/netMain.h"
  28. #include "./include/protocolhdr.h"
  29. #include "./include/struct_def.h"
  30. #include "MyLock.h"
  31. extern CLYFZReceiveMsg theServer;
  32. extern BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword);
  33. #ifdef _DEBUG
  34. #define new DEBUG_NEW
  35. #undef THIS_FILE
  36. static char THIS_FILE[] = __FILE__;
  37. #endif
  38. extern CLYFZReceiveMsg *g_pWndServer;
  39. CStringArray g_baduserarray;
  40. ////////////////////////////////////////insert into
  41. BOOL BadUser(CString account)
  42. {
  43. for(int i=0; i<g_baduserarray.GetSize (); i++)
  44. {
  45. if(account==g_baduserarray.ElementAt (i))return 1;
  46. }
  47. return 0;
  48. }
  49. //----------------------------------------------------------------------------------
  50. // Remark by Jeff;
  51. // 函数:CheckUser;
  52. // 描述:检查短信用户账号、密码;
  53. // 参数:
  54. // account: 短信账号;
  55. // psw: 短信账号密码;
  56. // name: 用户名?
  57. // phone: 接收短信的手机;
  58. // signname: 短信内容签名;
  59. // pDb: 连接的数据库;
  60. // nRoadType: 短信通道类型?
  61. //
  62. // 返回:
  63. // FALSE 表示账号和密码错误;
  64. // TRUE 表示账号和密码正确;
  65. //
  66. //
  67. //
  68. //----------------------------------------------------------------------------------
  69. BOOL CheckUser(CString account, CString psw, CString &name, CString &phone, CString &signname, CDatabase *pDb, int &nRoadType)
  70. {
  71. name="";
  72. phone="";
  73. signname="";
  74. nRoadType=0;
  75. CRstClientInfo rsSt;
  76. rsSt.m_pDatabase=pDb;
  77. rsSt.m_strFilter ="[account]='"+account+"' and [psw]='"+psw+"'";
  78. rsSt.Open();
  79. if(!rsSt.IsEOF())
  80. {
  81. name=rsSt.m_name;
  82. phone=rsSt.m_phone;
  83. nRoadType=atoi(rsSt.m_road);
  84. signname=rsSt.m_signname;
  85. rsSt.Close();
  86. return 1;
  87. }
  88. rsSt.Close();
  89. return 0;
  90. }
  91. //------------------------------------------------------------------------------
  92. // Remark by Jeff;
  93. // 函数:CheckPhoneType
  94. // 描述:判断手机号属于哪个运营商的;
  95. // 参数:
  96. // phoneno: 欲判断的手机号;
  97. //
  98. // 返回:返回0表示移动; 1表示联通; 2表示电信; 3表示小灵通; -1表示未知的;
  99. //
  100. // 注意:
  101. //
  102. //------------------------------------------------------------------------------
  103. int CheckPhoneType(CString phoneno)
  104. {
  105. int i = 0;
  106. if(phoneno.IsEmpty ())return -1;
  107. for( i=0; i<phoneno.GetLength (); i++)
  108. {
  109. if(phoneno.GetAt (i)<'0'||phoneno.GetAt (i)>'9')return -1;
  110. }
  111. if(phoneno.GetAt (0)=='1')
  112. {
  113. if(phoneno.GetLength ()!=11)
  114. return -1;
  115. int mobile[]={139,138,137,136,135,134,159,158,152,151,150,157,188,187,144,182,147,183,184,178};
  116. int unicom[]={130,131,132,155,156,186,185,176};
  117. int telecom[]={133,153,189,180,181,177};
  118. for(i=0; i<sizeof(mobile)/sizeof(int); i++)
  119. {
  120. if(mobile[i]==atoi(phoneno.Left (3)))
  121. {
  122. return 0;
  123. }
  124. }
  125. for(i=0; i<sizeof(unicom)/sizeof(int); i++)
  126. {
  127. if(unicom[i]==atoi(phoneno.Left (3)))
  128. {
  129. return 1;
  130. }
  131. }
  132. for(i=0; i<sizeof(telecom)/sizeof(int); i++)
  133. {
  134. if(telecom[i]==atoi(phoneno.Left (3)))
  135. {
  136. return 2;
  137. }
  138. }
  139. return -1;
  140. }
  141. /* else if(phoneno.GetAt (0)=='0')
  142. {
  143. if(phoneno.GetLength ()>=10 && phoneno.GetLength ()<=12)
  144. {
  145. return 3;
  146. }
  147. }*/
  148. return -1;
  149. }
  150. BOOL MyExecuteSQL(CDatabase *pdb, CString sql)
  151. {
  152. // MyLock lock("xiaoaccessdbmsg");
  153. try
  154. {
  155. pdb->ExecuteSQL (sql);
  156. }
  157. catch(CDBException * e)
  158. {
  159. WriteError(sql+"操作数据库失败"+e->m_strError);
  160. return 0;
  161. }
  162. return 1;
  163. }
  164. void ReCalAccount(CString account, CDatabase *pDb)
  165. {
  166. CString sql;
  167. if(1)
  168. {
  169. // MyLock lock("xiaoaccessdbmsg");
  170. CRecordset myset(pDb);
  171. CString count1,count2,count3;
  172. #ifdef SQLSERVER_VERSION
  173. sql="select sum(cast([count] as float)) as cot from recharge where account='"+account+"'";
  174. #else
  175. sql="select sum([count]) as cot from recharge where account='"+account+"'";
  176. #endif
  177. myset.Open (CRecordset::forwardOnly, sql);
  178. if(!myset.IsEOF())
  179. myset.GetFieldValue ("cot", count1);
  180. myset.Close();
  181. #ifdef SQLSERVER_VERSION
  182. sql="select sum(cast([msgcount] as float)) as cot from sendreg where account='"+account+"'";
  183. #else
  184. sql="select sum([msgcount]) as cot from sendreg where account='"+account+"'";
  185. #endif
  186. myset.Open (CRecordset::forwardOnly, sql);
  187. if(!myset.IsEOF())
  188. myset.GetFieldValue ("cot", count2);
  189. myset.Close();
  190. sql="select count(*) as cot from receivemsg where [account]='"+account+"' and [replyok]='1'";
  191. myset.Open (CRecordset::forwardOnly, sql);
  192. if(!myset.IsEOF())
  193. myset.GetFieldValue ("cot", count3);
  194. myset.Close();
  195. // sql.Format ("update clientinfo set balance='%d' where account='%s'", atoi(count1)-min(atoi(count2),atoi(count3)), account);
  196. sql.Format ("update clientinfo set balance='%d' where account='%s'", (atoi(count1)-atoi(count2))-atoi(count3), account);
  197. }
  198. MyExecuteSQL(pDb, sql);
  199. }
  200. /*
  201. 移动:
  202. 139,138,137,136,135,134,159,158,152,151,150,157,188,187,144
  203. 联通:
  204. 130,131,132,155,156,186,185
  205. 电信:
  206. 133,153,189,180
  207. */
  208. extern void WriteLogin(CString str);
  209. //------------------------------------------------------------------------------------------
  210. // Remark by Jeff; 2014-09-21
  211. // 函数:GetBalance
  212. // 描述:通过Select Sum SQL语句,获取数据库指定条件值的总和;
  213. // 参数:
  214. // account: 短信账号;
  215. // pDB: 连接的数据库实例对象指针;
  216. //
  217. // 返回:查询失败返回-1;成功返回其他值;
  218. //
  219. // 注意:
  220. // 1.如果[sendreg].[msgcount]值不对的话,那么会造成很严重的后果,不仅返回给客户的值不对,扣费也错乱甚至造成亏本;
  221. //
  222. //
  223. //------------------------------------------------------------------------------------------
  224. int GetBalance(CString account, CDatabase *pDb)
  225. {
  226. CString count1=""; // 该账号充值的短信数量,[recharge]表; (应该不是剩余数量,这个充值的值应该是在下一次充值时才会改变)
  227. CString count2=""; // 短信发送成功的扣费总和,[sendreg]表;
  228. CString count3=""; // 短信接收成功的扣费总和,[receivemsg]表;
  229. try
  230. {
  231. CRecordset myset(pDb);
  232. #ifdef SQLSERVER_VERSION
  233. CString sql="select sum(cast([count] as float)) as cot from recharge where account='"+account+"'";
  234. #else
  235. CString sql="select sum([count]) as cot from recharge where account='"+account+"'";
  236. #endif
  237. myset.Open (CRecordset::forwardOnly, sql);
  238. if(!myset.IsEOF())
  239. myset.GetFieldValue ("cot", count1);
  240. myset.Close();
  241. #ifdef SQLSERVER_VERSION
  242. sql="select sum(cast([msgcount] as float)) as cot from sendreg where account='"+account+"'";
  243. #else
  244. sql="select sum([msgcount]) as cot from sendreg where account='"+account+"'";
  245. #endif
  246. myset.Open(CRecordset::forwardOnly, sql);
  247. if(!myset.IsEOF())
  248. myset.GetFieldValue("cot", count2);
  249. myset.Close();
  250. sql="select count(*) as cot from receivemsg where [account]='"+account+"' and [replyok]='1'";
  251. myset.Open (CRecordset::forwardOnly, sql);
  252. if(!myset.IsEOF())
  253. myset.GetFieldValue ("cot", count3);
  254. myset.Close();
  255. return (atoi(count1)-atoi(count2))-atoi(count3);
  256. }
  257. catch(...)
  258. {
  259. return -1;
  260. }
  261. }
  262. int GetLengthEx(CString str) // Jeff:获取多字节对应的宽字符数;
  263. {
  264. #if 0 // Jeff delete;
  265. wchar_t wstr[500];
  266. int k = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, strlen(str), wstr, 500);
  267. return k;
  268. #else
  269. return ::MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0);// Jeff.无论constent是否空,MultiByteToWideChar返回结果都>=1;
  270. #endif
  271. }
  272. //------------------------------------------------------------------------------------------
  273. // Remark by Jeff; 2014-09-21
  274. // 函数:GetBalanceUsed
  275. // 描述:获取账号可用剩余短信数,并更新到数据库表记录中;
  276. // 参数:
  277. // account: 短信账号;
  278. // pDB: 连接的数据库实例对象指针;
  279. // ret1: 账号可用的剩余短信数量;
  280. // ret2: 账号已用的短信总数;
  281. //
  282. // 返回:查询失败返回-1;成功返回其他值;
  283. //
  284. //
  285. //------------------------------------------------------------------------------------------
  286. void GetBalanceUsed(CString account, int &ret1,int &ret2, CDatabase *pDb)
  287. {
  288. CString count1=""; // 该账号充值的短信数量,[recharge]表; (应该不是剩余数量,这个充值的值应该是在下一次充值时才会改变)
  289. CString count2=""; // 短信发送成功的扣费总和,[sendreg]表;
  290. CString count3=""; // 短信接收成功的扣费总和,[receivemsg]表;
  291. ret1=0;
  292. ret2=0;
  293. try
  294. {
  295. CRecordset myset(pDb);
  296. #ifdef SQLSERVER_VERSION
  297. CString sql="select sum(cast([count] as float)) as cot from recharge where account='"+account+"'";
  298. #else
  299. CString sql="select sum([count]) as cot from recharge where account='"+account+"'";
  300. #endif
  301. myset.Open (CRecordset::forwardOnly, sql);
  302. if(!myset.IsEOF())
  303. myset.GetFieldValue ("cot", count1);
  304. myset.Close();
  305. #ifdef SQLSERVER_VERSION
  306. sql="select sum(cast([msgcount] as float)) as cot from sendreg where account='"+account+"'";
  307. #else
  308. sql="select sum([msgcount]) as cot from sendreg where account='"+account+"'";
  309. #endif
  310. myset.Open (CRecordset::forwardOnly, sql);
  311. if(!myset.IsEOF())
  312. myset.GetFieldValue ("cot", count2);
  313. myset.Close();
  314. sql="select count(*) as cot from receivemsg where [account]='"+account+"' and [replyok]='1'";
  315. myset.Open (CRecordset::forwardOnly, sql);
  316. if(!myset.IsEOF())
  317. myset.GetFieldValue ("cot", count3);
  318. myset.Close();
  319. ret2 = atoi(count2)+atoi(count3) ;
  320. ret1 = atoi(count1)-ret2;
  321. sql.Format ("update clientinfo set balance='%d' where account='%s'", ret1, account);
  322. MyExecuteSQL(pDb, sql);
  323. }
  324. catch(...)
  325. {
  326. }
  327. }
  328. //------------------------------------------------------------------------------------------
  329. // Remark by Jeff: 2014.09.21
  330. // 函数:GetCount
  331. // 描述:指定短信内容和手机号,计算出要发送的短信条数;
  332. // 参数:
  333. // phones: 以逗号分隔的手机号串;
  334. // content: 短信内容;
  335. //
  336. // 返回:短信条数;
  337. //
  338. // 注意:
  339. //
  340. //------------------------------------------------------------------------------------------
  341. int GetCount(CString phones, CString content)
  342. {
  343. #if 0 // Jeff delete;
  344. int count=0;
  345. int leng=GetLengthEx(content);
  346. int m_nMobile=0; // Jeff:移动联通电信手机数;
  347. int m_nPhone=0; // Jeff:小灵通手机数;
  348. CStringArray phonearray;
  349. int pos=phones.Find (",");
  350. if(pos!=-1)
  351. {
  352. phonearray.Add(phones.Left (pos));
  353. phones=phones.Right (phones.GetLength ()-pos-1);
  354. pos=phones.Find (",");
  355. while(pos!=-1)
  356. {
  357. phonearray.Add (phones.Left (pos));
  358. phones=phones.Right (phones.GetLength ()-pos-1);
  359. pos=phones.Find (",");
  360. }
  361. phonearray.Add (phones);
  362. }
  363. else
  364. phonearray.Add (phones);
  365. for(int i=0; i<phonearray.GetSize (); i++)
  366. {
  367. phones=phonearray.ElementAt (i);
  368. if(CheckPhoneType(phones)==0 || CheckPhoneType(phones)==1 || CheckPhoneType(phones)==2)
  369. m_nMobile++;
  370. else if(CheckPhoneType(phones)==3)
  371. m_nPhone++;
  372. }
  373. // Jeff:小灵通和以外的手机短信字符长度有差别;
  374. if(m_nMobile)
  375. {
  376. count+=m_nMobile*(leng/70);
  377. if(leng%70)
  378. count+=m_nMobile;
  379. }
  380. if(m_nPhone)
  381. {
  382. count+=m_nPhone*(leng/56);
  383. if(leng%56)
  384. count+=m_nPhone;
  385. }
  386. return count;
  387. #else
  388. // 1.首先分析出短信内容会被拆分成多少条短信发送;
  389. int nNumberOfTextMsg = 0; // 短信条数;
  390. int nWCharLen = ::MultiByteToWideChar(CP_ACP,0,content,-1,NULL,0); // Jeff.无论constent是否空,MultiByteToWideChar返回结果都>=1;
  391. // 魔数MSG_LENGTH = 67:
  392. // 当短信字符数超过70时,短信服务商会分为两部分;
  393. // 1.将前67个字符为一条短信;
  394. // 2.将67后的为另一部分,若仍大于70,再次执行1;
  395. // 3.若短信小于等于70,则为一条短信;
  396. nNumberOfTextMsg = nWCharLen / 67;
  397. if (nWCharLen % 67 > 2 ) ++nNumberOfTextMsg;
  398. //if (nWCharLen % 67 ) ++nNumberOfTextMsg; // 如果 nWCharLen%67 不大于2,会多扣客户一条短信费用;
  399. // 2.其次解析手机字符串,获取手机数量;(不考虑小灵通,通用使用67字符截断短信)
  400. int nCountOfMobile = 0;
  401. int nIndex = 0;
  402. do
  403. {
  404. nIndex = phones.Find(",");
  405. if (nIndex != -1)
  406. {
  407. if ( nIndex == 11 && CheckPhoneType(phones.Left(nIndex)) != -1) // Jeff,CheckPhoneType返回-1表示未知手机类型;
  408. ++nCountOfMobile;
  409. phones = phones.Mid(nIndex+1);
  410. }
  411. else
  412. {
  413. if ( phones.GetLength() == 11 && CheckPhoneType(phones) != -1)
  414. ++nCountOfMobile;
  415. }
  416. } while (nIndex != -1);
  417. return nNumberOfTextMsg *= nCountOfMobile;
  418. #endif
  419. }
  420. //---------------------------------------------------------------------------------------------
  421. // Remark by Jeff;
  422. // 函数:IsExist
  423. // 描述:查询指定的短信账号要发送的【目标内容+手机+时间戳】是否存在;
  424. // 参数:
  425. // account: 短信账号;
  426. // phones: 接收短信的手机串;
  427. // content: 短信内容;
  428. // timestamp: 发送时间;
  429. // pDb: 连接数据库的对象指针;
  430. //
  431. // 返回:存在返回TRUE;
  432. //
  433. //---------------------------------------------------------------------------------------------
  434. BOOL IsExist(CString account, CString phones, CString content, CString timestamp, CDatabase *pDb)
  435. {
  436. //已保存此条记录, 不再保存, 否则重复
  437. // MyLock lock("xiaoaccessdbmsg");
  438. CRecordset myset(pDb);
  439. CString sql="select count(*) as cot from sendreg where [account]='"+account+"' and [content]='"+content+"' and [phones]='"+phones+"' and [timestamp]='"+timestamp+"'";
  440. myset.Open (CRecordset::forwardOnly, sql);
  441. if(!myset.IsEOF())
  442. {
  443. myset.GetFieldValue ("cot", sql);
  444. if(atoi(sql))
  445. return 1;
  446. else
  447. return 0;
  448. }
  449. else
  450. return 0;
  451. }
  452. void WriteLog(CString account, CString str)
  453. {
  454. }
  455. /********************************************************************/
  456. /* */
  457. /* Function name : CConnectSocket::CConnectSocket */
  458. /* Description : Constructor */
  459. /* */
  460. /********************************************************************/
  461. CConnectSocket::CConnectSocket()
  462. {
  463. m_bLoggedon = FALSE;
  464. m_bRenameFile = FALSE;
  465. m_pDataSocket = NULL;
  466. m_nRemotePort = -1;
  467. m_dwRestartOffset = 0;
  468. m_bPassiveMode = FALSE;
  469. m_reved=0;
  470. m_dateleng=0;
  471. m_pRevBuf=NULL;
  472. }
  473. /********************************************************************/
  474. /* */
  475. /* Function name : CConnectSocket::~CConnectSocket */
  476. /* Description : Destructor */
  477. /* */
  478. /********************************************************************/
  479. CConnectSocket::~CConnectSocket()
  480. {
  481. DestroyDataConnection();
  482. // tell our thread we have been closed
  483. AfxGetThread()->PostThreadMessage(WM_QUIT,0,0);
  484. TRACE0("CConnectSocket destroyed.\n");
  485. if(m_pRevBuf)delete []m_pRevBuf;
  486. }
  487. // Do not edit the following lines, which are needed by ClassWizard.
  488. #if 0
  489. BEGIN_MESSAGE_MAP(CConnectSocket, CSocket)
  490. //{{AFX_MSG_MAP(CConnectSocket)
  491. //}}AFX_MSG_MAP
  492. END_MESSAGE_MAP()
  493. #endif // 0
  494. /********************************************************************/
  495. /* */
  496. /* Function name : OnClose */
  497. /* Description : Send WM_QUIT message to the thread containing */
  498. /* the socket to shutdown once the connection is */
  499. /* closed. */
  500. /* */
  501. /********************************************************************/
  502. void CConnectSocket::OnClose(int nErrorCode)
  503. {
  504. try
  505. {
  506. Close();
  507. // destroy connection
  508. m_pThread->PostThreadMessage(WM_THREADMSG, 1, 0);
  509. TRACE("CConnectSocket() OnClose()\n");
  510. CSocket::OnClose(nErrorCode);
  511. }
  512. catch(...)
  513. {
  514. }
  515. }
  516. #define BUFFERSIZE 4096
  517. /********************************************************************/
  518. /* */
  519. /* Function name : OnReceive */
  520. /* Description : Called by the framework to notify this socket */
  521. /* that there is data in the buffer. */
  522. /* */
  523. /********************************************************************/
  524. //由主窗口调用通知套接字缓冲区中有数据要接收
  525. void CConnectSocket::OnReceive(int nErrorCode)
  526. {
  527. TCHAR buff[BUFFERSIZE];
  528. //接收数据并放到缓冲区
  529. int nRead = Receive(buff, BUFFERSIZE);
  530. switch (nRead)
  531. {
  532. case 0:
  533. Close();
  534. break;
  535. case SOCKET_ERROR:
  536. if (GetLastError() != WSAEWOULDBLOCK)
  537. {
  538. // TCHAR szError[256];
  539. // wsprintf(szError, "OnReceive error: %d", GetLastError());
  540. // AfxMessageBox (szError);
  541. }
  542. break;
  543. default:
  544. if (nRead != SOCKET_ERROR && nRead != 0)
  545. {
  546. ((CConnectThread *)AfxGetThread())->IncReceivedBytes(nRead);
  547. TMessageHeader *pMessageHeader = (TMessageHeader *)buff;
  548. /*网络字节顺序的转换*/
  549. ntohs(pMessageHeader->wHeaderFlag);
  550. ntohs(pMessageHeader->wMessageId);
  551. ntohs(pMessageHeader->wMessageSubId);
  552. ntohl(pMessageHeader->dwDataLen);
  553. ntohs(pMessageHeader->wCheckSum);
  554. ntohl(pMessageHeader->wReserve);
  555. if(pMessageHeader->wMessageId ==MSG_LOGIN_REQ)
  556. {
  557. WORD wMessageId = MSG_LOGIN_RESP;
  558. LOGIN_RESULT_STRU tLoginResult = {0};
  559. tLoginResult.tCommonMsg.dwConnectionID = 0;
  560. tLoginResult.tCommonMsg.wMessageId = wMessageId;
  561. tLoginResult.byResult = LOGIN_RESULT_SUC;
  562. tLoginResult.dwUserID = 0;
  563. tLoginResult.byStatus = USER_STATUS_ONLINE ;
  564. DWORD dwDataLen = sizeof(LOGIN_RESULT_STRU);
  565. TMessageHeader tHeader = {0};
  566. tHeader.wMessageId = wMessageId;
  567. tHeader.dwDataLen = dwDataLen;
  568. net_Send(&tHeader, (void *)&tLoginResult, dwDataLen);
  569. }
  570. else //if(pMessageHeader->wMessageId ==MSG_CHATMESSAGE_REQ)
  571. {
  572. /* char *pDataBuf = (char *)buff + MESSAGE_HEADER_LEN;
  573. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pDataBuf;
  574. DWORD dwConnectionID = pChatMessage->tCommonMsg.dwConnectionID;
  575. processChatMessageRequest((void *)pChatMessage);*/
  576. // 结束字符串
  577. if(pMessageHeader->wHeaderFlag==MESSAGE_HEADER_FLAG && pMessageHeader->wMessageId ==MSG_CHATMESSAGE_REQ)
  578. {
  579. if(m_pRevBuf)delete []m_pRevBuf;
  580. m_pRevBuf=NULL;
  581. m_reved=0;
  582. m_dateleng=0;
  583. m_dateleng=pMessageHeader->dwDataLen+MESSAGE_HEADER_LEN;
  584. m_pRevBuf=new BYTE[m_dateleng];
  585. //WriteLogin("开始接收");
  586. }
  587. if(m_dateleng==0)
  588. return;
  589. ::memcpy(m_pRevBuf+m_reved, buff, min(nRead, m_dateleng-m_reved));
  590. m_reved+=min(nRead, m_dateleng-m_reved);
  591. // CString ss;
  592. // ss.Format ("接收:%d/%d", m_reved, m_dateleng);
  593. // WriteLogin(ss);
  594. if(m_reved>=m_dateleng)
  595. {
  596. // WriteLogin("接收完成");
  597. char *pDataBuf = (char *)m_pRevBuf + MESSAGE_HEADER_LEN;
  598. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pDataBuf;
  599. m_reved=0;m_dateleng=0;
  600. processChatMessageRequest((void *)pChatMessage);
  601. }
  602. }
  603. }
  604. break;
  605. }
  606. CSocket::OnReceive(nErrorCode);
  607. }
  608. /********************************************************************/
  609. /* */
  610. /* Function name: GetRxCommand */
  611. /* Description : Get command from receiver buffer. */
  612. /* */
  613. /********************************************************************/
  614. //从接收缓冲区获得命令
  615. BOOL CConnectSocket::GetRxCommand(CString &strCommand, CString &strArguments)
  616. {
  617. return FALSE;
  618. }
  619. /********************************************************************/
  620. /* */
  621. /* Function name: GetRxLine */
  622. /* Description : Parse complete command line */
  623. /* */
  624. /********************************************************************/
  625. //解析整个命令行
  626. void CConnectSocket::GetRxLine()
  627. {
  628. }
  629. /********************************************************************/
  630. /* */
  631. /* Function name: OnConnect */
  632. /* Description : Called by the framework to notify this connecting */
  633. /* socket that its connection attempt is completed. */
  634. /* */
  635. /********************************************************************/
  636. void CConnectSocket::OnConnect(int nErrorCode)
  637. {
  638. CSocket::OnConnect(nErrorCode);
  639. }
  640. /********************************************************************/
  641. /* */
  642. /* Function name: HasConnectionDropped */
  643. /* Description : Check if connection has been dropped. */
  644. /* Used to detect if client has crashed. */
  645. /* */
  646. /********************************************************************/
  647. BOOL CConnectSocket::HasConnectionDropped(void)
  648. {
  649. try
  650. {
  651. BOOL bConnDropped = FALSE;
  652. INT iRet = 0;
  653. BOOL bOK = TRUE;
  654. if (m_hSocket == INVALID_SOCKET)
  655. return TRUE;
  656. struct timeval timeout = { 0, 0 };
  657. fd_set readSocketSet;
  658. FD_ZERO(&readSocketSet);
  659. FD_SET(m_hSocket, &readSocketSet);
  660. iRet = ::select(0, &readSocketSet, NULL, NULL, &timeout);
  661. bOK = (iRet > 0);
  662. if(bOK)
  663. {
  664. bOK = FD_ISSET(m_hSocket, &readSocketSet);
  665. }
  666. if(bOK)
  667. {
  668. CHAR szBuffer[1] = "";
  669. iRet = ::recv(m_hSocket, szBuffer, 1, MSG_PEEK);
  670. bOK = (iRet > 0);
  671. if(!bOK)
  672. {
  673. INT iError = ::WSAGetLastError();
  674. bConnDropped = (( iError == WSAENETRESET) ||
  675. (iError == WSAECONNABORTED) ||
  676. (iError == WSAECONNRESET) ||
  677. (iError == WSAEINVAL) ||
  678. (iRet == 0));
  679. }
  680. }
  681. return(bConnDropped);
  682. }
  683. catch(...)
  684. {
  685. }
  686. return 1;
  687. }
  688. /********************************************************************/
  689. /* */
  690. /* Function name: SendResponse */
  691. /* Description : Send response to client. */
  692. /* */
  693. /********************************************************************/
  694. //发送响应给客户端
  695. BOOL CConnectSocket::SendResponse(LPCTSTR pstrFormat, ...)
  696. {
  697. return TRUE;
  698. }
  699. //从客户端解析并执行命令
  700. void CConnectSocket::ParseCommand()
  701. {
  702. }
  703. /********************************************************************/
  704. /* */
  705. /* Function name: FireStatusMessage */
  706. /* Description : Fire status message. */
  707. /* */
  708. /********************************************************************/
  709. void CConnectSocket::FireStatusMessage(LPCTSTR lpszStatus, int nType)
  710. {
  711. try
  712. {
  713. g_pWndServer->AddTraceLine(nType, "[%d] %s", m_pThread->m_nThreadID, lpszStatus);
  714. }
  715. catch(...)
  716. {
  717. }
  718. }
  719. /********************************************************************/
  720. /* */
  721. /* Function name: CreateDataConnection */
  722. /* Description : Create data transfer connection. */
  723. /* */
  724. /********************************************************************/
  725. BOOL CConnectSocket::CreateDataConnection(int nTransferType, LPCTSTR lpszData)
  726. {
  727. return TRUE;
  728. }
  729. /********************************************************************/
  730. /* */
  731. /* Function name: DestroyDataConnection */
  732. /* Description : Close data transfer connection. */
  733. /* */
  734. /********************************************************************/
  735. void CConnectSocket::DestroyDataConnection()
  736. {
  737. }
  738. unsigned long CConnectSocket::net_Send(IN void *pHeader, IN void *pMessage, IN unsigned long ulDataLen)
  739. {
  740. try
  741. {
  742. unsigned long ulSendLen = 0;
  743. unsigned long ulBufLen = 0;
  744. TMessageHeader *pSendHeader = (TMessageHeader *)pHeader;
  745. ulBufLen = MESSAGE_HEADER_LEN + ulDataLen;
  746. char *pSendBuf = new char[ulBufLen];
  747. memset(pSendBuf, 0, ulBufLen);
  748. TMessageHeader *pMessageHeader = (TMessageHeader *)pSendBuf;
  749. pMessageHeader->byVersion = 101;
  750. pMessageHeader->wHeaderFlag = MESSAGE_HEADER_FLAG;
  751. pMessageHeader->wMessageId = pSendHeader->wMessageId;
  752. pMessageHeader->wMessageSubId = pSendHeader->wMessageSubId;
  753. pMessageHeader->dwDataLen = ulDataLen;
  754. pMessageHeader->wReserve = 0;
  755. memcpy((void *)(pSendBuf+MESSAGE_HEADER_LEN), pMessage, ulDataLen);
  756. // convert network word
  757. htons(pMessageHeader->wHeaderFlag);
  758. htons(pMessageHeader->wMessageId);
  759. htons(pMessageHeader->wMessageSubId);
  760. htonl(pMessageHeader->dwDataLen);
  761. htons(pMessageHeader->wCheckSum);
  762. htonl(pMessageHeader->wReserve);
  763. //Send the message
  764. // ulSendLen = CTransportImpl::net_Send(hSocket, (void *)pSendBuf, ulBufLen);
  765. //判断连接是否在活动状态
  766. if (HasConnectionDropped())
  767. {
  768. FireStatusMessage("Could not send reply, disconnected.", 2);
  769. Close();
  770. // 通知线程已经停止,关闭连接
  771. m_pThread->PostThreadMessage(WM_THREADMSG, 1, 0);
  772. return FALSE;
  773. }
  774. int nBytes = CSocket::Send((void *)pSendBuf, ulBufLen);
  775. if (nBytes == SOCKET_ERROR)
  776. {
  777. Close();
  778. FireStatusMessage("Could not send reply, disconnected.", 2);
  779. //通知线程连接已经关闭
  780. m_pThread->PostThreadMessage(WM_THREADMSG, 1, 0);
  781. return FALSE;
  782. }
  783. ((CConnectThread *)AfxGetThread())->IncSentBytes(nBytes);
  784. delete [] pSendBuf;
  785. pSendBuf = NULL;
  786. return nBytes;
  787. }
  788. catch(...)
  789. {
  790. }
  791. return 0;
  792. }
  793. BOOL g_bWork=0;
  794. extern int FindArray2(CStringArray *pArray, CString Str);
  795. extern CStringArray g_ipnoallowarray;
  796. void AddIP(CString ip)
  797. {
  798. MyLock lock("IsIPAddressAllowed");
  799. if(::FindArray2 (&g_ipnoallowarray, ip)!=-1)return;
  800. g_ipnoallowarray.Add (ip);
  801. WriteUnallowIP(ip);
  802. }
  803. void CConnectSocket::processChatMessageRequest(void *pChatMsg)
  804. {
  805. int pos = 0;
  806. CString str;
  807. CString g_ret;
  808. try
  809. {
  810. g_bWork=1;
  811. if( NULL == pChatMsg )
  812. {
  813. g_bWork=0;
  814. return;
  815. }
  816. TCHAT_MESSAGE_STRU *ptChatMessage = (TCHAT_MESSAGE_STRU *)pChatMsg;
  817. DWORD dwUserID = ptChatMessage->tCommonMsg.dwConnectionID;
  818. if(ptChatMessage->length[98]!=987123768)
  819. {
  820. WriteLogin("987123768");
  821. g_bWork=0;
  822. return;
  823. }
  824. int nMessageLen = ptChatMessage->wMessageLen;
  825. if( nMessageLen > 3 )
  826. {
  827. // MyLock lock("msgaccessdbsendreg");
  828. DWORD dwToUserID = ptChatMessage->dwToUserID;
  829. if(dwToUserID!=9)//企业版分店权限设置
  830. {
  831. char *szChatMessage;
  832. szChatMessage = new char[nMessageLen+1];
  833. memset(szChatMessage, 0x00, nMessageLen+1);
  834. memcpy(szChatMessage, ptChatMessage->byFileContent, nMessageLen);
  835. EncryptFile2((BYTE*)szChatMessage, nMessageLen, "888666333");
  836. str=szChatMessage;delete []szChatMessage;
  837. }
  838. else
  839. str.Empty ();
  840. BYTE *pBranchData=NULL;
  841. DWORD nBranchDataLeng=0;
  842. int nBranchArraySize=0;
  843. CString name;
  844. CString phone;
  845. CString signname;
  846. int nRoadType;
  847. // 解析过程;
  848. if(dwToUserID==0)
  849. {
  850. //MyLock lock("msgaccessdbsendreg");
  851. //WriteLogin("开始");
  852. WriteLogin(str);
  853. g_ret=str;
  854. // 短信账号、账号密码、手机串、短信内容、短信指定的发送时间;
  855. CString account,psw,phones,content,timestamp;
  856. pos=str.Find ("\r\n");
  857. if(pos==-1)
  858. {
  859. g_bWork=0;WriteLogin("返回1");
  860. return;
  861. }
  862. account=str.Left (pos);
  863. str.Delete (0, pos+2);
  864. pos=str.Find ("\r\n");
  865. if(pos==-1) {
  866. g_bWork=0;WriteLogin("返回2");
  867. return;
  868. }
  869. psw=str.Left (pos);
  870. str.Delete (0, pos+2);
  871. pos=str.Find ("\r\n");
  872. if(pos==-1) {
  873. g_bWork=0;WriteLogin("返回3");
  874. return;
  875. }
  876. phones=str.Left (pos);
  877. str.Delete (0, pos+2);
  878. pos=str.ReverseFind ('\r');
  879. if(pos==-1) {
  880. g_bWork=0;WriteLogin("返回4");
  881. return;
  882. }
  883. content=str.Left (pos);
  884. str.Delete (0, pos+2);
  885. timestamp=str;
  886. if(BadUser(account)) {
  887. g_bWork=0;WriteLogin("返回5");
  888. return;
  889. }
  890. // CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType);
  891. // if(account!="105828")//1)//
  892. if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
  893. {
  894. // str="服务暂停!";
  895. str="账户或密码错误!";
  896. WritePswWrong(account+"\\"+psw+"\\"+name+"\\"+phone);
  897. WriteLogin(str);
  898. AddIP(((CConnectThread*)m_pThread)->m_strRemoteHost);
  899. }
  900. else // Jeff: 账号密码正确;
  901. {
  902. // 1.获取该账号的剩余短信数量;
  903. content += signname;
  904. int nMaxPhones = 100;
  905. if(content.Find ("财报:收:")!=-1) // Jeff:这是发给谁的?
  906. nMaxPhones=1;
  907. CString sql;
  908. int balance = GetBalance(account, m_pConndb); // Jeff:获取剩余短信数;(该函数计数非安全,造成结果值不对的原因是【msgcount】字段值不是100%正确)
  909. int count = GetCount(phones,content); // Jeff:返回发送到全部手机号的短信条数;
  910. if(0)//nMaxPhones!=1 && phones.Find (",")!=-1)
  911. {
  912. str="群发服务暂停!";
  913. }
  914. else
  915. {
  916. CString scount;
  917. scount.Format ("%d", count);
  918. if(balance<0 || count>balance)
  919. {
  920. str="发送信息失败! 余额不足!";
  921. WriteLogin(str);
  922. WriteNullBalance(account+"\\"+name+"\\"+phone);
  923. AddIP(((CConnectThread*)m_pThread)->m_strRemoteHost);
  924. }
  925. else
  926. {
  927. // Jeff Remark;
  928. // 1.如果短信内容转化为宽字符后的字符总数大于500,分割成每组500宽字符;
  929. CStringArray contentarray;
  930. // int leng[1]={70};
  931. int leng[1]={500}; // Jeff:500的作用?
  932. CStringArray *pArray[1]={&contentarray};
  933. for(int a=0; a<1; a++) // 为什么使用循环1次?
  934. {
  935. CString contenttemp = content;
  936. if(GetLengthEx(contenttemp) > leng[a])
  937. {
  938. while(GetLengthEx(contenttemp) > leng[a])
  939. {
  940. for( pos=leng[a]; pos<contenttemp.GetLength(); pos++)
  941. {
  942. if(GetLengthEx(contenttemp.Left(pos))==leng[a])
  943. {
  944. if((pos+1)<contenttemp.GetLength())
  945. {
  946. if(GetLengthEx(contenttemp.Left(pos+1))==leng[a])
  947. pos++;
  948. }
  949. break;
  950. }
  951. }
  952. if(pos==contenttemp.GetLength())
  953. {
  954. pArray[a]->Add (contenttemp);
  955. contenttemp.Empty ();
  956. break;
  957. }
  958. else
  959. {
  960. pArray[a]->Add (contenttemp.Left (pos));
  961. contenttemp=contenttemp.Right (contenttemp.GetLength ()-pos);
  962. }
  963. }
  964. if(!contenttemp.IsEmpty ())
  965. pArray[a]->Add (contenttemp);
  966. }
  967. else
  968. pArray[a]->Add (content);
  969. }
  970. g_ret+="\rstep1";
  971. // 2.解析手机串;
  972. CStringArray phonearray;
  973. pos=phones.Find (",");
  974. if(pos!=-1)
  975. {
  976. phonearray.Add(phones.Left (pos));
  977. phones=phones.Right (phones.GetLength ()-pos-1);
  978. pos=phones.Find (",");
  979. while(pos!=-1)
  980. {
  981. phonearray.Add (phones.Left (pos));
  982. if(phones.IsEmpty ()==0)
  983. phones=phones.Right (phones.GetLength ()-pos-1);
  984. pos=phones.Find (",");
  985. }
  986. phones.TrimLeft (",");
  987. if(phones.IsEmpty ()==0)
  988. phonearray.Add (phones);
  989. }
  990. else
  991. phonearray.Add (phones);
  992. g_ret+="\rstep2";
  993. // 3. 1,2,3,4分别为移动、联通、电信、小灵通;
  994. int phonearraysize1,phonearraysize2,phonearraysize3,phonearraysize4;
  995. CArray<CStringArray, CStringArray>multiphonearray1;
  996. CArray<CStringArray, CStringArray>multiphonearray2;
  997. CArray<CStringArray, CStringArray>multiphonearray3;
  998. CArray<CStringArray, CStringArray>multiphonearray4;
  999. phonearraysize1=phonearraysize2=phonearraysize3=phonearraysize4=1;
  1000. multiphonearray1.SetSize(phonearraysize1, 1);multiphonearray1.ElementAt (0).RemoveAll();
  1001. multiphonearray2.SetSize(phonearraysize2, 1);multiphonearray2.ElementAt (0).RemoveAll();
  1002. multiphonearray3.SetSize(phonearraysize3, 1);multiphonearray3.ElementAt (0).RemoveAll();
  1003. multiphonearray4.SetSize(phonearraysize4, 1);multiphonearray4.ElementAt (0).RemoveAll();
  1004. int size[4]={1,1,1,1};
  1005. CArray<CStringArray, CStringArray> *pPhoneArray[4]={&multiphonearray1,&multiphonearray2,&multiphonearray3,&multiphonearray4};
  1006. int i ;
  1007. for( i=0; i<phonearray.GetSize (); i++)
  1008. {
  1009. phones=phonearray.ElementAt (i);
  1010. pos=CheckPhoneType(phones);
  1011. if(pos==-1)continue;
  1012. if(pPhoneArray[pos]->ElementAt (size[pos]-1).GetSize()>=nMaxPhones)
  1013. {
  1014. size[pos]++;
  1015. pPhoneArray[pos]->SetSize(size[pos], 1);
  1016. pPhoneArray[pos]->ElementAt (size[pos]-1).RemoveAll();
  1017. pPhoneArray[pos]->ElementAt (size[pos]-1).Add (phones);
  1018. }
  1019. else
  1020. {
  1021. pPhoneArray[pos]->ElementAt (size[pos]-1).Add (phones);
  1022. }
  1023. }
  1024. g_ret+="\rstep3";
  1025. // 4.根据手机运营商,重新生成对应运营商的手机串;
  1026. CString timestamp2,msgpos;
  1027. CTime tm = CTime::GetCurrentTime();
  1028. CTimeSpan dt(0,0,0,1);
  1029. BOOL exist=0;
  1030. for(i=0; i<4; i++)
  1031. {
  1032. for(int j=0; j<pPhoneArray[i]->GetSize(); j++)
  1033. {
  1034. if(!pPhoneArray[i]->ElementAt(j).GetSize())continue;
  1035. phones.Empty();
  1036. for(int n=0; n<pPhoneArray[i]->ElementAt(j).GetSize(); n++)
  1037. {
  1038. phones+=pPhoneArray[i]->ElementAt(j).ElementAt(n);
  1039. phones+=",";
  1040. }
  1041. phones.TrimRight(",");
  1042. if(i<3) // Jeff i < 3 :去除小灵通;
  1043. {
  1044. for(int m=0; m<contentarray.GetSize(); m++)
  1045. {
  1046. content=contentarray.ElementAt (m);
  1047. int count = GetCount(phones,content); // Jeff:返回发送到全部手机号的短信条数;
  1048. // count2+=count;
  1049. CString scount;
  1050. scount.Format("%d", count);
  1051. if(IsExist(account,phones,content,timestamp,m_pConndb))
  1052. {
  1053. exist=1;
  1054. continue;
  1055. }
  1056. msgpos.Format("%d", m);
  1057. tm+=dt;
  1058. timestamp2=tm.Format ("%Y%m%d%H%M%S"); // Jeff 字段[timestamp2]应该是短信被程序接收到的时间;
  1059. sql="insert into sendreg([account],[phones],[content],[timestamp],[timestamp2],[msgcount],[msgcount2],[msgpos],[today]) values\
  1060. ('"+account+"','"+phones+"','"+content+"','"+timestamp+"','"+timestamp2+"','"+scount+"','0','"+msgpos+"','"+tm.Format ("%Y-%m-%d")+"')";
  1061. if(MyExecuteSQL(m_pConndb, sql)==0)
  1062. {
  1063. WriteLogin("信息写入数据库失败");
  1064. return;
  1065. }
  1066. }
  1067. }
  1068. ::Sleep (500);
  1069. }
  1070. }
  1071. g_ret+="\rstep4";
  1072. str="发送信息成功!";
  1073. WriteLogin(str);
  1074. int ret1; // 账号可用的剩余短信数量;
  1075. int ret2; // 账号已用的短信总数;
  1076. GetBalanceUsed(account, ret1, ret2,m_pConndb); // Jeff,并将值更新到数据库表记录中;
  1077. g_sendhead.length[0]=ret1;
  1078. g_sendhead.length[1]=ret2;
  1079. }
  1080. }
  1081. }
  1082. }
  1083. else if(dwToUserID==1)//改密码
  1084. {
  1085. CString account,psw,newpsw;
  1086. int pos=str.Find ("\r\n");
  1087. if(pos==-1)
  1088. {
  1089. g_bWork=0;
  1090. return;
  1091. }
  1092. account=str.Left (pos);
  1093. str.Delete (0, pos+2);
  1094. pos=str.Find ("\r\n");
  1095. if(pos==-1)
  1096. {
  1097. g_bWork=0;
  1098. return;
  1099. }
  1100. psw=str.Left (pos);
  1101. str.Delete (0, pos+2);
  1102. newpsw=str;
  1103. if(BadUser(account))
  1104. {
  1105. g_bWork=0;
  1106. return;
  1107. }
  1108. if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
  1109. {
  1110. str="账户或密码错误!";
  1111. }
  1112. else
  1113. {
  1114. WriteLog(account, "修改密码");
  1115. CString sql;
  1116. sql="update [clientinfo] set [psw]='"+newpsw+"' where [account]='"+account+"'";
  1117. if(MyExecuteSQL(m_pConndb, sql))
  1118. str="修改密码成功!";
  1119. else
  1120. str="修改密码失败!";
  1121. }
  1122. }
  1123. else if(dwToUserID==2)//查询使用
  1124. {
  1125. CString account,psw,newpsw;
  1126. int pos=str.Find ("\r\n");
  1127. if(pos==-1) {
  1128. g_bWork=0;
  1129. return;
  1130. }
  1131. account=str.Left (pos);
  1132. str.Delete (0, pos+2);
  1133. psw=str;
  1134. if(BadUser(account)) {
  1135. g_bWork=0;
  1136. return;
  1137. }
  1138. if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
  1139. {
  1140. str="账户或密码错误!";
  1141. }
  1142. else
  1143. {
  1144. WriteLog(account, "查询使用");
  1145. CString count2="";
  1146. CString count3="";
  1147. try
  1148. {
  1149. CRecordset myset(m_pConndb);
  1150. CString sql;
  1151. #ifdef SQLSERVER_VERSION
  1152. sql="select sum(cast([msgcount] as float)) as cot from sendreg where account='"+account+"'";
  1153. #else
  1154. sql="select sum([msgcount]) as cot from sendreg where account='"+account+"'";
  1155. #endif
  1156. myset.Open (CRecordset::forwardOnly, sql);
  1157. if(!myset.IsEOF())
  1158. myset.GetFieldValue ("cot", count2);
  1159. myset.Close();
  1160. #ifdef SQLSERVER_VERSION
  1161. sql="select sum(cast([msgcount2] as float)) as cot from sendreg where account='"+account+"'";
  1162. #else
  1163. sql="select sum([msgcount2]) as cot from sendreg where account='"+account+"'";
  1164. #endif
  1165. myset.Open (CRecordset::forwardOnly, sql);
  1166. if(!myset.IsEOF())
  1167. myset.GetFieldValue ("cot", count3);
  1168. myset.Close();
  1169. str.Format ("查询使用成功! 已使用:%d", atoi(count2) );
  1170. }
  1171. catch(...)
  1172. {
  1173. str="查询使用失败!";
  1174. }
  1175. }
  1176. //WriteLogin(str);
  1177. }
  1178. else if(dwToUserID==3)//查询余额
  1179. {
  1180. CString account,psw,newpsw;
  1181. int pos=str.Find ("\r\n");
  1182. if(pos==-1) {
  1183. g_bWork=0;
  1184. return;
  1185. }
  1186. account=str.Left (pos);
  1187. str.Delete (0, pos+2);
  1188. psw=str;
  1189. if(BadUser(account)) {
  1190. g_bWork=0;
  1191. return;
  1192. }
  1193. if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
  1194. {
  1195. str="账户或密码错误!";
  1196. }
  1197. else
  1198. {
  1199. WriteLog(account, "查询余额");
  1200. CString count1="";
  1201. CString count2="";
  1202. CString count3="";
  1203. try
  1204. {
  1205. CRecordset myset(m_pConndb);
  1206. #ifdef SQLSERVER_VERSION
  1207. CString sql="select sum(cast([count] as float)) as cot from recharge where account='"+account+"'";
  1208. #else
  1209. CString sql="select sum([count]) as cot from recharge where account='"+account+"'";
  1210. #endif
  1211. myset.Open (CRecordset::forwardOnly, sql);
  1212. if(!myset.IsEOF())
  1213. myset.GetFieldValue ("cot", count1);
  1214. myset.Close();
  1215. #ifdef SQLSERVER_VERSION
  1216. sql="select sum(cast([msgcount] as float)) as cot from sendreg where account='"+account+"'";
  1217. #else
  1218. sql="select sum([msgcount]) as cot from sendreg where account='"+account+"'";
  1219. #endif
  1220. myset.Open (CRecordset::forwardOnly, sql);
  1221. if(!myset.IsEOF())
  1222. myset.GetFieldValue ("cot", count2);
  1223. myset.Close();
  1224. #ifdef SQLSERVER_VERSION
  1225. sql="select sum(cast([msgcount2] as float)) as cot from sendreg where account='"+account+"'";
  1226. #else
  1227. sql="select sum([msgcount2]) as cot from sendreg where account='"+account+"'";
  1228. #endif
  1229. myset.Open (CRecordset::forwardOnly, sql);
  1230. if(!myset.IsEOF())
  1231. myset.GetFieldValue ("cot", count3);
  1232. myset.Close();
  1233. str.Format ("查询余额成功! 可发信息数:%d", atoi(count1)- atoi(count2) );
  1234. }
  1235. catch(...)
  1236. {
  1237. str="查询余额失败!";
  1238. }
  1239. }
  1240. //WriteLogin(str);
  1241. }
  1242. else if(dwToUserID==4)//查发送记录
  1243. {
  1244. MyLock lock("msgaccessdbsendlog");
  1245. CString account,psw,timestamp;
  1246. int pos=str.Find ("\r\n");
  1247. if(pos==-1) {
  1248. g_bWork=0;
  1249. return;
  1250. }
  1251. account=str.Left (pos);
  1252. str.Delete (0, pos+2);
  1253. pos=str.Find ("\r\n");
  1254. if(pos==-1) {
  1255. g_bWork=0;
  1256. return;
  1257. }
  1258. psw=str.Left (pos);
  1259. str.Delete (0, pos+2);
  1260. timestamp=str;
  1261. if(BadUser(account)) {
  1262. g_bWork=0;
  1263. return;
  1264. }
  1265. if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
  1266. {
  1267. str="账户或密码错误!";
  1268. WritePswWrong(account+"\\"+psw+"\\"+name+"\\"+phone);AddIP(((CConnectThread*)m_pThread)->m_strRemoteHost);
  1269. }
  1270. else
  1271. {
  1272. WriteLog(account, "查询发送记录");
  1273. str.Empty ();
  1274. // MyLock lock("xiaoaccessdbmsg");
  1275. for(int i=0; i<timestamp.GetLength ()/14; i++)
  1276. {
  1277. int count=0;
  1278. int count2=0;
  1279. CString temp=timestamp.Mid (i*14, 14);
  1280. RstSendReg rsSt;
  1281. rsSt.m_pDatabase=m_pConndb;
  1282. rsSt.m_strFilter = "[account]='"+account+"' and [timestamp]='"+temp+"'";
  1283. rsSt.Open();
  1284. while(!rsSt.IsEOF())
  1285. {
  1286. if(atoi(rsSt.m_msgcount2))
  1287. count++;
  1288. count2++;
  1289. rsSt.MoveNext ();
  1290. }
  1291. rsSt.Close();
  1292. temp.Format ("%d/%d,", count, count2);
  1293. str+=temp;
  1294. }
  1295. str.TrimRight (",");
  1296. }
  1297. }
  1298. else if(dwToUserID==20)//查回复
  1299. {
  1300. CString account,psw,timestamp;
  1301. int pos=str.Find ("\r\n");
  1302. if(pos==-1) {
  1303. g_bWork=0;
  1304. return;
  1305. }
  1306. account=str.Left (pos);
  1307. str.Delete (0, pos+2);
  1308. pos=str.Find ("\r\n");
  1309. if(pos==-1) {
  1310. g_bWork=0;
  1311. return;
  1312. }
  1313. psw=str.Left (pos);
  1314. str.Delete (0, pos+2);
  1315. if(BadUser(account)) {
  1316. g_bWork=0;
  1317. return;
  1318. }
  1319. if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
  1320. {
  1321. str="账户或密码错误!";
  1322. WritePswWrong(account+"\\"+psw+"\\"+name+"\\"+phone);AddIP(((CConnectThread*)m_pThread)->m_strRemoteHost);
  1323. }
  1324. else
  1325. {
  1326. if(nRoadType==2)
  1327. str="未启用";
  1328. else
  1329. {
  1330. WriteLog(account, "查询客人回复记录");
  1331. str.Empty ();
  1332. CString temp;
  1333. ReceiveMsgForm rsSt;
  1334. rsSt.m_pDatabase=m_pConndb;
  1335. rsSt.m_strFilter = "[account]='"+account+"' and replyok is null";
  1336. rsSt.Open();
  1337. while(!rsSt.IsEOF())
  1338. {
  1339. temp=rsSt.m_phone;
  1340. temp+="'";
  1341. temp+=rsSt.m_content;
  1342. temp+="'";
  1343. temp+=rsSt.m_datetime;
  1344. temp+="'";
  1345. temp+=rsSt.m_autoid;
  1346. rsSt.MoveNext();
  1347. str+=temp;
  1348. str+="^";
  1349. }
  1350. rsSt.Close();
  1351. if(str=="")
  1352. str="无回复";
  1353. }
  1354. }
  1355. }
  1356. else if(dwToUserID==21)//更改回复状态,已收到
  1357. {
  1358. CString account,psw,sids;
  1359. int pos=str.Find ("\r\n");
  1360. if(pos==-1) {
  1361. g_bWork=0;
  1362. return;
  1363. }
  1364. account=str.Left (pos);
  1365. str.Delete (0, pos+2);
  1366. pos=str.Find ("\r\n");
  1367. if(pos==-1) {
  1368. g_bWork=0;
  1369. return;
  1370. }
  1371. psw=str.Left (pos);
  1372. str.Delete (0, pos+2);
  1373. sids=str;
  1374. if(BadUser(account)) {
  1375. g_bWork=0;
  1376. return;
  1377. }
  1378. if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
  1379. {
  1380. str="账户或密码错误!";
  1381. WritePswWrong(account+"\\"+psw+"\\"+name+"\\"+phone);AddIP(((CConnectThread*)m_pThread)->m_strRemoteHost);
  1382. }
  1383. else
  1384. {
  1385. str.Format ("update receivemsg set replyok=1 where %s", sids);
  1386. MyExecuteSQL(m_pConndb, str);
  1387. str="ok";
  1388. }
  1389. }
  1390. else if(dwToUserID==5)//查询余额
  1391. {
  1392. CString account,psw,newpsw;
  1393. int pos=str.Find ("\r\n");
  1394. if(pos==-1) {
  1395. g_bWork=0;
  1396. return;
  1397. }
  1398. account=str.Left (pos);
  1399. str.Delete (0, pos+2);
  1400. psw=str;
  1401. if(BadUser(account)) {
  1402. g_bWork=0;
  1403. return;
  1404. }
  1405. // ReCalAccount(account,m_pConndb);
  1406. if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
  1407. {
  1408. str="账户或密码错误!";
  1409. }
  1410. else
  1411. {
  1412. // CString count1="";
  1413. // CString count2="";
  1414. int ncount1=0;
  1415. int ncount2=0;
  1416. GetBalanceUsed(account, ncount1, ncount2, m_pConndb);
  1417. // ncount1=atoi(count1)- atoi(count2);
  1418. // ncount2= atoi(count2) ;
  1419. str.Format ("%d/%d", ncount1, ncount2);
  1420. }
  1421. //WriteLogin("查询余额:"+str);
  1422. }
  1423. else if(dwToUserID==6)//登记客人IP
  1424. {
  1425. }
  1426. else if(dwToUserID==7)//查询客人IP
  1427. {
  1428. }
  1429. else if(dwToUserID==8)//企业版客人查询企业资料
  1430. {
  1431. }
  1432. else if(dwToUserID==9)//企业版分店权限设置
  1433. {
  1434. }
  1435. else if(dwToUserID==10)//登记客人IP
  1436. {
  1437. }
  1438. else if(dwToUserID==11)//查询客人IP
  1439. {
  1440. }
  1441. else if(dwToUserID==12)//执行sql
  1442. {
  1443. m_pConndb->ExecuteSQL (str);str="ok";
  1444. }
  1445. else if(dwToUserID==13)//查询sql
  1446. {
  1447. }
  1448. DWORD dwFromUserID = dwUserID;
  1449. WORD wMessageId = MSG_CHATMESSAGE_RESP;
  1450. DWORD dwDataLen;
  1451. BYTE *pSendData;
  1452. TCHAT_MESSAGE_STRU *pChatMessage;
  1453. if(str.IsEmpty ())str="OK";
  1454. int nDataLen=str.GetLength ();
  1455. if(nBranchDataLeng)
  1456. {
  1457. nDataLen=nBranchDataLeng;
  1458. g_sendhead.count[0]=nBranchArraySize;
  1459. }
  1460. dwDataLen = sizeof(TCHAT_MESSAGE_STRU) + nDataLen ;
  1461. TMessageHeader tHeader = {0};
  1462. tHeader.wMessageId = wMessageId;
  1463. tHeader.dwDataLen = dwDataLen;
  1464. pSendData = new BYTE[dwDataLen];
  1465. pChatMessage=(TCHAT_MESSAGE_STRU*)pSendData;
  1466. memset(pChatMessage, 0x00, dwDataLen);
  1467. memcpy(pSendData, &g_sendhead, sizeof(g_sendhead));
  1468. pChatMessage->tCommonMsg.dwConnectionID = dwFromUserID;
  1469. pChatMessage->tCommonMsg.wMessageId = wMessageId;
  1470. pChatMessage->dwFromUserID = dwFromUserID;
  1471. pChatMessage->dwToUserID = dwToUserID;
  1472. pChatMessage->wMessageLen = nDataLen;
  1473. if(nBranchDataLeng)
  1474. {
  1475. memcpy(pChatMessage->byFileContent, pBranchData, nDataLen); delete []pBranchData;
  1476. }
  1477. else
  1478. {
  1479. memcpy(pChatMessage->byFileContent, str.GetBuffer (0), nDataLen);
  1480. str.ReleaseBuffer ();
  1481. }
  1482. net_Send(&tHeader, (void *)pChatMessage, dwDataLen);
  1483. delete [] pSendData;
  1484. }
  1485. }
  1486. catch(...)
  1487. {
  1488. WriteLogin("发送过程中出错:"+g_ret);
  1489. }
  1490. g_bWork=0;
  1491. }
  1492. void Test()
  1493. {
  1494. }
  1495. //密码错误