Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

WndPacket.cpp

Go to the documentation of this file.
00001 //
00002 // WndPacket.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "Network.h"
00026 #include "Neighbours.h"
00027 #include "Neighbour.h"
00028 #include "G1Packet.h"
00029 #include "G2Packet.h"
00030 #include "EDPacket.h"
00031 #include "WndPacket.h"
00032 #include "CoolMenu.h"
00033 #include "LiveList.h"
00034 #include "Skin.h"
00035 
00036 #ifdef _DEBUG
00037 #define new DEBUG_NEW
00038 #undef THIS_FILE
00039 static char THIS_FILE[] = __FILE__;
00040 #endif
00041 
00042 IMPLEMENT_SERIAL(CPacketWnd, CPanelWnd, 0)
00043 
00044 BEGIN_MESSAGE_MAP(CPacketWnd, CPanelWnd)
00045         //{{AFX_MSG_MAP(CPacketWnd)
00046         ON_WM_CREATE()
00047         ON_WM_SIZE()
00048         ON_NOTIFY(NM_CUSTOMDRAW, IDC_PACKETS, OnCustomDrawList)
00049         ON_WM_CONTEXTMENU()
00050         ON_WM_MEASUREITEM()
00051         ON_WM_DRAWITEM()
00052         ON_UPDATE_COMMAND_UI(ID_SYSTEM_CLEAR, OnUpdateSystemClear)
00053         ON_WM_DESTROY()
00054         ON_WM_TIMER()
00055         //}}AFX_MSG_MAP
00056         ON_UPDATE_COMMAND_UI_RANGE(1, 3200, OnUpdateBlocker)
00057 END_MESSAGE_MAP()
00058 
00059 LPCSTR CPacketWnd::m_pszG2[] = { "PI", "PO", "LNI", "KHL", "HAW", "QKR", "QKA", "Q1", "QH1", "Q2", "QH2", "QA", "QHT", "PUSH", "UPROC", "UPROD", NULL };
00060 
00061 
00063 // CPacketWnd construction
00064 
00065 CPacketWnd::CPacketWnd(CChildWnd* pOwner)
00066 {
00067         m_pOwner        = pOwner;
00068         m_bPaused       = TRUE;
00069         Create( IDR_PACKETFRAME );
00070 }
00071 
00072 CPacketWnd::~CPacketWnd()
00073 {
00074 }
00075 
00077 // CPacketWnd create
00078 
00079 int CPacketWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
00080 {
00081         if ( CPanelWnd::OnCreate( lpCreateStruct ) == -1 ) return -1;
00082         
00083         m_wndList.Create( WS_VISIBLE|LVS_ICON|LVS_AUTOARRANGE|LVS_REPORT,
00084                 rectDefault, this, IDC_PACKETS );
00085 
00086         m_pSizer.Attach( &m_wndList );
00087         
00088         m_wndList.SendMessage( LVM_SETEXTENDEDLISTVIEWSTYLE,
00089                 LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_LABELTIP,
00090                 LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_LABELTIP );
00091         
00092         m_wndList.InsertColumn( 0, _T("Address"), LVCFMT_LEFT, 100, -1 );
00093         m_wndList.InsertColumn( 1, _T("Protocol"), LVCFMT_CENTER, 40, 0 );
00094     m_wndList.InsertColumn( 2, _T("Type"), LVCFMT_CENTER, 45, 1 );
00095         m_wndList.InsertColumn( 3, _T("T/H"), LVCFMT_CENTER, 40, 2 );
00096         m_wndList.InsertColumn( 4, _T("Hex"), LVCFMT_LEFT, 200, 3 );
00097         m_wndList.InsertColumn( 5, _T("ASCII"), LVCFMT_LEFT, 120, 4 );
00098         m_wndList.InsertColumn( 6, _T("G1-ID"), LVCFMT_LEFT, 0, 5 );
00099         // 210 for full width ID column
00100         
00101         m_pFont.CreateFont( -(theApp.m_nDefaultFontSize - 1), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
00102                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
00103                 DEFAULT_PITCH|FF_DONTCARE, theApp.m_sPacketDumpFont );
00104 
00105         m_wndList.SetFont( &m_pFont );
00106 
00107         CWnd* pWnd = CWnd::FromHandle( (HWND)m_wndList.SendMessage( LVM_GETHEADER ) );
00108         pWnd->SetFont( &theApp.m_gdiFont );
00109         
00110         LoadState( _T("CPacketWnd"), TRUE );
00111 
00112         m_pCoolMenu             = NULL;
00113         m_nInputFilter  = 0;
00114         m_nOutputFilter = 0;
00115         m_bPaused               = FALSE;
00116 
00117         for ( int nType = 0 ; nType < G1_PACKTYPE_MAX ; nType++ ) m_bTypeG1[ nType ] = TRUE;
00118         for ( int nType = 0 ; nType < 16 ; nType++ ) m_bTypeG2[ nType ] = TRUE;
00119 
00120         SetTimer( 2, 500, NULL );
00121 
00122         return 0;
00123 }
00124 
00125 void CPacketWnd::OnDestroy() 
00126 {
00127         KillTimer( 2 );
00128 
00129         CSingleLock pLock( &m_pSection, TRUE );
00130         m_bPaused = TRUE;
00131 
00132         for ( POSITION pos = m_pQueue.GetHeadPosition() ; pos ; )
00133         {
00134                 delete (CLiveItem*)m_pQueue.GetNext( pos );
00135         }
00136         m_pQueue.RemoveAll();
00137 
00138         pLock.Unlock();
00139 
00140         Settings.SaveList( _T("CPacketWnd"), &m_wndList );
00141         SaveState( _T("CPacketWnd") );
00142 
00143         CPanelWnd::OnDestroy();
00144 }
00145 
00146 void CPacketWnd::OnSkinChange()
00147 {
00148         CPanelWnd::OnSkinChange();
00149         Settings.LoadList( _T("CPacketWnd"), &m_wndList );
00150 }
00151 
00153 // CPacketWnd operations
00154 
00155 void CPacketWnd::Process(const CNeighbour* pNeighbour, const IN_ADDR* pUDP, BOOL bOutgoing, const CPacket* pPacket)
00156 {
00157         if ( m_bPaused || m_hWnd == NULL ) return;
00158 
00159         if ( pNeighbour )
00160         {
00161                 if ( bOutgoing )
00162                 {
00163                         if ( m_nOutputFilter && m_nOutputFilter != pNeighbour->m_nUnique ) return;
00164                 }
00165                 else
00166                 {
00167                         if ( m_nInputFilter && m_nInputFilter != pNeighbour->m_nUnique ) return;
00168                 }
00169         }
00170         else
00171         {
00172                 if ( bOutgoing )
00173                 {
00174                         if ( m_nOutputFilter ) return;
00175                 }
00176                 else
00177                 {
00178                         if ( m_nInputFilter ) return;
00179                 }
00180         }
00181 
00182         CG1Packet* pPacketG1 = ( pPacket->m_nProtocol == PROTOCOL_G1 ) ? (CG1Packet*)pPacket : NULL;
00183         CG2Packet* pPacketG2 = ( pPacket->m_nProtocol == PROTOCOL_G2 ) ? (CG2Packet*)pPacket : NULL;
00184         CEDPacket* pPacketED = ( pPacket->m_nProtocol == PROTOCOL_ED2K ) ? (CEDPacket*)pPacket : NULL;
00185 
00186         if ( pPacketG1 )
00187         {
00188                 if ( ! m_bTypeG1[ pPacketG1->m_nTypeIndex ] ) return;
00189         }
00190         else if ( pPacketG2 )
00191         {
00192                 for ( int nType = 0 ; m_pszG2[ nType ] ; nType++ )
00193                 {
00194                         if ( strcmp( pPacketG2->m_sType, m_pszG2[ nType ] ) == 0 )
00195                         {
00196                                 if ( ! m_bTypeG2[ nType ] ) return;
00197                                 break;
00198                         }
00199                 }
00200         }
00201         else if ( pPacketED )
00202         {
00203                 // TODO: Filter ED2K packets
00204         }
00205         
00206         CSingleLock pLock( &m_pSection, TRUE );
00207         
00208         if ( m_bPaused ) return;
00209         
00210         CLiveItem* pItem = new CLiveItem( 7, 0 );
00211         
00212         pItem->m_nParam = bOutgoing;
00213         
00214         if ( pNeighbour )
00215         {
00216                 pItem->Set( 0, pNeighbour->m_sAddress );
00217         }
00218         else
00219         {
00220                 pItem->Set( 0, _T("(") + CString( inet_ntoa( *pUDP ) ) + _T(")") );
00221         }
00222         
00223         if ( pPacketG2 )
00224                 pItem->Set( 1, _T("G2") );
00225         else if ( pPacketG1 )
00226                 pItem->Set( 1, _T("G1") );
00227         else if ( pPacketED )
00228                 pItem->Set( 1, _T("ED2K") );
00229         
00230         pItem->Set( 2, pPacket->GetType() );
00231         pItem->Set( 4, pPacket->ToHex() );
00232         pItem->Set( 5, pPacket->ToASCII() );
00233         
00234         if ( pPacketG1 )
00235         {
00236                 pItem->Format( 3, _T("%u/%u"), unsigned( pPacketG1->m_nTTL ), unsigned( pPacketG1->m_nHops ) );
00237                 pItem->Set( 6, pPacketG1->GetGUID() );
00238         }
00239         
00240         m_pQueue.AddTail( pItem );
00241 }
00242 
00243 void CPacketWnd::OnTimer(UINT nIDEvent) 
00244 {
00245         if ( nIDEvent != 2 ) return;
00246 
00247         BOOL bScroll = m_wndList.GetTopIndex() + m_wndList.GetCountPerPage() >= m_wndList.GetItemCount();
00248 
00249         CSingleLock pLock( &m_pSection );
00250         BOOL bAny = FALSE;
00251 
00252         while ( TRUE )
00253         {
00254                 pLock.Lock();
00255 
00256                 if ( m_pQueue.GetCount() == 0 ) break;
00257                 CLiveItem* pItem = (CLiveItem*)m_pQueue.RemoveHead();
00258 
00259                 pLock.Unlock();
00260 
00261                 if ( ! bAny )
00262                 {
00263                         bAny = TRUE;
00264                         m_wndList.ModifyStyle( WS_VISIBLE , 0);
00265                 }
00266 
00267                 if ( (DWORD)m_wndList.GetItemCount() >= Settings.Search.MonitorQueue && Settings.Search.MonitorQueue > 0 )
00268                 {
00269                         m_wndList.DeleteItem( 0 );
00270                 }
00271 
00272                 int nItem = pItem->Add( &m_wndList, -1, 7 );
00273 
00274                 delete pItem;
00275         }
00276 
00277         if ( bAny )
00278         {
00279                 if ( bScroll ) m_wndList.EnsureVisible( m_wndList.GetItemCount() - 1, FALSE );
00280                 m_wndList.ShowWindow( SW_SHOW );
00281         }
00282 }
00283 
00285 // CPacketWnd message handlers
00286 
00287 void CPacketWnd::OnSize(UINT nType, int cx, int cy) 
00288 {
00289         CPanelWnd::OnSize( nType, cx, cy );
00290         m_pSizer.Resize( cx );
00291         m_wndList.SetWindowPos( NULL, 0, 0, cx, cy, SWP_NOZORDER );
00292 }
00293 
00294 void CPacketWnd::OnCustomDrawList(NMHDR* pNMHDR, LRESULT* pResult)
00295 {
00296         NMLVCUSTOMDRAW* pDraw = (NMLVCUSTOMDRAW*)pNMHDR;
00297 
00298         if ( pDraw->nmcd.dwDrawStage == CDDS_PREPAINT )
00299         {
00300                 *pResult = CDRF_NOTIFYITEMDRAW;
00301         }
00302         else if ( pDraw->nmcd.dwDrawStage == CDDS_ITEMPREPAINT )
00303         {
00304                 if ( m_nInputFilter != 1 && m_nOutputFilter != 1 )
00305                 {
00306                         if ( pDraw->nmcd.lItemlParam )
00307                                 pDraw->clrText = RGB( 127, 0, 0 );
00308                         else
00309                                 pDraw->clrText = RGB( 0, 0, 127 );
00310                 }
00311                 *pResult = CDRF_DODEFAULT;
00312         }
00313 }
00314 
00315 void CPacketWnd::OnContextMenu(CWnd* pWnd, CPoint point) 
00316 {
00317         CSingleLock pLock( &Network.m_pSection, TRUE );
00318 
00319         CMenu pMenu, pHosts[2], pTypes1, pTypes2;
00320 
00321         for ( int nGroup = 0 ; nGroup < 2 ; nGroup++ )
00322         {
00323                 UINT nID = nGroup ? 2000 : 1000;
00324 
00325                 pHosts[nGroup].CreatePopupMenu();
00326 
00327                 AddNeighbour( pHosts, nGroup, nID++, 1, _T("Disable") );
00328                 AddNeighbour( pHosts, nGroup, nID++, 0, _T("Any Neighbour") );
00329                 pHosts[nGroup].AppendMenu( MF_SEPARATOR, ID_SEPARATOR );
00330 
00331                 for ( POSITION pos = Neighbours.GetIterator() ; pos ; nID++ )
00332                 {
00333                         CNeighbour* pNeighbour = Neighbours.GetNext( pos );
00334                         if ( pNeighbour->m_nState < nrsConnected ) continue;
00335                         AddNeighbour( pHosts, nGroup, nID, pNeighbour->m_nUnique, pNeighbour->m_sAddress );
00336                 }
00337 
00338                 if ( ( nID % 1000 ) == 2 )
00339                         pHosts[nGroup].AppendMenu( MF_STRING|MF_GRAYED, 999, _T("No Neighbours") );
00340         }
00341         
00342         pTypes1.CreatePopupMenu();
00343 
00344         for ( int nType = 0 ; nType < G1_PACKTYPE_MAX ; nType++ )
00345         {
00346                 if ( m_bTypeG1[ nType ] )
00347                         pTypes1.AppendMenu( MF_STRING|MF_CHECKED, 3000 + nType, CG1Packet::m_pszPackets[ nType ] );
00348                 else
00349                         pTypes1.AppendMenu( MF_STRING, 3000 + nType, CG1Packet::m_pszPackets[ nType ] );
00350         }
00351 
00352         pTypes2.CreatePopupMenu();
00353 
00354         for ( int nType = 0 ; m_pszG2[ nType ] ; nType++ )
00355         {
00356                 if ( m_bTypeG2[ nType ] )
00357                         pTypes2.AppendMenu( MF_STRING|MF_CHECKED, 3100 + nType, CString( m_pszG2[ nType ] ) );
00358                 else
00359                         pTypes2.AppendMenu( MF_STRING, 3100 + nType, CString( m_pszG2[ nType ] ) );
00360         }
00361 
00362         pMenu.CreatePopupMenu();
00363         pMenu.AppendMenu( MF_STRING|MF_POPUP, (UINT)pHosts[0].GetSafeHmenu(), _T("Incoming") );
00364         pMenu.AppendMenu( MF_STRING|MF_POPUP, (UINT)pHosts[1].GetSafeHmenu(), _T("Outgoing") );
00365         pMenu.AppendMenu( MF_STRING|MF_POPUP, (UINT)pTypes1.GetSafeHmenu(), _T("G1 Types") );
00366         pMenu.AppendMenu( MF_STRING|MF_POPUP, (UINT)pTypes2.GetSafeHmenu(), _T("G2 Types") );
00367         pMenu.AppendMenu( MF_SEPARATOR, ID_SEPARATOR );
00368         pMenu.AppendMenu( MF_STRING | ( m_bPaused ? MF_CHECKED : 0 ), 1, _T("&Pause Display") );
00369         pMenu.AppendMenu( MF_STRING, ID_SYSTEM_CLEAR, _T("&Clear Buffer") );
00370 
00371         m_pCoolMenu = new CCoolMenu();
00372         m_pCoolMenu->AddMenu( &pMenu, TRUE );
00373         m_pCoolMenu->SetWatermark( Skin.GetWatermark( _T("CCoolMenu") ) );
00374 
00375         pLock.Unlock();
00376 
00377         UINT nCmd = pMenu.TrackPopupMenu( TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON|TPM_RETURNCMD,
00378                 point.x, point.y, this );
00379 
00380         delete m_pCoolMenu;
00381         m_pCoolMenu = NULL;
00382 
00383         DWORD* pModify = NULL;
00384 
00385         if ( nCmd == 1 )
00386         {
00387                 m_bPaused = ! m_bPaused;
00388                 return;
00389         }
00390         else if ( nCmd == ID_SYSTEM_CLEAR )
00391         {
00392                 m_wndList.DeleteAllItems();
00393                 return;
00394         }
00395         else if ( nCmd >= 3000 && nCmd < 3000 + G1_PACKTYPE_MAX )
00396         {
00397                 nCmd -= 3000;
00398 
00399                 if ( GetAsyncKeyState( VK_SHIFT ) & 0x8000 )
00400                 {
00401                         for ( int nType = 0 ; nType < G1_PACKTYPE_MAX ; nType++ )
00402                         {
00403                                 m_bTypeG1[ nType ] = ( nCmd == (UINT)nType ) ? TRUE : FALSE;
00404                         }
00405                 }
00406                 else
00407                 {
00408                         m_bTypeG1[ nCmd ] = ! m_bTypeG1[ nCmd ];
00409                 }
00410 
00411                 return;
00412         }
00413         else if ( nCmd >= 3100 && nCmd < 3100 + 16 )
00414         {
00415                 nCmd -= 3100;
00416 
00417                 if ( GetAsyncKeyState( VK_SHIFT ) & 0x8000 )
00418                 {
00419                         for ( int nType = 0 ; nType < 16 ; nType++ )
00420                         {
00421                                 m_bTypeG2[ nType ] = ( nCmd == (UINT)nType ) ? TRUE : FALSE;
00422                         }
00423                 }
00424                 else
00425                 {
00426                         m_bTypeG2[ nCmd ] = ! m_bTypeG2[ nCmd ];
00427                 }
00428 
00429                 return;
00430         }
00431         else if ( nCmd >= 1000 && nCmd < 2000 )
00432         {
00433                 pModify = &m_nInputFilter;
00434                 nCmd -= 1000;
00435         }
00436         else if ( nCmd >= 2000 && nCmd < 3000 )
00437         {
00438                 pModify = &m_nOutputFilter;
00439                 nCmd -= 2000;
00440         }
00441         else
00442         {
00443                 return;
00444         }
00445 
00446         if ( nCmd == 0 )
00447         {
00448                 *pModify = 1;
00449         }
00450         else if ( nCmd == 1 )
00451         {
00452                 *pModify = 0;
00453         }
00454         else
00455         {
00456                 pLock.Lock();
00457                 nCmd -= 2;
00458 
00459                 for ( POSITION pos = Neighbours.GetIterator() ; pos ; nCmd-- )
00460                 {
00461                         CNeighbour* pNeighbour = Neighbours.GetNext( pos );
00462                         if ( ! nCmd )
00463                         {
00464                                 *pModify = pNeighbour->m_nUnique;
00465                                 break;
00466                         }
00467                 }
00468         }
00469 
00470         Invalidate();
00471 }
00472 
00473 void CPacketWnd::AddNeighbour(CMenu* pMenus, int nGroup, UINT nID, DWORD nTarget, LPCTSTR pszText)
00474 {
00475         UINT nChecked = ( ( nGroup == 1 && m_nOutputFilter == nTarget ) ||
00476                  ( nGroup == 0 && m_nInputFilter == nTarget ) )
00477                  ? MF_CHECKED : 0;
00478 
00479         pMenus[nGroup].AppendMenu( MF_STRING|nChecked, nID, pszText );
00480 }
00481 
00482 void CPacketWnd::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
00483 {
00484         if ( m_pCoolMenu ) m_pCoolMenu->OnMeasureItem( lpMeasureItemStruct );
00485 }
00486 
00487 void CPacketWnd::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
00488 {
00489         if ( m_pCoolMenu ) m_pCoolMenu->OnDrawItem( lpDrawItemStruct );
00490 }
00491 
00492 void CPacketWnd::OnUpdateBlocker(CCmdUI* pCmdUI)
00493 {
00494         if ( m_pCoolMenu ) pCmdUI->Enable( pCmdUI->m_nID != 999 );
00495         else pCmdUI->ContinueRouting();
00496 }
00497 
00498 void CPacketWnd::OnUpdateSystemClear(CCmdUI* pCmdUI) 
00499 {
00500         pCmdUI->Enable( TRUE ); 
00501 }
00502 

Generated on Thu Dec 15 10:39:53 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2