Пример #1
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>
 private void OnInitiationResult(InitiationResult result, Jid to, string name,
                                 Stream stream, long size, string description, Action <bool, FileTransfer> cb)
 {
     try
     {
         FileTransfer transfer = new FileTransfer(im.Jid, to, name, size, result.SessionId,
                                                  description);
         // 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)
         {
             System.Diagnostics.Debug.WriteLine(e.Message + e.StackTrace + e.ToString());
             // Nothing to do here.
         }
     }
     catch
     {
         FileTransfer transfer = new FileTransfer(im.Jid, to, name, size, null,
                                                  description);
         // 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);
         }
     }
 }
Пример #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);
                }
            }
        }