protected int SaveBufferToFile(FileStream outputFile, int bytesLeft, bool bFileOpen = true) { int bytesToWrite = Math.Min(bytesLeft, m_xmlStream.getBytesInBuffer() - m_xmlStream.getBufferOffset()); //for convenience, this function may be called even if the file wasn't correctly opened if (bFileOpen) { outputFile.Write(m_xmlStream.getBuffer(), m_xmlStream.getBufferOffset(), bytesToWrite); } m_xmlStream.addProcessedBytes(bytesToWrite);// m_bufferOffset += bytesToWrite; return(bytesToWrite); }
public void CheckCancellationRequests() { try { if (!m_netStream.DataAvailable) { return; } XMLStream inputXMLStream = new XMLStream(); var bytes = m_netStream.Read(inputXMLStream.getBuffer(), inputXMLStream.getBufferOffset(), inputXMLStream.getBufferSize() - inputXMLStream.getBufferOffset()); inputXMLStream.addBytesRead(bytes); //we let the xmlstream object know that some bytes have been read in its buffer string xmlItem = inputXMLStream.peekNextXMLItem(); if (xmlItem != "") { string xmlItemContent = inputXMLStream.getLastXMLItemContent(); if (xmlItemContent == JobTransmitter.m_quitMessage) { inputXMLStream.addProcessedBytes(bytes); inputXMLStream.discardProcessedData(); LogMessage("Stopping job execution"); m_cancelTokenSource.Cancel(); } } } catch (IOException) { LogMessage("IOException in CheckCancellationRequests()"); } catch (OperationCanceledException) { LogMessage("Thread finished gracefully"); } catch (ObjectDisposedException) { LogMessage("Network stream closed: async read finished"); } catch (InvalidOperationException ex) { LogMessage("InvalidOperationException in CheckCancellationRequests"); LogMessage(ex.ToString()); } catch (Exception ex) { LogMessage("Unhandled exception in CheckCancellationRequests"); LogMessage(ex.ToString()); } }