Beispiel #1
0
        /// <summary>
        /// Read from the given stream and send the data over a data connection
        /// </summary>
        private void SendData(Stream stream)
        {
            try {
                bool passive = (dataPort == null);
                using (Socket socket = OpenDataConnection()) {
                    if (socket == null)
                    {
                        return;
                    }

                    IPEndPoint remote = (IPEndPoint)socket.RemoteEndPoint;
                    IPEndPoint local  = (IPEndPoint)socket.LocalEndPoint;

                    if (logHandler != null)
                    {
                        logHandler.NewDataConnection(remote, local, passive);
                    }

                    try
                    {
                        #region for SentData log
                        FileStream fileStream = stream as FileStream;
                        long       fileSize   = -1;
                        string     fileName   = null;
                        if (fileStream != null)
                        {
                            fileSize = fileStream.Length;
                            fileName = fileStream.Name;
                        }
                        #endregion

                        while (true)
                        {
                            int bytes = stream.Read(dataBuffer, 0, dataBufferSize);
                            if (bytes <= 0)
                            {
                                break;
                            }

                            if (transferDataType == DataType.IMAGE || noAsciiConv)
                            {
                                // TYPE I -> just pass through
                                socket.Send(dataBuffer, bytes, SocketFlags.None);
                            }
                            else
                            {
                                // TYPE A -> convert local EOL style to CRLF

                                // if the buffer ends with a potential partial EOL,
                                // try to read the rest of the EOL
                                // (i assume that the EOL has max. two bytes)
                                if (localEolBytes.Length == 2 &&
                                    dataBuffer[bytes - 1] == localEolBytes[0])
                                {
                                    if (stream.Read(dataBuffer, bytes, 1) == 1)
                                    {
                                        ++bytes;
                                    }
                                }

                                byte[] convBuffer = null;
                                int    convBytes  = ConvertAsciiBytes(dataBuffer, bytes, true, out convBuffer);
                                socket.Send(convBuffer, convBytes, SocketFlags.None);
                            }

                            #region for SentData log
                            if (logHandler != null && fileStream != null)
                            {
                                logHandler.SentData(remote, local, fileName, fileSize, fileStream.Position);
                            }
                            #endregion
                        }

                        // flush socket before closing (done by using-statement)
                        socket.Shutdown(SocketShutdown.Send);
                        Respond(226, "Transfer complete.");
                    } catch (Exception ex) {
                        Respond(500, ex);
                        return;
                    } finally {
                        if (logHandler != null)
                        {
                            logHandler.ClosedDataConnection(remote, local, passive);
                        }
                    }
                }
            } finally {
                stream.Close();
            }
        }