Esempio n. 1
0
bool DccVoiceThread::readWriteStep()
{
#ifndef COMPILE_DISABLE_DCC_VOICE
    // Socket management
    bool bCanRead;
    bool bCanWrite;

    if(kvi_select(m_fd,&bCanRead,&bCanWrite))
    {
        if(bCanRead)
        {
            unsigned int actualSize = m_inFrameBuffer.size();
            m_inFrameBuffer.resize(actualSize + 1024);
            int readLen = kvi_socket_recv(m_fd,(void *)(m_inFrameBuffer.data() + actualSize),1024);
            if(readLen > 0)
            {
                if(readLen < 1024)m_inFrameBuffer.resize(actualSize + readLen);
                m_pOpt->pCodec->decode(&m_inFrameBuffer,&m_inSignalBuffer);
//#warning "A maximum length for the signal buffer is actually needed!!!"
            } else {
                if(!handleInvalidSocketRead(readLen))return false;
                m_inFrameBuffer.resize(actualSize);
            }
        }// else {
        //	m_uSleepTime += 100;
        //}

        if(bCanWrite)
        {
            // Have somethihg to write ?
            if(m_outFrameBuffer.size() > 0)
            {
                int written = kvi_socket_send(m_fd,m_outFrameBuffer.data(),m_outFrameBuffer.size());
                if(written > 0)
                {
                    m_outFrameBuffer.remove(written);
                } else {
                    if(!handleInvalidSocketRead(written))return false;
                }
            }// else {
            //			m_uSleepTime += 100;
            //		}
        }// else {
        //		m_uSleepTime += 100;
//		}
//#warning "Usleep here ?"
    }// else {
//		if(!(m_bPlaying || m_bRecording))
//		{
//			// Really NOTHING is happening...sleep a bit more
//			m_uSleepTime += 800;
//		} else {
//			m_uSleepTime += 100;
//		}
//	}

#endif // !COMPILE_DISABLE_DCC_VOICE
    return true;
}
Esempio n. 2
0
void DccChatThread::run()
{
	KviDccThreadIncomingData data;
	data.iLen = 0;
	data.buffer = nullptr;

	for(;;)
	{
		// Dequeue events
		while(KviThreadEvent * e = dequeueEvent())
		{
			if(e->id() == KVI_THREAD_EVENT_TERMINATE)
			{
				delete e;
				goto out_of_the_loop;
			}
			else
			{
				// Other events are senseless to us
				delete e;
			}
		}

		bool bCanRead;
		bool bCanWrite;
		if(kvi_select(m_fd, &bCanRead, &bCanWrite))
		{
			if(bCanWrite)
			{
				if(!tryFlushOutBuffers())
					goto out_of_the_loop;
			}
			if(bCanRead)
			{
				data.buffer = (char *)KviMemory::reallocate(data.buffer, (data.iLen + 512) * sizeof(char));
				int readLen;
#ifdef COMPILE_SSL_SUPPORT
				if(m_pSSL)
				{
					readLen = m_pSSL->read(data.buffer + data.iLen, 512);
				}
				else
				{
#endif
					readLen = kvi_socket_recv(m_fd, data.buffer + data.iLen, 512);
#ifdef COMPILE_SSL_SUPPORT
				}
#endif
				if(readLen > 0)
				{
					data.iLen += readLen;
					data.buffer = (char *)KviMemory::reallocate(data.buffer, data.iLen * sizeof(char));
					if(!handleIncomingData(&data, false))
						break; // non critical...
				}
				else
				{

#ifdef COMPILE_SSL_SUPPORT
					if(m_pSSL)
					{
						// ssl error....?
						switch(m_pSSL->getProtocolError(readLen))
						{
							case KviSSL::ZeroReturn:
								readLen = 0;
								break;
							case KviSSL::WantRead:
							case KviSSL::WantWrite:
								// hmmm...
								break;
							case KviSSL::SyscallError:
							{
								int iE = m_pSSL->getLastError(true);
								if(iE != 0)
								{
									raiseSSLError();
									postErrorEvent(KviError::SSLError);
									goto out_of_the_loop;
								}
							}
							break;
							case KviSSL::SSLError:
							{
								raiseSSLError();
								postErrorEvent(KviError::SSLError);
								goto out_of_the_loop;
							}
							break;
							default:
								// Raise unknown SSL ERROR
								postErrorEvent(KviError::SSLError);
								goto out_of_the_loop;
								break;
						}
					}
#endif
					if(data.iLen > 0)
					{
						data.buffer = (char *)KviMemory::reallocate(data.buffer, data.iLen * sizeof(char));
					}
					else
					{
						KviMemory::free(data.buffer);
						data.buffer = nullptr;
					}

					if(!handleInvalidSocketRead(readLen))
					{
						if(data.iLen)
							handleIncomingData(&data, true); // critical
						KVI_ASSERT(!data.iLen);
						break; // error
					}
				}
			}
			msleep(100);
		}
	}

out_of_the_loop:

	if(data.iLen)
		KviMemory::free(data.buffer);

#ifdef COMPILE_SSL_SUPPORT
	if(m_pSSL)
	{
		KviSSLMaster::freeSSL(m_pSSL);
		m_pSSL = nullptr;
	}
#endif

	if(m_fd != KVI_INVALID_SOCKET)
		::kvi_socket_close(m_fd);
	m_fd = KVI_INVALID_SOCKET;
}