public PooledSocket(SocketPool socketPool, IPEndPoint endPoint, int sendReceiveTimeout, int connectTimeout) { this.socketPool = socketPool; Created = DateTime.Now; //Set up the socket. socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, sendReceiveTimeout); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, sendReceiveTimeout); socket.ReceiveTimeout = sendReceiveTimeout; socket.SendTimeout = sendReceiveTimeout; //Do not use Nagle's Algorithm socket.NoDelay = true; //Establish connection asynchronously to enable connect timeout. IAsyncResult result = socket.BeginConnect(endPoint, null, null); bool success = result.AsyncWaitHandle.WaitOne(connectTimeout, false); if (!success) { try { socket.Close(); } catch { } throw new SocketException(); } socket.EndConnect(result); //Wraps two layers of streams around the socket for communication. stream = new BufferedStream(new NetworkStream(socket, false)); }
private Dictionary<string, string> stats(SocketPool pool) { if (pool == null) { return null; } Dictionary<string, string> result = new Dictionary<string, string>(); serverPool.Execute(pool, delegate (PooledSocket socket) { socket.Write("stats\r\n"); string line; while (!(line = socket.ReadResponse().TrimEnd('\0', '\r', '\n')).StartsWith("END")) { string[] s = line.Split(' '); result.Add(s[1], s[2]); } }); return result; }