当前位置: 首页>>代码示例>>C#>>正文


C# this.BeginReceive方法代码示例

本文整理汇总了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;
        }
开发者ID:nieve,项目名称:ravenmq,代码行数:30,代码来源:SocketExtensions.cs

示例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;
        }
开发者ID:vebin,项目名称:madb,代码行数:54,代码来源:SocketExtensions.cs

示例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 });
        }
开发者ID:viktorblondal,项目名称:Nugget,代码行数:7,代码来源:SocketExtension.cs

示例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);
        }
开发者ID:JeffreySu,项目名称:MemcachedSharp,代码行数:10,代码来源:SocketExtensions.cs

示例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);
 }
开发者ID:JonHanna,项目名称:corefx,代码行数:12,代码来源:SocketTaskExtensions.cs

示例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;
		}
开发者ID:rbarinov,项目名称:imap-gmail-nokia-e71-fix,代码行数:13,代码来源:Program.cs

示例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;
        }
开发者ID:buddalasunil999,项目名称:RserveCLI2,代码行数:13,代码来源:SocketExtensions.cs

示例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);
        }
开发者ID:ruisebastiao,项目名称:Omron.Communications,代码行数:14,代码来源:UdpClientExtensions.cs

示例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);
        }
开发者ID:Everlight,项目名称:DesertProject,代码行数:15,代码来源:NetworkExt.cs

示例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;
        }
开发者ID:chakrit,项目名称:kayak,代码行数:18,代码来源:IO.cs

示例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;
        }
开发者ID:debop,项目名称:NFramework,代码行数:21,代码来源:SocketAsync.cs

示例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);
        }
开发者ID:gobixm,项目名称:learn,代码行数:21,代码来源:SocketExtensions.cs

示例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;
        }
开发者ID:ktj007,项目名称:Lz,代码行数:22,代码来源:AsyncSocketExtension.cs

示例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);
 }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:5,代码来源:SocketAPMExtensions.cs

示例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); });
 }
开发者ID:nithinphilips,项目名称:bbwifimusicsync,代码行数:6,代码来源:IO.cs


注:本文中的this.BeginReceive方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。