Ejemplo n.º 1
0
        public void ParseRaw(byte[] b, int length)
        {
            // We may have data on our stack even when we have disconnected the transfer.
            if (this.connectionStatus == TcpConnection.Disconnected)
                return;

            if (rawData)
            {
                if (length < 0)
                    throw new System.ArgumentOutOfRangeException("length has to be above zero");

                // Do we have content left that we need to convert?
                if (this.received.Length > 0)
                {
                    byte[] old = this.Encoding.GetBytes(this.received);
                    received = string.Empty;
            #if !COMPACT_FRAMEWORK
                    long size = (long)length + old.LongLength;

                    byte[] tmp = new byte[size];
                    System.Array.Copy(old, 0, tmp, 0, old.LongLength);
                    if (b != null)
                        System.Array.Copy(b, 0, tmp, old.LongLength, (long)length);
            #else
                    int size = length + old.Length;

                    byte[] tmp = new byte[size];
                    System.Array.Copy(old, 0, tmp, 0, old.Length);
                    if (b != null)
                        System.Array.Copy(b, 0, tmp, old.Length, length);
            #endif
                    b = tmp;
                    length += old.Length;

                    tmp = null;
                    old = null;
                }

                // Do we have a working byte array?
                if (b != null && length != 0)
                {
                    BinaryMessage conMsg = new BinaryMessage(trans, b, length);
                    // Plugin handling here
                    FmdcEventArgs e = new FmdcEventArgs(Actions.CommandIncomming, conMsg);
                    MessageReceived(trans, e);
                    if (!e.Handled)
                    {
                        if (this.download)
                        {
                            if (trans.DownloadItem != null && trans.CurrentSegment != null && trans.CurrentSegment.Index != -1)
                            {
                                if (trans.CurrentSegment.Length < length)
                                {
                                    trans.Disconnect("You are sending more then i want.. Why?!");
                                    return;
                                }

                                if (trans.CurrentSegment.Position == 0 && !Utils.FileOperations.PathExists(trans.DownloadItem.ContentInfo.Get(ContentInfo.STORAGEPATH)))
                                {
                                    Utils.FileOperations.AllocateFile(trans.DownloadItem.ContentInfo.Get(ContentInfo.STORAGEPATH), trans.DownloadItem.ContentInfo.Size);
                                }

                                // Create the file.
                                SegmentInfo curInfo = trans.CurrentSegment;
                                try
                                {
                                    Utils.FileOperations.WriteContent(trans.DownloadItem.ContentInfo.Get(ContentInfo.STORAGEPATH), ref curInfo, b, length);
                                }
                                catch (System.Exception exp)
                                {
                                    //trans.DownloadItem.Cancel(trans.CurrentSegment.Index, trans.Source);
                                    trans.Disconnect("Exception thrown when trying to write to file: " + exp.ToString());
                                    return;
                                }
                                curInfo = null;

                                if (trans.CurrentSegment.Position >= trans.CurrentSegment.Length)
                                {
                                    EnsureCurrentSegmentFinishing();
                                    //// Searches for a download item and a segment id
                                    // Request new segment from user. IF we have found one. ELSE disconnect.
                                    if (GetSegment(true))
                                    {
                                        OnDownload();
                                    }
                                    else
                                        trans.Disconnect("All content downloaded");
                                }
                            }
                        }
                        else
                        {
                            trans.Disconnect("I dont want to download from you. F**k off!");
                        }
                    }
                }
            }
            else
            {
                ParseRaw(this.Encoding.GetString(b, 0, length));
            }
        }
        public void ParseRaw(byte[] b, int length)
        {
            if (rawData)
            {
                if (length < 0)
                    throw new System.ArgumentOutOfRangeException("length has to be above zero");

                // Do we have content left that we need to convert?
                if (this.received.Length > 0)
                {
                    byte[] old = this.Encoding.GetBytes(this.received);
            #if !COMPACT_FRAMEWORK
                    long size = (long)length + old.LongLength;

                    byte[] tmp = new byte[size];
                    System.Array.Copy(old, 0, tmp, 0, old.LongLength);
                    if (b != null)
                        System.Array.Copy(b, 0, tmp, old.LongLength, (long)length);
            #else
                    int size = length + old.Length;

                    byte[] tmp = new byte[size];
                    System.Array.Copy(old, 0, tmp, 0, old.Length);
                    if (b != null)
                        System.Array.Copy(b, 0, tmp, old.Length, length);
            #endif
                    b = tmp;
                    length += old.Length;
                    received = string.Empty;
                }

                // Do we have a working byte array?
                if (b != null && length != 0)
                {
                    BinaryMessage conMsg = new BinaryMessage(trans, b, length);
                    // Plugin handling here
                    FmdcEventArgs e = new FmdcEventArgs(Actions.CommandIncomming, conMsg);
                    MessageReceived(trans, e);
                    if (!e.Handled)
                    {
                        if (this.download)
                        {
                            if (trans.DownloadItem != null && trans.CurrentSegment.Index != -1)
                            {
                                if (trans.CurrentSegment.Length < length)
                                {
                                    trans.Disconnect("You are sending more then i want.. Why?!");
                                    return;
                                }

                                if (trans.CurrentSegment.Position == 0 && !Utils.FileOperations.PathExists(trans.DownloadItem.ContentInfo.Get(ContentInfo.STORAGEPATH)))
                                {
                                    Utils.FileOperations.AllocateFile(trans.DownloadItem.ContentInfo.Get(ContentInfo.STORAGEPATH), trans.DownloadItem.ContentInfo.Size);
                                }

                                // Create the file.
                                //using (System.IO.FileStream fs = System.IO.File.OpenWrite(trans.DownloadItem.ContentInfo.Get(ContentInfo.STORAGEPATH)))
                                using (System.IO.FileStream fs = new System.IO.FileStream(trans.DownloadItem.ContentInfo.Get(ContentInfo.STORAGEPATH), System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.Write))
                                {
                                    try
                                    {
                                        // Lock this segment of file
                                        fs.Lock(trans.CurrentSegment.Start, trans.CurrentSegment.Length);
                                        // Set position
                                        fs.Position = trans.CurrentSegment.Start + trans.CurrentSegment.Position;
                                        // Write this byte array to file
                                        fs.Write(b, 0, length);
                                        trans.CurrentSegment.Position += length;
                                        // Saves and unlocks file
                                        fs.Flush();
                                        fs.Unlock(trans.CurrentSegment.Start, trans.CurrentSegment.Length);
                                    }
                                    catch (System.Exception exp)
                                    {
                                        //trans.DownloadItem.Cancel(trans.CurrentSegment.Index, trans.Source);
                                        trans.Disconnect("Exception thrown when trying to write to file: " + exp.ToString());
                                        return;
                                    }
                                    finally
                                    {
                                        fs.Dispose();
                                        fs.Close();
                                    }
                                    if (trans.CurrentSegment.Position >= trans.CurrentSegment.Length)
                                    {
                                        trans.DownloadItem.Finished(trans.CurrentSegment.Index, trans.Source);
                                        //// Searches for a download item and a segment id
                                        // Request new segment from user. IF we have found one. ELSE disconnect.
                                        if (GetSegment(true))
                                        {
                                            OnDownload();
                                        }
                                        else
                                            trans.Disconnect("All content downloaded");
                                    }
                                }
                            }
                        }
                        else
                        {
                            trans.Disconnect("I dont want to download from you. Shove off!");
                        }
                    }
                }
            }
            else
            {
                ParseRaw(this.Encoding.GetString(b, 0, length));
            }
        }