RunLoop::EventSourcePtr AsyncByteStream::EventDispatchSource() { return RunLoop::EventSource::New([this](RunLoop::EventSource&) { if ( _err != 0 ) { if ( bool(_eventHandler) ) _eventHandler(AsyncEvent::ErrorOccurred, this); if ( bool(_eventDispatchSource) ) _eventDispatchSource->Cancel(); if ( bool(_eventSource) ) _eventSource->Cancel(); return; } if ( _eof ) { if ( bool(_eventHandler) ) _eventHandler(AsyncEvent::EndEncountered, this); if ( bool(_eventDispatchSource) ) _eventDispatchSource->Cancel(); if ( bool(_eventSource) ) _eventSource->Cancel(); return; } if ( BytesAvailable() && bool(_eventHandler) ) _eventHandler(AsyncEvent::HasBytesAvailable, this); if ( SpaceAvailable() && bool(_eventHandler) ) _eventHandler(AsyncEvent::HasSpaceAvailable, this); }); }
bool BufferedSocketDevice::CanReadLine() { ReadBytes(); if (( BytesAvailable() > 0 ) && m_bufRead.scanNewline( nullptr ) ) return true; return false; }
qulonglong BufferedSocketDevice::WaitForMore( int msecs, bool *pTimeout /* = nullptr*/ ) { bool bTimeout = false; if ( !m_pSocket->isValid() ) return 0; qlonglong nBytes = BytesAvailable(); if (nBytes == 0) { /* The following code is a possible workaround to the lost request problem I just hate looping too much to put it in. I believe there is something I'm missing that is causing the lost packets... Just need to find it. bTimeout = true; int nCount = 0; int msWait = msecs / 100; while (((nBytes = ReadBytes()) == 0 ) && (nCount++ < 100 ) && bTimeout && m_pSocket->isValid() ) { // give up control // should be some multiple of msWait. std::this_thread::sleep_for(std::chrono::milliseconds(1)); } } */ // -=>TODO: Override the timeout to 1 second... Closes connection sooner // to help recover from lost requests. (hack until better fix found) msecs = 1000; nBytes = m_pSocket->waitForMore( msecs, &bTimeout ); if (pTimeout != nullptr) *pTimeout = bTimeout; } return nBytes; // nBytes //m_bufRead.size(); }
AnsiString TCommThread::GetAvailableData(void) { unsigned long AvailableBytes; unsigned long ReadBytes; ReceivedData=""; AvailableBytes=BytesAvailable(); if(AvailableBytes>0) { ReceivedData.SetLength(AvailableBytes); if(ReadFile(DeviceHandle,(void*)ReceivedData.data(),AvailableBytes,&ReadBytes,NULL)==true) { if(ReadBytes>0) { return(ReceivedData); }//if }//if } return(""); }
//--------------------------------------------------------------------------- void __fastcall TCommThread::Execute() { unsigned long AvailableBytes; unsigned long ReadBytes; while(Terminated==false) { Sleep(ReceiveInterval); AvailableBytes=BytesAvailable(); if(AvailableBytes>0) { ReceivedData.SetLength(AvailableBytes); if(ReadFile(DeviceHandle,(void*)ReceivedData.data(),AvailableBytes,&ReadBytes,NULL)==true) { if(ReadBytes>0) { Synchronize(AfterReceiveData); ReceivedData=""; }//if }//if }//if } }
int TCommPort::ReadString(char *str, unsigned int MaxBytes) { VerifyOpen(); if(MaxBytes == 0u) return 0; str[0]='\0'; if(BytesAvailable() ==0) return 0; BYTE NewChar; unsigned int Index=0; while(Index < MaxBytes) { NewChar = GetByte(); // if the byte is a \r or \n, don't add it to the string if( (NewChar != '\r') && (NewChar != '\n')) { str[Index] = (char) NewChar; Index++; } // when /r is received, we are done reading the string, so return // don't forget to terminate the string with a \0. if(NewChar == '\r') { str[Index] = '\0'; return Index +1; } } // if the while loop false through, then MaxBytes were received without // receiveing a \n. Add null terminator to the string and return the number str[MaxBytes-1]='\0'; return MaxBytes; }
qlonglong BufferedSocketDevice::Size() { return (qlonglong)BytesAvailable(); }