Skip to content

Commit

Permalink
Fix (quick)connect page.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertabcd committed Sep 15, 2017
1 parent 752b225 commit 0fce579
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 32 deletions.
13 changes: 13 additions & 0 deletions Lite/Address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,17 @@ CString CAddress::Description() const
s.Format(_T("[CAddress protocol=%s server=%s port=%d path=%s url=%s]"),
protocol_, server_, port_, path_, url_);
return s;
}

unsigned short CAddress::DefaultPort() const
{
const CString& protocol = Protocol();
if (protocol == "telnet" || protocol == "bbs")
return 23;
else if (protocol == "http" || protocol == "ws")
return 80;
else if (protocol == "https" || protocol == "wss")
return 443;
else
return 0;
}
5 changes: 3 additions & 2 deletions Lite/Address.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class CAddress
const CString& URL() const { return url_; }
const CString& Protocol() const { assert(valid_); return protocol_; }
const CString& Server() const { assert(valid_); return server_; }
unsigned short Port(unsigned short default_port = 0) const {
unsigned short Port() const {
assert(valid_);
return port_ ? port_ : default_port;
return port_ ? port_ : DefaultPort();
}
unsigned short DefaultPort() const;
const CString& Path() const { assert(valid_); return path_; }

CString Description() const;
Expand Down
37 changes: 35 additions & 2 deletions Lite/AddressDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//

#include "stdafx.h"
#include "Address.h"
#include "AddressDlg.h"
#include "AppConfig.h"
#include "MainFrm.h"
Expand All @@ -24,9 +25,23 @@ CAddressDlg::CAddressDlg(CWnd* pParent /*=NULL*/)
//}}AFX_DATA_INIT
}

CString CAddressDlg::GetFormattedAddress() const
{
// XXX: Duplicate code.
CAddress addr(address);
if (addr.IsValid())
return addr.URL();
if (port == 23 || port <= 0)
return address;
CString buf;
buf.Format("%s:%d", LPCTSTR(address), port);
return buf;
}

BEGIN_MESSAGE_MAP(CAddressDlg, CDialog)
//{{AFX_MSG_MAP(CAddressDlg)
// NOTE: the ClassWizard will add message map macros here
ON_CBN_EDITCHANGE(IDC_ADDRESS, &CAddressDlg::OnAddressChanged)
ON_CBN_SELCHANGE(IDC_ADDRESS, &CAddressDlg::OnAddressChanged)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

Expand Down Expand Up @@ -74,8 +89,26 @@ void CAddressDlg::DoDataExchange(CDataExchange* pDX)
{
//{{AFX_DATA_MAP(CAddressDlg)
DDX_Text(pDX, IDC_PORT, port);
DDV_MinMaxInt(pDX, port, 1, 65535);
DDV_MinMaxInt(pDX, port, 0, 65535);
DDX_Text(pDX, IDC_NAME, name);
//}}AFX_DATA_MAP
CDialog::DoDataExchange(pDX);
}

void CAddressDlg::OnAddressChanged()
{
// XXX: Duplicate code.
CEdit* port_field = (CEdit*)GetDlgItem(IDC_PORT);

CString addr_text;
GetDlgItemText(IDC_ADDRESS, addr_text);

CAddress addr(addr_text);
port_field->EnableWindow(!addr.IsValid());
if (addr.IsValid())
{
CString port_text;
port_text.Format("%d", addr.Port());
port_field->SetWindowText(port_text);
}
}
4 changes: 3 additions & 1 deletion Lite/AddressDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class CAddressDlg : public CDialog
BOOL OnInitDialog();
CAddressDlg(CWnd* pParent = NULL); // standard constructor

CString GetFormattedAddress() const;

// Dialog Data
//{{AFX_DATA(CAddressDlg)
enum { IDD = IDD_QUICKCONNECT };
Expand All @@ -42,7 +44,7 @@ class CAddressDlg : public CDialog

// Generated message map functions
//{{AFX_MSG(CAddressDlg)
// NOTE: the ClassWizard will add member functions here
afx_msg void OnAddressChanged();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
Expand Down
54 changes: 53 additions & 1 deletion Lite/ConnectPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "stdafx.h"
#include "pcman.h"
#include "Address.h"
#include "ConnectPage.h"

#ifdef _DEBUG
Expand All @@ -26,14 +27,44 @@ CConnectPage::~CConnectPage()
{
}

void CConnectPage::InitWithAddress(const CString& addr)
{
address = addr;
port = 0;

CAddress a(address);
if (!a.IsValid())
{
port = 23;
int pos = address.ReverseFind(':');
if (pos != -1)
{
port = (unsigned short)atoi(address.Mid(pos + 1));
address = address.Left(pos);
}
}
}

CString CConnectPage::GetFormattedAddress() const
{
CAddress addr(address);
if (addr.IsValid())
return addr.URL();
if (port == 23 || port <= 0)
return address;
CString buf;
buf.Format("%s:%d", LPCTSTR(address), port);
return buf;
}

void CConnectPage::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CConnectPage)
DDX_CBString(pDX, IDC_ADDRESS, address);
DDX_Text(pDX, IDC_NAME, name);
DDV_MinMaxInt(pDX, port, 1, 65535);
DDX_Text(pDX, IDC_PORT, port);
DDV_MinMaxInt(pDX, port, 0, 65535);
//}}AFX_DATA_MAP
if (name.IsEmpty())
name = address;
Expand All @@ -44,6 +75,8 @@ void CConnectPage::DoDataExchange(CDataExchange* pDX)

