Exemple #1
0
        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());
            }
        }
Exemple #2
0
        protected void SendFile(string fileName, FileType type, bool sendContent
                                , bool fromCachedDir, CancellationToken cancelToken, string rename = null)
        {
            string fileTypeXMLTag;
            string header;

            byte[] headerBytes;
            string footer = "";

            fileTypeXMLTag = getFileTypeXMLTag(type);

            string sentFilename;

            if (rename != null)
            {
                sentFilename = rename;
            }
            else
            {
                sentFilename = fileName;
            }

            header = "<" + fileTypeXMLTag + " Name=\"" + sentFilename + "\"";
            if (sendContent)
            {
                footer = "</" + fileTypeXMLTag + ">";
            }

            if (sendContent)
            {
                FileStream fileStream = null;
                long       fileSize   = 0;
                LogMessage("Sending file " + fileName);

                if (fromCachedDir)
                {
                    fileName = getCachedFilename(fileName);
                }

                try { fileStream = File.OpenRead(fileName); }
                catch
                {
                    LogMessage("Could not find input file: " + fileName);
                    return;
                }

                fileSize = fileStream.Length;
                header  += " Size=\"" + fileSize + "\">";

                //send the header
                headerBytes = Encoding.ASCII.GetBytes(header);
                WriteAsync(headerBytes, 0, headerBytes.Length, cancelToken);


                long readBytes = 0;
                int  lastReadBytes;

                while (readBytes < fileSize)
                {
                    byte[] buffer = new byte[m_xmlStream.getBufferSize()];
                    lastReadBytes = fileStream.Read(buffer, 0, m_xmlStream.getBufferSize());
                    readBytes    += lastReadBytes;

                    try
                    {
                        WriteAsync(buffer, 0, (int)lastReadBytes, cancelToken);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message + ex.StackTrace);
                    }
                }
                fileStream.Close();

                //Send the footer: </Exe>, </Input> or </Output>
                byte[] footerBytes = Encoding.ASCII.GetBytes(footer);
                WriteAsync(footerBytes, 0, footerBytes.Length, cancelToken);
                m_netStream.Flush();
            }
            else
            {
                header += "/>";
                //send the header
                headerBytes = Encoding.ASCII.GetBytes(header);
                WriteAsync(headerBytes, 0, headerBytes.Length, cancelToken);

                m_netStream.Flush();
            }
        }