本文整理汇总了C#中this.BeginReceive方法的典型用法代码示例。如果您正苦于以下问题:C# this.BeginReceive方法的具体用法?C# this.BeginReceive怎么用?C# this.BeginReceive使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类this
的用法示例。
在下文中一共展示了this.BeginReceive方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadBuffer
public static Task<ArraySegment<byte>> ReadBuffer(this Socket socket, int bufferSize)
{
var completionSource = new TaskCompletionSource<ArraySegment<byte>>();
var buffer = new byte[bufferSize];
var start = 0;
AsyncCallback callback = null;
callback = ar =>
{
int read;
try
{
read = socket.EndReceive(ar);
start += read;
}
catch (Exception e)
{
completionSource.SetException(e);
return;
}
if (read == 0 || start == bufferSize)
{
completionSource.SetResult(new ArraySegment<byte>(buffer, 0, start));
return;
}
socket.BeginReceive(buffer, start, bufferSize - start, SocketFlags.None, callback, null);
};
socket.BeginReceive(buffer, start, bufferSize - start, SocketFlags.Partial, callback, null);
return completionSource.Task;
}
示例2: ReceiveAsync
/// <summary>
/// Asynchronously receives data from a connected socket.
/// </summary>
/// <param name="socket">
/// The socket from which to read data.
/// </param>
/// <param name="buffer">
/// An array of type <see cref="byte"/> that is the storage location for
/// the received data.
/// </param>
/// <param name="offset">
/// The zero-based position in the <paramref name="buffer"/> parameter at which to
/// start storing data.
/// </param>
/// <param name="size">
/// The number of bytes to receive.
/// </param>
/// <param name="socketFlags">
/// A bitwise combination of the <see cref="SocketFlags"/> values.
/// </param>
/// <returns>
/// The number of bytes received.
/// </returns>
public static Task<int> ReceiveAsync(
this Socket socket,
byte[] buffer,
int offset,
int size,
SocketFlags socketFlags)
{
var tcs = new TaskCompletionSource<int>(socket);
socket.BeginReceive(
buffer,
offset,
size,
socketFlags,
iar =>
{
var t = (TaskCompletionSource<int>)iar.AsyncState;
var s = (Socket)t.Task.AsyncState;
try
{
t.TrySetResult(s.EndReceive(iar));
}
catch (Exception ex)
{
t.TrySetException(ex);
}
},
tcs);
return tcs.Task;
}
示例3: AsyncReceive
public static void AsyncReceive(this Socket socket, byte[] buffer, object state, Action<int, object> callback)
{
if (state == null)
throw new InvalidOperationException("State cannot be null");
socket.BeginReceive(buffer, 0, buffer.Length, 0, new AsyncCallback(ReceiveCallback), new State() { Socket = socket, Callback = new Callback(callback), UserDefinedState = state });
}
示例4: ReceiveAsync
public static Task<int> ReceiveAsync(this Socket socket, byte[] buffer, int offset, int size, SocketFlags flags = SocketFlags.None)
{
if (buffer == null) throw new ArgumentNullException("buffer");
return Task<int>.Factory.FromAsync(
(ac, state) => socket.BeginReceive(buffer, offset, size, flags, ac, state),
socket.EndReceive,
null,
TaskCreationOptions.None);
}
示例5: ReceiveAsync
public static Task<int> ReceiveAsync(this Socket socket, ArraySegment<byte> buffer, SocketFlags socketFlags)
{
return Task<int>.Factory.FromAsync(
(callback, state) => socket.BeginReceive(
buffer.Array,
buffer.Offset,
buffer.Count,
socketFlags,
callback,
state),
socket.EndReceive, null);
}
示例6: ReceiveAsync
public static Task<int> ReceiveAsync(this Socket socket, byte[] buffer, int offset, int size, SocketFlags flags, out SocketError error) {
var tsc = new TaskCompletionSource<int> ();
socket.BeginReceive (buffer, offset, size, flags, out error, asyncResult => {
try {
tsc.SetResult (socket.EndReceive (asyncResult));
} catch (Exception e) {
tsc.SetException (e);
}
}, null);
return tsc.Task;
}
示例7: ReceiveAsync
public static Task<int> ReceiveAsync(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.BeginReceive(buffer, offset, size, socketFlags, ar =>
{
try { tcs.TrySetResult(socket.EndReceive(ar)); }
catch (Exception e) { tcs.TrySetException(e); }
}, state: null);
return tcs.Task;
}
示例8: ReceiveAsync
public static async Task<UdpDatagram> ReceiveAsync(this UdpClient client)
{
byte[] bytes;
IPEndPoint ipEndPoint = null;
var task = Task.Factory
.FromAsync<byte[]>(
(callback, state) => client.BeginReceive(callback, state),
(IAsyncResult asyncResult) => client.EndReceive(asyncResult, ref ipEndPoint), null);
bytes = await task;
return new UdpDatagram(ipEndPoint, bytes);
}
示例9: AsyncReceiveFixed
public static void AsyncReceiveFixed(this Socket socket, byte[] buffer, int offset, int len, OnReceived receivedCallback, object state)
{
socket.BeginReceive(buffer, offset, len, SocketFlags.None, ar =>
{
var received = socket.EndReceive(ar);
if (received == 0)
receivedCallback.Invoke(state, null);
if (received < len)
AsyncReceiveFixed(socket, buffer, offset + received, len - received, receivedCallback, state);
else
receivedCallback.Invoke(state, buffer);
}, state);
}
示例10: ReceiveAsync
internal static Task<int> ReceiveAsync(this Socket s, byte[] buffer, int offset, int count)
{
var tcs = new TaskCompletionSource<int>();
s.BeginReceive(buffer, offset, count, SocketFlags.None, iasr =>
{
try
{
tcs.SetResult(s.EndReceive(iasr));
}
catch (Exception e)
{
tcs.SetException(e);
}
}, null);
return tcs.Task;
}
示例11: ReceiveAsync
public static Task<int> ReceiveAsync(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.BeginReceive(buffer, offset, size, socketFlags,
iar => {
try {
tcs.TrySetResult(socket.EndReceive(iar));
}
catch(Exception ex) {
tcs.TrySetException(ex);
}
}, null);
return tcs.Task;
}
示例12: RecieveAsyncTask
public static Task<byte[]> RecieveAsyncTask(this Socket socket, CancellationTokenSource cancellationTokenSource)
{
var buff = new List<ArraySegment<byte>>();
buff.Add(new ArraySegment<byte>(new byte[65535]));
return Task<byte[]>.Factory.FromAsync(
(ac, o) => socket.BeginReceive(buff, SocketFlags.None, ac, null),
ar =>
{
cancellationTokenSource.Token.ThrowIfCancellationRequested();
if (!socket.Connected)
{
throw new InvalidOperationException();
}
var length = socket.EndReceive(ar);
var result = new byte[length];
Array.Copy(buff[0].Array, result, length);
return result;
},
null);
}
示例13: ReceiveAsync
public static Task<int> ReceiveAsync(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.BeginReceive(buffer, offset, size, flags, state =>
{
try
{
source.SetResult(socket.EndReceive(state));
}
catch (Exception e)
{
source.SetException(e);
}
}, source);
return source.Task;
}
示例14: ReceiveAPM
public static void ReceiveAPM(this Socket socket, byte[] buffer, int offset, int count, SocketFlags flags, Action<int> handler)
{
var callback = new AsyncCallback(asyncResult => handler(((Socket)asyncResult.AsyncState).EndReceive(asyncResult)));
socket.BeginReceive(buffer, offset, count, flags, callback, socket);
}
示例15: ReceiveAsync
internal static IObservable<int> ReceiveAsync(this Socket s, byte[] buffer, int offset, int count)
{
return new AsyncOperation<int>(
(c, st) => s.BeginReceive(buffer, offset, count, SocketFlags.None, c, st),
iasr => { return s.EndReceive(iasr); });
}