Add accept connection dialog

This commit is contained in:
Grayson Riffe 2023-10-01 13:48:29 -05:00
parent bd2772ae0e
commit 8435304147
5 changed files with 80 additions and 11 deletions

Binary file not shown.

View File

@ -7,6 +7,7 @@
#define IDI_ICONMAIN 106 #define IDI_ICONMAIN 106
#define IDD_DIALOGCONNECTING 107 #define IDD_DIALOGCONNECTING 107
#define IDD_DIALOGCHAT 109 #define IDD_DIALOGCHAT 109
#define IDD_DIALOGACCEPTCONNECTION 111
#define IDC_STATICTITLE 1001 #define IDC_STATICTITLE 1001
#define IDC_BUTTONEXIT 1003 #define IDC_BUTTONEXIT 1003
#define IDC_BUTTONCONNECT 1005 #define IDC_BUTTONCONNECT 1005
@ -20,6 +21,7 @@
#define IDC_EDITCHATINPUT 1013 #define IDC_EDITCHATINPUT 1013
#define IDC_BUTTONSEND 1014 #define IDC_BUTTONSEND 1014
#define IDC_BUTTONDISCONNECT 1015 #define IDC_BUTTONDISCONNECT 1015
#define IDC_STATICREMOTEINFO 1017
#define ID_FILE_EXIT 40001 #define ID_FILE_EXIT 40001
#define ID_HELP_ABOUT 40002 #define ID_HELP_ABOUT 40002
@ -27,9 +29,9 @@
// //
#ifdef APSTUDIO_INVOKED #ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS #ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 111 #define _APS_NEXT_RESOURCE_VALUE 113
#define _APS_NEXT_COMMAND_VALUE 40003 #define _APS_NEXT_COMMAND_VALUE 40003
#define _APS_NEXT_CONTROL_VALUE 1016 #define _APS_NEXT_CONTROL_VALUE 1018
#define _APS_NEXT_SYMED_VALUE 101 #define _APS_NEXT_SYMED_VALUE 101
#endif #endif
#endif #endif

View File

