本文整理汇总了C#中this.BeginSend方法的典型用法代码示例。如果您正苦于以下问题:C# this.BeginSend方法的具体用法?C# this.BeginSend怎么用?C# this.BeginSend使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类this
的用法示例。
在下文中一共展示了this.BeginSend方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendAsync
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);
}
示例2: SendAsyncTask
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);
}
示例3: SendAsync
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;
}
示例4: SendAsync
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;
}
示例5: SendAsync
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;
}
示例6: AsyncSend
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;
}
示例7: SendAsync
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;
}
示例8: SendAsync
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;
}
示例9: SendBytesAsync
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);
}
}
示例10: AsyncSend
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 });
}
示例11: BeginSend
/// <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);
示例12: SendNonBlocking
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;
}
示例13: SendAsync
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;
}
示例14: SendAPM
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);
}
示例15: BeginSend
/// <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);
}