예제 #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 {
                        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);
                            }
                        }

                        // 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();
            }
        }