Пример #1
0
        /// <summary>
        /// 异步发送
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="arraySegment">缓冲区</param>
        /// <param name="timeout">等待数据的超时时间</param>
        /// <exception cref="SocketException"></exception>
        /// <exception cref="TimeoutException"></exception>
        /// <returns></returns>
        public static async Task <int> SendTaskAsync(this Socket socket, ArraySegment <byte> arraySegment, TimeSpan?timeout)
        {
            var token = new TaskSetter <int>(timeout);
            var e     = new SocketAsyncEventArgs
            {
                UserToken = token
            };

            using (e)
            {
                e.SetBuffer(arraySegment.Array, arraySegment.Offset, arraySegment.Count);
                e.Completed += OnEndSend;

                if (socket.SendAsync(e) == false)
                {
                    OnEndSend(socket, e);
                }
                return(await token.Task);
            }
        }
Пример #2
0
        /// <summary>
        /// 异步执行握手请求
        /// </summary>
        /// <param name="client">客户端</param>
        /// <returns></returns>
        public Task <SocketError> ExecuteAsync(WebSocketClient client)
        {
            try
            {
                this.IsWaitting = true;
                this.taskSetter = new TaskSetter <SocketError>((s) => this.TrySetResult(SocketError.TimedOut)).TimeoutAfter(this.timeout);

                var content         = this.GenerateHandshakeContent(client, out this.secKey);
                var handshakeBuffer = Encoding.ASCII.GetBytes(content);
                client.Send(handshakeBuffer);
            }
            catch (SocketException ex)
            {
                this.TrySetResult(ex.SocketErrorCode);
            }
            catch (Exception)
            {
                this.TrySetResult(SocketError.SocketError);
            }
            return(this.taskSetter.Task);
        }
Пример #3
0
        /// <summary>
        /// 异步连接
        /// </summary>
        /// <param name="socket">socket</param>
        /// <param name="remoteEndPoint">远程终结点</param>
        /// <param name="timeout">连接的超时时间</param>
        /// <exception cref="SocketException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="TimeoutException"></exception>
        /// <returns></returns>
        public static async Task ConnectTaskAsync(this Socket socket, EndPoint remoteEndPoint, TimeSpan?timeout)
        {
            void OnEndConnect(object sender, SocketAsyncEventArgs e)
            {
                var setter = e.UserToken as TaskSetter <object>;

                if (e.SocketError == SocketError.Success)
                {
                    setter.SetResult(null);
                }
                else
                {
                    var ex = new SocketException((int)e.SocketError);
                    setter.SetException(ex);
                }
            }

            if (remoteEndPoint == null)
            {
                throw new ArgumentNullException(nameof(remoteEndPoint));
            }

            var taskSetter = new TaskSetter <object>(timeout);

            using (var args = new SocketAsyncEventArgs())
            {
                args.UserToken      = taskSetter;
                args.RemoteEndPoint = remoteEndPoint;
                args.Completed     += OnEndConnect;

                if (socket.ConnectAsync(args) == false)
                {
                    OnEndConnect(socket, args);
                }
                await taskSetter.Task.ConfigureAwait(false);
            }
        }
Пример #4
0
        /// <summary>
        /// 异步连接
        /// </summary>
        /// <param name="socket">socket</param>
        /// <param name="remoteEndPoint">远程终结点</param>
        /// <param name="timeout">连接的超时时间</param>
        /// <exception cref="SocketException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="TimeoutException"></exception>
        /// <returns></returns>
        public static async Task ConnectTaskAsync(this Socket socket, EndPoint remoteEndPoint, TimeSpan?timeout)
        {
            if (remoteEndPoint == null)
            {
                throw new ArgumentNullException(nameof(remoteEndPoint));
            }

            var token = new TaskSetter <object>(timeout);
            var e     = new SocketAsyncEventArgs
            {
                RemoteEndPoint = remoteEndPoint,
                UserToken      = token
            };

            using (e)
            {
                e.Completed += OnEndConnect;
                if (socket.ConnectAsync(e) == false)
                {
                    OnEndConnect(socket, e);
                }
                await token.Task;
            }
        }