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(Helpers.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);
        }
        private void AssetUploadCompleteHandler(Packet packet, Simulator simulator)
        {
            AssetUploadCompletePacket complete = (AssetUploadCompletePacket)packet;

            if (OnAssetUploaded != null)
            {
                bool found = false;
                KeyValuePair <LLUUID, Transfer> foundTransfer = new KeyValuePair <LLUUID, Transfer>();

                // Xfer system sucks really really bad. Where is the damn XferID?
                lock (Transfers)
                {
                    foreach (KeyValuePair <LLUUID, Transfer> transfer in Transfers)
                    {
                        if (transfer.Value.GetType() == typeof(AssetUpload))
                        {
                            AssetUpload upload = (AssetUpload)transfer.Value;

                            if ((upload).AssetID == complete.AssetBlock.UUID)
                            {
                                found          = true;
                                foundTransfer  = transfer;
                                upload.Success = complete.AssetBlock.Success;
                                upload.Type    = (AssetType)complete.AssetBlock.Type;
                                found          = true;
                                break;
                            }
                        }
                    }
                }

                if (found)
                {
                    lock (Transfers) Transfers.Remove(foundTransfer.Key);

                    try { OnAssetUploaded((AssetUpload)foundTransfer.Value); }
                    catch (Exception e) { Client.Log(e.ToString(), Helpers.LogLevel.Error); }
                }
            }
        }
        private void RequestXferHandler(Packet packet, Simulator simulator)
        {
            if (PendingUpload == null)
            {
                Client.Log("Received a RequestXferPacket for an unknown asset upload", Helpers.LogLevel.Warning);
            }
            else
            {
                AssetUpload upload = PendingUpload;
                PendingUpload = null;
                PendingUploadEvent.Set();
                RequestXferPacket request = (RequestXferPacket)packet;

                upload.XferID = request.XferID.ID;
                upload.Type   = (AssetType)request.XferID.VFileType;

                LLUUID transferID = new LLUUID(upload.XferID);
                Transfers[transferID] = upload;

                // Send the first packet containing actual asset data
                SendNextUploadPacket(upload);
            }
        }
Exemple #4
0
        //Separate thread
        private void Assets_OnAssetUploaded(AssetUpload upload)
        {
            if (upload.ID != uploadID) return;

            saving = false;
            if (closePending)
            {
                closePending = false;
                this.Close();
                return;
            }

            BeginInvoke(new MethodInvoker(SaveComplete));
        }
Exemple #5
0
        private void RequestXferHandler(Packet packet, Simulator simulator)
        {
            if (PendingUpload == null)
                Client.Log("Received a RequestXferPacket for an unknown asset upload", Helpers.LogLevel.Warning);
            else
            {
                AssetUpload upload = PendingUpload;
                PendingUpload = null;
                PendingUploadEvent.Set();
                RequestXferPacket request = (RequestXferPacket)packet;

                upload.XferID = request.XferID.ID;
                upload.Type = (AssetType)request.XferID.VFileType;

                LLUUID transferID = new LLUUID(upload.XferID);
                Transfers[transferID] = upload;

                // Send the first packet containing actual asset data
                SendNextUploadPacket(upload);
            }
        }
Exemple #6
0
        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(Helpers.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);
        }
