ServiceStack.Redis
private void Connect()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
{
SendTimeout = SendTimeout
};
try
{
socket.Connect(Host, Port);
if (!socket.Connected)
{
socket.Close();
socket = null;
return;
}
Bstream = new BufferedStream(new NetworkStream(socket), 16 * 1024);
if (Password != null)
SendExpectSuccess(Commands.Auth, Password.ToUtf8Bytes());
db = 0;
var ipEndpoint = socket.LocalEndPoint as IPEndPoint;
clientPort = ipEndpoint != null ? ipEndpoint.Port : -1;
lastCommand = null;
lastSocketException = null;
LastConnectedAtTimestamp = Stopwatch.GetTimestamp();
if (isPreVersion1_26 == null)
{
isPreVersion1_26 = this.ServerVersion.CompareTo("1.2.6") <= 0;
}
}
catch (SocketException ex)
{
if (socket != null)
socket.Close();
socket = null;
HadExceptions = true;
var throwEx = new RedisException("could not connect to redis Instance at " + Host + ":" + Port, ex);
log.Error(throwEx.Message, ex);
throw throwEx;
}
}
internal bool IsDisposed { get; set; }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~RedisNativeClient()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (ClientManager != null)
{
ClientManager.DisposeClient(this);
return;
}
if (disposing)
{
//dispose un managed resources
DisposeConnection();
}
}
internal void DisposeConnection()
{
if (IsDisposed) return;
IsDisposed = true;
if (socket == null) return;
try
{
Quit();
}
catch (Exception ex)
{
log.Error("Error when trying to Quit()", ex);
}
finally
{
SafeConnectionClose();
}
}
private void SafeConnectionClose()
{
try
{
// workaround for a .net bug: http://support.microsoft.com/kb/821625
if (Bstream != null)
Bstream.Close();
}
catch { }
try
{
if (socket != null)
socket.Close();
}
catch { }
Bstream = null;
socket = null;
}
Redis-Sharp
void Connect()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.NoDelay = true;
socket.SendTimeout = SendTimeout;
socket.Connect(Host, Port);
if (!socket.Connected)
{
socket.Close();
socket = null;
return;
}
//Redis read buffer size is now 16k. Limit to 1MB payloads removed.
bstream = new BufferedStream(new NetworkStream(socket), 16 * 1024);
if (Password != null)
SendExpectSuccess("AUTH {0}\r\n", Password);
}
byte[] end_data = new byte[] { (byte)'\r', (byte)'\n' };
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~Redis()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
SendCommand("QUIT\r\n");
socket.Close();
socket = null;
}
}