@ -118,6 +118,8 @@ namespace wc {
m_inAddress = toWideStr(remoteStr); m_inAddress = toWideStr(remoteStr);
m_inSocket = conSock; m_inSocket = conSock;
//Wait here until there is no incoming connection anymore before accepting again
} }
closesocket(sock); closesocket(sock);
@ -144,15 +146,13 @@ namespace wc {
GetMonitorInfo(MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST), &mi); GetMonitorInfo(MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST), &mi);
xPos = mi.rcMonitor.left + 100, yPos = mi.rcMonitor.top + 100; xPos = mi.rcMonitor.left + 100, yPos = mi.rcMonitor.top + 100;
} }
SetWindowPos(dlg, nullptr, xPos, yPos, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
SetWindowPos(dlg, nullptr, xPos, yPos, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
SetDlgItemText(dlg, IDC_STATICTITLE, app->m_appName.c_str()); SetDlgItemText(dlg, IDC_STATICTITLE, app->m_appName.c_str());
LOGFONT lFont = { .lfHeight = 50 }; LOGFONT lFont = { .lfHeight = 50 };
HFONT font = CreateFontIndirect(&lFont); HFONT font = CreateFontIndirect(&lFont);
SendMessage(GetDlgItem(dlg, IDC_STATICTITLE), WM_SETFONT, reinterpret_cast<LPARAM>(font), NULL); SendMessage(GetDlgItem(dlg, IDC_STATICTITLE), WM_SETFONT, reinterpret_cast<LPARAM>(font), NULL);
SetDlgItemText(dlg, IDC_STATICDESC, L"A simple Windows chat app"); SetDlgItemText(dlg, IDC_STATICDESC, L"A simple Windows chat app");
SendDlgItemMessage(dlg, IDC_EDITADDRESS, EM_SETCUEBANNER, TRUE, reinterpret_cast<LPARAM>(L"Address")); SendDlgItemMessage(dlg, IDC_EDITADDRESS, EM_SETCUEBANNER, TRUE, reinterpret_cast<LPARAM>(L"Address"));
SendDlgItemMessage(dlg, IDC_EDITSCREENNAME, EM_SETCUEBANNER, TRUE, reinterpret_cast<LPARAM>(L"User")); SendDlgItemMessage(dlg, IDC_EDITSCREENNAME, EM_SETCUEBANNER, TRUE, reinterpret_cast<LPARAM>(L"User"));
@ -202,10 +202,23 @@ namespace wc {
case WM_TIMER: case WM_TIMER:
if (wParam == IDT_CHECKINCONN && app->m_inSocket != INVALID_SOCKET) { if (wParam == IDT_CHECKINCONN && app->m_inSocket != INVALID_SOCKET) {
KillTimer(dlg, IDT_CHECKINCONN); KillTimer(dlg, IDT_CHECKINCONN);
//Ask if to connect and for screen name here
RECT dlgRect = { }; RECT dlgRect = { };
GetWindowRect(dlg, &dlgRect); GetWindowRect(dlg, &dlgRect);
EndDialog(dlg, reinterpret_cast<INT_PTR>(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<DLGPROC>(acceptDlgProc), reinterpret_cast<LPARAM>(&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<MainDlgOutput*>(result);
EndDialog(dlg, reinterpret_cast<INT_PTR>(new MainDlgOutput{ app->m_inAddress, out->screenname, dlgRect.left, dlgRect.top, app->m_inSocket }));
delete out;
app->m_inSocket = INVALID_SOCKET; app->m_inSocket = INVALID_SOCKET;
return TRUE; return TRUE;
} }
@ -220,6 +233,50 @@ namespace wc {
return FALSE; 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<MainDlgInput*>(lParam);
app = in->appPtr;
SendMessage(dlg, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(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<LPARAM>(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<LPARAM>(&balloon));
return TRUE;
}
std::wstring buffer(inputLength, 0);
GetDlgItemText(dlg, IDC_EDITSCREENNAME, buffer.data(), static_cast<int>(buffer.size() + 1));
EndDialog(dlg, reinterpret_cast<INT_PTR>(new MainDlgOutput{ std::wstring(), buffer, NULL, NULL, INVALID_SOCKET}));
return TRUE;
}
case IDCANCEL:
EndDialog(dlg, wParam);
return TRUE;
}
return FALSE;
}
return FALSE;
}
Application::~Application() { Application::~Application() {
WSACleanup(); WSACleanup();
} }

View File

@ -15,6 +15,7 @@ namespace wc {
private: private:
void startListen(); void startListen();
static BOOL CALLBACK mainDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam); 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_appName;
const std::wstring m_appVersion; const std::wstring m_appVersion;

View File

@ -77,7 +77,16 @@ namespace wc {
//Send and receive screen names //Send and receive screen names
send(sock, reinterpret_cast<const char*>(m_screenname.c_str()), static_cast<int>(m_screenname.size()) * sizeof(wchar_t), NULL); send(sock, reinterpret_cast<const char*>(m_screenname.c_str()), static_cast<int>(m_screenname.size()) * sizeof(wchar_t), NULL);
m_remoteScreenname.resize(recv(sock, reinterpret_cast<char*>(m_remoteScreenname.data()), 1000, NULL) / 2); int bytesRemoteScreenname = recv(sock, reinterpret_cast<char*>(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); ioctlsocket(sock, FIONBIO, &yes);
m_connected = true; m_connected = true;
@ -85,7 +94,7 @@ namespace wc {
std::wstring recvBuffer(2000, 0); std::wstring recvBuffer(2000, 0);
std::wstring sendStr; std::wstring sendStr;
int bytesRecvd = 0; int bytesRecvd = 0;
while (m_connected) { while (m_connected && !m_connectionError) {
while (!m_sendQueue.empty()) { while (!m_sendQueue.empty()) {
sendStr = m_sendQueue.pop(); sendStr = m_sendQueue.pop();
send(sock, reinterpret_cast<const char*>(sendStr.c_str()), static_cast<int>(sendStr.size()) * sizeof(wchar_t), NULL); send(sock, reinterpret_cast<const char*>(sendStr.c_str()), static_cast<int>(sendStr.size()) * sizeof(wchar_t), NULL);
@ -147,9 +156,11 @@ namespace wc {
ChatDlgInput* in = reinterpret_cast<ChatDlgInput*>(lParam); ChatDlgInput* in = reinterpret_cast<ChatDlgInput*>(lParam);
chat = in->chatPtr; chat = in->chatPtr;
SendMessage(dlg, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICONMAIN))));
SetWindowPos(dlg, nullptr, in->xPos + 100, in->yPos + 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER); SetWindowPos(dlg, nullptr, in->xPos + 100, in->yPos + 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
SetDlgItemText(dlg, IDC_STATICADDRESS, chat->m_address.c_str()); SetDlgItemText(dlg, IDC_STATICADDRESS, chat->m_address.c_str());
SendDlgItemMessage(dlg, IDC_PROGRESS, PBM_SETMARQUEE, TRUE, NULL); SendDlgItemMessage(dlg, IDC_PROGRESS, PBM_SETMARQUEE, TRUE, NULL);
SetTimer(dlg, IDT_CHECKCONN, 100, nullptr); SetTimer(dlg, IDT_CHECKCONN, 100, nullptr);
return TRUE; return TRUE;
@ -175,9 +186,7 @@ namespace wc {
SetWindowText(dlg, std::format(L"{} - WinChat", chat->m_remoteScreenname).c_str()); SetWindowText(dlg, std::format(L"{} - WinChat", chat->m_remoteScreenname).c_str());
SendMessage(dlg, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICONMAIN)))); SendMessage(dlg, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICONMAIN))));
SetWindowPos(dlg, nullptr, in->xPos - 50, in->yPos - 50, 0, 0, SWP_NOSIZE | SWP_NOZORDER); SetWindowPos(dlg, nullptr, in->xPos - 50, in->yPos - 50, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
SendDlgItemMessage(dlg, IDC_EDITCHATINPUT, EM_SETCUEBANNER, TRUE, reinterpret_cast<LPARAM>(L"Message")); SendDlgItemMessage(dlg, IDC_EDITCHATINPUT, EM_SETCUEBANNER, TRUE, reinterpret_cast<LPARAM>(L"Message"));
SetDlgItemText(dlg, IDC_EDITCHATDISPLAY, std::format(L"Connected to {} at {}", chat->m_remoteScreenname, chat->m_address).c_str()); SetDlgItemText(dlg, IDC_EDITCHATDISPLAY, std::format(L"Connected to {} at {}", chat->m_remoteScreenname, chat->m_address).c_str());