Ejemplo n.º 1
0
 public static void SendFileAPM(this Socket socket, string filename, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags, Action handler)
 {
     var callback = new AsyncCallback(asyncResult =>
     {
         ((Socket)asyncResult.AsyncState).EndSendFile(asyncResult);
         handler();
     });
     socket.BeginSendFile(filename, preBuffer, postBuffer, flags, callback, socket);
 }
Ejemplo n.º 2
0
Archivo: IO.cs Proyecto: chakrit/kayak
        internal static Task SendFileAsync(this Socket s, string file)
        {
            var tcs = new TaskCompletionSource<int>();

            s.BeginSendFile(file, iasr =>
            {
                try
                {
                    s.EndSendFile(iasr);
                }
                catch (Exception e)
                {
                    tcs.SetException(e);
                }
            }, null);

            return tcs.Task;
        }
Ejemplo n.º 3
0
 internal static IObservable<int> SendFileAsync(this Socket s, string file)
 {
     return new AsyncOperation<int>(
         (c, st) => s.BeginSendFile(file, c, st),
         iasr => { s.EndSendFile(iasr); return 0; });
 }
Ejemplo n.º 4
0
		public static IObservable<Unit> SendFileObservable(this Socket socket, string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags)
		{
			Contract.Requires(socket != null);
			Contract.Ensures(Contract.Result<IObservable<Unit>>() != null);

			return Task.Factory.FromAsync<string, Tuple<byte[], byte[]>, TransmitFileOptions>(
				(fn, buffers, fg, callback, state) => socket.BeginSendFile(fn, buffers.Item1, buffers.Item2, fg, callback, state),
				socket.EndSendFile,
				fileName,
				Tuple.Create(preBuffer, postBuffer),
				flags,
				state: null)
				.ToObservable();
		}
        /// <summary>
        /// Extends BeginSendFile so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// socket.BeginSendFile(fileName, preBuffer, postBuffer, flags, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginSendFile(this Socket socket, String fileName, Byte[] preBuffer, Byte[] postBuffer, TransmitFileOptions flags, AsyncCallback callback)
        {
            if(socket == null) throw new ArgumentNullException("socket");

            return socket.BeginSendFile(fileName, preBuffer, postBuffer, flags, callback, null);
        }
        /// <summary>
        /// Extends BeginSendFile so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// socket.BeginSendFile(fileName, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginSendFile(this Socket socket, String fileName, AsyncCallback callback)
        {
            if(socket == null) throw new ArgumentNullException("socket");

            return socket.BeginSendFile(fileName, callback, null);
        }