示例#1
0
        /// <summary>
        /// 编码处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <IResponse> Code(IRequest request, CancellationToken cancellationToken = default(CancellationToken))
        {
            using (UdpClient udp = new UdpClient())
            {
                await udp.SendAsync(request.ToArray(), request.Size, dns).WithCancellationTimeout(TimeSpan.FromMilliseconds(timeout), cancellationToken);

                UdpReceiveResult result = await udp.ReceiveAsync().WithCancellationTimeout(TimeSpan.FromMilliseconds(timeout), cancellationToken);

                if (!result.RemoteEndPoint.Equals(dns))
                {
                    throw new IOException("Remote endpoint mismatch");
                }

                byte[] buffer = result.Buffer;

                DnsResponseMessage response = DnsResponseMessage.FromArray(buffer);

                if (response.Truncated)
                {
                    return(await fallback.Code(request, cancellationToken));
                }

                return(new Model.DnsClientResponse(request, response, buffer));
            }
        }
示例#2
0
        /// <summary>
        /// 编码处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <IResponse> Code(IRequest request, CancellationToken cancellationToken = default(CancellationToken))
        {
            using (var udpClient = SocketFactory.CreateClientSocket(SocketOptionBuilder.Instance.SetSocket(SAEASocketType.Udp)
                                                                    .SetIPEndPoint(_ipEndpoint)
                                                                    .UseIocp <BaseUnpacker>()
                                                                    .SetReadBufferSize(SocketOption.UDPMaxLength)
                                                                    .SetWriteBufferSize(SocketOption.UDPMaxLength)
                                                                    .ReusePort()
                                                                    .Build()))
            {
                udpClient.OnReceive += UdpClient_OnReceive;

                udpClient.Connect();

                var buffer = _orderSyncHelper.Wait(() =>
                {
                    udpClient.SendAsync(request.ToArray());
                });

                DnsResponseMessage response = DnsResponseMessage.FromArray(buffer);

                if (response.Truncated)
                {
                    return(await _fallback.Code(request, cancellationToken));
                }
                return(new Model.DnsClientResponse(request, response, buffer));
            }
        }
示例#3
0
        public static DnsClientResponse FromArray(IRequest request, byte[] message)
        {
            DnsResponseMessage response = DnsResponseMessage.FromArray(message);

            return(new DnsClientResponse(request, response, message));
        }