public static Task SendAsync(this Socket socket, byte[] buffer, int offset, int size, SocketFlags flags = SocketFlags.None)
        {
            if (socket == null) throw new ArgumentNullException("socket");
            if (buffer == null) throw new ArgumentNullException("buffer");

            return Task<int>.Factory.FromAsync(
                (ac, state) => socket.BeginSend(buffer, offset, size, flags, ac, state),
                socket.EndSend,
                null,
                TaskCreationOptions.None);
        }
Example #2
0
 public static Task SendAsyncTask(this Socket socket, byte[] dataToSend, CancellationTokenSource cancellationTokenSource)
 {
     return Task.Factory.FromAsync(
         (ac, o) => socket.BeginSend(dataToSend, 0, dataToSend.Length, SocketFlags.None, ac, null),
         ar =>
         {
             cancellationTokenSource.Token.ThrowIfCancellationRequested();
             socket.EndSend(ar);
         },
         null);
 }
        public static Task<int> SendAsync(this Socket socket, IList<ArraySegment<byte>> buffers, SocketFlags flags = SocketFlags.None)
        {
            if (socket == null) throw new ArgumentNullException("socket");
            if (buffers == null) throw new ArgumentNullException("buffers");

            return Task<int>.Factory.FromAsync(
                (ac, state) => socket.BeginSend(buffers, flags, ac, state),
                socket.EndSend,
                null,
                TaskCreationOptions.None);
        }
Example #4
0
 public static Task SendAsync(this Socket socket, byte[] buffer, int offset, int count, SocketFlags socketFlags)
 {
     var tcs = new TaskCompletionSource<int>(socket);
     socket.BeginSend(buffer, offset, count, socketFlags, iar =>
     {
         var t = (TaskCompletionSource<int>)iar.AsyncState;
         var s = (Socket)t.Task.AsyncState;
         try { t.TrySetResult(s.EndSend(iar)); }
         catch (Exception exc) { t.TrySetException(exc); }
     }, tcs);
     return tcs.Task;
 }
		public static Task<int> SendAsync(this Socket socket, byte[] buffer, int offset, int size, SocketFlags flags, out SocketError error) {
			var tsc = new TaskCompletionSource<int> ();

			socket.BeginSend (buffer, offset, size, flags, out error, asyncResult => {
				try {
					tsc.SetResult (socket.EndSend (asyncResult));
				} catch (Exception e) {
					tsc.SetException (e);
				}
			}, null);

			return tsc.Task;
		}
        public static Task<int> SendAsync(this Socket socket,
            byte[] buffer, int offset, int size, SocketFlags socketFlags)
        {
            if (socket == null) throw new ArgumentNullException(nameof(socket));

            var tcs = new TaskCompletionSource<int>();
            socket.BeginSend(buffer, offset, size, socketFlags, ar =>
            {
                try { tcs.TrySetResult(socket.EndReceive(ar)); }
                catch (Exception e) { tcs.TrySetException(e);  }
            }, state: null);
            return tcs.Task;
        }
Example #7
0
 public static Future<Packet> AsyncSend(
     this Session session, Packet packet
 )
 {
     var f = new Future<Packet>();
     session.BeginSend(packet, (_) => {
         try {
             f.Complete(session.EndSend(_));
         } catch (Exception ex) {
             f.Fail(ex);
         }
     });
     return f;
 }
Example #8
0
File: IO.cs Project: chakrit/kayak
        internal static Task<int> SendAsync(this Socket s, byte[] buffer, int offset, int count)
        {
            var tcs = new TaskCompletionSource<int>();

            s.BeginSend(buffer, offset, count, SocketFlags.None, iasr =>
            {
                try
                {
                    tcs.SetResult(s.EndSend(iasr));
                }
                catch (Exception e)
                {
                    tcs.SetException(e);
                }
            }, null);

            return tcs.Task;
        }
Example #9
0
        public static Task<int> SendAsync(this Socket socket,
                                          byte[] buffer,
                                          int offset = 0,
                                          int size = 0,
                                          SocketFlags socketFlags = SocketFlags.None) {
            if(size <= 0)
                size = buffer.Length;

            var tcs = new TaskCompletionSource<int>();
            socket.BeginSend(buffer, offset, size, socketFlags,
                             iar => {
                                 try {
                                     tcs.TrySetResult(socket.EndSend(iar));
                                 }
                                 catch(Exception ex) {
                                     tcs.TrySetException(ex);
                                 }
                             }, null);

            return tcs.Task;
        }
