/// <summary>
 /// Sends the specified <paramref name="file"/> as binary data asynchronously
 /// to the client on a session.
 /// </summary>
 /// <remarks>
 ///   <para>
 ///   This method is available after the WebSocket connection has been established.
 ///   </para>
 ///   <para>
 ///   This method doesn't wait for the send to be complete.
 ///   </para>
 /// </remarks>
 /// <param name="file">
 /// A <see cref="FileInfo"/> that represents the file to send.
 /// </param>
 /// <param name="completed">
 /// An <c>Action&lt;bool&gt;</c> delegate that references the method(s) called when
 /// the send is complete. A <see cref="bool"/> passed to this delegate is <c>true</c>
 /// if the send is complete successfully.
 /// </param>
 protected void SendAsync(SSMono.IO.FileInfo file, Action <bool> completed)
 {
     if (_websocket != null)
     {
         _websocket.SendAsync(file, completed);
     }
 }
 /// <summary>
 /// Sends the specified <paramref name="file"/> as binary data to the client
 /// on the current session.
 /// </summary>
 /// <remarks>
 /// This method is available after the WebSocket connection has been established.
 /// </remarks>
 /// <param name="file">
 /// A <see cref="FileInfo"/> that represents the file to send.
 /// </param>
 protected void Send(SSMono.IO.FileInfo file)
 {
     if (_websocket != null)
     {
         _websocket.Send(file);
     }
 }
        public void TransmitFile(string fileName, bool willBlock)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            var fi = new FileInfo(fileName);

            if (!fi.Exists)
            {
                throw new FileNotFoundException(fileName + " not found");
            }

            var len = (int)fi.Length;

            ContentLength64 = len;

            var output = OutputStream;

            if (willBlock)
            {
                using (var fs = fi.OpenRead())
                {
                    byte[] data;
                    while ((data = fs.ReadBytes(8192)).Length != 0)
                    {
                        output.Write(data, 0, data.Length);
                    }
                    close(false);
                }
                return;
            }

            var fst = fi.OpenRead();

            Action <byte[]> fsCallback = null;

            fsCallback = data =>
            {
                if (data.Length == 0)
                {
                    close(false);
                    return;
                }

                output.BeginWrite(data, 0, data.Length,
                                  ar =>
                {
                    output.EndWrite(ar);

                    fst.ReadBytesAsync(8192, fsCallback, null);
                },
                                  null);
            };


            fst.ReadBytesAsync(8192, fsCallback, null);
        }
示例#4
0
        /// <summary>
        /// Sends the specified file to a client using the WebSocket connection.
        /// </summary>
        /// <param name="fileInfo">
        ///   <para>
        ///   A <see cref="SSMono.IO.FileInfo"/> that specifies the file to send.
        ///   </para>
        ///   <para>
        ///   The file is sent as the binary data.
        ///   </para>
        /// </param>
        /// <exception cref="InvalidOperationException">
        /// The current state of the connection is not Open.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="fileInfo"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///   <para>
        ///   The file does not exist.
        ///   </para>
        ///   <para>
        ///   -or-
        ///   </para>
        ///   <para>
        ///   The file could not be opened.
        ///   </para>
        /// </exception>
        protected void Send(SSMono.IO.FileInfo fileInfo)
        {
            if (_websocket == null)
            {
                var msg = "The current state of the connection is not Open.";
                throw new InvalidOperationException(msg);
            }

            _websocket.Send(fileInfo);
        }
示例#5
0
        /// <summary>
        /// Sends the specified file to a client asynchronously using the WebSocket
        /// connection.
        /// </summary>
        /// <remarks>
        /// This method does not wait for the send to be complete.
        /// </remarks>
        /// <param name="fileInfo">
        ///   <para>
        ///   A <see cref="SSMono.IO.FileInfo"/> that specifies the file to send.
        ///   </para>
        ///   <para>
        ///   The file is sent as the binary data.
        ///   </para>
        /// </param>
        /// <param name="completed">
        ///   <para>
        ///   An <c>Action&lt;bool&gt;</c> delegate or <see langword="null"/>
        ///   if not needed.
        ///   </para>
        ///   <para>
        ///   The delegate invokes the method called when the send is complete.
        ///   </para>
        ///   <para>
        ///   <c>true</c> is passed to the method if the send has done with
        ///   no error; otherwise, <c>false</c>.
        ///   </para>
        /// </param>
        /// <exception cref="InvalidOperationException">
        /// The current state of the connection is not Open.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="fileInfo"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///   <para>
        ///   The file does not exist.
        ///   </para>
        ///   <para>
        ///   -or-
        ///   </para>
        ///   <para>
        ///   The file could not be opened.
        ///   </para>
        /// </exception>
        protected void SendAsync(SSMono.IO.FileInfo fileInfo, Action <bool> completed)
        {
            if (_websocket == null)
            {
                var msg = "The current state of the connection is not Open.";
                throw new InvalidOperationException(msg);
            }

            _websocket.SendAsync(fileInfo, completed);
        }