public override bool ProcessData(P2PBridge bridge, byte[] data, bool reset)
        {
            if (_sending)
            {
                // We are sender but remote client want to kill me :)
                return(false);
            }

            if (reset)
            {
                _dataStream.SetLength(0);
            }

            if (data.Length > 0)
            {
                _dataStream.Write(data, 0, data.Length);
            }

            OnProgressed(EventArgs.Empty);

            if (_dataStream.Length == (long)_context.FileSize)
            {
                // Finished transfer
                OnTransferFinished(EventArgs.Empty);
                P2PSession.Close();
            }

            return(true);
        }
        /// <summary>
        /// Ends the P2P session.
        /// </summary>
        public virtual void Abort()
        {
            OnTransferAborted(new ContactEventArgs(Local));

            if (WarnIfP2PSessionDisposed("abort"))
            {
                return;
            }

            p2pSession.Close();
        }
Exemple #3
0
        public override bool ProcessData(P2PBridge bridge, byte[] data, bool reset)
        {
            if (sending)
            {
                // We are sender but remote client want to kill me :)
                return(false);
            }

            if (reset)
            {
                // Data prep or TFCombination.First
                objStream.SetLength(0);
            }

            if (data.Length > 0)
            {
                objStream.Write(data, 0, data.Length);

                Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose,
                                  String.Format("Received {0} / {1}", objStream.Length, msnObject.Size), GetType().Name);

                if (objStream.Length == msnObject.Size)
                {
                    // Finished transfer
                    byte[] allData = new byte[msnObject.Size];

                    objStream.Seek(0, SeekOrigin.Begin);
                    objStream.Read(allData, 0, allData.Length);

                    string dataSha = Convert.ToBase64String(new SHA1Managed().ComputeHash(allData));

                    if (dataSha != msnObject.Sha)
                    {
                        Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning,
                                          "Object hash doesn't match data hash, data invalid", GetType().Name);

                        return(false);
                    }

                    MemoryStream ms = new MemoryStream(allData);
                    ms.Position = 0;

                    // Data CHECKSUM is ok, update MsnObject
                    if (msnObject.ObjectType == MSNObjectType.UserDisplay)
                    {
                        DisplayImage newDisplayImage = new DisplayImage(Remote.Account.ToLowerInvariant(), ms);
                        Remote.SetDisplayImageAndFireDisplayImageChangedEvent(newDisplayImage);

                        msnObject = newDisplayImage;
                    }
                    else if (msnObject.ObjectType == MSNObjectType.Scene)
                    {
                        SceneImage newSceneImage = new SceneImage(Remote.Account.ToLowerInvariant(), ms);
                        Remote.SetSceneImageAndFireSceneImageChangedEvent(newSceneImage);

                        msnObject = newSceneImage;
                    }
                    else if (msnObject.ObjectType == MSNObjectType.Emoticon)
                    {
                        ((Emoticon)msnObject).Image = Image.FromStream(objStream);
                    }
                    else if (msnObject.ObjectType == MSNObjectType.Wink)
                    {
                        Stream s = ((Wink)msnObject).OpenStream();
                        s.Write(allData, 0, allData.Length);
                    }

                    objStream.Close();
                    OnTransferFinished(EventArgs.Empty);

                    if (P2PSession != null)
                    {
                        P2PSession.Close(); // Send first BYE
                    }
                }
            }
            return(true);
        }