Client.cpp
上传用户:guangzhiyw
上传日期:2007-01-09
资源大小:495k
文件大小:11k
源码类别:

ICQ/即时通讯

开发平台:

Visual C++

  1. // Client.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "Client.h"
  5. #include "MainFrm.h"
  6. #include "LoginDlg.h"
  7. #include "registerdlg.h"
  8. #include "SetupServerDlg.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CClientApp
  16. BEGIN_MESSAGE_MAP(CClientApp, CWinApp)
  17. //{{AFX_MSG_MAP(CClientApp)
  18. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  19. ON_COMMAND(ID_SYS_SETUP, OnSysSetup)
  20. ON_COMMAND(ID_SEND_FILE, OnSendFile)
  21. //}}AFX_MSG_MAP
  22. END_MESSAGE_MAP()
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CClientApp construction
  25. CClientApp::CClientApp()
  26. {
  27. // TODO: add construction code here,
  28. // Place all significant initialization in InitInstance
  29. m_uCurrentUserID=0;
  30. m_bOnlineState=0;
  31. m_nCurSysMsg=0;
  32. m_nCurNotice=0;
  33. m_nCurOther=0;
  34. m_nBitmapNum=0;
  35. m_nNumberHeadBmp=0;
  36. m_nTotalOnline=0;
  37. }
  38. /////////////////////////////////////////////////////////////////////////////
  39. // The one and only CClientApp object
  40. CClientApp theApp;
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CClientApp initialization
  43. BOOL CClientApp::InitInstance()
  44. {
  45. if (!AfxSocketInit())
  46. {
  47. AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
  48. return FALSE;
  49. }
  50. // Standard initialization
  51. // If you are not using these features and wish to reduce the size
  52. //  of your final executable, you should remove from the following
  53. //  the specific initialization routines you do not need.
  54. #ifdef _AFXDLL
  55. Enable3dControls(); // Call this when using MFC in a shared DLL
  56. #else
  57. Enable3dControlsStatic(); // Call this when linking to MFC statically
  58. #endif
  59. // Change the registry key under which our settings are stored.
  60. // TODO: You should modify this string to be something appropriate
  61. // such as the name of your company or organization.
  62. SetRegistryKey(_T("My Program"));
  63. if(!m_Socket.Create())
  64. {
  65. AfxMessageBox(IDS_CREATE_SOCKET_ERROR);
  66. return FALSE;
  67. }
  68. if(!LoadFaceBmp())
  69. {
  70. AfxMessageBox(IDS_FACE_FILE_LOAD_FAILE);
  71. return FALSE;
  72. }
  73. CArray<int,int>AllUserID;
  74. int LoginType=ONLINE;
  75. BOOL bApplyID=FALSE;
  76. if(!LoadLocalAllUserID(AllUserID))
  77. Regiser_begin:
  78. {
  79. CRegisterDlg dlg;
  80. m_Socket.SetOwner(&dlg);
  81. CStdioFile file1;
  82. if(file1.Open("Server.ini",CFile::modeRead))
  83. {
  84. CString str1,str2,str3,str4;
  85. file1.ReadString(str1);
  86. file1.ReadString(str2);
  87. file1.Close();
  88. m_strServerIP=str1.Mid(3);
  89.     m_uServerPort=atoi(str2.GetBuffer(50)+5);
  90. }
  91. else
  92. {
  93. m_strServerIP=DEFAULT_SERVER_IP;
  94. m_uServerPort=DEFAULT_SERVER_PORT;
  95. }
  96. //m_strServerIP="192.168.0.208";
  97. //m_uServerPort=4000;
  98. dlg.m_strServerIP=m_strServerIP;
  99. dlg.m_uServerPort=m_uServerPort;
  100. if(dlg.DoModal()==IDCANCEL)
  101. {
  102. m_Socket.Close();
  103. return FALSE;
  104. }
  105. m_uCurrentUserID=dlg.m_uUserID;
  106. m_strCurUserDir.Format("%d\",m_uCurrentUserID);
  107. m_strCurIDpwd=dlg.m_strPassword;
  108. m_strServerIP=dlg.m_strServerIP;
  109. m_uServerIP=dlg.m_uServerIP;
  110. m_uServerPort=dlg.m_uServerPort;
  111. SaveCurIDToFile();
  112. ::CreateDirectory(m_strCurUserDir,NULL);
  113. CFile f1(m_strCurUserDir+USERSETUP,CFile::modeCreate|CFile::modeWrite);
  114. f1.Write((char*)&m_uServerIP,sizeof(DWORD));
  115. f1.Write((char*)&m_uServerPort,sizeof(DWORD));
  116. f1.Close();
  117. if(dlg.m_bApplyID)
  118. {
  119. bApplyID=TRUE;
  120. }
  121. }
  122. else
  123. {
  124. CLoginDlg dlg;
  125. dlg.SetUserID(AllUserID);
  126. m_Socket.SetOwner(&dlg);
  127. if(dlg.DoModal()==IDCANCEL)
  128. {
  129. m_Socket.Close();
  130. return FALSE;
  131. }
  132. if(dlg.lresult==1)
  133. goto Regiser_begin;
  134. if(dlg.m_LoginType)LoginType=ONHIDE;
  135. else LoginType=ONLINE;
  136. m_uCurrentUserID=dlg.uCurID;
  137. m_strCurIDpwd=dlg.pwd;
  138. m_strCurUserDir.Format("%d\",m_uCurrentUserID);
  139. m_uServerIP=dlg.uServerIP;
  140. m_uServerPort=dlg.uPort;
  141. LoadFriendInfo();
  142. }
  143. in_addr tIP;
  144. tIP.S_un.S_addr=m_uServerIP;
  145. m_strServerIP=inet_ntoa(tIP);
  146. CMainFrame* pFrame = new CMainFrame;
  147. m_pMainWnd = pFrame;
  148. pFrame->LoadFrame(IDR_MAINFRAME,
  149. WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
  150. NULL);
  151. m_Socket.SetOwner(pFrame);
  152. pFrame->ShowWindow(SW_SHOW);
  153. pFrame->UpdateWindow();
  154. if(bApplyID)
  155. {
  156. if(!m_bOnlineState)
  157. pFrame->SetTimer(1,TimerSpanClient,NULL);
  158. m_bOnlineState=1;
  159. CString str=m_strCurUserDir;
  160. str=str.Left(str.GetLength()-1);
  161. pFrame->m_TrayIcon.SetIcon(IDR_ONLINE,str);
  162. }
  163. else
  164. {
  165. CData msg;
  166. msg.index=LoginType;
  167. msg.MyId=m_uCurrentUserID;
  168. msg.tarIP=m_uServerIP;
  169. msg.nPort=m_uServerPort;
  170. m_Socket.SendDataDirect(&msg);
  171. }
  172. return TRUE;
  173. }
  174. /////////////////////////////////////////////////////////////////////////////
  175. // CClientApp message handlers
  176. BOOL CClientApp::LoadLocalAllUserID(CArray<int,int>&aID)
  177. {
  178. CFile file;
  179. if(!file.Open(ALLUSERIDFILE,CFile::modeRead))
  180. return FALSE;
  181. int num=0,data;
  182. file.Read(&num,sizeof(int));
  183. for(int i=0;i<num;i++)
  184. {
  185. file.Read(&data,sizeof(int));
  186. aID.Add(data);
  187. }
  188. file.Close();
  189. return TRUE;
  190. }
  191. BOOL CClientApp::LoadFaceBmp()
  192. {
  193. FILE* file;
  194. int num=0;
  195. file=fopen(FACEINIFILE,"r");
  196. if(!file)return FALSE;
  197. fscanf(file,"%d",&num);
  198. CArray<CString,CString>aFilename;
  199. char temp[50];
  200. for(int i=0;i<num;i++)
  201. {
  202. fscanf(file,"%s",temp);
  203. aFilename.Add(temp);
  204. }
  205. fclose(file);
  206. m_imaLarge.Create(32,32,ILC_COLOR16,num+1,70);
  207. m_imaSmall.Create(16,16,ILC_COLOR16,num+1,70);
  208. m_imaHead.Create(32,32,ILC_COLOR16,num/2+1,70);
  209. m_imaSmallHead.Create(16,16,ILC_COLOR16,num/2+1,70);
  210. m_nBitmapNum=num;
  211. m_nNumberHeadBmp=num/2;
  212. m_paBitmap=new CBitmap*[num];
  213. m_paSmallBitmap=new CBitmap*[num];
  214. memset(m_paBitmap,0,sizeof(CBitmap*)*num);
  215. memset(m_paSmallBitmap,0,sizeof(CBitmap*)*num);
  216. HBITMAP bitmap,bit;
  217. for(i=0;i<num;i++)
  218. {
  219. CString str="face\"+aFilename.GetAt(i);
  220. bitmap=(HBITMAP)LoadImage(AfxGetInstanceHandle(),str,
  221. IMAGE_BITMAP,0,0,
  222. LR_LOADFROMFILE|LR_CREATEDIBSECTION);
  223. m_paBitmap[i]=new CBitmap;
  224. m_paSmallBitmap[i]=new CBitmap;
  225. m_paBitmap[i]->Attach(bitmap);
  226. bit=(HBITMAP)::CopyImage(bitmap,IMAGE_BITMAP,16,16,LR_CREATEDIBSECTION);
  227. m_paSmallBitmap[i]->Attach(bit);
  228. m_imaLarge.Add(m_paBitmap[i],crColorMask);
  229. m_imaSmall.Add(m_paSmallBitmap[i],crColorMask);
  230. if(i%2==0)
  231. {
  232. m_imaHead.Add(m_paBitmap[i],crColorMask);
  233. m_imaSmallHead.Add(m_paSmallBitmap[i],crColorMask);
  234. }
  235. }
  236. aFilename.RemoveAll();
  237. return TRUE;
  238. }
  239. BOOL CClientApp::LoadFriendInfo()
  240. {
  241. CFile file;
  242. int bufoffset=UserFileReservedLength+MaxPasswordLength+3;
  243. int countoffset=UserFileReservedLength+1;
  244. if(!file.Open(m_strCurUserDir+USERFILE,CFile::modeRead))
  245. {
  246. return FALSE;
  247. }
  248. WORD uCount=0;
  249. file.Seek(countoffset,CFile::begin);
  250. file.Read(&uCount,sizeof(WORD));
  251. file.Seek(bufoffset,CFile::begin);
  252. for(int i=0;i<uCount;i++)
  253. {
  254. UserInfo * pInfo=new UserInfo;
  255. file.Read(pInfo,sizeof(UserInfo));
  256. m_aUserInfo.Add(pInfo);
  257. FriendState* pState=new FriendState;
  258. m_aUserState.Add(pState);
  259. }
  260. file.Close();
  261. return TRUE;
  262. }
  263. BOOL CClientApp::SaveUserInfo()
  264. {
  265. CFile file;
  266. if(!file.Open(m_strCurUserDir+USERFILE,CFile::modeCreate|CFile::modeWrite))
  267. {
  268. // AfxMessageBox(IDS_CREATE_USERFILE_FAIL);
  269. return FALSE;
  270. }
  271. char * pRand=new char[UserFileReservedLength];
  272. file.Write(pRand,UserFileReservedLength);
  273. UCHAR ch;
  274. ch=m_strCurIDpwd.GetLength();
  275. //  jia mi this pwd
  276. char* pPassword=new char[MaxPasswordLength+3];
  277. pPassword[0]=ch;
  278. int nFriendCount=m_aUserInfo.GetSize();
  279. StrNCopy(pPassword+1,(char*)&nFriendCount,2);
  280. StrNCopy(pPassword+3,m_strCurIDpwd.GetBuffer(ch),ch);
  281. file.Write(pPassword,MaxPasswordLength+3);
  282. for(int i=0;i<m_aUserInfo.GetSize();i++)
  283. file.Write(m_aUserInfo.GetAt(i),sizeof(UserInfo));
  284. file.Close();
  285. delete pRand;
  286. delete pPassword;
  287. return TRUE;
  288. }
  289. void CClientApp::SaveCurIDToFile()
  290. {
  291. CFile file;
  292. int num=0;
  293. if(!file.Open(ALLUSERIDFILE,CFile::modeReadWrite))
  294. {
  295. file.Open(ALLUSERIDFILE,CFile::modeCreate|CFile::modeWrite);
  296. }
  297. else
  298. {
  299. file.Read(&num,sizeof(int));
  300. }
  301. num++;
  302. file.Seek(0,CFile::begin);
  303. file.Write(&num,sizeof(int));
  304. file.Seek(0,CFile::end);
  305. file.Write(&m_uCurrentUserID,sizeof(int));
  306. file.Close();
  307. }
  308. int CClientApp::ExitInstance() 
  309. {
  310. if(m_bOnlineState)
  311. {
  312. CData msg;
  313. msg.index=OFFLINE;
  314. msg.MyId=m_uCurrentUserID;
  315. msg.tarIP=m_uServerIP;
  316. msg.nPort=m_uServerPort;
  317. m_Socket.SendDataDirect(&msg);
  318. }
  319. //---------- save user info --------------
  320. if(m_uCurrentUserID!=0)
  321. SaveUserInfo();
  322. //----- destroy -------
  323. for(int i=0;i<m_aUserInfo.GetSize();i++)
  324. {
  325. delete m_aUserInfo.GetAt(i);
  326. FriendState *pState=m_aUserState.GetAt(i);
  327. delete pState;
  328. }
  329. for(i=0;i<m_aStranger.GetSize();i++)
  330. {
  331. delete m_aStranger.GetAt(i);
  332. FriendState * pState=m_aStrangerState.GetAt(i);
  333. delete pState;
  334. }
  335. for(i=0;i<m_nBitmapNum;i++)
  336. {
  337. if(m_paBitmap[i]!=NULL)delete m_paBitmap[i];
  338. if(m_paSmallBitmap[i]!=NULL)delete m_paSmallBitmap[i];
  339. }
  340. delete[m_nBitmapNum]m_paBitmap;
  341. delete[m_nBitmapNum]m_paSmallBitmap;
  342. m_imaLarge.DeleteImageList();
  343. m_imaSmall.DeleteImageList();
  344. m_imaHead.DeleteImageList();
  345. m_imaSmallHead.DeleteImageList();
  346. return CWinApp::ExitInstance();
  347. }
  348. /////////////////////////////////////////////////////////////////////////////
  349. // CAboutDlg dialog used for App About
  350. class CAboutDlg : public CDialog
  351. {
  352. public:
  353. CAboutDlg();
  354. // Dialog Data
  355. //{{AFX_DATA(CAboutDlg)
  356. enum { IDD = IDD_ABOUTBOX };
  357. //}}AFX_DATA
  358. // ClassWizard generated virtual function overrides
  359. //{{AFX_VIRTUAL(CAboutDlg)
  360. protected:
  361. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  362. //}}AFX_VIRTUAL
  363. // Implementation
  364. protected:
  365. //{{AFX_MSG(CAboutDlg)
  366. // No message handlers
  367. //}}AFX_MSG
  368. DECLARE_MESSAGE_MAP()
  369. };
  370. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  371. {
  372. //{{AFX_DATA_INIT(CAboutDlg)
  373. //}}AFX_DATA_INIT
  374. }
  375. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  376. {
  377. CDialog::DoDataExchange(pDX);
  378. //{{AFX_DATA_MAP(CAboutDlg)
  379. //}}AFX_DATA_MAP
  380. }
  381. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  382. //{{AFX_MSG_MAP(CAboutDlg)
  383. // No message handlers
  384. //}}AFX_MSG_MAP
  385. END_MESSAGE_MAP()
  386. // App command to run the dialog
  387. void CClientApp::OnAppAbout()
  388. {
  389. CAboutDlg aboutDlg;
  390. aboutDlg.DoModal();
  391. }
  392. void CClientApp::OnSysSetup() 
  393. {
  394. static CString strServerIP=m_strServerIP;
  395. static DWORD uServerPort=m_uServerPort;
  396. CSetupServerDlg dlg;
  397. dlg.m_port=uServerPort;
  398. dlg.m_strServerIP=strServerIP;
  399. if(dlg.DoModal()==IDOK)
  400. {
  401. if(!(dlg.m_strServerIP==strServerIP&&dlg.uPort==uServerPort))
  402. {
  403. strServerIP=dlg.m_strServerIP;
  404. uServerPort=dlg.uPort;
  405. CFile file;
  406. if(file.Open(m_strCurUserDir+USERSETUP,CFile::modeCreate|CFile::modeWrite))
  407. {
  408. file.Write((char*)&dlg.uServerIP,sizeof(DWORD));
  409. file.Write((char*)&dlg.uPort,sizeof(DWORD));
  410. file.Close();
  411. AfxMessageBox(IDS_SETUP_NOTICE);
  412. }
  413. }
  414. }
  415. }
  416. void CClientApp::OnSendFile() 
  417. {
  418. AfxMessageBox(IDS_NOT_FINISH);
  419. }