Exemple #7
0
        /// <summary>
        /// Initiate an asset upload
        /// </summary>
        /// <param name="assetID">The ID this asset will have if the
        /// upload succeeds</param>
        /// <param name="type">Asset type to upload this data as</param>
        /// <param name="data">Raw asset data to upload</param>
        /// <param name="tempFile">Whether this is a temporary file or not</param>
        /// <param name="storeLocal">Whether to store this asset on the local
        /// simulator or the grid-wide asset server</param>
        /// <param name="isPriority">Give this upload a higher priority</param>
        /// <returns>The transaction ID of this transfer</returns>
        public LLUUID RequestUpload(out LLUUID assetID, AssetType type, byte[] data, bool tempFile, bool storeLocal, bool isPriority)
        {
            AssetUpload upload = new AssetUpload();
            upload.AssetData = data;
            upload.AssetType = type;
            upload.ID = LLUUID.Random();
            assetID = LLUUID.Combine(upload.ID, Client.Self.SecureSessionID);
            upload.AssetID = assetID;
            upload.Size = data.Length;
            upload.XferID = 0;

            // Build and send the upload packet
            AssetUploadRequestPacket request = new AssetUploadRequestPacket();
            request.AssetBlock.StoreLocal = storeLocal;
            request.AssetBlock.Tempfile = tempFile;
            request.AssetBlock.TransactionID = upload.ID;
            request.AssetBlock.Type = (sbyte)type;

            if (data.Length + 100 < Settings.MAX_PACKET_SIZE)
            {
                Client.Log(
                    String.Format("Beginning asset upload [Single Packet], ID: {0}, AssetID: {1}, Size: {2}",
                    upload.ID.ToString(), upload.AssetID.ToString(), upload.Size),
                    Helpers.LogLevel.Info);

                // The whole asset will fit in this packet, makes things easy
                request.AssetBlock.AssetData = data;
                upload.Transferred = data.Length;
            }
            else
            {
                Client.Log(
                    String.Format("Beginning asset upload [Multiple Packets], ID: {0}, AssetID: {1}, Size: {2}",
                    upload.ID.ToString(), upload.AssetID.ToString(), upload.Size),
                    Helpers.LogLevel.Info);

                // Asset is too big, send in multiple packets
                request.AssetBlock.AssetData = new byte[0];
            }

            //Client.DebugLog(request.ToString());

            /*
            // Add this upload to the Transfers dictionary using the assetID as the key.
            // Once the simulator assigns an actual identifier for this upload it will be
            // removed from Transfers and reinserted with the proper identifier
            lock (Transfers) Transfers[upload.AssetID] = upload;
            */

            // Wait for the previous upload to receive a RequestXferPacket
            if (PendingUploadEvent.WaitOne(10000, false))
            {
                PendingUpload = upload;

                Client.Network.SendPacket(request);

                return upload.ID;
            }
            else
                throw new Exception("Timeout waiting for previous asset upload to begin");
        }
 static void Assets_OnAssetUploaded(AssetUpload upload)
 {
     CurrentUpload = upload;
     UploadEvent.Set();
 }
        static LLUUID UploadImage(string filename, bool lossless)
        {
            byte[] jp2data = null;

            try
            {
                Bitmap image = (Bitmap)Bitmap.FromFile(filename);
                jp2data = OpenJPEGNet.OpenJPEG.EncodeFromImage(image, lossless);
            }
            catch (Exception ex)
            {
                Client.Log("Failed to encode image file " + filename + ": " + ex.ToString(), Helpers.LogLevel.Error);
                return LLUUID.Zero;
            }

            CurrentUpload = null;
            Assets.RequestUpload(LLUUID.Random(), AssetType.Texture, jp2data, false, false, true);

            // The textures are small, 60 seconds should be plenty
            UploadEvent.WaitOne(1000 * 60, false);

            if (CurrentUpload != null)
            {
                if (CurrentUpload.Success)
                {
                    // Pay for the upload
                    Client.Self.GiveMoney(LLUUID.Zero, Client.Settings.UPLOAD_COST, "importprimscript");

                    Console.WriteLine("Finished uploading image " + filename + ", AssetID: " +
                        CurrentUpload.AssetID.ToStringHyphenated());
                    return CurrentUpload.AssetID;
                }
                else
                {
                    Client.Log("Upload rejected for image file " + filename, Helpers.LogLevel.Error);
                    return LLUUID.Zero;
                }
            }
            else
            {
                Client.Log("Failed to upload image file " + filename + ", upload timed out", Helpers.LogLevel.Error);
                // TODO: Add a libsecondlife method for aborting a transfer
                return LLUUID.Zero;
            }
        }
