Home Forums Support Send file with progress and stats Reply To: Send file with progress and stats

#492
MarcF
Keymaster

Heya Sp1rit,

Thanks for your interest in NetworkComms.Net. Setting up a progress bar is relatively straight forward. You are already on the right track with the StreamSendWrapper, a short example that should get you going is as follows:

On the client side you break the send into multiple instances using ThreadSafeStream and StreamSendWrapper.

//This is our progress percent, between 0 and 1 
double progressPercentage = 0;

//Initialise stream with 1MB
byte[] buffer = new byte[1024*1024];
DPSBase.ThreadSafeStream dataToSend = new DPSBase.ThreadSafeStream(new System.IO.MemoryStream(buffer));
int totalBytesSent = 0;

//We will send in chunks of 50KB
int sendInChunksOfNBytes = 50 * 1024;

//Get the connection to the target
Connection connection = TCPConnection.GetConnection(new ConnectionInfo("192.168.0.1", 10000));

do
{
    //Determine the total number of bytes to send
    //We need to watch out for the end of the buffer when we send less than sendInChunksOfNBytes
    int bytesToSend = (buffer.Length - totalBytesSent > sendInChunksOfNBytes ? sendInChunksOfNBytes : buffer.Length - totalBytesSent);
    DPSBase.StreamSendWrapper streamWrapper = new DPSBase.StreamSendWrapper(dataToSend, totalBytesSent, bytesToSend);
    connection.SendObject("PartitionedSend", streamWrapper);
    totalBytesSent+=bytesToSend;
    progressPercentage = ((double)totalBytesSent / buffer.Length);
}
while (totalBytesSent < buffer.Length);

On the server side you just reassemble the stream as the data comes in:

System.IO.Stream recievedData = new System.IO.MemoryStream();
NetworkComms.AppendGlobalIncomingPacketHandler<byte[]>("PartitionedSend", (packetHeader, connection, incomingBytes) => 
{ 
    Console.WriteLine("\n  ... Incoming data from " + connection.ToString());
    recievedData.Write(incomingBytes, 0, incomingBytes.Length);
});

If you have any issues just post back.

Marc