Ejemplo n.º 1
0
        public void InitiateStreamAsync(Jid to, string mimeType, string profile, IEnumerable <string> streamOptions, XmlElement data = null, Action <InitiationResult, Iq> cb = null)
        {
            to.ThrowIfNull <Jid>("to");
            mimeType.ThrowIfNull <string>("mimeType");
            profile.ThrowIfNull <string>("profile");
            streamOptions.ThrowIfNull <IEnumerable <string> >("streamOptions");
            if (streamOptions.Count <string>() == 0)
            {
                throw new ArgumentException("The streamOptions enumerable must include one or more stream-options.");
            }
            if (!this.ecapa.Supports(to, new Extension[] { Extension.StreamInitiation }))
            {
                throw new NotSupportedException("The XMPP entity does not support the 'Stream Initiation' extension.");
            }
            string     sid     = GenerateSessionId();
            XmlElement element = this.CreateSiElement(sid, mimeType, profile, streamOptions, data);

            base.im.IqRequestAsync(IqType.Set, to, base.im.Jid, element, null, delegate(string id, Iq iq) {
                CommonConfig.Logger.WriteInfo("文件握手阶段InitiateStreamAsync返回,响应结果:" + iq.ToString());
                if (cb != null)
                {
                    InitiationResult result = null;
                    if (iq.Type != IqType.Error)
                    {
                        XmlElement feature = iq.Data["si"]["feature"];
                        string method      = this.ParseStreamMethod(feature);
                        result             = new InitiationResult(sid, method, iq.Data["si"]);
                    }
                    cb(result, iq);
                }
            });
        }