Exemple #10
0
        private void Assets_OnUploadProgress(AssetUpload upload)
        {
            Transferred = upload.Transferred;

            if (this.InvokeRequired)
                BeginInvoke(new MethodInvoker(SetProgress));
            else
                SetProgress();
        }
        private void Assets_OnAssetUploaded(AssetUpload upload)
        {
            lock (PendingUploads)
            {
                if (PendingUploads.ContainsKey(upload.AssetID))
                {
                    if (upload.Success)
                    {
                        // Setup the TextureEntry with the new baked upload
                        TextureIndex index = PendingUploads[upload.AssetID];
                        AgentTextures[(int)index] = upload.AssetID;

                        Client.DebugLog("Upload complete, AgentTextures " + index.ToString() + " set to " + 
                            upload.AssetID.ToString());
                    }
                    else
                    {
                        Client.Log("Asset upload " + upload.AssetID.ToString() + " failed", 
                            Helpers.LogLevel.Warning);
                    }

                    PendingUploads.Remove(upload.AssetID);

                    Client.DebugLog("Pending uploads: " + PendingUploads.Count + ", pending downloads: " +
                        ImageDownloads.Count);

                    if (PendingUploads.Count == 0 && ImageDownloads.Count == 0)
                    {
                        Client.DebugLog("All pending image downloads and uploads complete");

                        CachedResponseEvent.Set();
                    }
                }
                else
                {
                    // TEMP
                    Client.DebugLog("Upload " + upload.AssetID.ToString() + " was not found in PendingUploads");
                }
            }
        }
Exemple #12
0
        /// <summary>
        /// Initiate an asset upload
        /// </summary>
        /// <param name="transactionID">The ID this asset will have if the
        /// upload succeeds</param>
        /// <param name="type">Asset type to upload this data as</param>
        /// <param name="data">Raw asset data to upload</param>
        /// <param name="tempFile">Whether this is a temporary file or not</param>
        /// <param name="storeLocal">Whether to store this asset on the local
        /// simulator or the grid-wide asset server</param>
        /// <param name="isPriority">Give this upload a higher priority</param>
        /// <returns>The transaction ID of this transfer</returns>
        public LLUUID RequestUpload(out LLUUID assetID, AssetType type, byte[] data, bool tempFile, bool storeLocal, bool isPriority)
        {
            AssetUpload upload = new AssetUpload();

            upload.AssetData = data;
            upload.AssetType = type;
            upload.ID        = LLUUID.Random();
            assetID          = LLUUID.Combine(upload.ID, Client.Self.SecureSessionID);
            upload.AssetID   = assetID;
            upload.Size      = data.Length;
            upload.XferID    = 0;

            // Build and send the upload packet
            AssetUploadRequestPacket request = new AssetUploadRequestPacket();

            request.AssetBlock.StoreLocal    = storeLocal;
            request.AssetBlock.Tempfile      = tempFile;
            request.AssetBlock.TransactionID = upload.ID;
            request.AssetBlock.Type          = (sbyte)type;

            if (data.Length + 100 < Settings.MAX_PACKET_SIZE)
            {
                Client.Log(
                    String.Format("Beginning asset upload [Single Packet], ID: {0}, AssetID: {1}, Size: {2}",
                                  upload.ID.ToString(), upload.AssetID.ToString(), upload.Size),
                    Helpers.LogLevel.Info);

                // The whole asset will fit in this packet, makes things easy
                request.AssetBlock.AssetData = data;
                upload.Transferred           = data.Length;
            }
            else
            {
                Client.Log(
                    String.Format("Beginning asset upload [Multiple Packets], ID: {0}, AssetID: {1}, Size: {2}",
                                  upload.ID.ToString(), upload.AssetID.ToString(), upload.Size),
                    Helpers.LogLevel.Info);

                // Asset is too big, send in multiple packets
                request.AssetBlock.AssetData = new byte[0];
            }

            //Client.DebugLog(request.ToString());

            /*
             * // Add this upload to the Transfers dictionary using the assetID as the key.
             * // Once the simulator assigns an actual identifier for this upload it will be
             * // removed from Transfers and reinserted with the proper identifier
             * lock (Transfers) Transfers[upload.AssetID] = upload;
             */

            // Wait for the previous upload to receive a RequestXferPacket
            if (PendingUploadEvent.WaitOne(10000, false))
            {
                PendingUpload = upload;

                Client.Network.SendPacket(request);

                return(upload.ID);
            }
            else
            {
                throw new Exception("Timeout waiting for previous asset upload to begin");
            }
        }
