From 8435304147b2a6a82495716325a347b012d9716c Mon Sep 17 00:00:00 2001 From: Grayson Riffe Date: Sun, 1 Oct 2023 13:48:29 -0500 Subject: [PATCH] Add accept connection dialog --- WinChat/WinChat.rc | Bin 9522 -> 11218 bytes WinChat/resource.h | 6 ++-- WinChat/src/Application.cpp | 67 +++++++++++++++++++++++++++++++++--- WinChat/src/Application.h | 1 + WinChat/src/Chat.cpp | 17 ++++++--- 5 files changed, 80 insertions(+), 11 deletions(-) diff --git a/WinChat/WinChat.rc b/WinChat/WinChat.rc index 387447df04f50d42725242260b42873d29f0198e..0116b46fdc258020789c343328cb28cff1144bb2 100644 GIT binary patch delta 515 zcma)3yG{a85FKAA7}ynwW)&lWs4-%2SynC;*(_^Z#Rs~Hl@&o^A&9ZDvC+bQY9k+D zB5@lle}Tk5An`*ym%{icG?RPpnVB;)XL6sn-i|tLh2NuD|3wi2F2`f@7TGX|_Q}J$ zN4qcw$Rk&J+>4}w1u`j32`X?V+T=*kTX3mCRcg{1odVU7v*9&hHN-Cwg~wf`&ex&M z0l~#kkptt&WeeFVIOpIb(bJ_8rfdTV%Hhsa7Pm=j+=}H_Yv_0N$+O9VHmS7?j!p(@ zzE@20V>p@ltxYQZp$&>p=t6G5mGG=$(t5wAoG!@Vd(}uCVA2lMd3X;oSNaYVScp1( zEhX-%ODkV_F7g|wNmBtQ+uupYR#K@>f*-=wQ3E&!9E@amaqiV`PpQLvrzQUUiYM)6 d1Wiynp&btiGP$Kq`<m_appName.c_str()); LOGFONT lFont = { .lfHeight = 50 }; HFONT font = CreateFontIndirect(&lFont); SendMessage(GetDlgItem(dlg, IDC_STATICTITLE), WM_SETFONT, reinterpret_cast(font), NULL); - SetDlgItemText(dlg, IDC_STATICDESC, L"A simple Windows chat app"); - SendDlgItemMessage(dlg, IDC_EDITADDRESS, EM_SETCUEBANNER, TRUE, reinterpret_cast(L"Address")); SendDlgItemMessage(dlg, IDC_EDITSCREENNAME, EM_SETCUEBANNER, TRUE, reinterpret_cast(L"User")); @@ -202,10 +202,23 @@ namespace wc { case WM_TIMER: if (wParam == IDT_CHECKINCONN && app->m_inSocket != INVALID_SOCKET) { KillTimer(dlg, IDT_CHECKINCONN); - //Ask if to connect and for screen name here RECT dlgRect = { }; GetWindowRect(dlg, &dlgRect); - EndDialog(dlg, reinterpret_cast(new MainDlgOutput{ app->m_inAddress, L"Temp screen name", dlgRect.left, dlgRect.top, app->m_inSocket })); + MainDlgInput acceptInput = { app, dlgRect.left, dlgRect.top }; + + INT_PTR result = DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOGACCEPTCONNECTION), dlg, reinterpret_cast(acceptDlgProc), reinterpret_cast(&acceptInput)); + + if (result == IDCANCEL) { + closesocket(app->m_inSocket); + app->m_inSocket = INVALID_SOCKET; + SetTimer(dlg, IDT_CHECKINCONN, 100, nullptr); + return TRUE; + } + + MainDlgOutput* out = reinterpret_cast(result); + + EndDialog(dlg, reinterpret_cast(new MainDlgOutput{ app->m_inAddress, out->screenname, dlgRect.left, dlgRect.top, app->m_inSocket })); + delete out; app->m_inSocket = INVALID_SOCKET; return TRUE; } @@ -220,6 +233,50 @@ namespace wc { return FALSE; } + BOOL CALLBACK Application::acceptDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam) { + static Application* app = nullptr; + + switch (msg) { + case WM_INITDIALOG: { + MainDlgInput* in = reinterpret_cast(lParam); + app = in->appPtr; + + SendMessage(dlg, WM_SETICON, ICON_BIG, reinterpret_cast(LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICONMAIN)))); + SetWindowPos(dlg, nullptr, in->xPos + 30, in->yPos + 30, 0, 0, SWP_NOSIZE | SWP_NOZORDER); + + SetDlgItemText(dlg, IDC_STATICREMOTEINFO, app->m_inAddress.c_str()); + SendDlgItemMessage(dlg, IDC_EDITSCREENNAME, EM_SETCUEBANNER, TRUE, reinterpret_cast(L"User")); + + return TRUE; + } + + case WM_COMMAND: + switch (LOWORD(wParam)) { + case IDOK: { + int inputLength = GetWindowTextLength(GetDlgItem(dlg, IDC_EDITSCREENNAME)); + if (!inputLength) { + EDITBALLOONTIP balloon = { .cbStruct = sizeof(balloon), .pszTitle = L"Alert", .pszText = L"You must enter a screen name." }; + SendDlgItemMessage(dlg, IDC_EDITSCREENNAME, EM_SHOWBALLOONTIP, NULL, reinterpret_cast(&balloon)); + return TRUE; + } + + std::wstring buffer(inputLength, 0); + GetDlgItemText(dlg, IDC_EDITSCREENNAME, buffer.data(), static_cast(buffer.size() + 1)); + EndDialog(dlg, reinterpret_cast(new MainDlgOutput{ std::wstring(), buffer, NULL, NULL, INVALID_SOCKET})); + return TRUE; + } + + case IDCANCEL: + EndDialog(dlg, wParam); + return TRUE; + } + + return FALSE; + } + + return FALSE; + } + Application::~Application() { WSACleanup(); } diff --git a/WinChat/src/Application.h b/WinChat/src/Application.h index 08d8f74..d7c55f3 100644 --- a/WinChat/src/Application.h +++ b/WinChat/src/Application.h @@ -15,6 +15,7 @@ namespace wc { private: void startListen(); static BOOL CALLBACK mainDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam); + static BOOL CALLBACK acceptDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam); const std::wstring m_appName; const std::wstring m_appVersion; diff --git a/WinChat/src/Chat.cpp b/WinChat/src/Chat.cpp index 27dc28e..75defdc 100644 --- a/WinChat/src/Chat.cpp +++ b/WinChat/src/Chat.cpp @@ -77,7 +77,16 @@ namespace wc { //Send and receive screen names send(sock, reinterpret_cast(m_screenname.c_str()), static_cast(m_screenname.size()) * sizeof(wchar_t), NULL); - m_remoteScreenname.resize(recv(sock, reinterpret_cast(m_remoteScreenname.data()), 1000, NULL) / 2); + int bytesRemoteScreenname = recv(sock, reinterpret_cast(m_remoteScreenname.data()), 1000, NULL); + + if (!bytesRemoteScreenname || WSAGetLastError() == WSAECONNRESET) { + MessageBox(nullptr, L"The remote host denied the connection.", L"Error", MB_ICONERROR); + m_connectionError = true; + closesocket(sock); + return; + } + + m_remoteScreenname.resize(bytesRemoteScreenname / 2); ioctlsocket(sock, FIONBIO, &yes); m_connected = true; @@ -85,7 +94,7 @@ namespace wc { std::wstring recvBuffer(2000, 0); std::wstring sendStr; int bytesRecvd = 0; - while (m_connected) { + while (m_connected && !m_connectionError) { while (!m_sendQueue.empty()) { sendStr = m_sendQueue.pop(); send(sock, reinterpret_cast(sendStr.c_str()), static_cast(sendStr.size()) * sizeof(wchar_t), NULL); @@ -147,9 +156,11 @@ namespace wc { ChatDlgInput* in = reinterpret_cast(lParam); chat = in->chatPtr; + SendMessage(dlg, WM_SETICON, ICON_BIG, reinterpret_cast(LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICONMAIN)))); SetWindowPos(dlg, nullptr, in->xPos + 100, in->yPos + 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER); SetDlgItemText(dlg, IDC_STATICADDRESS, chat->m_address.c_str()); SendDlgItemMessage(dlg, IDC_PROGRESS, PBM_SETMARQUEE, TRUE, NULL); + SetTimer(dlg, IDT_CHECKCONN, 100, nullptr); return TRUE; @@ -175,9 +186,7 @@ namespace wc { SetWindowText(dlg, std::format(L"{} - WinChat", chat->m_remoteScreenname).c_str()); SendMessage(dlg, WM_SETICON, ICON_BIG, reinterpret_cast(LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICONMAIN)))); - SetWindowPos(dlg, nullptr, in->xPos - 50, in->yPos - 50, 0, 0, SWP_NOSIZE | SWP_NOZORDER); - SendDlgItemMessage(dlg, IDC_EDITCHATINPUT, EM_SETCUEBANNER, TRUE, reinterpret_cast(L"Message")); SetDlgItemText(dlg, IDC_EDITCHATDISPLAY, std::format(L"Connected to {} at {}", chat->m_remoteScreenname, chat->m_address).c_str());