/// <summary> /// Create a new DnsServer object /// </summary> public TcpDnsRequestListener(DnsRecordProvider dnsRecordProvider, int defaultPoolSize = 5000, int port = 53) : base(dnsRecordProvider, port) { this.listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); this.connectionsSemaphore = new Semaphore(defaultPoolSize, defaultPoolSize); this.listenerSocket.ReceiveBufferSize = 20000000; //~20 megabytes // Initialize the SocketAsyncEventArgs pool for (int i = 0; i < defaultPoolSize; i++) { var socketEventArg = new SocketAsyncEventArgs(); socketEventArg.Completed += TcpSocketEventArg_Completed; socketEventArg.RemoteEndPoint = new IPEndPoint(IPAddress.Any, this.Port); socketEventArg.SetBuffer(new byte[4096], 0, 4096); // Connect with a request var request = new DnsRequestListenerSession(this, socketEventArg); socketEventArg.UserToken = request; // Add to the pool this._socketAsyncEventArgsPool.Push(socketEventArg); } }
/// <summary> /// Send the response back to the client /// </summary> /// <param name="request"></param> /// <param name="bytesWritten"></param> public abstract void SendResponse(DnsRequestListenerSession request, int bytesWritten);
public override void SendResponse(DnsRequestListenerSession request, int bytesWritten) { request.SocketAsyncEventArgs.SetBuffer(0, bytesWritten); request.Socket.SendAsync(request.SocketAsyncEventArgs); }
public override void SendResponse(DnsRequestListenerSession request, int bytesWritten) { request.SocketAsyncEventArgs.SetBuffer(0, bytesWritten); this.listenerSocket.SendToAsync(request.SocketAsyncEventArgs); }