BEGIN_MESSAGE_MAP(CConnectPage, CPropertyPage)
//{{AFX_MSG_MAP(CConnectPage)
ON_CBN_EDITCHANGE(IDC_ADDRESS, &CConnectPage::OnAddressChanged)
ON_CBN_SELCHANGE(IDC_ADDRESS, &CConnectPage::OnAddressChanged)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

Expand All @@ -67,5 +100,24 @@ BOOL CConnectPage::OnInitDialog()
AppConfig.history.GetNext(pos);
}

OnAddressChanged();

return TRUE;
}

void CConnectPage::OnAddressChanged()
{
CEdit* port_field = (CEdit*) GetDlgItem(IDC_PORT);

CString addr_text;
GetDlgItemText(IDC_ADDRESS, addr_text);

CAddress addr(addr_text);
port_field->EnableWindow(!addr.IsValid());
if (addr.IsValid())
{
CString port_text;
port_text.Format("%d", addr.Port());
port_field->SetWindowText(port_text);
}
}
4 changes: 4 additions & 0 deletions Lite/ConnectPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class CConnectPage : public CPropertyPage
CConnectPage();
~CConnectPage();

void InitWithAddress(const CString& addr);
CString GetFormattedAddress() const;

// Dialog Data
//{{AFX_DATA(CConnectPage)
enum { IDD = IDD_CONNECT };
Expand All @@ -43,6 +46,7 @@ class CConnectPage : public CPropertyPage
// Generated message map functions
//{{AFX_MSG(CConnectPage)
afx_msg BOOL OnInitDialog();
afx_msg void OnAddressChanged();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()

Expand Down
13 changes: 2 additions & 11 deletions Lite/ListDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,7 @@ void CListDlg::OnEditSite()

CConnectPage page0;
CString name = page0.name = str.Left(pos);
page0.address = str.Mid(pos + SEPARATOR_LEN);
page0.port = 23;
pos = page0.address.ReverseFind(':');
if (pos != -1)
{
page0.port = (unsigned short)atoi(page0.address.Mid(pos + 1));
page0.address = page0.address.Left(pos);
}
page0.InitWithAddress(str.Mid(pos + SEPARATOR_LEN));

CPropertySheet dlg(IDS_SITE_SETTINGS);
CSitePage page1;
Expand All @@ -416,9 +409,7 @@ void CListDlg::OnEditSite()
{
sites.changed = TRUE;

CString text = page0.name + SEPARATOR + page0.address;
if (page0.port != 23 && page0.port > 0)
text.Format("%s%s%s:%d", LPCTSTR(page0.name), SEPARATOR, LPCTSTR(page0.address), page0.port);
CString text = page0.name + SEPARATOR + page0.GetFormattedAddress();

sites.SetItemText(item, text);
path1 = CSiteSettings::GetFilePath(path1);
Expand Down
13 changes: 4 additions & 9 deletions Lite/MainFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,11 @@ CString NameFromAddress(const CAddress& address)
const CString& protocol = address.Protocol();

unsigned short default_port;
if (protocol == "telnet" || protocol == "bbs")
default_port = 23;
else if (protocol == "ws")
default_port = 80;
else if (protocol == "wss")
default_port = 443;
else
if (protocol != "telnet" && protocol != "bbs" &&
protocol != "ws" && protocol != "wss")
return address.URL(); // Non-BBS. Show full URL.

if (address.Port(default_port) != default_port)
if (address.Port() != address.DefaultPort())
{
CString s;
s.Format(_T("%s:%d"), address.Server(), address.Port());
Expand Down Expand Up @@ -3872,7 +3867,7 @@ void CMainFrame::OnNewConn()
return;
}

CAddress address = ParseAddress(dlg.address);
CAddress address = ParseAddress(dlg.GetFormattedAddress());
if (!address.IsValid())
return;

Expand Down
4 changes: 2 additions & 2 deletions Lite/TermView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,7 @@ LRESULT CTermView::OnDNSLookupEnd(WPARAM found, LPARAM lparam)
sockaddr.sin_family = AF_INET;
// XXX: This path only serves telnet, but we might server others as well later.
// Don't hard code default port 23.
sockaddr.sin_port = htons((u_short)new_telnet->address.Port(23));
sockaddr.sin_port = htons((u_short)new_telnet->address.Port());
// sockaddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;

if (found)
Expand Down Expand Up @@ -2063,7 +2063,7 @@ void CTermView::ConnectSocket(CTelnetConn *new_telnet)
if (protocol == "telnet" ||
protocol == "bbs")
{
ConnectTcp(new_telnet, address.Server(), address.Port(23));
ConnectTcp(new_telnet, address.Server(), address.Port());
}
else if (protocol == "ws" ||
protocol == "wss")
Expand Down
7 changes: 3 additions & 4 deletions Lite/Websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,11 @@ bool CWebsocket::Connect()
cli_info.address = address_.Server();
cli_info.ssl_connection = (address_.Protocol() == "wss");

unsigned short default_port = cli_info.ssl_connection ? 443 : 80;
cli_info.port = address_.Port(default_port);
cli_info.port = address_.Port();
cli_info.path = address_.Path();

if (cli_info.port != default_port)
host_header_.Format(_T(":%d"), address_.Port());
if (cli_info.port != address_.DefaultPort())
host_header_.Format(_T(":%d"), cli_info.port);
host_header_ = address_.Server() + host_header_;
cli_info.host = host_header_;
cli_info.origin = "app://pcman";
Expand Down

0 comments on commit 0fce579

Please sign in to comment.