Exemple #13
0
 public void Assets_OnAssetUploaded(AssetUpload upload)
 {
     Hashtable hash = new Hashtable();
     hash.Add("MessageType", "AssetUploaded");
     hash.Add("AssetID", upload.AssetID);
     hash.Add("TransferID", upload.XferID);
     hash.Add("ID", upload.ID);
     hash.Add("Success", upload.Success);
     enqueue(hash);
 }
Exemple #14
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="transactionID">Usually a randomly generated UUID</param>
        /// <param name="type"></param>
        /// <param name="data"></param>
        /// <param name="tempFile"></param>
        /// <param name="storeLocal"></param>
        /// <param name="isPriority"></param>
        public void RequestUpload(LLUUID transactionID, AssetType type, byte[] data, bool tempFile, bool storeLocal, 
            bool isPriority)
        {
            if (!Transfers.ContainsKey(transactionID))
            {
                AssetUpload upload = new AssetUpload();
                upload.AssetData = data;
                upload.ID = transactionID;
                upload.AssetID = ((transactionID == LLUUID.Zero) ? transactionID : transactionID.Combine(Client.Network.SecureSessionID));
                upload.Size = data.Length;
                upload.XferID = 0;

                // Build and send the upload packet
                AssetUploadRequestPacket request = new AssetUploadRequestPacket();
                request.AssetBlock.StoreLocal = storeLocal;
                request.AssetBlock.Tempfile = tempFile;
                request.AssetBlock.TransactionID = upload.ID;
                request.AssetBlock.Type = (sbyte)type;

                if (data.Length + 100 < Settings.MAX_PACKET_SIZE)
                {
                    Client.Log(
                        String.Format("Beginning asset upload [Single Packet], ID: {0}, AssetID: {1}, Size: {2}",
                        upload.ID.ToStringHyphenated(), upload.AssetID.ToStringHyphenated(), upload.Size),
                        Helpers.LogLevel.Info);

                    // The whole asset will fit in this packet, makes things easy
                    request.AssetBlock.AssetData = data;
                    upload.Transferred = data.Length;
                }
                else
                {
                    Client.Log(
                        String.Format("Beginning asset upload [Multiple Packets], ID: {0}, AssetID: {1}, Size: {2}",
                        upload.ID.ToStringHyphenated(), upload.AssetID.ToStringHyphenated(), upload.Size),
                        Helpers.LogLevel.Info);

                    // Asset is too big, send in multiple packets
                    request.AssetBlock.AssetData = new byte[0];
                }

                //Client.DebugLog(request.ToString());

                // Add this upload to the Transfers dictionary using the assetID as the key.
                // Once the simulator assigns an actual identifier for this upload it will be
                // removed from Transfers and reinserted with the proper identifier
                lock (Transfers) Transfers[upload.AssetID] = upload;

                Client.Network.SendPacket(request);
            }
            else
            {
                Client.Log("RequestUpload() called for an asset we are already uploading, ignoring",
                    Helpers.LogLevel.Info);
            }
        }