Ejemplo n.º 2
0
        private void OnInitiationResult(InitiationResult result, Jid to, string name, Stream stream, long size, string description, Action <bool, FileTransfer> cb)
        {
            Exception    exception;
            FileTransfer transfer = new FileTransfer(base.im.Jid, to, name, size, null, description, 0L);

            try
            {
                IDataStream extension = base.im.GetExtension(result.Method) as IDataStream;
                SISession   session   = new SISession(result.SessionId, stream, size, false, base.im.Jid, to, extension);
                this.siSessions.TryAdd(result.SessionId, session);
                this.metaData.TryAdd(result.SessionId, new FileMetaData(name, description));
                if (cb != null)
                {
                    cb(true, transfer);
                }
                try
                {
                    CommonConfig.Logger.WriteInfo("开始执行Transfer文件传输方法");
                    extension.Transfer(session);
                }
                catch (Exception exception1)
                {
                    exception = exception1;
                    CommonConfig.Logger.WriteError("发送文件过程出错:" + exception.ToString(), exception);
                }
            }
            catch (Exception exception2)
            {
                exception = exception2;
                if (stream != null)
                {
                    stream.Dispose();
                }
                CommonConfig.Logger.WriteError("OnInitiationResult发送文件异常", exception);
                if (cb != null)
                {
                    cb(false, transfer);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Attempts to initiate a data stream with the XMPP entity with the specified
        /// JID.
        /// </summary>
        /// <param name="to">The JID of the XMPP entity to initiate a data-stream
        /// with.</param>
        /// <param name="mimeType">The MIME type of the data to be transferred across
        /// the stream.</param>
        /// <param name="profile">The 'Stream Initiation' profile to use.</param>
        /// <param name="streamOptions">An enumerable collection of supported
        /// stream methods which are advertised to the receiving XMPP
        /// entity.</param>
        /// <param name="data">An XML element containing any additional data the
        /// specified 'Stream Initiation' profile may require.</param>
        /// <param name="cb">A callback method to invoke once the result of the
        /// stream-initation operation has been received.</param>
        /// <returns>An initialized instance of the InitiationResult class containing
        /// the negotiated stream-method and session identifier.</returns>
        /// <exception cref="ArgumentNullException">The to parameter or the mimeType
        /// parameter or the profile parameter or the streamOptions parameter
        /// is null.</exception>
        /// <exception cref="ArgumentException">The streamOptions enumerable contains
        /// no elements, or the stream-initiation response received contained no
        /// selection for the stream-method.</exception>
        /// <exception cref="NotSupportedException">The XMPP entity with
        /// the specified JID does not support the 'Stream Initiation' XMPP
        /// extension.</exception>
        public void InitiateStreamAsync(Jid to, string mimeType, string profile,
                                        IEnumerable <string> streamOptions, XmlElement data = null,
                                        Action <InitiationResult, Iq> cb = null)
        {
            to.ThrowIfNull("to");
            mimeType.ThrowIfNull("mimeType");
            profile.ThrowIfNull("profile");
            streamOptions.ThrowIfNull("streamOptions");
            if (streamOptions.Count() == 0)
            {
                throw new ArgumentException("The streamOptions enumerable must " +
                                            "include one or more stream-options.");
            }
            if (!ecapa.Supports(to, Extension.StreamInitiation))
            {
                throw new NotSupportedException("The XMPP entity does not support " +
                                                "the 'Stream Initiation' extension.");
            }
            string sid = GenerateSessionId();
            var    si  = CreateSiElement(sid, mimeType, profile, streamOptions, data);

            // Perform the actual request.
            im.IqRequestAsync(IqType.Set, to, im.Jid, si, null, (id, iq) => {
                if (cb == null)
                {
                    return;
                }
                InitiationResult result = null;
                if (iq.Type != IqType.Error)
                {
                    // Result must contain a 'feature' element.
                    var feat        = iq.Data["si"]["feature"];
                    string selected = ParseStreamMethod(feat);
                    // Construct the initiation result and call the provided callback.
                    result = new InitiationResult(sid, selected, iq.Data["si"]);
                }
                cb(result, iq);
            });
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Invoked once the result of a pending stream-initiation operation has been
        /// received.
        /// </summary>
        /// <param name="result">The result of the stream-initiation operation. If
        /// this parameter is null, stream-initiation failed.</param>
        /// <param name="to">The JID of the XMPP user to offer the file to.</param>
        /// <param name="stream">The stream to read the file-data from.</param>
        /// <param name="name">The name of the file, as offered to the XMPP user
        /// with the specified JID.</param>
        /// <param name="size">The number of bytes to transfer.</param>
        /// <param name="cb">A callback method invoked once the other site has
        /// accepted or rejected the file-transfer request.</param>
        /// <param name="description">A description of the file so the receiver can
        /// better understand what is being sent.</param>
        /// <remarks>This is called in the context of an arbitrary thread.</remarks>
        void OnInitiationResult(InitiationResult result, Jid to, string name,
                                Stream stream, long size, string description, Action <bool, FileTransfer> cb)
        {
            FileTransfer transfer = new FileTransfer(im.Jid, to, name, size, null,
                                                     description);

            try {
                // Get the instance of the data-stream extension that the other site has
                // selected.
                IDataStream ext = im.GetExtension(result.Method) as IDataStream;
                // Register the session.
                SISession session = new SISession(result.SessionId, stream, size, false,
                                                  im.Jid, to, ext);
                siSessions.TryAdd(result.SessionId, session);
                // Store the file's meta data.
                metaData.TryAdd(result.SessionId, new FileMetaData(name, description));
                // Invoke user-provided callback.
                if (cb != null)
                {
                    cb.Invoke(true, transfer);
                }
                // Perform the actual data-transfer.
                try {
                    ext.Transfer(session);
                } catch (Exception e) {
                    // Nothing to do here.
                }
            } catch {
                // Something went wrong. Invoke user-provided callback to let them know
                // the file-transfer can't be performed.
                if (cb != null)
                {
                    cb.Invoke(false, transfer);
                }
            }
        }
Ejemplo n.º 5
0
		/// <summary>
		/// Invoked once the result of a pending stream-initiation operation has been
		/// received.
		/// </summary>
		/// <param name="result">The result of the stream-initiation operation. If
		/// this parameter is null, stream-initiation failed.</param>
		/// <param name="to">The JID of the XMPP user to offer the file to.</param>
		/// <param name="stream">The stream to read the file-data from.</param>
		/// <param name="name">The name of the file, as offered to the XMPP user
		/// with the specified JID.</param>
		/// <param name="size">The number of bytes to transfer.</param>
		/// <param name="cb">A callback method invoked once the other site has
		/// accepted or rejected the file-transfer request.</param>
		/// <param name="description">A description of the file so the receiver can
		/// better understand what is being sent.</param>
		/// <remarks>This is called in the context of an arbitrary thread.</remarks>
		void OnInitiationResult(InitiationResult result, Jid to, string name,
			Stream stream, long size, string description, Action<bool, FileTransfer> cb) {
			FileTransfer transfer = new FileTransfer(im.Jid, to, name, size, null,
				description);
			try {
				// Get the instance of the data-stream extension that the other site has
				// selected.
				IDataStream ext = im.GetExtension(result.Method) as IDataStream;
				// Register the session.
				SISession session = new SISession(result.SessionId, stream, size, false,
					im.Jid, to, ext);
				siSessions.TryAdd(result.SessionId, session);
				// Store the file's meta data.
				metaData.TryAdd(result.SessionId, new FileMetaData(name, description));
				// Invoke user-provided callback.
				if (cb != null)
					cb.Invoke(true, transfer);
				// Perform the actual data-transfer.
				try {
					ext.Transfer(session);
				} catch(Exception e) {
					// Nothing to do here.
				}
			} catch {
				// Something went wrong. Invoke user-provided callback to let them know
				// the file-transfer can't be performed.
				if (cb != null)
					cb.Invoke(false, transfer);
			}
		}