author | Michael Krelin <hacker@klever.net> | 2004-11-03 21:50:28 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2004-11-03 21:50:28 (UTC) |
commit | 5f552506513653f08acc6921b8c158489a7ebbbb (patch) (unidiff) | |
tree | 74ab06cf90c488915b87f48e74359554521cbec9 /PumpKINDlg.cpp | |
parent | 4435ed85c3acb4df42135e009b133fb8e9d2f09c (diff) | |
download | pumpkin-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
-rw-r--r-- | PumpKINDlg.cpp | 1 |
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) | |||
138 | ON_NOTIFY(LVN_INSERTITEM, IDC_CONNECTIONS, OnInsertitemConnections) | 138 | ON_NOTIFY(LVN_INSERTITEM, IDC_CONNECTIONS, OnInsertitemConnections) |
139 | ON_NOTIFY(LVN_ITEMCHANGED, IDC_CONNECTIONS, OnItemchangedConnections) | 139 | ON_NOTIFY(LVN_ITEMCHANGED, IDC_CONNECTIONS, OnItemchangedConnections) |
140 | ON_BN_CLICKED(IDC_ABORT, OnAbort) | 140 | ON_BN_CLICKED(IDC_ABORT, OnAbort) |
141 | ON_WM_CLOSE() | 141 | ON_WM_CLOSE() |
142 | ON_COMMAND(ID_TRAY_SHOWPUMPKINWINDOW, OnTrayShowpumpkinwindow) | 142 | ON_COMMAND(ID_TRAY_SHOWPUMPKINWINDOW, OnTrayShowpumpkinwindow) |
143 | ON_COMMAND(ID_TRAY_EXIT, OnTrayExit) | 143 | ON_COMMAND(ID_TRAY_EXIT, OnTrayExit) |
144 | ON_COMMAND(ID_TRAY_ABOUTPUMPKIN, OnTrayAboutpumpkin) | 144 | ON_COMMAND(ID_TRAY_ABOUTPUMPKIN, OnTrayAboutpumpkin) |
145 | ON_COMMAND(ID_TRAY_FETCHFILE, OnTrayFetchfile) | 145 | ON_COMMAND(ID_TRAY_FETCHFILE, OnTrayFetchfile) |
146 | ON_COMMAND(ID_TRAY_HELP, OnTrayHelp) | 146 | ON_COMMAND(ID_TRAY_HELP, OnTrayHelp) |
147 | ON_COMMAND(ID_TRAY_OPTIONS, OnTrayOptions) | 147 | ON_COMMAND(ID_TRAY_OPTIONS, OnTrayOptions) |
148 | ON_COMMAND(ID_TRAY_SENDFILE, OnTraySendfile) | 148 | ON_COMMAND(ID_TRAY_SENDFILE, OnTraySendfile) |
149 | ON_WM_WINDOWPOSCHANGING() | 149 | ON_WM_WINDOWPOSCHANGING() |
150 | ON_LBN_SELCHANGE(IDC_LOG, OnSelchangeLog) | 150 | ON_LBN_SELCHANGE(IDC_LOG, OnSelchangeLog) |
151 | ON_COMMAND(ID_TRAY_OPENFILESFOLDER, OnTrayOpenfilesfolder) | 151 | ON_COMMAND(ID_TRAY_OPENFILESFOLDER, OnTrayOpenfilesfolder) |
152 | ON_WM_DROPFILES() | 152 | ON_WM_DROPFILES() |
153 | ON_BN_CLICKED(ID_HELP, OnHelp) | 153 | ON_BN_CLICKED(ID_HELP, OnHelp) |
154 | //}}AFX_MSG_MAP | 154 | //}}AFX_MSG_MAP |
155 | END_MESSAGE_MAP() | 155 | END_MESSAGE_MAP() |
156 | 156 | ||
157 | ///////////////////////////////////////////////////////////////////////////// | 157 | ///////////////////////////////////////////////////////////////////////////// |
158 | // CPumpKINDlg message handlers | 158 | // CPumpKINDlg message handlers |
159 | 159 | ||
160 | BOOL CPumpKINDlg::OnInitDialog() | 160 | BOOL CPumpKINDlg::OnInitDialog() |
161 | { | 161 | { |
162 | CDialog::OnInitDialog(); | 162 | CDialog::OnInitDialog(); |
163 | 163 | ||
164 | // Add "About..." menu item to system menu. | 164 | // Add "About..." menu item to system menu. |
165 | 165 | ||
166 | // IDM_ABOUTBOX must be in the system command range. | 166 | // IDM_ABOUTBOX must be in the system command range. |
167 | ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); | 167 | ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); |
168 | ASSERT(IDM_ABOUTBOX < 0xF000); | 168 | ASSERT(IDM_ABOUTBOX < 0xF000); |
169 | 169 | ||
170 | CMenu* pSysMenu = GetSystemMenu(FALSE); | 170 | CMenu* pSysMenu = GetSystemMenu(FALSE); |
171 | CString strAboutMenu; | 171 | CString strAboutMenu; |
172 | strAboutMenu.LoadString(IDS_ABOUTBOX); | 172 | strAboutMenu.LoadString(IDS_ABOUTBOX); |
173 | if (!strAboutMenu.IsEmpty()) | 173 | if (!strAboutMenu.IsEmpty()) |
174 | { | 174 | { |
175 | pSysMenu->AppendMenu(MF_SEPARATOR); | 175 | pSysMenu->AppendMenu(MF_SEPARATOR); |
176 | pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); | 176 | pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); |
177 | } | 177 | } |
178 | 178 | ||
179 | // Set the icon for this dialog. The framework does this automatically | 179 | // Set the icon for this dialog. The framework does this automatically |
180 | // when the application's main window is not a dialog | 180 | // when the application's main window is not a dialog |
181 | SetIcon(m_hIcon, TRUE); // Set big icon | 181 | SetIcon(m_hIcon, TRUE); // Set big icon |
182 | SetIcon(m_hIcon, FALSE); // Set small icon | 182 | SetIcon(m_hIcon, FALSE); // Set small icon |
183 | 183 | ||
184 | VERIFY(m_Retrier->Create(NULL,"PumpKIN-Retrier",WS_CHILD,CRect(0,0,0,0),this,0)); | 184 | VERIFY(m_Retrier->Create(NULL,"PumpKIN-Retrier",WS_CHILD,CRect(0,0,0,0),this,0)); |
185 | 185 | ||
186 | 186 | ||
187 | m_Images.Create(16,16,TRUE,2,1); | 187 | m_Images.Create(16,16,TRUE,2,1); |
188 | m_iRRQ = m_Images.Add(AfxGetApp()->LoadIcon(IDI_RRQ)); | 188 | m_iRRQ = m_Images.Add(AfxGetApp()->LoadIcon(IDI_RRQ)); |
189 | m_iWRQ = m_Images.Add(AfxGetApp()->LoadIcon(IDI_WRQ)); | 189 | m_iWRQ = m_Images.Add(AfxGetApp()->LoadIcon(IDI_WRQ)); |
190 | ASSERT(m_iRRQ>=0); | 190 | ASSERT(m_iRRQ>=0); |
191 | ASSERT(m_iWRQ>=0); | 191 | ASSERT(m_iWRQ>=0); |
192 | m_List.SetImageList(&m_Images,LVSIL_NORMAL); | 192 | m_List.SetImageList(&m_Images,LVSIL_NORMAL); |
193 | m_List.SetImageList(&m_Images,LVSIL_SMALL); | 193 | m_List.SetImageList(&m_Images,LVSIL_SMALL); |
194 | m_List.SetImageList(&m_Images,LVSIL_STATE); | 194 | m_List.SetImageList(&m_Images,LVSIL_STATE); |
195 | m_List.SetTextColor(RGB(255,255,0));// Yellow | 195 | m_List.SetTextColor(RGB(255,255,0));// Yellow |
196 | m_List.SetTextBkColor(RGB(12,167,0));// Green | 196 | m_List.SetTextBkColor(RGB(12,167,0));// Green |
197 | m_List.SetBkColor(RGB(12,167,0));// Green | 197 | m_List.SetBkColor(RGB(12,167,0));// Green |
198 | CRect listrc; | 198 | CRect listrc; |
199 | m_List.GetClientRect(&listrc); | 199 | m_List.GetClientRect(&listrc); |
200 | m_List.InsertColumn(0,"File",LVCFMT_LEFT,listrc.Width()-((listrc.Width()/7)*3+listrc.Width()*2/7),subitemFile); | 200 | m_List.InsertColumn(0,"File",LVCFMT_LEFT,listrc.Width()-((listrc.Width()/7)*3+listrc.Width()*2/7),subitemFile); |
201 | m_List.InsertColumn(1,"type",LVCFMT_CENTER,listrc.Width()/7,subitemType); | 201 | m_List.InsertColumn(1,"type",LVCFMT_CENTER,listrc.Width()/7,subitemType); |
202 | m_List.InsertColumn(2,"peer",LVCFMT_LEFT,listrc.Width()*2/7,subitemPeer); | 202 | m_List.InsertColumn(2,"peer",LVCFMT_LEFT,listrc.Width()*2/7,subitemPeer); |
203 | m_List.InsertColumn(3,"ACK",LVCFMT_RIGHT,listrc.Width()/7,subitemBytes); | 203 | m_List.InsertColumn(3,"ACK",LVCFMT_RIGHT,listrc.Width()/7,subitemBytes); |
204 | m_List.InsertColumn(4,"tsize",LVCFMT_RIGHT,listrc.Width()/7,subitemTSize); | 204 | m_List.InsertColumn(4,"tsize",LVCFMT_RIGHT,listrc.Width()/7,subitemTSize); |
205 | 205 | ||
206 | LogLine(IDS_LOG_START); | 206 | LogLine(IDS_LOG_START); |
207 | 207 | ||
208 | SetupButtons(); | 208 | SetupButtons(); |
209 | 209 | ||
210 | CRect rc, drc; | 210 | CRect rc, drc; |
211 | GetWindowRect(rc); | 211 | GetWindowRect(rc); |
212 | GetDesktopWindow()->GetWindowRect(drc); | 212 | GetDesktopWindow()->GetWindowRect(drc); |
213 | SetWindowPos(NULL,drc.right-6-rc.Width(),6,0,0,SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOZORDER); | 213 | SetWindowPos(NULL,drc.right-6-rc.Width(),6,0,0,SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOZORDER); |
214 | 214 | ||
215 | if(m_bShown) | 215 | if(m_bShown) |
216 | ShowWindow(SW_SHOW); | 216 | ShowWindow(SW_SHOW); |
217 | else | 217 | else |
218 | ShowWindow(SW_HIDE); | 218 | ShowWindow(SW_HIDE); |
219 | 219 | ||
220 | // CG: The following block was added by the ToolTips component. | 220 | // CG: The following block was added by the ToolTips component. |
221 | { | 221 | { |
222 | // Create the ToolTip control. | 222 | // Create the ToolTip control. |
223 | m_tooltip.Create(this); | 223 | m_tooltip.Create(this); |
224 | m_tooltip.Activate(TRUE); | 224 | m_tooltip.Activate(TRUE); |
225 | 225 | ||
226 | m_tooltip.AddTool(&m_List,IDC_CONNECTIONS); | 226 | m_tooltip.AddTool(&m_List,IDC_CONNECTIONS); |
227 | m_tooltip.AddTool(GetDlgItem(IDC_PUT),IDC_PUT); | 227 | m_tooltip.AddTool(GetDlgItem(IDC_PUT),IDC_PUT); |
228 | m_tooltip.AddTool(GetDlgItem(IDC_GET),IDC_GET); | 228 | m_tooltip.AddTool(GetDlgItem(IDC_GET),IDC_GET); |
229 | m_tooltip.AddTool(&m_AbortCtl,IDC_ABORT); | 229 | m_tooltip.AddTool(&m_AbortCtl,IDC_ABORT); |
230 | m_tooltip.AddTool(GetDlgItem(IDC_OPTIONS),IDC_OPTIONS); | 230 | m_tooltip.AddTool(GetDlgItem(IDC_OPTIONS),IDC_OPTIONS); |
231 | m_tooltip.AddTool(GetDlgItem(IDC_EXIT),IDC_EXIT); | 231 | m_tooltip.AddTool(GetDlgItem(IDC_EXIT),IDC_EXIT); |
232 | m_tooltip.AddTool(GetDlgItem(ID_HELP),ID_HELP); | 232 | m_tooltip.AddTool(GetDlgItem(ID_HELP),ID_HELP); |
233 | m_tooltip.AddTool(GetDlgItem(IDC_LOG),IDC_LOG); | 233 | m_tooltip.AddTool(GetDlgItem(IDC_LOG),IDC_LOG); |
234 | } | 234 | } |
235 | return TRUE; // return TRUE unless you set the focus to a control | 235 | return TRUE; // return TRUE unless you set the focus to a control |
236 | } | 236 | } |
237 | 237 | ||
238 | void CPumpKINDlg::OnSysCommand(UINT nID, LPARAM lParam) | 238 | void CPumpKINDlg::OnSysCommand(UINT nID, LPARAM lParam) |
239 | { | 239 | { |
240 | if ((nID & 0xFFF0) == IDM_ABOUTBOX) | 240 | if ((nID & 0xFFF0) == IDM_ABOUTBOX) |
241 | { | 241 | { |
242 | CAboutDlg dlgAbout; | 242 | CAboutDlg dlgAbout; |
243 | dlgAbout.DoModal(); | 243 | dlgAbout.DoModal(); |
244 | } | 244 | } |
245 | else | 245 | else |
246 | { | 246 | { |
247 | CDialog::OnSysCommand(nID, lParam); | 247 | CDialog::OnSysCommand(nID, lParam); |
248 | } | 248 | } |
249 | } | 249 | } |
250 | 250 | ||
251 | void CPumpKINDlg::OnDestroy() | 251 | void CPumpKINDlg::OnDestroy() |
252 | { | 252 | { |
253 | SaveSettings(); | 253 | SaveSettings(); |
254 | 254 | ||
255 | NOTIFYICONDATA nid; | 255 | NOTIFYICONDATA nid; |
256 | memset(&nid,0,sizeof(nid)); | 256 | memset(&nid,0,sizeof(nid)); |
257 | nid.cbSize=sizeof(nid); | 257 | nid.cbSize=sizeof(nid); |
258 | nid.hWnd=m_Trayer->m_hWnd; | 258 | nid.hWnd=m_Trayer->m_hWnd; |
259 | nid.uID=IDC_TRAYICON; | 259 | nid.uID=IDC_TRAYICON; |
260 | nid.uFlags=0; | 260 | nid.uFlags=0; |
261 | VERIFY(Shell_NotifyIcon(NIM_DELETE,&nid)); | 261 | VERIFY(Shell_NotifyIcon(NIM_DELETE,&nid)); |
262 | 262 | ||
263 | WinHelp(0L, HELP_QUIT); | 263 | WinHelp(0L, HELP_QUIT); |
264 | CDialog::OnDestroy(); | 264 | CDialog::OnDestroy(); |
265 | POSITION p = m_LogTimes.GetStartPosition(); | 265 | POSITION p = m_LogTimes.GetStartPosition(); |
266 | while(p){ | 266 | while(p){ |
267 | CTime *t,*tt; | 267 | CTime *t,*tt; |
268 | m_LogTimes.GetNextAssoc(p,t,tt); | 268 | m_LogTimes.GetNextAssoc(p,t,tt); |
269 | ASSERT(t && tt && t==tt); | 269 | ASSERT(t && tt && t==tt); |
270 | delete t; | 270 | delete t; |
271 | } | 271 | } |
272 | // *** Abort and cleanup transfers | 272 | // *** Abort and cleanup transfers |
273 | m_LogTimes.RemoveAll(); | 273 | m_LogTimes.RemoveAll(); |
274 | } | 274 | } |
275 | 275 | ||
276 | // If you add a minimize button to your dialog, you will need the code below | 276 | // If you add a minimize button to your dialog, you will need the code below |
277 | // to draw the icon. For MFC applications using the document/view model, | 277 | // to draw the icon. For MFC applications using the document/view model, |
278 | // this is automatically done for you by the framework. | 278 | // this is automatically done for you by the framework. |
279 | 279 | ||
280 | void CPumpKINDlg::OnPaint() | 280 | void CPumpKINDlg::OnPaint() |
281 | { | 281 | { |
282 | if (IsIconic()) | 282 | if (IsIconic()) |
283 | { | 283 | { |
284 | CPaintDC dc(this); // device context for painting | 284 | CPaintDC dc(this); // device context for painting |
285 | 285 | ||
286 | SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); | 286 | SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); |
287 | 287 | ||
288 | // Center icon in client rectangle | 288 | // Center icon in client rectangle |
289 | int cxIcon = GetSystemMetrics(SM_CXICON); | 289 | int cxIcon = GetSystemMetrics(SM_CXICON); |
290 | int cyIcon = GetSystemMetrics(SM_CYICON); | 290 | int cyIcon = GetSystemMetrics(SM_CYICON); |
291 | CRect rect; | 291 | CRect rect; |
292 | GetClientRect(&rect); | 292 | GetClientRect(&rect); |
293 | int x = (rect.Width() - cxIcon + 1) / 2; | 293 | int x = (rect.Width() - cxIcon + 1) / 2; |
294 | int y = (rect.Height() - cyIcon + 1) / 2; | 294 | int y = (rect.Height() - cyIcon + 1) / 2; |
295 | 295 | ||
296 | // Draw the icon | 296 | // Draw the icon |
297 | dc.DrawIcon(x, y, m_hIcon); | 297 | dc.DrawIcon(x, y, m_hIcon); |
298 | } | 298 | } |
299 | else | 299 | else |
300 | { | 300 | { |
301 | CPaintDC pDC(this); | 301 | CPaintDC pDC(this); |
302 | CDC bmpDC; | 302 | CDC bmpDC; |
303 | bmpDC.CreateCompatibleDC(&pDC); | 303 | bmpDC.CreateCompatibleDC(&pDC); |
304 | bmpDC.SelectObject(&m_bmpBack); | 304 | bmpDC.SelectObject(&m_bmpBack); |
305 | CRect rc; | 305 | CRect rc; |
306 | GetClientRect(&rc); | 306 | GetClientRect(&rc); |
307 | for(int x=-m_bitmapBack.bmWidth*2/4;x<rc.Width();x+=m_bitmapBack.bmWidth) | 307 | for(int x=-m_bitmapBack.bmWidth*2/4;x<rc.Width();x+=m_bitmapBack.bmWidth) |
308 | for(int y=-m_bitmapBack.bmHeight*2/4;y<rc.Height();y+=m_bitmapBack.bmHeight) | 308 | for(int y=-m_bitmapBack.bmHeight*2/4;y<rc.Height();y+=m_bitmapBack.bmHeight) |
309 | pDC.BitBlt(x,y,m_bitmapBack.bmWidth,m_bitmapBack.bmHeight,&bmpDC,0,0,SRCCOPY); | 309 | pDC.BitBlt(x,y,m_bitmapBack.bmWidth,m_bitmapBack.bmHeight,&bmpDC,0,0,SRCCOPY); |
310 | bmpDC.DeleteDC(); | 310 | bmpDC.DeleteDC(); |
311 | CDialog::OnPaint(); | 311 | CDialog::OnPaint(); |
312 | } | 312 | } |
313 | } | 313 | } |
314 | 314 | ||
315 | // The system calls this to obtain the cursor to display while the user drags | 315 | // The system calls this to obtain the cursor to display while the user drags |
316 | // the minimized window. | 316 | // the minimized window. |
317 | HCURSOR CPumpKINDlg::OnQueryDragIcon() | 317 | HCURSOR CPumpKINDlg::OnQueryDragIcon() |
318 | { | 318 | { |
319 | return (HCURSOR) m_hIcon; | 319 | return (HCURSOR) m_hIcon; |
320 | } | 320 | } |
321 | 321 | ||
322 | int CPumpKINDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) | 322 | int CPumpKINDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) |
323 | { | 323 | { |
324 | if (CDialog::OnCreate(lpCreateStruct) == -1) | 324 | if (CDialog::OnCreate(lpCreateStruct) == -1) |
325 | return -1; | 325 | return -1; |
326 | 326 | ||
327 | m_Listener.m_Daddy=this; | 327 | m_Listener.m_Daddy=this; |
328 | if(!m_Listener.Create(m_ListenPort,SOCK_DGRAM)){ | 328 | if(!m_Listener.Create(m_ListenPort,SOCK_DGRAM)){ |
329 | TRACE0("Failed to create socket\n"); | 329 | TRACE0("Failed to create socket\n"); |
330 | AfxMessageBox(IDS_BOX_CANTBIND,MB_OK|MB_ICONEXCLAMATION); | ||
330 | return -1; | 331 | return -1; |
331 | } | 332 | } |
332 | 333 | ||
333 | if(!m_Trayer->Create(NULL,"PumpKIN TrayIcon",WS_CHILD,CRect(0,0,0,0),this,0)){ | 334 | if(!m_Trayer->Create(NULL,"PumpKIN TrayIcon",WS_CHILD,CRect(0,0,0,0),this,0)){ |
334 | TRACE0("Failed to create trayer\n"); | 335 | TRACE0("Failed to create trayer\n"); |
335 | return -1; | 336 | return -1; |
336 | } | 337 | } |
337 | 338 | ||
338 | NOTIFYICONDATA nid; | 339 | NOTIFYICONDATA nid; |
339 | memset(&nid,0,sizeof(nid)); | 340 | memset(&nid,0,sizeof(nid)); |
340 | nid.cbSize=sizeof(nid); | 341 | nid.cbSize=sizeof(nid); |
341 | nid.hWnd=m_Trayer->m_hWnd; | 342 | nid.hWnd=m_Trayer->m_hWnd; |
342 | nid.uID=IDC_TRAYICON; | 343 | nid.uID=IDC_TRAYICON; |
343 | nid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP; | 344 | nid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP; |
344 | nid.uCallbackMessage=WM_TRAYICON; | 345 | nid.uCallbackMessage=WM_TRAYICON; |
345 | nid.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME); | 346 | nid.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME); |
346 | // *** Load from resource | 347 | // *** Load from resource |
347 | strcpy(nid.szTip,"PumpKIN"); | 348 | strcpy(nid.szTip,"PumpKIN"); |
348 | VERIFY(Shell_NotifyIcon(NIM_ADD,&nid)); | 349 | VERIFY(Shell_NotifyIcon(NIM_ADD,&nid)); |
349 | 350 | ||
350 | return 0; | 351 | return 0; |
351 | } | 352 | } |
352 | 353 | ||
353 | void CListenSocket::OnReceive(int nErrorCode) | 354 | void CListenSocket::OnReceive(int nErrorCode) |
354 | { | 355 | { |
355 | ASSERT(m_Daddy); | 356 | ASSERT(m_Daddy); |
356 | if(nErrorCode){ | 357 | if(nErrorCode){ |
357 | m_Daddy->LogLine(IDS_LOG_LISTENRECEIVEERROR); | 358 | m_Daddy->LogLine(IDS_LOG_LISTENRECEIVEERROR); |
358 | return; | 359 | return; |
359 | } | 360 | } |
360 | DWORD fionread = 0; | 361 | DWORD fionread = 0; |
361 | VERIFY(IOCtl(FIONREAD,&fionread));// *** Do some checking on the value acquired | 362 | VERIFY(IOCtl(FIONREAD,&fionread));// *** Do some checking on the value acquired |
362 | tftp *tftpRQ = tftp::Allocate(fionread); | 363 | tftp *tftpRQ = tftp::Allocate(fionread); |
363 | ASSERT(tftpRQ); | 364 | ASSERT(tftpRQ); |
364 | SOCKADDR_IN sin; | 365 | SOCKADDR_IN sin; |
365 | if(!tftpRQ->Receive(this,fionread,&sin)){ | 366 | if(!tftpRQ->Receive(this,fionread,&sin)){ |
366 | m_Daddy->LogLine(IDS_LOG_LISTENACCEPTERROR); | 367 | m_Daddy->LogLine(IDS_LOG_LISTENACCEPTERROR); |
367 | delete tftpRQ; | 368 | delete tftpRQ; |
368 | return; | 369 | return; |
369 | } | 370 | } |
370 | #ifndefNDEBUG | 371 | #ifndefNDEBUG |
371 | CString tmp; | 372 | CString tmp; |
372 | tmp.Format("%u - %s - %u\n",tftpRQ->Opcode(),inet_ntoa(sin.sin_addr),sin.sin_port); | 373 | tmp.Format("%u - %s - %u\n",tftpRQ->Opcode(),inet_ntoa(sin.sin_addr),sin.sin_port); |
373 | TRACE0(tmp); | 374 | TRACE0(tmp); |
374 | #endif | 375 | #endif |
375 | POSITION p = m_Daddy->m_Xfers.GetStartPosition(); | 376 | POSITION p = m_Daddy->m_Xfers.GetStartPosition(); |
376 | while(p){ | 377 | while(p){ |
377 | SOCKET key; | 378 | SOCKET key; |
378 | CXferSocket *sock; | 379 | CXferSocket *sock; |
379 | m_Daddy->m_Xfers.GetNextAssoc(p,key,sock); | 380 | m_Daddy->m_Xfers.GetNextAssoc(p,key,sock); |
380 | ASSERT(sock); | 381 | ASSERT(sock); |
381 | if(sock->m_Peer.sin_addr.s_addr==sin.sin_addr.s_addr && sock->m_Peer.sin_port==sin.sin_port){ | 382 | if(sock->m_Peer.sin_addr.s_addr==sin.sin_addr.s_addr && sock->m_Peer.sin_port==sin.sin_port){ |
382 | TRACE0("Ignoring request which we are already processing\n"); | 383 | TRACE0("Ignoring request which we are already processing\n"); |
383 | delete tftpRQ; | 384 | delete tftpRQ; |
384 | return; | 385 | return; |
385 | } | 386 | } |
386 | } | 387 | } |
387 | switch(tftpRQ->Opcode()){ | 388 | switch(tftpRQ->Opcode()){ |
388 | case tftp::opRRQ: | 389 | case tftp::opRRQ: |
389 | // Read Request | 390 | // Read Request |
390 | { | 391 | { |
391 | CString tmp; | 392 | CString tmp; |
392 | tmp.Format(IDS_LOG_RRQSERVE,tftpRQ->rqFileName(),tftpRQ->rqType(),inet_ntoa(sin.sin_addr)); | 393 | tmp.Format(IDS_LOG_RRQSERVE,tftpRQ->rqFileName(),tftpRQ->rqType(),inet_ntoa(sin.sin_addr)); |
393 | m_Daddy->LogLine(tmp); | 394 | m_Daddy->LogLine(tmp); |
394 | CRRQSocket *s = new CRRQSocket(m_Daddy,tftpRQ->rqFileName(),tftpRQ->rqType(),&sin); | 395 | CRRQSocket *s = new CRRQSocket(m_Daddy,tftpRQ->rqFileName(),tftpRQ->rqType(),&sin); |
395 | ASSERT(s); | 396 | ASSERT(s); |
396 | tftpRQ->GetOptions(&s->m_Options); | 397 | tftpRQ->GetOptions(&s->m_Options); |
397 | if(!s->Create()) | 398 | if(!s->Create()) |
398 | s->Destroy(FALSE); | 399 | s->Destroy(FALSE); |
399 | } | 400 | } |
400 | break; | 401 | break; |
401 | case tftp::opWRQ: | 402 | case tftp::opWRQ: |
402 | // Write Request | 403 | // Write Request |
403 | { | 404 | { |
404 | CString tmp; | 405 | CString tmp; |
405 | tmp.Format(IDS_LOG_WRQSERVE,tftpRQ->rqFileName(),tftpRQ->rqType(),inet_ntoa(sin.sin_addr)); | 406 | tmp.Format(IDS_LOG_WRQSERVE,tftpRQ->rqFileName(),tftpRQ->rqType(),inet_ntoa(sin.sin_addr)); |
406 | m_Daddy->LogLine(tmp); | 407 | m_Daddy->LogLine(tmp); |
407 | CWRQSocket *s = new CWRQSocket(m_Daddy,tftpRQ->rqFileName(),tftpRQ->rqType(),&sin); | 408 | CWRQSocket *s = new CWRQSocket(m_Daddy,tftpRQ->rqFileName(),tftpRQ->rqType(),&sin); |
408 | ASSERT(s); | 409 | ASSERT(s); |
409 | tftpRQ->GetOptions(&s->m_Options); | 410 | tftpRQ->GetOptions(&s->m_Options); |
410 | if(!s->Create(NULL,NULL)) | 411 | if(!s->Create(NULL,NULL)) |
411 | s->Destroy(FALSE); | 412 | s->Destroy(FALSE); |
412 | } | 413 | } |
413 | break; | 414 | break; |
414 | default: | 415 | default: |
415 | m_Daddy->LogLine(IDS_LOG_LISTENOPCODE); | 416 | m_Daddy->LogLine(IDS_LOG_LISTENOPCODE); |
416 | delete tftpRQ; | 417 | delete tftpRQ; |
417 | return; | 418 | return; |
418 | } | 419 | } |
419 | delete tftpRQ; | 420 | delete tftpRQ; |
420 | } | 421 | } |
421 | 422 | ||
422 | BOOL tftp::Receive(CAsyncSocket* socket,UINT maxLength,SOCKADDR_IN *sin) | 423 | BOOL tftp::Receive(CAsyncSocket* socket,UINT maxLength,SOCKADDR_IN *sin) |
423 | { | 424 | { |
424 | ASSERT(socket); | 425 | ASSERT(socket); |
425 | int saddrLen = sizeof(SOCKADDR_IN); | 426 | int saddrLen = sizeof(SOCKADDR_IN); |
426 | length = sin ? | 427 | length = sin ? |
427 | socket->ReceiveFrom(udpBase(),maxLength,(SOCKADDR*)sin,&saddrLen) | 428 | socket->ReceiveFrom(udpBase(),maxLength,(SOCKADDR*)sin,&saddrLen) |
428 | : | 429 | : |
429 | socket->Receive(udpBase(),maxLength) | 430 | socket->Receive(udpBase(),maxLength) |
430 | ; | 431 | ; |
431 | if(!length) | 432 | if(!length) |
432 | return FALSE; | 433 | return FALSE; |
433 | if(length==(tftpLength)SOCKET_ERROR) | 434 | if(length==(tftpLength)SOCKET_ERROR) |
434 | return FALSE; | 435 | return FALSE; |
435 | return TRUE; | 436 | return TRUE; |
436 | } | 437 | } |
437 | 438 | ||
438 | UINT tftp::Opcode() | 439 | UINT tftp::Opcode() |
439 | { | 440 | { |
440 | return REVERSEBYTES(opcode); | 441 | return REVERSEBYTES(opcode); |
441 | } | 442 | } |
442 | 443 | ||
443 | CString tftp::rqFileName() | 444 | CString tftp::rqFileName() |
444 | { | 445 | { |
445 | ASSERT(length); | 446 | ASSERT(length); |
446 | ASSERT(Opcode()==opRRQ || Opcode()==opWRQ); | 447 | ASSERT(Opcode()==opRRQ || Opcode()==opWRQ); |
447 | CString rv; | 448 | CString rv; |
448 | if(memchr(&data.m_RQ.data,0,length-sizeof(opcode))) | 449 | if(memchr(&data.m_RQ.data,0,length-sizeof(opcode))) |
449 | rv = (LPCTSTR)data.m_RQ.data; | 450 | rv = (LPCTSTR)data.m_RQ.data; |
450 | return rv; | 451 | return rv; |
451 | } | 452 | } |
452 | 453 | ||
453 | CString tftp::rqType() | 454 | CString tftp::rqType() |
454 | { | 455 | { |
455 | ASSERT(length); | 456 | ASSERT(length); |
456 | ASSERT(Opcode()==opRRQ || Opcode()==opWRQ); | 457 | ASSERT(Opcode()==opRRQ || Opcode()==opWRQ); |
457 | CString rv; | 458 | CString rv; |
458 | char *tmp = (char*)memchr(&data.m_RQ.data,0,length-sizeof(opcode)); | 459 | char *tmp = (char*)memchr(&data.m_RQ.data,0,length-sizeof(opcode)); |
459 | if(tmp++) | 460 | if(tmp++) |
460 | rv = (LPCTSTR)tmp; | 461 | rv = (LPCTSTR)tmp; |
461 | return rv; | 462 | return rv; |
462 | } | 463 | } |
463 | 464 | ||
464 | UINT tftp::GetOptions(tftp::tftpOptions* ops) | 465 | UINT tftp::GetOptions(tftp::tftpOptions* ops) |
465 | { | 466 | { |
466 | ASSERT(length); | 467 | ASSERT(length); |
467 | ASSERT(Opcode()==opRRQ || Opcode()==opWRQ || Opcode()==opOACK); | 468 | ASSERT(Opcode()==opRRQ || Opcode()==opWRQ || Opcode()==opOACK); |
468 | ASSERT(ops); | 469 | ASSERT(ops); |
469 | tftpOptions& o = *ops; | 470 | tftpOptions& o = *ops; |
470 | LPSTR base = (LPSTR)&data.m_RQ.data; | 471 | LPSTR base = (LPSTR)&data.m_RQ.data; |
471 | UINT basePtr = 0; | 472 | UINT basePtr = 0; |
472 | if(Opcode()==opRRQ || Opcode()==opWRQ){ | 473 | if(Opcode()==opRRQ || Opcode()==opWRQ){ |
473 | base = (LPSTR)memchr(&data.m_RQ.data,0,length-sizeof(opcode)); | 474 | base = (LPSTR)memchr(&data.m_RQ.data,0,length-sizeof(opcode)); |
474 | if(!base) | 475 | if(!base) |
475 | return 0; | 476 | return 0; |
476 | base++; | 477 | base++; |
477 | basePtr = (base-(LPSTR)&data.m_RQ.data); | 478 | basePtr = (base-(LPSTR)&data.m_RQ.data); |
478 | base = (LPSTR)memchr(base,0,length-basePtr); | 479 | base = (LPSTR)memchr(base,0,length-basePtr); |
479 | if(!base) | 480 | if(!base) |
480 | return 0; | 481 | return 0; |
481 | base++; | 482 | base++; |
482 | basePtr = (base-(LPSTR)&data.m_RQ.data); | 483 | basePtr = (base-(LPSTR)&data.m_RQ.data); |
483 | } | 484 | } |
484 | ops->RemoveAll(); | 485 | ops->RemoveAll(); |
485 | UINT rv = 0; | 486 | UINT rv = 0; |
486 | while(basePtr<(length-sizeof(opcode))){ | 487 | while(basePtr<(length-sizeof(opcode))){ |
487 | CString onam = (LPSTR)&data.m_RQ.data[basePtr]; | 488 | CString onam = (LPSTR)&data.m_RQ.data[basePtr]; |
488 | basePtr+=onam.GetLength()+1; | 489 | basePtr+=onam.GetLength()+1; |
489 | CString oval = (LPSTR)&data.m_RQ.data[basePtr]; | 490 | CString oval = (LPSTR)&data.m_RQ.data[basePtr]; |
490 | basePtr+=oval.GetLength()+1; | 491 | basePtr+=oval.GetLength()+1; |
491 | onam.MakeLower(); | 492 | onam.MakeLower(); |
492 | o[onam]=oval; | 493 | o[onam]=oval; |
493 | rv++; | 494 | rv++; |
494 | } | 495 | } |
495 | return rv; | 496 | return rv; |
496 | } | 497 | } |
497 | 498 | ||
498 | tftp::tftp() | 499 | tftp::tftp() |
499 | { | 500 | { |
500 | length=0; | 501 | length=0; |
501 | } | 502 | } |
502 | 503 | ||
503 | 504 | ||
504 | void CXferSocket::OnSend(int nErrorCode) | 505 | void CXferSocket::OnSend(int nErrorCode) |
505 | { | 506 | { |
506 | if(nErrorCode){ | 507 | if(nErrorCode){ |
507 | ASSERT(m_Daddy); | 508 | ASSERT(m_Daddy); |
508 | m_Daddy->LogLine(IDS_LOG_XFERSEND); | 509 | m_Daddy->LogLine(IDS_LOG_XFERSEND); |
509 | return; | 510 | return; |
510 | } | 511 | } |
511 | if(!m_Queue.IsEmpty()){ | 512 | if(!m_Queue.IsEmpty()){ |
512 | tftp *p = m_Queue.GetHead(); | 513 | tftp *p = m_Queue.GetHead(); |
513 | ASSERT(p); | 514 | ASSERT(p); |
514 | m_Queue.RemoveHead(); | 515 | m_Queue.RemoveHead(); |
515 | if(!p->Send(this,&m_Peer)){ | 516 | if(!p->Send(this,&m_Peer)){ |
516 | ASSERT(m_Daddy); | 517 | ASSERT(m_Daddy); |
517 | m_Daddy->LogLine(IDS_LOG_XFERUDPSEND); | 518 | m_Daddy->LogLine(IDS_LOG_XFERUDPSEND); |
518 | } | 519 | } |
519 | delete p; | 520 | delete p; |
520 | } | 521 | } |
521 | DoSelect(); | 522 | DoSelect(); |