// Establishes SSL connection iff ssl is not null. public TcpConnection(string host, int port, SslOptions ssl) { _log.Info("Connecting to {0}:{1}...", host, port); _client = new TcpClient(host, port); if (ssl == null) { _strm = _client.GetStream(); } else { try { RemoteCertificateValidationCallback cb = (object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors) => { if (errors == SslPolicyErrors.None) return true; if (errors != SslPolicyErrors.RemoteCertificateChainErrors) { _log.Error("SSL handshake error: {0}", errors); return ssl.AllowAllErrors; } foreach (X509ChainStatus ch in chain.ChainStatus) { if (ch.Status == X509ChainStatusFlags.NotTimeValid && ssl.AllowExpiredCertificate) { _log.Warn("Ignoring NotTimeValid error in SSL handshake."); continue; } if (ch.Status == X509ChainStatusFlags.PartialChain) { _log.Warn("Ignoring PartialChain error in SSL handshake."); continue; } _log.Error("SSL handshake error: {0} {1}", ch.Status, ch.StatusInformation); return ssl.AllowAllErrors; } return true; }; var sslStrm = new SslStream(_client.GetStream(), leaveInnerStreamOpen: false, userCertificateValidationCallback: cb); var certs = new X509CertificateCollection(); if (ssl.CertificateFilename != null) certs.Add(new X509Certificate(ssl.CertificateFilename, ssl.CertificateFilePassword)); sslStrm.AuthenticateAsClient(ssl.CertificateName ?? host, certs, System.Security.Authentication.SslProtocols.Default, checkCertificateRevocation: false); _strm = sslStrm; } catch { Dispose(); throw; } } var protocols = new Dictionary<string, Mantle.IMessageFactory>() { { Mantle.Fix44.Protocol.Value, new Mantle.Fix44.MessageFactory() } }; _receiver = new Mantle.Receiver(_strm, 1 << 20, protocols); }
// Establishes SSL connection iff ssl is not null. public TcpConnection(string host, int port, SslOptions ssl) { _log.Info("Connecting to {0}:{1}...", host, port); _client = new TcpClient(host, port); if (ssl == null) { _strm = _client.GetStream(); } else { try { RemoteCertificateValidationCallback cb = (object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors) => { if (errors == SslPolicyErrors.None) { return(true); } if (errors != SslPolicyErrors.RemoteCertificateChainErrors) { _log.Error("SSL handshake error: {0}", errors); return(ssl.AllowAllErrors); } foreach (X509ChainStatus ch in chain.ChainStatus) { if (ch.Status == X509ChainStatusFlags.NotTimeValid && ssl.AllowExpiredCertificate) { _log.Warn("Ignoring NotTimeValid error in SSL handshake."); continue; } if (ch.Status == X509ChainStatusFlags.PartialChain) { _log.Warn("Ignoring PartialChain error in SSL handshake."); continue; } _log.Error("SSL handshake error: {0} {1}", ch.Status, ch.StatusInformation); return(ssl.AllowAllErrors); } return(true); }; var sslStrm = new SslStream(_client.GetStream(), leaveInnerStreamOpen: false, userCertificateValidationCallback: cb); var certs = new X509CertificateCollection(); if (ssl.CertificateFilename != null) { certs.Add(new X509Certificate(ssl.CertificateFilename, ssl.CertificateFilePassword)); } sslStrm.AuthenticateAsClient(ssl.CertificateName ?? host, certs, System.Security.Authentication.SslProtocols.Default, checkCertificateRevocation: false); _strm = sslStrm; } catch { Dispose(); throw; } } var protocols = new Dictionary <string, Mantle.IMessageFactory>() { { Mantle.Fix44.Protocol.Value, new Mantle.Fix44.MessageFactory() } }; _receiver = new Mantle.Receiver(_strm, 1 << 20, protocols); }
public TcpConnector(string host, int port, SslOptions ssl = null) { _host = host; _port = port; _ssl = ssl; }
public static void Main(string[] args) { try { // Get the keys from huobi.com. string accessKey = "FIXME"; string secretKey = "FIXME"; var ssl = new SslOptions() { AllowExpiredCertificate = true, AllowPartialChain = true, AllowAllErrors = true, }; // "trade" or "market". string api = "trade"; var client = new Crust.Fix44.Client( new Crust.Fix44.ClientConfig() { Username = accessKey, Password = secretKey, SenderCompID = "market", TargetCompID = "server", Account = accessKey, ReplaceEnabled = false, // Huobi supports two symbols: btc and ltc. // You can spell them as btccny and btc/cny if you want. // It's the same when sending order requests. MarketDataSymbols = api == "trade" ? null : new List<string> { "btc" }, Extensions = Crust.Fix44.Extensions.Huobi, SimulateFills = true, }, // One end-point for market data, another for trading. new TcpConnector("106.38.234.75", api == "trade" ? 5001 : 5000, ssl)); client.OnOrderEvent += e => _log.Info("Generated event: {0}", e); client.Connect().Wait(); if (api == "trade") { Thread.Sleep(5000); var req = new NewOrderRequest() { Symbol = "btc", Side = Side.Buy, Quantity = 0.001m, OrderType = OrderType.Limit, Price = 7400.00m, }; _log.Warn("Sending new order"); IOrderCtrl order = client.CreateOrder(req).Result; if (order == null) throw new Exception("Null order"); Thread.Sleep(3000); _log.Warn("Requesting status"); order.RequestStatus().Wait(); Thread.Sleep(3000); _log.Warn("Cancelling"); order.Cancel().Wait(); Thread.Sleep(3000); _log.Warn("Requesting status"); order.RequestStatus().Wait(); } while (true) Thread.Sleep(2000); client.Dispose(); } catch (Exception e) { _log.Fatal(e, "Unexpected exception. Terminating."); } }