PLAY.CPP
上传用户:tuheem
上传日期:2007-05-01
资源大小:21889k
文件大小:12k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. // play.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "play.h"
  5. #include "playDoc.h"
  6. #include "playView.h"
  7. #include <dos.h>
  8. #include <direct.h>
  9. #include "SetPwd.h"
  10. #include "InputPwd.h"
  11. #include "ModifyPwd.h"
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. #define LISTEN_PORT 1500
  18. #define WSA_ACCPET WM_USER+300;
  19. BOOL fail;
  20. HKEY PasswdKey;
  21. char dw_Passwd[100];
  22. BOOL m_Modify;
  23. SOCKET Lsock;          //监听套接字
  24. SOCKADDR_IN  DestAddr; //发送地址
  25. SOCKET MultiSock ;     //组播套接字
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CPlayApp
  28. BEGIN_MESSAGE_MAP(CPlayApp, CWinApp)
  29. //{{AFX_MSG_MAP(CPlayApp)
  30. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  31. // NOTE - the ClassWizard will add and remove mapping macros here.
  32. //    DO NOT EDIT what you see in these blocks of generated code!
  33. //}}AFX_MSG_MAP
  34. // Standard file based document commands
  35. ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  36. ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  37. END_MESSAGE_MAP()
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CPlayApp construction
  40. UINT CPlayApp::m_nImages[12] =
  41. {
  42. IDI_ICON1,
  43. IDI_ICON2,
  44. IDI_ICON3,
  45. IDI_ICON4,
  46. IDI_ICON5,
  47. IDI_ICON6,
  48. IDI_ICON7,
  49. IDI_ICON8,
  50. IDI_ICON9,
  51. IDI_ICON10,
  52. IDI_ICON11,
  53. IDI_ICON12,
  54. };
  55. CPlayApp::CPlayApp()
  56. {
  57. // TODO: add construction code here,
  58. // Place all significant initialization in InitInstance
  59. }
  60. /////////////////////////////////////////////////////////////////////////////
  61. // The one and only CPlayApp object
  62. CPlayApp theApp;
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CPlayApp initialization
  65. BOOL CPlayApp::InitInstance()
  66. {
  67. AfxEnableControlContainer();
  68. // Standard initialization
  69. // If you are not using these features and wish to reduce the size
  70. //  of your final executable, you should remove from the following
  71. //  the specific initialization routines you do not need.
  72. #ifdef _AFXDLL
  73. Enable3dControls(); // Call this when using MFC in a shared DLL
  74. #else
  75. Enable3dControlsStatic(); // Call this when linking to MFC statically
  76. #endif
  77. // Change the registry key under which our settings are stored.
  78. // TODO: You should modify this string to be something appropriate
  79. // such as the name of your company or organization.
  80. /* SetRegistryKey(_T("Monitor"));
  81.     DWORD IsExist;
  82. DWORD dwDataType;
  83. DWORD dwLength;
  84. dwDataType=REG_SZ;
  85.     dwLength=sizeof(dw_Passwd);
  86.     
  87. if((RegCreateKeyEx(HKEY_CURRENT_USER,"Software\Monitor\Sender",0,
  88. NULL,REG_OPTION_NON_VOLATILE,NULL,
  89. NULL,&PasswdKey,&IsExist))!=ERROR_SUCCESS)
  90. return FALSE;
  91. if(IsExist==REG_CREATED_NEW_KEY)
  92. {  
  93. CSetPwd SetPwd;
  94. SetPwd.DoModal();
  95. }
  96. if(IsExist==REG_OPENED_EXISTING_KEY)
  97. {
  98. if((RegQueryValueEx(PasswdKey,"Passwd",
  99. NULL,&dwDataType,(LPBYTE)dw_Passwd,&dwLength))!=ERROR_SUCCESS)
  100. return FALSE;
  101. // RegCloseKey(PasswdKey);
  102. CInputPwd InputPwd;
  103. InputPwd.DoModal();
  104. if(m_Modify)
  105. {
  106. CModifyPwd ModifyPwd;
  107. ModifyPwd.DoModal();
  108. }
  109. }
  110. if(fail)
  111. return FALSE;
  112. LoadStdProfileSettings();  // Load standard INI file options (including MRU)
  113. */
  114. // Register the application's document templates.  Document templates
  115. //  serve as the connection between documents, frame windows and views.
  116. CSingleDocTemplate* pDocTemplate;
  117. pDocTemplate = new CSingleDocTemplate(
  118. IDR_MAINFRAME,
  119. RUNTIME_CLASS(CPlayDoc),
  120. RUNTIME_CLASS(CMainFrame),       // main SDI frame window
  121. RUNTIME_CLASS(CPlayView));
  122. AddDocTemplate(pDocTemplate);
  123.     CoInitialize(NULL); 
  124. // Parse command line for standard shell commands, DDE, file open
  125. CCommandLineInfo cmdInfo;
  126. ParseCommandLine(cmdInfo);
  127. // Dispatch commands specified on the command line
  128. if (!ProcessShellCommand(cmdInfo))
  129. return FALSE;
  130.     int status;
  131. WSADATA WSAData;
  132. if (!(status = WSAStartup(MAKEWORD(2,2), &WSAData)) == 0) 
  133. {
  134. AfxMessageBox("error StartUp");
  135. return FALSE;
  136. }
  137. InitSocket();           //建立监听socket
  138. InitMultiSocket();      //建立组播socket
  139. AddDevToMenu();
  140. ::SetWindowText(m_pMainWnd->m_hWnd,"远程监控系统");
  141. return TRUE;
  142. }
  143. /////////////////////////////////////////////////////////////////////////////
  144. // CAboutDlg dialog used for App About
  145. class CAboutDlg : public CDialog
  146. {
  147. public:
  148. CAboutDlg();
  149. // Dialog Data
  150. //{{AFX_DATA(CAboutDlg)
  151. enum { IDD = IDD_ABOUTBOX };
  152. //}}AFX_DATA
  153. // ClassWizard generated virtual function overrides
  154. //{{AFX_VIRTUAL(CAboutDlg)
  155. protected:
  156. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  157. //}}AFX_VIRTUAL
  158. // Implementation
  159. protected:
  160. //{{AFX_MSG(CAboutDlg)
  161. // No message handlers
  162. //}}AFX_MSG
  163. DECLARE_MESSAGE_MAP()
  164. };
  165. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  166. {
  167. //{{AFX_DATA_INIT(CAboutDlg)
  168. //}}AFX_DATA_INIT
  169. }
  170. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  171. {
  172. CDialog::DoDataExchange(pDX);
  173. //{{AFX_DATA_MAP(CAboutDlg)
  174. //}}AFX_DATA_MAP
  175. }
  176. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  177. //{{AFX_MSG_MAP(CAboutDlg)
  178. // No message handlers
  179. //}}AFX_MSG_MAP
  180. END_MESSAGE_MAP()
  181. // App command to run the dialog
  182. void CPlayApp::OnAppAbout()
  183. {
  184. CAboutDlg aboutDlg;
  185. aboutDlg.DoModal();
  186. }
  187. /////////////////////////////////////////////////////////////////////////////
  188. // CPlayApp message handlers
  189. int CPlayApp::ExitInstance() 
  190. {
  191. // TODO: Add your specialized code here and/or call the base class
  192.     WSACleanup();
  193. CoUninitialize();
  194. return CWinApp::ExitInstance();
  195. }
  196. void CPlayApp::AddDevToMenu()
  197. {
  198.     CVideoPlay vPlay;
  199. CStringList DevList;
  200. int i;
  201. CString DevName;
  202. vPlay.FindDevice(DevList);
  203. for(i=0;i<DevList.GetCount();i++)
  204. {
  205. DevName=DevList.GetHead();
  206. CMenu* Pmenu=m_pMainWnd->GetMenu();
  207. CMenu* Smenu=Pmenu->GetSubMenu(0);
  208. Smenu->AppendMenu(MF_STRING,MENU_VDEVICE0 + i,DevName);
  209. Smenu->DeleteMenu(0,MF_BYPOSITION);
  210. Smenu->EnableMenuItem(MENU_VDEVICE0 + i,MF_ENABLED||MF_BYCOMMAND);   
  211.     }
  212. /* HRESULT hr;
  213. int uIndex=0;
  214. ICreateDevEnum *pCreateDevEnum;
  215.     hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
  216.   IID_ICreateDevEnum, (void**)&pCreateDevEnum);
  217.     IEnumMoniker *pEm;
  218.     hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,
  219. &pEm, 0);
  220.     HELPER_RELEASE(pCreateDevEnum);
  221.     pEm->Reset();
  222.     ULONG cFetched;
  223.     IMoniker *pM;
  224.     while(hr = pEm->Next(1, &pM, &cFetched), hr==S_OK)
  225.     {
  226.   IPropertyBag *pBag;
  227.   hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pBag);
  228.   if(SUCCEEDED(hr)) 
  229.   {
  230.     VARIANT var;
  231.     var.vt = VT_BSTR;
  232.     hr = pBag->Read(L"FriendlyName", &var, NULL);
  233.     if (hr == NOERROR)
  234. {
  235. char achName[80];
  236. WideCharToMultiByte(CP_ACP, 0, var.bstrVal, -1, achName, 80,NULL, NULL);
  237.     CMenu* Pmenu=m_pMainWnd->GetMenu();
  238.     CMenu* Smenu=Pmenu->GetSubMenu(0);
  239.     Smenu->AppendMenu(MF_STRING,MENU_VDEVICE0 + uIndex,achName);
  240.     Smenu->DeleteMenu(0,MF_BYPOSITION);
  241.     Smenu->EnableMenuItem(MENU_VDEVICE0 + uIndex,MF_ENABLED||MF_BYCOMMAND);   
  242.     SysFreeString(var.bstrVal);
  243.     }
  244.    HELPER_RELEASE(pBag);
  245.   }
  246.   
  247. }
  248. /* CMainFrame* pFrame=(CMainFrame*)m_pMainWnd;
  249. hr = pM->BindToObject(0, 0, IID_IBaseFilter, (void**)&(pFrame->pVCap));
  250. if(pFrame->pVCap== NULL) 
  251. {
  252.     
  253. return;
  254. } */
  255. /* HELPER_RELEASE(pM);
  256. uIndex++;
  257.     }
  258.     HELPER_RELEASE(pEm);*/
  259. }
  260. //建立监听socket
  261. DWORD CPlayApp::InitSocket()
  262. {
  263.   
  264. SOCKADDR_IN send_sin;
  265. int status;
  266. CMainFrame* pFrame;
  267. pFrame=(CMainFrame*)m_pMainWnd;
  268. HWND m_hwndRec=pFrame->m_hWnd;
  269. pFrame->m_bAutoMenuEnable=FALSE;
  270. //创建一个socket
  271. Lsock = socket(AF_INET,SOCK_STREAM,0);
  272. if (Lsock == INVALID_SOCKET)
  273. {
  274. ErrMsg(m_hwndRec,"Socket failed");
  275. return -1;
  276. }
  277. send_sin.sin_port=htons(1500); 
  278. send_sin.sin_family=AF_INET;
  279. send_sin.sin_addr.s_addr=INADDR_ANY;
  280. //绑定服务器主机地址与端口
  281. if (bind(Lsock, (struct sockaddr FAR *) &send_sin, sizeof(send_sin))==SOCKET_ERROR) 
  282. {
  283. ErrMsg(m_hwndRec,"bind failed");
  284.     closesocket(Lsock );
  285. return -1;
  286. }
  287. //设置端口状态为监听
  288. if (listen(Lsock, 10) < 0) 
  289. {
  290. ErrMsg(m_hwndRec,"Listen failed");
  291. closesocket(Lsock );
  292. return -1;
  293. }
  294.   
  295. //设定服务器响应的网络事件为FD_ACCEPT,即程序想要接收数据 
  296. //产生相应传递给窗口的消息为WSA_ACCEPT
  297. if ((status = WSAAsyncSelect(Lsock,m_hwndRec,WSA_ACCEPT,FD_ACCEPT))>0) 
  298. {
  299.   ErrMsg(m_hwndRec,"Error on WSAAsyncSelect()");
  300.         closesocket(Lsock );
  301. return -1;
  302. }
  303. return 0;
  304. }
  305. void CPlayApp::ErrMsg(HWND hwnd, const char *ErrMsg)
  306. {
  307.   char szBuff[80];
  308.   sprintf(szBuff, "%d is the error", WSAGetLastError());
  309.   MessageBox(hwnd,szBuff,ErrMsg,MB_OK);
  310. }
  311. //初始化IP组播套接字
  312. int CPlayApp::InitMultiSocket()
  313. {
  314.     int SendBuf;
  315. DWORD cbRet;
  316.     int status;
  317. BOOL bFlag;
  318.     SOCKADDR_IN  SrcAddr;
  319.     CMainFrame* pFrame=(CMainFrame*)m_pMainWnd;
  320.     HWND m_hwnd=pFrame->m_hWnd;
  321.    
  322. //创建一个IP组播套接字
  323. MultiSock = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
  324.   (LPWSAPROTOCOL_INFO)NULL, 0, 
  325.        WSA_FLAG_MULTIPOINT_C_LEAF | WSA_FLAG_MULTIPOINT_D_LEAF);
  326. if (MultiSock == INVALID_SOCKET) 
  327. {
  328.     ErrMsg(m_hwnd,"WSASocket Error");
  329. return -1;
  330.     } 
  331.      
  332. //设置套接字为可重用端口地址
  333. bFlag = TRUE;
  334. status = setsockopt(
  335. MultiSock,
  336. SOL_SOCKET,
  337. SO_REUSEADDR,
  338. (char *)&bFlag,
  339. sizeof (bFlag));
  340. if (status == SOCKET_ERROR) 
  341. {
  342.     ErrMsg(m_hwnd,"setsockopt Error");
  343. return -1;
  344. }
  345.    // 将套接字绑扎到用户指定端口及默认的接口
  346. SrcAddr.sin_family = AF_INET;
  347.     SrcAddr.sin_port = htons (DESTPORT);
  348.     SrcAddr.sin_addr.s_addr = INADDR_ANY;
  349.     status = bind (
  350.         MultiSock, 
  351.         (struct sockaddr FAR *)&SrcAddr, 
  352.         sizeof(struct sockaddr));
  353.     if (status == SOCKET_ERROR) 
  354. {
  355. ErrMsg(m_hwnd,"bind Error");
  356.     return -1;
  357.     }
  358. nIP_TTL = 2;   //允许跨网传播
  359. // 设置多址广播数据报传播范围,允许跨网传播
  360. status = WSAIoctl (MultiSock,     
  361. SIO_MULTICAST_SCOPE,   
  362. &nIP_TTL,              
  363. sizeof (nIP_TTL),      
  364. NULL,             
  365. 0,         
  366. &cbRet,                
  367.         NULL,                  
  368.         NULL);                 
  369.     if (status)
  370. {
  371.       ErrMsg(m_hwnd,"WSAIoctl Error");
  372. return -1;
  373.     } 
  374.   
  375.     // 允许内部回送(LOOPBACK)
  376.       bFlag = TRUE;
  377.       status = WSAIoctl (MultiSock,              /* socket */
  378.           SIO_MULTIPOINT_LOOPBACK,               /* LoopBack on or off */
  379.           &bFlag,                                /* input */
  380.           sizeof (bFlag),                        /* size */
  381.           NULL,                                      /* output */ 
  382.           0,                                         /* size */
  383.           &cbRet,                                    /* bytes returned */
  384.           NULL,                                      /* overlapped */
  385.           NULL);                                     /* completion routine */
  386.    if (status)
  387.    {
  388.         ErrMsg(m_hwnd,"WSAIoctl Error");
  389.    return -1;
  390.       } 
  391.   
  392. //设定发送缓冲区为64k
  393. SendBuf=65536;
  394. status = setsockopt(
  395. MultiSock,
  396.      SOL_SOCKET,
  397.     SO_SNDBUF,
  398.     (char *)&SendBuf,
  399.     sizeof(SendBuf));
  400. if (status == SOCKET_ERROR) 
  401. {
  402. ErrMsg(m_hwnd,"setsockopt Error");
  403. return -1;
  404. }
  405.     //测试设定的发送缓冲区是否为64k
  406. int ASendBuf;
  407. int SLen=sizeof(ASendBuf);
  408. status=getsockopt(MultiSock,SOL_SOCKET,SO_SNDBUF,
  409.        (char * )&ASendBuf, &SLen);
  410.     
  411. if (status == SOCKET_ERROR) 
  412. {
  413.     ErrMsg(m_hwnd,"setsockopt Error");
  414. return -1;
  415. }
  416.     if(status==0)
  417.   if(ASendBuf!=65536)
  418. return -1;
  419. }
  420.     DestAddr.sin_family = AF_INET; 
  421.     DestAddr.sin_port = htons (DESTPORT);
  422.     DestAddr.sin_addr.s_addr =inet_addr(MULTIDESTADDR);
  423. return 0;
  424. }