// UdpModeDlg.cpp : implementation file // #include "stdafx.h" #include "UdpMode.h" #include "UdpModeDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif HANDLE g_ahExitEvent; CUdpModeDlg *pointer; int port_id; int port_opened = 0; // 0: close 1:open DWORD WINAPI RecvThread(LPVOID param); DWORD WINAPI MonitorEventThread( LPVOID param ); DWORD WINAPI MonitorEventThread(LPVOID param) { CString szLogMsg = _T(""); if( WaitForSingleObject(g_ahExitEvent, INFINITE) != WAIT_TIMEOUT) { AfxTrace("MonitorEventThread called \n"); slan_close(port_id); CIPAddressCtrl *pIpAddrCtrl = NULL; pIpAddrCtrl = (CIPAddressCtrl *)(pointer->GetDlgItem(IDC_SERVER_IPADDRESS)); pIpAddrCtrl->EnableWindow( TRUE ); // pointer->m_ServerIPCtrl.EnableWindow(TRUE); pointer->m_ServerPortCtrl.EnableWindow(TRUE); // pointer->m_RemoteIPCtrl.EnableWindow(TRUE); CIPAddressCtrl *pRemoteIpAddrCtrl = NULL; pRemoteIpAddrCtrl = (CIPAddressCtrl *)(pointer->GetDlgItem(IDC_REMOTE_IPADDRESS)); pRemoteIpAddrCtrl->EnableWindow( TRUE ); pointer->m_RemotePortCtrl.EnableWindow(TRUE); pointer->m_OpenCtrl.EnableWindow(TRUE); pointer->m_CloseCtrl.EnableWindow(FALSE); AfxMessageBox("Stopped!!"); } return (DWORD) -1; } DWORD WINAPI RecvThread(LPVOID param) { int nReturn; char szStr[3072]; CString strTemp; CString strResult; int len = 2048; slan_SetReadTimeouts ( port_id, 0); while(TRUE) { if( port_opened == 1) { len = 2048; nReturn = slan_read(port_id, szStr, &len); if(nReturn >= 0) { if(!len) Sleep(10); if( len > 0) { // strcat(strClientInfo,pszThreadInput->sClientThreadInfo.szClientIp); szStr[len] = '\0'; pointer->m_Receiver.GetWindowText(strTemp); strResult.Format("%s%s", strTemp, szStr); pointer->m_Receiver.SetWindowText(strResult); } } else { SetEvent(g_ahExitEvent); Sleep(1000); break; } Sleep(10); } else break; } return (DWORD) -1; } ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CUdpModeDlg dialog CUdpModeDlg::CUdpModeDlg(CWnd* pParent /*=NULL*/) : CDialog(CUdpModeDlg::IDD, pParent) { //{{AFX_DATA_INIT(CUdpModeDlg) m_RemotePort = 0; m_ServerPort = 0; //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CUdpModeDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CUdpModeDlg) DDX_Control(pDX, IDC_CLOSE, m_CloseCtrl); DDX_Control(pDX, IDC_OPEN, m_OpenCtrl); DDX_Control(pDX, IDC_REMOTE_PORT, m_RemotePortCtrl); DDX_Control(pDX, IDC_SERVER_PORT, m_ServerPortCtrl); DDX_Control(pDX, IDC_RECEIVER, m_Receiver); DDX_Control(pDX, IDC_TRANSMITTER, m_Transmitter); DDX_Text(pDX, IDC_REMOTE_PORT, m_RemotePort); DDX_Text(pDX, IDC_SERVER_PORT, m_ServerPort); DDV_MinMaxInt(pDX, m_ServerPort, 0, 65536); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CUdpModeDlg, CDialog) //{{AFX_MSG_MAP(CUdpModeDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDOK, OnOpen) ON_EN_CHANGE(IDC_TRANSMITTER, OnChangeTransmitter) ON_BN_CLICKED(IDC_BTN_EXIT, OnBtnExit) ON_BN_CLICKED(IDC_OPEN, OnOpen) ON_BN_CLICKED(IDC_CLOSE, OnClose) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CUdpModeDlg message handlers BOOL CUdpModeDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here UpdateData(FALSE); pointer = this; m_OpenCtrl.EnableWindow(TRUE); m_CloseCtrl.EnableWindow(FALSE); CIPAddressCtrl *pIpAddrCtrl = NULL; pIpAddrCtrl = (CIPAddressCtrl *)(GetDlgItem(IDC_SERVER_IPADDRESS)); pIpAddrCtrl->EnableWindow( TRUE ); CIPAddressCtrl *pRemoteIpAddrCtrl = NULL; pRemoteIpAddrCtrl = (CIPAddressCtrl *)(GetDlgItem(IDC_REMOTE_IPADDRESS)); pRemoteIpAddrCtrl->EnableWindow( TRUE ); // m_ServerIPCtrl.EnableWindow(TRUE); m_ServerPortCtrl.EnableWindow(TRUE); // m_RemoteIPCtrl.EnableWindow(TRUE); m_RemotePortCtrl.EnableWindow(TRUE); m_Transmitter.EnableWindow(FALSE); return TRUE; // return TRUE unless you set the focus to a control } void CUdpModeDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CUdpModeDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CUdpModeDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CUdpModeDlg::OnOpen() { // TODO: Add your control notification handler code here GetInputData(); DWORD dwThreadId; HANDLE hRecvThread; HANDLE hMonitorThread; OPMODE eMode; INPUTS sParam; CString szServerIp = _T(""); CIPAddressCtrl *pServerIpAddrCtrl = NULL; pServerIpAddrCtrl = (CIPAddressCtrl *)GetDlgItem(IDC_SERVER_IPADDRESS); pServerIpAddrCtrl->GetWindowText( szServerIp ); CString szRemoteIp = _T(""); CIPAddressCtrl *pRemoteIpAddrCtrl = NULL; pRemoteIpAddrCtrl = (CIPAddressCtrl *)GetDlgItem(IDC_REMOTE_IPADDRESS); pRemoteIpAddrCtrl->GetWindowText( szRemoteIp ); //Validate the inputs... if( szServerIp.IsEmpty() || szRemoteIp.IsEmpty() || (0==pointer->m_ServerPort) || (0==pointer->m_RemotePort) ) { AfxMessageBox( "Enter the proper Inputs !!" ); return; } // port_id = slan_openudp( szTmpServerIP, pointer->m_ServerPort, \ // szTmpRemoteIP, pointer->m_RemotePort); eMode = UDP; sprintf(sParam.udp.localip, szServerIp); sParam.udp.localport = m_ServerPort; sprintf(sParam.udp.remoteip, szRemoteIp); sParam.udp.remoteport = m_RemotePort; port_id = slan_open (eMode, sParam); //PINT pIndex = (PINT)malloc(sizeof(int)*1); //*pIndex = port_id; int iTemp; if( port_id >= 0) { port_opened = 1; // port is opened g_ahExitEvent = CreateEvent(NULL, TRUE, FALSE, NULL ); hRecvThread = CreateThread(NULL, 0, RecvThread, (LPVOID)&iTemp, 0, &dwThreadId); if( NULL == hRecvThread ) { DWORD dwError = GetLastError(); AfxMessageBox("CreateThread RecvThread- Error"); return; } hMonitorThread = CreateThread(NULL, 0, MonitorEventThread, (LPVOID)&iTemp, 0, &dwThreadId); if( NULL == hMonitorThread ) { DWORD dwError = GetLastError(); AfxMessageBox("CreateThread MonitorEventThread - Error"); return; } m_OpenCtrl.EnableWindow(FALSE); m_CloseCtrl.EnableWindow(TRUE); CIPAddressCtrl *pIpAddrCtrl = NULL; pIpAddrCtrl = (CIPAddressCtrl *)(GetDlgItem(IDC_SERVER_IPADDRESS)); pIpAddrCtrl->EnableWindow( FALSE ); // m_ServerIPCtrl.EnableWindow(FALSE); m_ServerPortCtrl.EnableWindow(FALSE); CIPAddressCtrl *pRemoteIpAddrCtrl = NULL; pRemoteIpAddrCtrl = (CIPAddressCtrl *)(GetDlgItem(IDC_REMOTE_IPADDRESS)); pRemoteIpAddrCtrl->EnableWindow( FALSE ); // m_RemoteIPCtrl.EnableWindow(FALSE); m_RemotePortCtrl.EnableWindow(FALSE); m_Transmitter.EnableWindow(TRUE); m_Transmitter.SetFocus(); } else AfxMessageBox("Port Open Fail"); } void CUdpModeDlg::OnClose() { // TODO: Add your control notification handler code here if( port_opened == 1) { port_opened = 0; // port is closed SetEvent(g_ahExitEvent); Sleep(1000); } } void CUdpModeDlg::OnBtnExit() { // TODO: Add your control notification handler code here if( port_opened == 1) OnClose(); CDialog::OnCancel(); } void CUdpModeDlg::OnChangeTransmitter() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here if( port_opened == 1) { char cSendChar; findCharToSend(&cSendChar); int ret = slan_write(port_id, &cSendChar, 1 ); } else AfxMessageBox("Port not open!"); } void CUdpModeDlg::OnCancel() { // TODO: Add extra cleanup here } void CUdpModeDlg::GetInputData() { UpdateData(TRUE); } void CUdpModeDlg::findCharToSend(char* pcSendChar) { WORD wLineIndex = 0; WORD dwGetSel = 0; WORD wStart = 0; DWORD wColNumber = 0; DWORD wLineNumber = 0; DWORD cbText = 0; char szBuf[2048] = {0}; CEdit* pEdit = (CEdit*)GetDlgItem(IDC_TRANSMITTER); wLineNumber = pEdit->SendMessage(EM_LINEFROMCHAR, -1, 0L); dwGetSel = (WORD)pEdit->SendMessage(EM_GETSEL,0, 0L); wStart = LOWORD(dwGetSel); wLineIndex = (WORD)pEdit->SendMessage(EM_LINEINDEX, -1, 0L); wColNumber = wStart - wLineIndex; *(WORD *)szBuf = sizeof(szBuf); cbText = pEdit->SendMessage(EM_GETLINE, wLineNumber,(DWORD)(LPSTR) szBuf); szBuf[cbText] = '\0'; *pcSendChar = szBuf[wColNumber-1]; } BOOL CUdpModeDlg::PreTranslateMessage(MSG* pMsg) { if ((pMsg->message == WM_KEYDOWN) && (pMsg->hwnd == GetDlgItem( IDC_TRANSMITTER )->m_hWnd)) { int nVirtKey = (int) pMsg->wParam; if (nVirtKey == VK_DELETE || nVirtKey == VK_BACK) return TRUE; } return CDialog::PreTranslateMessage(pMsg); }