Esempio n. 1
0
//**************************************************
//
// reading Thread
//
void nuiAudioFifo::OnStart ()
{
    uint32 nbSampleFrames=0;
    bool fifoReadFlag=true;

    SetStopRequest (false);


    // NGL_LOG(_T("nuiAudioFifo"), NGL_LOG_DEBUG, _T("Reading Thread start\n"));

    while (!GetStopRequest ())
    {


        // all tracks had a full buffer in the previous pass. No need to read. Sleep for a while.
        if (!fifoReadFlag)
            MsSleep (1);



        {
            // you may now acces the tracks to read
            nglCriticalSectionGuard guard(mInputCS);

            // init. <=> suppose at least one track will have something to read (<=>its buffer isn't full already)
            fifoReadFlag = false;

            // ask each audio track to read samples and store them in their ringbuffers
            for (size_t i=0; i < mAudioTracks.size (); i++)
            {
                // LOCK pause flag before reading audioBuffer and then other thread must wait to change this flag
                nglCriticalSectionGuard guard(mAudioTracks[i]->mPausedCS);

                if (mAudioTracks[i] && !mAudioTracks[i]->IsPaused ())
                {
                    // check if it's necessary to read for this track (the buffer may already be full)
                    nbSampleFrames  = mAudioTracks[i]->CanRead ();
                    fifoReadFlag  |= (nbSampleFrames != 0);
                    // yes it is : do read
                    if (nbSampleFrames)
                    {
                        mAudioTracks[i]->Read(nbSampleFrames);
                    }
                }
            }
        }

        // mInputCS is automatically unlocked by the guard
    }

    // NGL_LOG(_T("nuiAudioFifo"), NGL_LOG_DEBUG, _T("Reading Thread stopped\n"));

    mStarted = false;
}
Esempio n. 2
0
void MessageQueueThread::OnStart()
{
  // first thread starts the talking session
  if (THREAD1)
  {
    // wait 1s before sending the first ping 
    MsSleep(300);
    
    // THREAD1 output "ping"
    nuiNotification* notif = new nuiNotification(NOTIF_PING);
    notif->SetToken(new nuiToken<uint>(mID)); // add a token to the notif: the sender's ID
    GetMainWindow()->PostNotification(notif);
    
    // THREAD1 says "ping" to THREAD2
    notif = new nuiNotification(NOTIF_PING);
    notif->SetToken (new nuiToken<uint>(mID));
    mpInterloc->Post(notif);
  }
  
  
  nuiNotification* message;
  while (!mKill)
  {
    // wait for a message to come
    message = mQueue.Get();
    
    
    // a 'ping' has been received
    if (!message->GetName().Compare(NOTIF_PING))
    {
      MsSleep(300);
      
      // output a 'pong'
      nuiNotification* notif = new nuiNotification(NOTIF_PONG);
      notif->SetToken (new nuiToken<uint>(mID));
      GetMainWindow()->PostNotification(notif);
      
      // wait for 500ms before sending an answer: let the user have the time to read what's going on:)
      MsSleep(500);
      
      // output a 'ping'
      notif = new nuiNotification (NOTIF_PING);
      notif->SetToken (new nuiToken<uint>(mID));
      GetMainWindow()->PostNotification(notif);
      
      // say 'ping' to the other thread
      notif = new nuiNotification (NOTIF_PING);
      notif->SetToken (new nuiToken<uint>(mID));
      mpInterloc->Post(notif);
    }
    
    // a 'kill' message has been reveived
    else if (!message->GetName().Compare(NOTIF_KILL))
    {
      // put a flag for the next cycle
      mKill = true;
    }
    
    
    else 
    {
      // should not happen
      NGL_ASSERT(0);
    }
    
    
    // don't forget to delete the nuiNotification
    if (message) 
      delete message;
    
  }
  
}
Esempio n. 3
0
CALL_BACK MediaSniffer::cap_routine( MediaSniffer* ms )
{
	SniffRec rec;
	struct pcap_pkthdr *pkt_header;
	const Packet_Hdr *pkt_data;
	const tcphdr *tcph;
	const char *b, *e, *hb, *he, *pe;
	int ret, len, j;
	bool match;

	pcap_setnonblock( ms->caphandle_, 1, NULL );
	while( ms->run_ )
		{
		ret = pcap_next_ex( ms->caphandle_, &pkt_header, reinterpret_cast<const u_char**>(&pkt_data) );
		if( ret == 1 )
			{
			if( ntohs(pkt_data->eh.ether_type) == ETH_P_PPP_SES )
				{
				if( ntohs(pkt_data->ppp.proto) == PPP_IP )
					{
					tcph = reinterpret_cast<const struct tcphdr*>(reinterpret_cast<const char*>(pkt_data)
							+ sizeof(struct ether_header) + sizeof(struct pppoe_hdr) + sizeof(u_int16_t) + (pkt_data->ppp.iph.ihl << 2));
					}
				else{// IPv6
					tcph = reinterpret_cast<const struct tcphdr*>(reinterpret_cast<const char*>(pkt_data)
							+ sizeof(struct ether_header) + sizeof(struct pppoe_hdr) + sizeof(u_int16_t) + sizeof(struct ip6_hdr));
					}//end if
				}
			else if( ntohs(pkt_data->eh.ether_type) == ETH_P_IPV6 )
				{
				tcph = reinterpret_cast<const struct tcphdr*>(reinterpret_cast<const char*>(pkt_data)
						+ sizeof(struct ether_header) + sizeof(struct ip6_hdr));
				}
			else{// IPv4
				tcph = reinterpret_cast<const struct tcphdr*>(reinterpret_cast<const char*>(pkt_data)
						+ sizeof(struct ether_header) + (pkt_data->iph.ihl << 2));
				}//end if
			pe = reinterpret_cast<const char*>(pkt_data) + pkt_header->caplen;
			b = reinterpret_cast<const char*>(tcph) + (tcph->doff << 2) + sizeof(kGetTag);
			e = strnchr( b, ' ', pe - b );
			if( e == NULL )
				{
				e = pe;
				}//end if

			if( ms->key_[0] != NULL )
				{
				match = false;
				for( j = 0; ms->key_[j] != NULL; ++j )
					{
					if( my_stristr( b, ms->key_[j] ) != NULL )
						{
						match = true;
						break;
						}//end if
					}//end for
				}
			else{// do not filter
				match = true;
				}//end if

			if( match )
				{
				len = e - b;
				if( len > kHttpProLen && strncasecmp( b, kHttpPro, kHttpProLen ) == 0 )
					{
					rec.url = string( b, len );
					}
				else{
					rec.url = string( kHttpPro );
					// find 'Host: '
					hb = e + 1;
					if( hb < pe )
						{
						do	{
							hb = strnchr( hb, '\r', pe - hb );
							if( hb != NULL )
								{
								hb += 2;
								if( hb + kHostLen < pe && strncasecmp( hb, kHost, kHostLen ) == 0 )
									{
									hb += kHostLen;
									he = strnchr( hb, '\r', pe - hb );
									if( he != NULL )
										{
										rec.url.append( hb, he - hb );
										}//end if
									break;
									}//end if
								}//end if
							} while( hb != NULL && *hb != '\r' );
						}//end if
					rec.url.append( b -4, e - b+4 );
					}//end if

				if( ms->hash_ == NULL || !(*ms->hash_)[rec.url] )
					{
					// find 'User-Agent: '
					rec.ua.clear();
					hb = e + 1;
					if( hb < pe )
						{
						do	{
							hb = strnchr( hb, '\r', pe - hb );
							if( hb != NULL )
								{
								hb += 2;
								if( hb + kUserAgentLen < pe && strncasecmp( hb, kUserAgent, kUserAgentLen ) == 0 )
									{
									hb += kUserAgentLen;
									he = strnchr( hb, '\r', pe - hb );
									if( he != NULL )
										{
										rec.ua = string( hb, he - hb );
										}//end if
									break;
									}//end if
								}//end if
							} while( hb != NULL && *hb != '\r' );
						}//end if
					if( rec.ua.empty() )
						{
						rec.ua = "<null>";
						}//end if
					ms->buff_.push_back( rec );
					if( ms->show_rec_ != NULL )
						{
						ms->show_rec_( ms->sr_arg_, &rec );
						}//end if
					}//end if
				}//end if
			}
		else if( ret == 0 )
			{
			// do nothing
			MsSleep( LISTEN_TIMESLICE );
			}
		else{
			break;
			}//end if
		}//end while

	return 0;
}//end MediaSniffer::cap_routine