summaryrefslogtreecommitdiffabout
path: root/PumpKINDlg.cpp
authorMichael Krelin <hacker@klever.net>2004-11-03 21:50:28 (UTC)
committer Michael Krelin <hacker@klever.net>2004-11-03 21:50:28 (UTC)
commit5f552506513653f08acc6921b8c158489a7ebbbb (patch) (side-by-side diff)
tree74ab06cf90c488915b87f48e74359554521cbec9 /PumpKINDlg.cpp
parent4435ed85c3acb4df42135e009b133fb8e9d2f09c (diff)
downloadpumpkin-5f552506513653f08acc6921b8c158489a7ebbbb.zip
pumpkin-5f552506513653f08acc6921b8c158489a7ebbbb.tar.gz
pumpkin-5f552506513653f08acc6921b8c158489a7ebbbb.tar.bz2
Added dialog box in case we can't bind listening socket.
git-svn-id: http://svn.klever.net/kin/pumpkin/trunk@47 fe716a7a-6dde-0310-88d9-d003556173a8
Diffstat (limited to 'PumpKINDlg.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--PumpKINDlg.cpp1
1 files changed, 1 insertions, 0 deletions
diff --git a/PumpKINDlg.cpp b/PumpKINDlg.cpp
index 0b3bf41..fc24596 100644
--- a/PumpKINDlg.cpp
+++ b/PumpKINDlg.cpp
@@ -138,384 +138,385 @@ BEGIN_MESSAGE_MAP(CPumpKINDlg, CDialog)
ON_NOTIFY(LVN_INSERTITEM, IDC_CONNECTIONS, OnInsertitemConnections)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_CONNECTIONS, OnItemchangedConnections)
ON_BN_CLICKED(IDC_ABORT, OnAbort)
ON_WM_CLOSE()
ON_COMMAND(ID_TRAY_SHOWPUMPKINWINDOW, OnTrayShowpumpkinwindow)
ON_COMMAND(ID_TRAY_EXIT, OnTrayExit)
ON_COMMAND(ID_TRAY_ABOUTPUMPKIN, OnTrayAboutpumpkin)
ON_COMMAND(ID_TRAY_FETCHFILE, OnTrayFetchfile)
ON_COMMAND(ID_TRAY_HELP, OnTrayHelp)
ON_COMMAND(ID_TRAY_OPTIONS, OnTrayOptions)
ON_COMMAND(ID_TRAY_SENDFILE, OnTraySendfile)
ON_WM_WINDOWPOSCHANGING()
ON_LBN_SELCHANGE(IDC_LOG, OnSelchangeLog)
ON_COMMAND(ID_TRAY_OPENFILESFOLDER, OnTrayOpenfilesfolder)
ON_WM_DROPFILES()
ON_BN_CLICKED(ID_HELP, OnHelp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPumpKINDlg message handlers
BOOL CPumpKINDlg::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);
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
VERIFY(m_Retrier->Create(NULL,"PumpKIN-Retrier",WS_CHILD,CRect(0,0,0,0),this,0));
m_Images.Create(16,16,TRUE,2,1);
m_iRRQ = m_Images.Add(AfxGetApp()->LoadIcon(IDI_RRQ));
m_iWRQ = m_Images.Add(AfxGetApp()->LoadIcon(IDI_WRQ));
ASSERT(m_iRRQ>=0);
ASSERT(m_iWRQ>=0);
m_List.SetImageList(&m_Images,LVSIL_NORMAL);
m_List.SetImageList(&m_Images,LVSIL_SMALL);
m_List.SetImageList(&m_Images,LVSIL_STATE);
m_List.SetTextColor(RGB(255,255,0)); // Yellow
m_List.SetTextBkColor(RGB(12,167,0)); // Green
m_List.SetBkColor(RGB(12,167,0)); // Green
CRect listrc;
m_List.GetClientRect(&listrc);
m_List.InsertColumn(0,"File",LVCFMT_LEFT,listrc.Width()-((listrc.Width()/7)*3+listrc.Width()*2/7),subitemFile);
m_List.InsertColumn(1,"type",LVCFMT_CENTER,listrc.Width()/7,subitemType);
m_List.InsertColumn(2,"peer",LVCFMT_LEFT,listrc.Width()*2/7,subitemPeer);
m_List.InsertColumn(3,"ACK",LVCFMT_RIGHT,listrc.Width()/7,subitemBytes);
m_List.InsertColumn(4,"tsize",LVCFMT_RIGHT,listrc.Width()/7,subitemTSize);
LogLine(IDS_LOG_START);
SetupButtons();
CRect rc, drc;
GetWindowRect(rc);
GetDesktopWindow()->GetWindowRect(drc);
SetWindowPos(NULL,drc.right-6-rc.Width(),6,0,0,SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOZORDER);
if(m_bShown)
ShowWindow(SW_SHOW);
else
ShowWindow(SW_HIDE);
// CG: The following block was added by the ToolTips component.
{
// Create the ToolTip control.
m_tooltip.Create(this);
m_tooltip.Activate(TRUE);
m_tooltip.AddTool(&m_List,IDC_CONNECTIONS);
m_tooltip.AddTool(GetDlgItem(IDC_PUT),IDC_PUT);
m_tooltip.AddTool(GetDlgItem(IDC_GET),IDC_GET);
m_tooltip.AddTool(&m_AbortCtl,IDC_ABORT);
m_tooltip.AddTool(GetDlgItem(IDC_OPTIONS),IDC_OPTIONS);
m_tooltip.AddTool(GetDlgItem(IDC_EXIT),IDC_EXIT);
m_tooltip.AddTool(GetDlgItem(ID_HELP),ID_HELP);
m_tooltip.AddTool(GetDlgItem(IDC_LOG),IDC_LOG);
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CPumpKINDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CPumpKINDlg::OnDestroy()
{
SaveSettings();
NOTIFYICONDATA nid;
memset(&nid,0,sizeof(nid));
nid.cbSize=sizeof(nid);
nid.hWnd=m_Trayer->m_hWnd;
nid.uID=IDC_TRAYICON;
nid.uFlags=0;
VERIFY(Shell_NotifyIcon(NIM_DELETE,&nid));
WinHelp(0L, HELP_QUIT);
CDialog::OnDestroy();
POSITION p = m_LogTimes.GetStartPosition();
while(p){
CTime *t,*tt;
m_LogTimes.GetNextAssoc(p,t,tt);
ASSERT(t && tt && t==tt);
delete t;
}
// *** Abort and cleanup transfers
m_LogTimes.RemoveAll();
}
// 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 CPumpKINDlg::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
{
CPaintDC pDC(this);
CDC bmpDC;
bmpDC.CreateCompatibleDC(&pDC);
bmpDC.SelectObject(&m_bmpBack);
CRect rc;
GetClientRect(&rc);
for(int x=-m_bitmapBack.bmWidth*2/4;x<rc.Width();x+=m_bitmapBack.bmWidth)
for(int y=-m_bitmapBack.bmHeight*2/4;y<rc.Height();y+=m_bitmapBack.bmHeight)
pDC.BitBlt(x,y,m_bitmapBack.bmWidth,m_bitmapBack.bmHeight,&bmpDC,0,0,SRCCOPY);
bmpDC.DeleteDC();
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CPumpKINDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
int CPumpKINDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
m_Listener.m_Daddy=this;
if(!m_Listener.Create(m_ListenPort,SOCK_DGRAM)){
TRACE0("Failed to create socket\n");
+ AfxMessageBox(IDS_BOX_CANTBIND,MB_OK|MB_ICONEXCLAMATION);
return -1;
}
if(!m_Trayer->Create(NULL,"PumpKIN TrayIcon",WS_CHILD,CRect(0,0,0,0),this,0)){
TRACE0("Failed to create trayer\n");
return -1;
}
NOTIFYICONDATA nid;
memset(&nid,0,sizeof(nid));
nid.cbSize=sizeof(nid);
nid.hWnd=m_Trayer->m_hWnd;
nid.uID=IDC_TRAYICON;
nid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
nid.uCallbackMessage=WM_TRAYICON;
nid.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
// *** Load from resource
strcpy(nid.szTip,"PumpKIN");
VERIFY(Shell_NotifyIcon(NIM_ADD,&nid));
return 0;
}
void CListenSocket::OnReceive(int nErrorCode)
{
ASSERT(m_Daddy);
if(nErrorCode){
m_Daddy->LogLine(IDS_LOG_LISTENRECEIVEERROR);
return;
}
DWORD fionread = 0;
VERIFY(IOCtl(FIONREAD,&fionread)); // *** Do some checking on the value acquired
tftp *tftpRQ = tftp::Allocate(fionread);
ASSERT(tftpRQ);
SOCKADDR_IN sin;
if(!tftpRQ->Receive(this,fionread,&sin)){
m_Daddy->LogLine(IDS_LOG_LISTENACCEPTERROR);
delete tftpRQ;
return;
}
#ifndef NDEBUG
CString tmp;
tmp.Format("%u - %s - %u\n",tftpRQ->Opcode(),inet_ntoa(sin.sin_addr),sin.sin_port);
TRACE0(tmp);
#endif
POSITION p = m_Daddy->m_Xfers.GetStartPosition();
while(p){
SOCKET key;
CXferSocket *sock;
m_Daddy->m_Xfers.GetNextAssoc(p,key,sock);
ASSERT(sock);
if(sock->m_Peer.sin_addr.s_addr==sin.sin_addr.s_addr && sock->m_Peer.sin_port==sin.sin_port){
TRACE0("Ignoring request which we are already processing\n");
delete tftpRQ;
return;
}
}
switch(tftpRQ->Opcode()){
case tftp::opRRQ:
// Read Request
{
CString tmp;
tmp.Format(IDS_LOG_RRQSERVE,tftpRQ->rqFileName(),tftpRQ->rqType(),inet_ntoa(sin.sin_addr));
m_Daddy->LogLine(tmp);
CRRQSocket *s = new CRRQSocket(m_Daddy,tftpRQ->rqFileName(),tftpRQ->rqType(),&sin);
ASSERT(s);
tftpRQ->GetOptions(&s->m_Options);
if(!s->Create())
s->Destroy(FALSE);
}
break;
case tftp::opWRQ:
// Write Request
{
CString tmp;
tmp.Format(IDS_LOG_WRQSERVE,tftpRQ->rqFileName(),tftpRQ->rqType(),inet_ntoa(sin.sin_addr));
m_Daddy->LogLine(tmp);
CWRQSocket *s = new CWRQSocket(m_Daddy,tftpRQ->rqFileName(),tftpRQ->rqType(),&sin);
ASSERT(s);
tftpRQ->GetOptions(&s->m_Options);
if(!s->Create(NULL,NULL))
s->Destroy(FALSE);
}
break;
default:
m_Daddy->LogLine(IDS_LOG_LISTENOPCODE);
delete tftpRQ;
return;
}
delete tftpRQ;
}
BOOL tftp::Receive(CAsyncSocket* socket,UINT maxLength,SOCKADDR_IN *sin)
{
ASSERT(socket);
int saddrLen = sizeof(SOCKADDR_IN);
length = sin ?
socket->ReceiveFrom(udpBase(),maxLength,(SOCKADDR*)sin,&saddrLen)
:
socket->Receive(udpBase(),maxLength)
;
if(!length)
return FALSE;
if(length==(tftpLength)SOCKET_ERROR)
return FALSE;
return TRUE;
}
UINT tftp::Opcode()
{
return REVERSEBYTES(opcode);
}
CString tftp::rqFileName()
{
ASSERT(length);
ASSERT(Opcode()==opRRQ || Opcode()==opWRQ);
CString rv;
if(memchr(&data.m_RQ.data,0,length-sizeof(opcode)))
rv = (LPCTSTR)data.m_RQ.data;
return rv;
}
CString tftp::rqType()
{
ASSERT(length);
ASSERT(Opcode()==opRRQ || Opcode()==opWRQ);
CString rv;
char *tmp = (char*)memchr(&data.m_RQ.data,0,length-sizeof(opcode));
if(tmp++)
rv = (LPCTSTR)tmp;
return rv;
}
UINT tftp::GetOptions(tftp::tftpOptions* ops)
{
ASSERT(length);
ASSERT(Opcode()==opRRQ || Opcode()==opWRQ || Opcode()==opOACK);
ASSERT(ops);
tftpOptions& o = *ops;
LPSTR base = (LPSTR)&data.m_RQ.data;
UINT basePtr = 0;
if(Opcode()==opRRQ || Opcode()==opWRQ){
base = (LPSTR)memchr(&data.m_RQ.data,0,length-sizeof(opcode));
if(!base)
return 0;
base++;
basePtr = (base-(LPSTR)&data.m_RQ.data);
base = (LPSTR)memchr(base,0,length-basePtr);
if(!base)
return 0;
base++;
basePtr = (base-(LPSTR)&data.m_RQ.data);
}
ops->RemoveAll();
UINT rv = 0;
while(basePtr<(length-sizeof(opcode))){
CString onam = (LPSTR)&data.m_RQ.data[basePtr];
basePtr+=onam.GetLength()+1;
CString oval = (LPSTR)&data.m_RQ.data[basePtr];
basePtr+=oval.GetLength()+1;
onam.MakeLower();
o[onam]=oval;
rv++;
}
return rv;
}
tftp::tftp()
{
length=0;
}
void CXferSocket::OnSend(int nErrorCode)
{
if(nErrorCode){
ASSERT(m_Daddy);
m_Daddy->LogLine(IDS_LOG_XFERSEND);
return;
}
if(!m_Queue.IsEmpty()){
tftp *p = m_Queue.GetHead();
ASSERT(p);
m_Queue.RemoveHead();
if(!p->Send(this,&m_Peer)){
ASSERT(m_Daddy);
m_Daddy->LogLine(IDS_LOG_XFERUDPSEND);
}
delete p;
}
DoSelect();