private void SendNextUploadPacket(AssetUpload upload)
{
SendXferPacketPacket send = new SendXferPacketPacket();
send.XferID.ID = upload.XferID;
send.XferID.Packet = upload.PacketNum++;
if (send.XferID.Packet == 0)
{
// The first packet reserves the first four bytes of the data for the
// total length of the asset and appends 1000 bytes of data after that
send.DataPacket.Data = new byte[1004];
Buffer.BlockCopy(Utils.IntToBytes(upload.Size), 0, send.DataPacket.Data, 0, 4);
Buffer.BlockCopy(upload.AssetData, 0, send.DataPacket.Data, 4, 1000);
upload.Transferred += 1000;
lock (Transfers)
{
Transfers.Remove(upload.AssetID);
Transfers[upload.ID] = upload;
}
}
else if ((send.XferID.Packet + 1) * 1000 < upload.Size)
{
// This packet is somewhere in the middle of the transfer, or a perfectly
// aligned packet at the end of the transfer
send.DataPacket.Data = new byte[1000];
Buffer.BlockCopy(upload.AssetData, upload.Transferred, send.DataPacket.Data, 0, 1000);
upload.Transferred += 1000;
}
else
{
// Special handler for the last packet which will be less than 1000 bytes
int lastlen = upload.Size - ((int)send.XferID.Packet * 1000);
send.DataPacket.Data = new byte[lastlen];
Buffer.BlockCopy(upload.AssetData, (int)send.XferID.Packet * 1000, send.DataPacket.Data, 0, lastlen);
send.XferID.Packet |= (uint)0x80000000; // This signals the final packet
upload.Transferred += lastlen;
}
Client.Network.SendPacket(send);
}