Example #10
0
 public static void SendBytesAsync(this Socket socket, byte[] bytes, int offset, int count, Action<bool> callback)
 {
     if (!MiscHelpers.Try(() => socket.BeginSend(bytes, offset, count, SocketFlags.None, result => socket.OnBytesSent(result, bytes, offset, count, callback), null)))
     {
         callback(false);
     }
 }
Example #11
0
 public static void AsyncSend(this Socket socket, byte[] buffer)
 {
     socket.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(SendCallback), new State() { Socket = socket, Callback = null });
 }
Example #12
0
 /// <summary>
 /// BeginSend
 /// </summary>
 /// <param name="socket"></param>
 /// <param name="content"></param>
 /// <param name="asyncCallback"></param>
 /// <returns></returns>
 public static IAsyncResult BeginSend(this Socket socket, byte[] content, AsyncCallback asyncCallback) => socket.BeginSend(content, asyncCallback, null);
        public static Task<Either<int, SocketError>> SendNonBlocking(this Socket s, ArraySegment<byte> buf, SocketFlags flags)
        {
            SocketError err;

            var tcs = new TaskCompletionSource<Either<int, SocketError>>();

            try
            {
                IAsyncResult r = s.BeginSend(buf.Array, buf.Offset, buf.Count, flags, out err, iar =>
                {
                    try
                    {
                        SocketError errInner;
                        int n = s.EndSend(iar, out errInner);
                        if (n <= 0)
                            tcs.SetResult(errInner);
                        else
                            tcs.SetResult(n);
                    }
                    catch (SocketException skex)
                    {
                        tcs.SetResult(skex.SocketErrorCode);
                    }
                    catch (Exception ex)
                    {
                        tcs.SetException(ex);
                    }
                }, (object)null);

                if (r == null)
                    tcs.SetResult(err);
            }
            catch (SocketException skex)
            {
                tcs.SetResult(skex.SocketErrorCode);
            }
            catch (Exception ex)
            {
                tcs.SetException(ex);
            }

            return tcs.Task;
        }
Example #14
0
        public static Task<int> SendAsync(this Socket socket, byte[] buffer,
                                          int offset = 0, int size = -1,
                                          SocketFlags flags = SocketFlags.None)
        {
            if (size < 0)
                size = buffer.Length;

            var source = new TaskCompletionSource<int>(socket);
            socket.BeginSend(buffer, offset, size, flags, state =>
                {
                    try
                    {
                        source.SetResult(socket.EndSend(state));
                    }
                    catch (Exception e)
                    {
                        source.SetException(e);
                    }
                }, source);
            return source.Task;
        }
Example #15
0
 public static void SendAPM(this Socket socket, byte[] buffer, int offset, int count, SocketFlags flags, Action<int> handler)
 {
     var callback = new AsyncCallback(asyncResult => handler(((Socket)asyncResult.AsyncState).EndSend(asyncResult)));
     socket.BeginSend(buffer, offset, count, flags, callback, socket);
 }
        /// <summary>
        /// Extends BeginSend so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// socket.BeginSend(buffers, socketFlags, errorCode, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginSend(this Socket socket, System.Collections.Generic.IList<ArraySegment<Byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback)
        {
            if(socket == null) throw new ArgumentNullException("socket");

            return socket.BeginSend(buffers, socketFlags, out errorCode, callback, null);
        }
        /// <summary>
        /// Extends BeginSend so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// socket.BeginSend(buffer, socketFlags, errorCode, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginSend(this Socket socket, Byte[] buffer, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback)
        {
            if(socket == null) throw new ArgumentNullException("socket");

            if(buffer == null) throw new ArgumentNullException("buffer");

            return socket.BeginSend(buffer, 0, buffer.Length, socketFlags, out errorCode, callback);
        }
        /// <summary>
        /// Extends BeginSend so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// socket.BeginSend(buffer, offset, size, socketFlags, errorCode, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginSend(this Socket socket, Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback)
        {
            if(socket == null) throw new ArgumentNullException("socket");

            return socket.BeginSend(buffer, offset, size, socketFlags, out errorCode, callback, null);
        }
        /// <summary>
        /// Extends BeginSend so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// socket.BeginSend(buffer, socketFlags, callback, state);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginSend(this Socket socket, Byte[] buffer, SocketFlags socketFlags, AsyncCallback callback, Object state)
        {
            if(socket == null) throw new ArgumentNullException("socket");

            if(buffer == null) throw new ArgumentNullException("buffer");

            return socket.BeginSend(buffer, 0, buffer.Length, socketFlags, callback, state);
        }
