public static async Task <T> GetDataOverTCPAsync <T>(string dbName, string key) { if (!TcpConnectionPool.Initialized) { TcpConnectionPool.InitializeConnectionPool(_tcpHostAddress, _port, _minPoolSize, _maxPoolSize, _socketRecycleAgeAsMinute); } var CustomSocket = TcpConnectionPool.GetSocket(); try { byte[] sendData = Encoding.ASCII.GetBytes($"get {dbName} {key}"); string response; using (NetworkStream stream = CustomSocket.GetStream()) { await stream.WriteAsync(sendData, 0, sendData.Length); using (MemoryStream buffer = new MemoryStream()) { int b; while ((b = stream.ReadByte()) != -1) { buffer.WriteByte((byte)b); } response = Encoding.UTF8.GetString(buffer.GetBuffer()); } } return(JsonConvert.DeserializeObject <T>(response)); } finally { TcpConnectionPool.PutSocket(CustomSocket); } }
public static T GetDataOverTCP <T>(string dbName, string key) { var CustomSocket = TcpConnectionPool.GetSocket(); try { byte[] sendData = Encoding.ASCII.GetBytes($"get {dbName} {key}"); string response; using (NetworkStream stream = CustomSocket.GetStream()) { stream.Write(sendData, 0, sendData.Length); using (MemoryStream buffer = new MemoryStream()) { int b; while ((b = stream.ReadByte()) != -1) { buffer.WriteByte((byte)b); } response = Encoding.UTF8.GetString(buffer.GetBuffer()); } } return(JsonConvert.DeserializeObject <T>(response)); } finally { TcpConnectionPool.PutSocket(CustomSocket); } }