/// <summary> /// Sends data asynchronously to a specific remote host /// </summary> /// <param name="socket">socket</param> /// <param name="buffer">an array of type System.Byte that contains the data to send</param> /// <param name="offset">the zero-based position in buffer at which to begin sending data</param> /// <param name="size">the number of bytes to send</param> /// <param name="flags">a bitwise combination of the System.Net.Sockets.SocketFlags values.</param> /// <param name="remoteEP">an System.Net.EndPoint that represents the remote device</param> /// <returns>Task</returns> public static Task SendToAsync(this Socket socket, byte[] buffer, int offset, int size, SocketFlags flags, EndPoint remoteEP) { return Task<int>.Factory.FromAsync( (ac, state) => socket.BeginSendTo(buffer, offset, size, flags, remoteEP, ac, state), socket.EndSendTo, null, TaskCreationOptions.None); }
/// <summary> /// Extends BeginSendTo so that buffer offset of 0 and call to Array.Length are not needed. /// <example> /// udpanysourcemulticastclient.BeginSendTo(buffer, remoteEndPoint, callback, state); /// </example> /// </summary> public static IAsyncResult BeginSendTo(this UdpAnySourceMulticastClient udpanysourcemulticastclient, Byte[] buffer, System.Net.IPEndPoint remoteEndPoint, AsyncCallback callback, Object state) { if(udpanysourcemulticastclient == null) throw new ArgumentNullException("udpanysourcemulticastclient"); if(buffer == null) throw new ArgumentNullException("buffer"); return udpanysourcemulticastclient.BeginSendTo(buffer, 0, buffer.Length, remoteEndPoint, callback, state); }
public static Task<Either<int, SocketError>> SendToNonBlocking(this Socket s, ArraySegment<byte> buf, SocketFlags flags, EndPoint remoteEP) { var tcs = new TaskCompletionSource<Either<int, SocketError>>(); try { IAsyncResult r = s.BeginSendTo(buf.Array, buf.Offset, buf.Count, flags, remoteEP, iar => { try { int n = s.EndSendTo(iar); tcs.SetResult(n); } catch (SocketException skex) { tcs.SetResult(skex.SocketErrorCode); } catch (Exception ex) { tcs.SetException(ex); } }, (object)null); Debug.Assert(r != null); } catch (SocketException skex) { tcs.SetResult(skex.SocketErrorCode); } catch (Exception ex) { tcs.SetException(ex); } return tcs.Task; }
/// <summary> /// Extends BeginSendTo so that when a state object is not needed, null does not need to be passed. /// <example> /// udpanysourcemulticastclient.BeginSendTo(buffer, offset, count, remoteEndPoint, callback); /// </example> /// </summary> public static IAsyncResult BeginSendTo(this UdpAnySourceMulticastClient udpanysourcemulticastclient, Byte[] buffer, Int32 offset, Int32 count, System.Net.IPEndPoint remoteEndPoint, AsyncCallback callback) { if(udpanysourcemulticastclient == null) throw new ArgumentNullException("udpanysourcemulticastclient"); return udpanysourcemulticastclient.BeginSendTo(buffer, offset, count, remoteEndPoint, callback, null); }
public static void SendToAPM(this Socket socket, byte[] buffer, int offset, int count, SocketFlags flags, EndPoint remoteEndpoint, Action<int> handler) { var callback = new AsyncCallback(asyncResult => handler(((Socket)asyncResult.AsyncState).EndSendTo(asyncResult))); socket.BeginSendTo(buffer, offset, count, flags, remoteEndpoint, callback, socket); }
/// <summary> /// Extends BeginSendTo so that buffer offset of 0 and call to Array.Length are not needed. /// <example> /// socket.BeginSendTo(buffer, socketFlags, remoteEP, callback); /// </example> /// </summary> public static IAsyncResult BeginSendTo(this Socket socket, Byte[] buffer, SocketFlags socketFlags, System.Net.EndPoint remoteEP, AsyncCallback callback) { if(socket == null) throw new ArgumentNullException("socket"); if(buffer == null) throw new ArgumentNullException("buffer"); return socket.BeginSendTo(buffer, 0, buffer.Length, socketFlags, remoteEP, callback); }
/// <summary> /// Extends BeginSendTo so that when a state object is not needed, null does not need to be passed. /// <example> /// socket.BeginSendTo(buffer, offset, size, socketFlags, remoteEP, callback); /// </example> /// </summary> public static IAsyncResult BeginSendTo(this Socket socket, Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, System.Net.EndPoint remoteEP, AsyncCallback callback) { if(socket == null) throw new ArgumentNullException("socket"); return socket.BeginSendTo(buffer, offset, size, socketFlags, remoteEP, callback, null); }
public static Task<int> SendToAsync(this Socket socket, byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEp) { var tcs = new TaskCompletionSource<int>(socket); socket.BeginSendTo(buffer, offset, size, socketFlags, remoteEp, ar => { var t = (TaskCompletionSource<int>)ar.AsyncState; var s = (Socket)t.Task.AsyncState; try { t.TrySetResult(s.EndSendTo(ar)); } catch (Exception ex) { t.TrySetException(ex); } }, tcs); return tcs.Task; }
public static Task<int> SendToAsync( this Socket socket, ArraySegment<byte> buffer, SocketFlags socketFlags, EndPoint remoteEndPoint) { return Task<int>.Factory.FromAsync( (callback, state) => socket.BeginSendTo( buffer.Array, buffer.Offset, buffer.Count, socketFlags, remoteEndPoint, callback, state), socket.EndSendTo, null); }