Example #20
0
 public static Future<int> AsyncSend(this UdpClient udpClient, byte[] datagram, int bytes, string hostname, int port)
 {
     var f = new Future<int>();
     try {
         udpClient.BeginSend(
             datagram, bytes, hostname, port,
             (ar) => {
                 try {
                     var bytesSent = udpClient.EndSend(ar);
                     f.Complete(bytesSent);
                 } catch (FutureHandlerException) {
                     throw;
                 } catch (Exception ex) {
                     f.Fail(ex);
                 }
             },
             null
         );
     } catch (Exception ex) {
         f.Fail(ex);
     }
     return f;
 }
Example #21
0
 internal static IObservable<int> SendAsync(this Socket s, byte[] buffer, int offset, int count)
 {
     return new AsyncOperation<int>(
         (c, st) => s.BeginSend(buffer, offset, count, SocketFlags.None, c, st),
         iasr => { return s.EndSend(iasr); });
 }
 ///<summary>Sends a datagram to a remote host asynchronously. The destination was specified previously by a call to .</summary>
 ///<returns>Task object that represents the asynchronous send.</returns>
 ///<param name="datagram">A  array that contains the data to be sent.</param>
 ///<param name="bytes">The number of bytes to send.</param>
 ///<param name="hostname">The host name.</param>
 ///<param name="port">The host name.</param>
 ///<param name="source">The source.</param>
 public static Task<int> SendAsync(this System.Net.Sockets.UdpClient source, byte[] datagram, int bytes, string hostname, int port)
 {
     return Task<int>.Factory.FromAsync((callback, state) => source.BeginSend(datagram, bytes, hostname, port, callback, state), source.EndSend, null);
 }
Example #23
0
 public static void SendAPM(this Socket socket, IList<ArraySegment<byte>> buffers, SocketFlags flags, Action<int> handler)
 {
     var callback = new AsyncCallback(asyncResult => handler(((Socket)asyncResult.AsyncState).EndSend(asyncResult)));
     socket.BeginSend(buffers, flags, callback, socket);
 }
        /// <summary>
        /// Extends BeginSend so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// udpclient.BeginSend(datagram, bytes, requestCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginSend(this UdpClient udpclient, Byte[] datagram, Int32 bytes, AsyncCallback requestCallback)
        {
            if(udpclient == null) throw new ArgumentNullException("udpclient");

            return udpclient.BeginSend(datagram, bytes, requestCallback, null);
        }
Example #25
0
        public static Task Write(this Socket socket, byte[] buffer)
        {
            var completionSource = new TaskCompletionSource<object>();
            var start = 0;
            AsyncCallback callback = null;
            callback = ar =>
            {
                int write;
                try
                {
                    write = socket.EndSend(ar);
                    start += write;
                }
                catch (Exception e)
                {
                    completionSource.SetException(e);
                    return;
                }
                if (start == buffer.Length)
                {
                    completionSource.SetResult(null);
                    return;
                }
                socket.BeginSend(buffer, start, buffer.Length - start, SocketFlags.None, callback, null);
            };
            socket.BeginSend(buffer, start, buffer.Length - start, SocketFlags.None, callback, null);

            return completionSource.Task;
        }
Example #26
0
 public static void AsyncSend(this Socket socket, byte[] buffer, object state, Action<int, object> callback)
 {
     socket.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(SendCallback), new State() { Socket = socket, Callback = new Callback(callback), UserDefinedState = state});
 }
 public static async Task<int> SendAsync(this Socket socket, byte[] buffer, int offset, int size, SocketFlags flags)
 {
     return await Task<int>.Factory.FromAsync((callback, state) => socket.BeginSend(buffer, offset, size, flags, callback, state), ias => socket.EndSend(ias), null);
 }
Example #28
0
 public static void AsyncSend(this Socket socket, byte[] buffer, Action<int> callback)
 {
     socket.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(SendCallback), new State() { Socket = socket, Callback = new Callback(callback) });
 }
 public static Task SendAsync(this TopicClient client, BrokeredMessage message)
 {
     return Task.Factory.FromAsync((cb, state) => client.BeginSend((BrokeredMessage)state, cb, null), client.EndSend, message);
 }
Example #30
0
 /// <summary>
 /// BeginSend
 /// </summary>
 /// <param name="socket"></param>
 /// <param name="buffer"></param>
 /// <param name="callback"></param>
 /// <param name="state"></param>
 /// <returns></returns>
 public static IAsyncResult BeginSend(this Socket socket, byte[] buffer, AsyncCallback callback, object state) => socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, callback, state);