/// <summary>Put as binary, i.e. read and write raw bytes.</summary>
/// <param name="srcStream">Input stream of data to put.</param>
/// <param name="remoteFile">Name of remote file in current directory.</param>
/// <param name="append"><c>true</c> if appending, <c>false</c> otherwise</param>
private void PutBinary(Stream srcStream, string remoteFile, bool append)
{
BufferedStream input = null;
BinaryWriter output = null;
SystemException storedEx = null;
long size = 0;
try
{
input = new BufferedStream(srcStream);
if (TransferStarted != null)
TransferStarted(this, new EventArgs());
if (TransferStartedEx != null)
TransferStartedEx(this, new TransferEventArgs(remoteFile, TransferDirection.UPLOAD, FTPTransferType.BINARY));
InitPut(remoteFile, append);
// get an output stream
output = new BinaryWriter(data.DataStream);
// if resuming, we skip over the unwanted bytes
if (resume)
{
input.Seek(resumeMarker, SeekOrigin.Current);
}
byte[] buf = new byte[transferBufferSize];
/*
* Es metodo de control de ancho de banda utiliza Sleep variable
* que cambia segun las mediciones de tiempo obtenidas en el envio
* de bytes. El valor del sleep varia siempre en 0.0 y 1.0 segundos.
* */
// read a chunk at a time and write to the data socket
int SEND_PORTION = 512;
long monitorCount = 0;
int count = input.Read(buf, 0, buf.Length);
int countSended = 0;
int countRest = count;
int partialCount = 0;
long partialTotalSended = 0;
long ticksBegin = DateTime.Now.Ticks;
TimeSpan tsElapsed;
while (count > 0 && !cancelTransfer)
{
if (bytesPerSecond > 0)
{
if (countRest < SEND_PORTION)
partialCount = countRest;
else
partialCount = SEND_PORTION;
}
else
partialCount = count;
if (partialCount > 0)
{
output.Write(buf, countSended, partialCount);
countSended += partialCount;
if (bytesPerSecond > 0)
{
partialTotalSended += partialCount;
tsElapsed = TimeSpan.FromTicks(DateTime.Now.Ticks - ticksBegin);
// Se hace unas comprobaciones dentro del periodo de un 1 seg.
// Si dentro de ese segundo, se han superado el limite
// de ancho de banda, se provoca una pausa.
if ((tsElapsed.TotalSeconds <= 1.0) &&
(bytesPerSecond > 0 && (partialTotalSended >= bytesPerSecond)))
{
int iTimeToSleep = Convert.ToInt32(1000.0 - tsElapsed.TotalMilliseconds);
//Console.WriteLine("Time sleep : "+ iTimeToSleep + "("+ tsElapsed.TotalMilliseconds +")");
System.Threading.Thread.Sleep(iTimeToSleep);
ticksBegin = DateTime.Now.Ticks;