private void Connect() { if (UsageTimer == null) { //Save Timer Resource for licensed usage if (!LicenseUtils.HasLicensedFeature(LicenseFeature.Redis)) { UsageTimer = new Timer(delegate { __requestsPerHour = 0; }, null, TimeSpan.FromMilliseconds(0), TimeSpan.FromHours(1)); } } socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { SendTimeout = SendTimeout, ReceiveTimeout = ReceiveTimeout }; try { if (ConnectTimeout <= 0) { socket.Connect(Host, Port); } else { var connectResult = socket.BeginConnect(Host, Port, null, null); connectResult.AsyncWaitHandle.WaitOne(ConnectTimeout, true); } if (!socket.Connected) { socket.Close(); socket = null; DeactivatedAt = DateTime.UtcNow; return; } Stream networkStream = new NetworkStream(socket); if (Ssl) { if (Env.IsMono) { //Mono doesn't support EncryptionPolicy sslStream = new SslStream(networkStream, leaveInnerStreamOpen: false, userCertificateValidationCallback: RedisConfig.CertificateValidationCallback, userCertificateSelectionCallback: RedisConfig.CertificateSelectionCallback); } else { var ctor = typeof(SslStream).GetConstructors() .First(x => x.GetParameters().Length == 5); var policyType = AssemblyUtils.FindType("System.Net.Security.EncryptionPolicy"); var policyValue = Enum.Parse(policyType, "RequireEncryption"); sslStream = (SslStream)ctor.Invoke(new[] { networkStream, false, RedisConfig.CertificateValidationCallback, RedisConfig.CertificateSelectionCallback, policyValue, }); } sslStream.AuthenticateAsClient(Host); if (!sslStream.IsEncrypted) { throw new Exception("Could not establish an encrypted connection to " + Host); } networkStream = sslStream; } Bstream = new BufferedStream(networkStream, 16 * 1024); if (!string.IsNullOrEmpty(Password)) { SendExpectSuccess(Commands.Auth, Password.ToUtf8Bytes()); } if (db != 0) { SendExpectSuccess(Commands.Select, db.ToUtf8Bytes()); } if (Client != null) { SendExpectSuccess(Commands.Client, Commands.SetName, Client.ToUtf8Bytes()); } try { if (ServerVersionNumber == 0) { var parts = ServerVersion.Split('.'); var version = int.Parse(parts[0]) * 1000; if (parts.Length > 1) { version += int.Parse(parts[1]) * 100; } if (parts.Length > 2) { version += int.Parse(parts[2]); } ServerVersionNumber = version; } } catch (Exception) { //Twemproxy doesn't support the INFO command so automatically closes the socket //Fallback to ServerVersionNumber=Unknown then try re-connecting ServerVersionNumber = Unknown; Connect(); return; } var ipEndpoint = socket.LocalEndPoint as IPEndPoint; clientPort = ipEndpoint != null ? ipEndpoint.Port : -1; lastCommand = null; lastSocketException = null; LastConnectedAtTimestamp = Stopwatch.GetTimestamp(); OnConnected(); if (ConnectionFilter != null) { ConnectionFilter(this); } } catch (SocketException) { log.Error(ErrorConnect.Fmt(Host, Port)); throw; } }
private void Connect() { if (UsageTimer == null) { //Save Timer Resource for licensed usage if (!LicenseUtils.HasLicensedFeature(LicenseFeature.Redis)) { UsageTimer = new Timer(delegate { __requestsPerHour = 0; }, null, TimeSpan.FromMilliseconds(0), TimeSpan.FromHours(1)); } } socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { SendTimeout = SendTimeout, ReceiveTimeout = ReceiveTimeout }; try { if (ConnectTimeout == 0) { socket.Connect(Host, Port); } else { var connectResult = socket.BeginConnect(Host, Port, null, null); connectResult.AsyncWaitHandle.WaitOne(ConnectTimeout, true); } if (!socket.Connected) { socket.Close(); socket = null; HadExceptions = true; return; } Bstream = new BufferedStream(new NetworkStream(socket), 16 * 1024); if (Password != null) { SendExpectSuccess(Commands.Auth, Password.ToUtf8Bytes()); } if (db != 0) { SendExpectSuccess(Commands.Select, db.ToUtf8Bytes()); } try { if (ServerVersionNumber == 0) { var parts = ServerVersion.Split('.'); var version = int.Parse(parts[0]) * 1000; if (parts.Length > 1) { version += int.Parse(parts[1]) * 100; } if (parts.Length > 2) { version += int.Parse(parts[2]); } ServerVersionNumber = version; } } catch (Exception) { //Twemproxy doesn't support the INFO command so automatically closes the socket //Fallback to ServerVersionNumber=Unknown then try re-connecting ServerVersionNumber = Unknown; Connect(); return; } var ipEndpoint = socket.LocalEndPoint as IPEndPoint; clientPort = ipEndpoint != null ? ipEndpoint.Port : -1; lastCommand = null; lastSocketException = null; LastConnectedAtTimestamp = Stopwatch.GetTimestamp(); OnConnected(); if (ConnectionFilter != null) { ConnectionFilter(this); } } 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; } }
private void Connect() { if (UsageTimer == null) { //Save Timer Resource for licensed usage if (!LicenseUtils.HasLicensedFeature(LicenseFeature.Redis)) { UsageTimer = new Timer(delegate { __requestsPerHour = 0; }, null, TimeSpan.FromMilliseconds(0), TimeSpan.FromHours(1)); } } socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { SendTimeout = SendTimeout, ReceiveTimeout = ReceiveTimeout }; try { if (ConnectTimeout == 0) { socket.Connect(Host, Port); } else { var connectResult = socket.BeginConnect(Host, Port, null, null); connectResult.AsyncWaitHandle.WaitOne(ConnectTimeout, true); } if (!socket.Connected) { socket.Close(); socket = null; HadExceptions = true; return; } Bstream = new BufferedStream(new NetworkStream(socket), 16 * 1024); if (Password != null) { SendExpectSuccess(Commands.Auth, Password.ToUtf8Bytes()); } if (db != 0) { SendExpectSuccess(Commands.Select, db.ToUtf8Bytes()); } try { if (ServerVersionNumber == 0) { ServerVersionNumber = int.Parse(ServerVersion.Replace(".", "").PadRight(4, '0')); } } catch {} var ipEndpoint = socket.LocalEndPoint as IPEndPoint; clientPort = ipEndpoint != null ? ipEndpoint.Port : -1; lastCommand = null; lastSocketException = null; LastConnectedAtTimestamp = Stopwatch.GetTimestamp(); OnConnected(); if (ConnectionFilter != null) { ConnectionFilter(this); } } 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; } }
public static void Main(string[] args) { if (args.Length != 2) { if (args.Length > 0) { ("Invalid Arguments: " + args.Join(" ")).Print(); } "Syntax:".Print(); "northwind-data [provider] [connectionString]".Print(); "\nAvailable Data Providers:".Print(); Providers.Each(x => $" - {x}".Print()); return; } var hasOrmLite = LicenseUtils.HasLicensedFeature(LicenseFeature.OrmLite); var dbFactory = new OrmLiteConnectionFactory("~/../../apps/northwind.sqlite".MapProjectPath(), SqliteDialect.Provider); var db = dbFactory.Open(); var categories = db.Select <Category>(); var customers = db.Select <Customer>(); var employees = db.Select <Employee>(); var orders = db.Select <Order>(); var orderDetails = db.Select <OrderDetail>(); var products = db.Select <Product>(); var regions = db.Select <Region>(); var shippers = db.Select <Shipper>(); var suppliers = db.Select <Supplier>(); var territories = db.Select <Territory>(); var employeeTerritories = hasOrmLite ? db.Select <EmployeeTerritory>() : null; db.Dispose(); var provider = ResolveValue(args[0]); var connectionString = ResolveValue(args[1]); dbFactory = GetDbFactory(provider, connectionString); if (dbFactory != null) { using (db = dbFactory.Open()) { db.DropAndCreateTable <Category>(); $"Created table {nameof(Category)}".Print(); db.DropAndCreateTable <Customer>(); $"Created table {nameof(Customer)}".Print(); db.DropAndCreateTable <Employee>(); $"Created table {nameof(Employee)}".Print(); db.DropAndCreateTable <Order>(); $"Created table {nameof(Order)}".Print(); db.DropAndCreateTable <OrderDetail>(); $"Created table {nameof(OrderDetail)}".Print(); db.DropAndCreateTable <Product>(); $"Created table {nameof(Product)}".Print(); db.DropAndCreateTable <Region>(); $"Created table {nameof(Region)}".Print(); db.DropAndCreateTable <Shipper>(); $"Created table {nameof(Shipper)}".Print(); db.DropAndCreateTable <Supplier>(); $"Created table {nameof(Supplier)}".Print(); db.DropAndCreateTable <Territory>(); $"Created table {nameof(Territory)}".Print(); if (hasOrmLite) { db.DropAndCreateTable <EmployeeTerritory>(); $"Created table {nameof(EmployeeTerritory)}".Print(); } "".Print(); db.InsertAll(categories); $"Inserted {categories.Count} rows in {nameof(Category)}".Print(); db.InsertAll(customers); $"Inserted {customers.Count} rows in {nameof(Customer)}".Print(); db.InsertAll(employees); $"Inserted {employees.Count} rows in {nameof(Employee)}".Print(); db.InsertAll(orders); $"Inserted {orders.Count} rows in {nameof(Order)}".Print(); db.InsertAll(orderDetails); $"Inserted {orderDetails.Count} rows in {nameof(OrderDetail)}".Print(); db.InsertAll(products); $"Inserted {products.Count} rows in {nameof(Product)}".Print(); db.InsertAll(regions); $"Inserted {regions.Count} rows in {nameof(Region)}".Print(); db.InsertAll(shippers); $"Inserted {shippers.Count} rows in {nameof(Shipper)}".Print(); db.InsertAll(suppliers); $"Inserted {suppliers.Count} rows in {nameof(Supplier)}".Print(); db.InsertAll(territories); $"Inserted {territories.Count} rows in {nameof(Territory)}".Print(); if (hasOrmLite) { db.InsertAll(employeeTerritories); $"Inserted {employeeTerritories.Count} rows in {nameof(EmployeeTerritory)}".Print(); } } } else if (provider == "redis") { var redisManager = new RedisManagerPool(connectionString); using (var redis = redisManager.GetClient()) { redis.StoreAll(categories); redis.StoreAll(customers); redis.StoreAll(employees); redis.StoreAll(employeeTerritories); redis.StoreAll(orders); redis.StoreAll(orderDetails); redis.StoreAll(products); redis.StoreAll(regions); redis.StoreAll(shippers); redis.StoreAll(suppliers); redis.StoreAll(territories); } } else { $"Unknown Provider: {provider}".Print(); "Available Providers:".Print(); Providers.Join(", ").Print(); } }