void CLocalSearch::SendHits(const CList< T* >& oFiles) { CPacket* pPacket = NULL; CSchemaMap pSchemas; BYTE nHits = 0; for ( POSITION pos = oFiles.GetHeadPosition(); pos; ) { if ( ! pPacket ) pPacket = CreatePacket(); AddHit( pPacket, pSchemas, oFiles.GetNext( pos ), nHits ++ ); bool bSend = false; switch ( m_nProtocol ) { case PROTOCOL_G1: case PROTOCOL_G2: bSend = nHits >= Settings.Gnutella.HitsPerPacket || pPacket->m_nLength >= MAX_QUERY_PACKET_SIZE; break; case PROTOCOL_DC: // One hit per packet in DC++ protocol bSend = true; break; default: ASSERT( FALSE ); } // Send full packet if ( bSend ) { WriteTrailer( pPacket, pSchemas, nHits ); DispatchPacket( pPacket ); pPacket = NULL; nHits = 0; } } if ( nHits ) { WriteTrailer( pPacket, pSchemas, nHits ); DispatchPacket( pPacket ); pPacket = NULL; } ASSERT( pPacket == NULL ); ASSERT( pSchemas.IsEmpty() ); }
int CLocalSearch::ExecuteSharedFiles(int nMaximum) { CQuickLock oLock( Library.m_pSection ); CPtrList* pFiles = Library.Search( m_pSearch, nMaximum ); if ( pFiles == NULL ) return 0; int nHits = pFiles->GetCount(); while ( pFiles->GetCount() ) { int nInThisPacket = min( pFiles->GetCount(), (int)Settings.Gnutella.HitsPerPacket ); CreatePacket( nInThisPacket ); int nHitB = 0; for ( int nHitA = 0 ; nHitA < nInThisPacket ; nHitA++ ) { CLibraryFile* pFile = (CLibraryFile*)pFiles->RemoveHead(); if ( AddHit( pFile, nHitB ) ) nHitB ++; } WriteTrailer(); if ( nHitB > 0 ) DispatchPacket(); else DestroyPacket(); } delete pFiles; return nHits; }
/* ================== Connection::MainLoop Return values: -1 on error 0 on success ================== */ int Connection::MainLoop() { pthread_t inputThread; if (pthread_mutex_init(&inlock, NULL)) { Error("Failed to create mutex", errno); return -1; } if (pthread_create(&inputThread, NULL, &StdinThread, NULL)) { Error("Failed to create stdin-thread", errno); return -1; } if (!channel->Init()) { return -1; } while (!quit) { while (socket->HasData()) { DispatchPacket(); } HandleInput(); usleep(10000); } socket->Disconnect(); t_continue = false; pthread_join(inputThread, NULL); pthread_mutex_destroy(&inlock); return 0; }
void rpc2_ExpireEvents() { int i; struct SL_Entry *sl; struct TM_Elem *t; for (i = TM_Rescan(rpc2_TimerQueue); i > 0; i--) { t = TM_GetExpired(rpc2_TimerQueue); if (!t) continue; sl = (struct SL_Entry *)t->BackPointer; rpc2_DeactivateSle(sl, TIMEOUT); if (sl->Type == REPLY) FreeHeld(sl); else if (sl->Type == DELACK) DelayedAck(sl); else if (sl->Type == DELAYED_SEND) rpc2_SendDelayedPacket(sl); else if (sl->Type == DELAYED_RECV) { RPC2_PacketBuffer *pb = rpc2_RecvDelayedPacket(sl); DispatchPacket(pb); } else LWP_NoYieldSignal((char *)sl); } }
void CLocalSearch::WriteVirtualTree() { CSingleLock oLock( &Library.m_pSection ); if ( oLock.Lock( 100 ) ) { m_pPacket = AlbumToPacket( Library.GetAlbumRoot() ); oLock.Unlock(); if ( m_pPacket != NULL ) DispatchPacket(); } if ( oLock.Lock( 100 ) ) { m_pPacket = FoldersToPacket(); oLock.Unlock(); if ( m_pPacket != NULL ) DispatchPacket(); } }
bool CLocalSearch::ExecuteSharedFiles(INT_PTR nMaximum, INT_PTR& nHits) { CSingleLock oLock( &Library.m_pSection ); if ( ! oLock.Lock( 250 ) ) return false; auto_ptr< CFileList > pFiles( Library.Search( m_pSearch, nMaximum, FALSE, // Ghost files only for G2 m_nProtocol != PROTOCOL_G2 ) ); if ( pFiles.get() ) { CFileList oFilesInPacket; for ( POSITION pos = pFiles->GetHeadPosition() ; pos && ( ! nMaximum || ( nHits + oFilesInPacket.GetCount() < nMaximum ) ); ) { CLibraryFile* pFile = pFiles->GetNext( pos ); if ( IsValidForHit( pFile ) ) { oFilesInPacket.AddTail( pFile ); } } SendHits( oFilesInPacket ); nHits += oFilesInPacket.GetCount(); } // Is it a browser request? if ( ! m_pSearch && m_nProtocol == PROTOCOL_G2 ) { // Send virtual tree DispatchPacket( AlbumToPacket( Library.GetAlbumRoot() ) ); // Send physical tree DispatchPacket( FoldersToPacket() ); } return true; }
static void rpc2_ProcessPacket(int fd) { RPC2_PacketBuffer *pb = NULL; struct timeval tv; int rc; /* We are guaranteed that there is a packet in the socket buffer at this point */ RPC2_AllocBuffer(RPC2_MAXPACKETSIZE - sizeof(RPC2_PacketBuffer), &pb); assert(pb != NULL); assert(pb->Prefix.LE.Queue == &rpc2_PBList); if (rpc2_RecvPacket(fd, pb) < 0) { say(9, RPC2_DebugLevel, "Recv error, ignoring.\n"); RPC2_FreeBuffer(&pb); return; } #ifdef RPC2DEBUG if (RPC2_DebugLevel > 9) { fprintf(rpc2_tracefile, "Packet received from "); rpc2_printaddrinfo(pb->Prefix.PeerAddr, rpc2_tracefile); if (pb->Prefix.sa && pb->Prefix.sa->decrypt) fprintf(rpc2_tracefile, " (secure)"); fprintf(rpc2_tracefile, "\n"); } #endif if (pb->Prefix.LengthOfPacket < (ssize_t)sizeof(struct RPC2_PacketHeader)) { /* avoid memory reference errors */ BOGUS(pb, "Runt packet\n"); return; } rc = LUA_fail_delay(pb->Prefix.PeerAddr, pb, 0, &tv); if (rc == -1) { say(9, RPC2_DebugLevel, "Dropping incoming packet\n"); RPC2_FreeBuffer(&pb); return; } if (rc && rpc2_DelayedRecv(pb, &tv)) { return; } /* delay */ DispatchPacket(pb); }
int CLocalSearch::ExecutePartialFiles(int nMaximum) { ASSERT( m_nProtocol == PROTOCOL_G2 ); ASSERT( m_pSearch != NULL ); if ( m_pSearch->m_bTiger == FALSE && m_pSearch->m_bSHA1 == FALSE && m_pSearch->m_bED2K == FALSE && m_pSearch->m_bBTH == FALSE ) return 0; CSingleLock pLock( &Transfers.m_pSection ); if ( ! pLock.Lock( 50 ) ) return 0; int nCount = 0; m_pPacket = NULL; for ( POSITION pos = Downloads.GetIterator() ; pos ; ) { CDownload* pDownload = Downloads.GetNext( pos ); if ( ! pDownload->IsShared() ) continue; if ( ( m_pSearch->m_bTiger && pDownload->m_bTiger && m_pSearch->m_pTiger == pDownload->m_pTiger ) || ( m_pSearch->m_bSHA1 && pDownload->m_bSHA1 && m_pSearch->m_pSHA1 == pDownload->m_pSHA1 ) || ( m_pSearch->m_bED2K && pDownload->m_bED2K && m_pSearch->m_pED2K == pDownload->m_pED2K ) || ( m_pSearch->m_bBTH && pDownload->m_bBTH && m_pSearch->m_pBTH == pDownload->m_pBTH ) ) { if ( pDownload->m_bBTH || pDownload->IsStarted() ) { if ( m_pPacket == NULL ) CreatePacketG2(); AddHit( pDownload, nCount++ ); } } } if ( m_pPacket != NULL ) { WriteTrailerG2(); DispatchPacket(); } return nCount; }