private void TcpReceiveLengthCompleted <TMessage>(IAsyncResult ar) where TMessage : DnsMessageBase, new() { DnsClientAsyncState <TMessage> state = (DnsClientAsyncState <TMessage>)ar.AsyncState; if (state.Timer != null) { state.Timer.Dispose(); } if (state.TimedOut) { state.EndpointInfoIndex++; TcpBeginConnect(state); } else { try { state.TcpBytesToReceive -= state.TcpStream.EndRead(ar); if (state.TcpBytesToReceive > 0) { IAsyncResult asyncResult = state.TcpStream.BeginRead(state.Buffer, 2 - state.TcpBytesToReceive, state.TcpBytesToReceive, TcpReceiveLengthCompleted <TMessage>, state); state.Timer = new Timer(TcpTimedOut <TMessage>, asyncResult, state.TimeRemaining, Timeout.Infinite); } else { int tmp = 0; int responseLength = DnsMessageBase.ParseUShort(state.Buffer, ref tmp); state.Buffer = new byte[responseLength]; state.TcpBytesToReceive = responseLength; IAsyncResult asyncResult = state.TcpStream.BeginRead(state.Buffer, 0, responseLength, TcpReceiveCompleted <TMessage>, state); state.Timer = new Timer(TcpTimedOut <TMessage>, asyncResult, state.TimeRemaining, Timeout.Infinite); } } catch (Exception e) { Trace.TraceError("Error on dns query: " + e); try { state.TcpClient.Close(); state.Timer.Dispose(); } catch { } state.EndpointInfoIndex++; TcpBeginConnect(state); } } }
private byte[] QueryByTcp(IPAddress nameServer, byte[] messageData, int messageLength, ref TcpClient tcpClient, ref NetworkStream tcpStream, out IPAddress responderAddress) { responderAddress = nameServer; IPEndPoint endPoint = new IPEndPoint(nameServer, _port); try { if (tcpClient == null) { tcpClient = new TcpClient(nameServer.AddressFamily) { ReceiveTimeout = QueryTimeout, SendTimeout = QueryTimeout }; tcpClient.Connect(endPoint); tcpStream = tcpClient.GetStream(); } int tmp = 0; byte[] lengthBuffer = new byte[2]; if (messageLength > 0) { DnsMessageBase.EncodeUShort(lengthBuffer, ref tmp, (ushort)messageLength); tcpStream.Write(lengthBuffer, 0, 2); tcpStream.Write(messageData, 0, messageLength); } lengthBuffer[0] = (byte)tcpStream.ReadByte(); lengthBuffer[1] = (byte)tcpStream.ReadByte(); tmp = 0; int length = DnsMessageBase.ParseUShort(lengthBuffer, ref tmp); byte[] resultData = new byte[length]; int readBytes = 0; while (readBytes < length) { readBytes += tcpStream.Read(resultData, readBytes, length - readBytes); } return(resultData); } catch (Exception e) { Trace.TraceError("Error on dns query: " + e); return(null); } }
private void EndTcpReadLength(IAsyncResult ar) { TcpClient client = null; NetworkStream stream = null; try { MyState state = (MyState)ar.AsyncState; client = state.Client; stream = state.Stream; state.Timer.Dispose(); state.BytesToReceive -= stream.EndRead(ar); if (state.BytesToReceive > 0) { if (!IsTcpClientConnected(client)) { HandleTcpException(null, stream, client); return; } state.Timer = new Timer(TcpTimedOut, state, state.TimeRemaining, System.Threading.Timeout.Infinite); state.AsyncResult = stream.BeginRead(state.Buffer, state.Buffer.Length - state.BytesToReceive, state.BytesToReceive, EndTcpReadLength, state); } else { int tmp = 0; int length = DnsMessageBase.ParseUShort(state.Buffer, ref tmp); if (length > 0) { state.Buffer = new byte[length]; state.Timer = new Timer(TcpTimedOut, state, state.TimeRemaining, System.Threading.Timeout.Infinite); state.AsyncResult = stream.BeginRead(state.Buffer, 0, length, EndTcpReadData, state); } else { HandleTcpException(null, stream, client); } } } catch (Exception e) { HandleTcpException(e, stream, client); } }