📄 pipeclientdlg.cpp
字号:
// PipeClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PipeClient.h"
#include "PipeClientDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define BUFFER_SIZE 512
#define WM_USER_UPDATE_RECV_DATA WM_USER+200
#define WM_USER_SET_IPIPE_HANDLE WM_USER+201
#define WM_USER_SET_OPIPE_HANDLE WM_USER+202
#define WM_USER_GET_IPIPE_HANDLE WM_USER+203
#define WM_USER_GET_OPIPE_HANDLE WM_USER+204
#define WM_USER_CLEAR_SEND_DATA WM_USER+205
#define WM_USER_ENABLE_SEND_BUTTON WM_USER+206
#define WM_USER_TERMINATE_APP WM_USER+207
struct ThreadData {
HANDLE hINPipe;
HANDLE hONPipe;
HANDLE hIEvent;
HANDLE hOEvent;
HWND hWnd;
CString data;
};
LONG ReadServerData( LPVOID pTD);
LONG WriteServerData( LPVOID pTD);
LONG WaitForServerIConnect( LPVOID pTD);
LONG WaitForServerOConnect( LPVOID pTD);
/////////////////////////////////////////////////////////////////////////////
// CPipeClientDlg dialog
CPipeClientDlg::CPipeClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPipeClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPipeClientDlg)
m_sendmsg = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPipeClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPipeClientDlg)
DDX_Control(pDX, IDC_RECEIVEDMSG, m_rcvdmsg);
DDX_Text(pDX, IDC_SENDMSG, m_sendmsg);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPipeClientDlg, CDialog)
//{{AFX_MSG_MAP(CPipeClientDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, OnSend)
ON_MESSAGE(WM_USER_UPDATE_RECV_DATA, OnUpdateRecvData)
ON_MESSAGE(WM_USER_SET_IPIPE_HANDLE, OnSetIPipeHandle)
ON_MESSAGE(WM_USER_SET_OPIPE_HANDLE, OnSetOPipeHandle)
ON_MESSAGE(WM_USER_GET_IPIPE_HANDLE, OnGetIPipeHandle)
ON_MESSAGE(WM_USER_GET_OPIPE_HANDLE, OnGetOPipeHandle)
ON_MESSAGE(WM_USER_CLEAR_SEND_DATA, OnClearSendData)
ON_MESSAGE(WM_USER_ENABLE_SEND_BUTTON, OnEnableSendButton)
ON_MESSAGE(WM_USER_TERMINATE_APP, OnCancel)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPipeClientDlg message handlers
BOOL CPipeClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
GetDlgItem(IDOK)->EnableWindow(FALSE);
hIEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
hOEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
hINPipe = INVALID_HANDLE_VALUE;
hONPipe = INVALID_HANDLE_VALUE;
ThreadData *td1 = new ThreadData;
ThreadData *td2 = new ThreadData;
ThreadData *td3 = new ThreadData;
td1->hINPipe = td2->hINPipe = td3->hINPipe = hINPipe;
td1->hONPipe = td2->hONPipe = td3->hONPipe = hONPipe;
td1->hIEvent = td2->hIEvent = td3->hIEvent = hIEvent;
td1->hOEvent = td2->hOEvent = td3->hOEvent = hOEvent;
td1->hWnd = td2->hWnd = td3->hWnd = GetSafeHwnd();
td1->data = td2->data = td3->data = "";
DWORD dwThreadServerIConnect, dwThreadServerOConnect, dwThreadReadServer;
ThreadServerIConnect = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)WaitForServerIConnect, (LPVOID)td1, 0, &dwThreadServerIConnect);
ThreadServerOConnect = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)WaitForServerOConnect, (LPVOID)td2, 0, &dwThreadServerOConnect);
ThreadReadServer = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)ReadServerData, (LPVOID)td3, 0, &dwThreadReadServer);
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CPipeClientDlg::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 CPipeClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPipeClientDlg::OnSend()
{
// TODO: Add your control notification handler code here
ThreadData *td = new ThreadData;
UpdateData();
td->hONPipe = hONPipe;
td->hOEvent = hOEvent;
td->hWnd = GetSafeHwnd();
td->data = m_sendmsg;
DWORD dwThreadWriteServer;
ThreadWriteServer = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)WriteServerData, (LPVOID)td, 0, &dwThreadWriteServer);
}
void CPipeClientDlg::OnCancel()
{
// TODO: Add extra cleanup here
TerminateThread (ThreadReadServer, -1);
TerminateThread (ThreadWriteServer, -1);
TerminateThread (ThreadServerIConnect, -1);
TerminateThread (ThreadServerOConnect, -1);
CloseHandle(hIEvent);
CloseHandle(hOEvent);
CloseHandle(hINPipe);
CloseHandle(hONPipe);
CDialog::OnCancel();
}
LRESULT CPipeClientDlg::OnUpdateRecvData(WPARAM wParam, LPARAM lParam)
{
char szAppendBuf[BUFFER_SIZE + 10];
sprintf(szAppendBuf, "%s\r\n", (LPCTSTR)lParam);
m_rcvdmsg.SetSel(-1, 0);
m_rcvdmsg.ReplaceSel(szAppendBuf);
return 0;
}
void CPipeClientDlg::OnSetIPipeHandle(WPARAM wParam, LPARAM lParam)
{
hINPipe = (HANDLE)lParam;
}
void CPipeClientDlg::OnSetOPipeHandle(WPARAM wParam, LPARAM lParam)
{
hONPipe = (HANDLE)lParam;
GetDlgItem(IDOK)->EnableWindow(TRUE);
}
LRESULT CPipeClientDlg::OnGetIPipeHandle(WPARAM wParam, LPARAM lParam)
{
return ((LRESULT)hINPipe);
}
LRESULT CPipeClientDlg::OnGetOPipeHandle(WPARAM wParam, LPARAM lParam)
{
return ((LRESULT)hONPipe);
}
LRESULT CPipeClientDlg::OnClearSendData(WPARAM wParam, LPARAM lParam)
{
m_sendmsg = "";
UpdateData(FALSE);
return 0;
}
LRESULT CPipeClientDlg::OnEnableSendButton(WPARAM wParam, LPARAM lParam)
{
return 0;
}
LONG ReadServerData( LPVOID pTD)
{
ThreadData *td = (ThreadData *)pTD;
HANDLE hINPipe;
HWND hWnd = td->hWnd;
HANDLE hIEvent = td->hIEvent;
BOOL rc;
char * szBuf = new char [BUFFER_SIZE];
DWORD dwBytesRead;
DWORD dwRC;
WaitForSingleObject(hIEvent, INFINITE);
hINPipe = (HANDLE)SendMessage(hWnd, WM_USER_GET_IPIPE_HANDLE, NULL, NULL);
while (TRUE)
{
dwBytesRead = 0;
rc = ReadFile (hINPipe, szBuf, BUFFER_SIZE, &dwBytesRead, NULL);
dwRC = GetLastError();
if (dwBytesRead)
{
szBuf[dwBytesRead] = 0;
SendMessage(hWnd, WM_USER_UPDATE_RECV_DATA, NULL, (LPARAM)szBuf);
}
if (dwRC != ERROR_SUCCESS)
{
char szErrorMsg[1024];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwRC, 0, szErrorMsg, 1024, NULL);
AfxMessageBox(szErrorMsg);
AfxMessageBox("Server seems to have terminated");
break;
}
}
delete [] szBuf;
delete td;
PostMessage((HWND)hWnd, WM_USER_TERMINATE_APP, NULL, NULL);
return 0;
}
LONG WriteServerData( LPVOID pTD)
{
DWORD dwBytesWritten = 0;
BOOL rc;
ThreadData *td = (ThreadData *)pTD;
HANDLE hONPipe;
HWND hWnd = td->hWnd;
HANDLE hOEvent = td->hOEvent;
CString Serverdata = td->data;
WaitForSingleObject(hOEvent, INFINITE);
hONPipe = (HANDLE)SendMessage(hWnd, WM_USER_GET_OPIPE_HANDLE, NULL, NULL);
rc = WriteFile (hONPipe, (LPCVOID)((LPCTSTR)Serverdata),
Serverdata.GetLength(), &dwBytesWritten, NULL);
if (!rc)
{
DWORD dwRC;
char szErrorMsg[1024];
dwRC = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwRC, 0, szErrorMsg, 1024, NULL);
AfxMessageBox(szErrorMsg);
}
SendMessage((HWND)hWnd, WM_USER_CLEAR_SEND_DATA, NULL, NULL);
FlushFileBuffers(hONPipe);
delete td;
return 0;
}
LONG WaitForServerIConnect(LPVOID pTD)
{
HANDLE hINPipe;
ThreadData *td = (ThreadData *)pTD;
HANDLE hIEvent = td->hIEvent;
HWND hWnd = td->hWnd;
WaitNamedPipe ("\\\\.\\pipe\\OChat", NMPWAIT_WAIT_FOREVER);
hINPipe = CreateFile("\\\\.\\pipe\\OChat",
GENERIC_READ, 0, NULL,
OPEN_EXISTING, 0, NULL);
if (hINPipe != INVALID_HANDLE_VALUE)
{
SendMessage(hWnd, WM_USER_SET_IPIPE_HANDLE, NULL, (LPARAM)hINPipe);
SetEvent(hIEvent);
}
else
{
char szErrorMsg[1024];
DWORD dwRC = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwRC, 0, szErrorMsg, 1024, NULL);
AfxMessageBox(szErrorMsg);
AfxMessageBox("Invalid pipe handle. Terminating application");
PostMessage(hWnd, WM_USER_TERMINATE_APP, NULL, NULL);
}
delete td;
return 0;
}
LONG WaitForServerOConnect(LPVOID pTD)
{
HANDLE hONPipe;
ThreadData *td = (ThreadData *)pTD;
HANDLE hOEvent = td->hOEvent;
HWND hWnd = td->hWnd;
WaitNamedPipe ("\\\\.\\pipe\\IChat", NMPWAIT_WAIT_FOREVER);
hONPipe = CreateFile("\\\\.\\pipe\\IChat",
GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
if (hONPipe != INVALID_HANDLE_VALUE)
{
SendMessage(hWnd, WM_USER_SET_OPIPE_HANDLE, NULL, (LPARAM)hONPipe);
SetEvent(hOEvent);
}
else
{
char szErrorMsg[1024];
DWORD dwRC = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwRC, 0, szErrorMsg, 1024, NULL);
AfxMessageBox(szErrorMsg);
AfxMessageBox("Invalid pipe handle. Terminating application");
PostMessage(hWnd, WM_USER_TERMINATE_APP, NULL, NULL);
}
delete td;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -