public static IAppBuilder ContentLength(this IAppBuilder builder) { return builder.Transform((response, respond, fault) => { var status = response.Item1; var headers = response.Item2; var body = response.Item3; if (!ExcludeStatusesStartingWith.Any(i => status.StartsWith(i)) && !headers.ContainsKey("content-length") && !headers.ContainsKey("transfer-encoding") && body != null) { var buffer = new DataBuffer(); body((data, ack) => { buffer.Add(data); return false; }, fault, () => { headers["Content-Length"] = buffer.GetCount().ToString(); respond(Tuple.Create<string, IDictionary<string, string>, BodyDelegate>(status, headers, (onNext, onError, onComplete) => { buffer.Each(d => onNext(new ArraySegment<byte>(d), null)); onComplete(); return null; })); }); } else respond(response); }); }
static DataBuffer CreateBody(string[] body) { var bodyBuffer = new DataBuffer(); foreach (var b in body) bodyBuffer.Add(new ArraySegment<byte>(Encoding.ASCII.GetBytes(b))); return bodyBuffer; }
/// <summary> /// Send message. /// </summary> /// <param name="message">Message.</param> public override void SendInMessage(Message message) { switch (message.Type) { case MessageTypes.Reset: { _subscriptions.Clear(); _ticksBuffer.Clear(); _level1Buffer.Clear(); _candleBuffer.Clear(); _orderLogBuffer.Clear(); _orderBooksBuffer.Clear(); _transactionsBuffer.Clear(); _newsBuffer.Clear(); _positionChangesBuffer.Clear(); //SendOutMessage(new ResetMessage()); break; } case MessageTypes.OrderRegister: var regMsg = (OrderRegisterMessage)message; if (!CanStore <ExecutionMessage>(regMsg.SecurityId, ExecutionTypes.Transaction)) { break; } _transactionsBuffer.Add(regMsg.SecurityId, new ExecutionMessage { ServerTime = DateTimeOffset.Now, ExecutionType = ExecutionTypes.Transaction, SecurityId = regMsg.SecurityId, HasOrderInfo = true, OrderPrice = regMsg.Price, OrderVolume = regMsg.Volume, Currency = regMsg.Currency, PortfolioName = regMsg.PortfolioName, ClientCode = regMsg.ClientCode, BrokerCode = regMsg.BrokerCode, Comment = regMsg.Comment, Side = regMsg.Side, TimeInForce = regMsg.TimeInForce, ExpiryDate = regMsg.TillDate, VisibleVolume = regMsg.VisibleVolume, LocalTime = regMsg.LocalTime, TransactionId = regMsg.TransactionId, IsMarketMaker = regMsg.IsMarketMaker, OrderType = regMsg.OrderType, UserOrderId = regMsg.UserOrderId, //RepoInfo = regMsg.RepoInfo?.Clone(), //RpsInfo = regMsg.RpsInfo?.Clone(), }); break; case MessageTypes.OrderCancel: var cancelMsg = (OrderCancelMessage)message; if (!CanStore <ExecutionMessage>(cancelMsg.SecurityId, ExecutionTypes.Transaction)) { break; } _transactionsBuffer.Add(cancelMsg.SecurityId, new ExecutionMessage { ServerTime = DateTimeOffset.Now, ExecutionType = ExecutionTypes.Transaction, SecurityId = cancelMsg.SecurityId, HasOrderInfo = true, IsCancelled = true, OrderId = cancelMsg.OrderId, OrderStringId = cancelMsg.OrderStringId, OriginalTransactionId = cancelMsg.OrderTransactionId, OrderVolume = cancelMsg.Volume, //Side = cancelMsg.Side, }); break; } base.SendInMessage(message); }
/// <inheritdoc /> protected override void OnInnerAdapterNewOutMessage(Message message) { switch (message.Type) { case MessageTypes.Level1Change: { var level1Msg = (Level1ChangeMessage)message; if (CanStore <Level1ChangeMessage>(level1Msg.SecurityId)) { _level1Buffer.Add(level1Msg.SecurityId, (Level1ChangeMessage)level1Msg.Clone()); } break; } case MessageTypes.QuoteChange: { var quotesMsg = (QuoteChangeMessage)message; if (CanStore <QuoteChangeMessage>(quotesMsg.SecurityId)) { _orderBooksBuffer.Add(quotesMsg.SecurityId, (QuoteChangeMessage)quotesMsg.Clone()); } break; } case MessageTypes.Execution: { var execMsg = (ExecutionMessage)message; DataBuffer <SecurityId, ExecutionMessage> buffer; var secId = execMsg.SecurityId; var execType = execMsg.ExecutionType; switch (execType) { case ExecutionTypes.Tick: buffer = _ticksBuffer; break; case ExecutionTypes.Transaction: // some responses do not contains sec id if (secId.IsDefault() && !_securityIds.TryGetValue(execMsg.OriginalTransactionId, out secId)) { base.OnInnerAdapterNewOutMessage(message); return; } buffer = _transactionsBuffer; break; case ExecutionTypes.OrderLog: buffer = _orderLogBuffer; break; default: throw new ArgumentOutOfRangeException(nameof(message), execType, LocalizedStrings.Str1695Params.Put(message)); } if (execType == ExecutionTypes.Transaction || CanStore <ExecutionMessage>(secId, execType)) { buffer.Add(secId, (ExecutionMessage)message.Clone()); } break; } case MessageTypes.CandlePnF: case MessageTypes.CandleRange: case MessageTypes.CandleRenko: case MessageTypes.CandleTick: case MessageTypes.CandleTimeFrame: case MessageTypes.CandleVolume: { var candleMsg = (CandleMessage)message; if (CanStore(candleMsg.SecurityId, candleMsg.GetType(), candleMsg.Arg)) { _candleBuffer.Add(Tuple.Create(candleMsg.SecurityId, candleMsg.GetType(), candleMsg.Arg), (CandleMessage)candleMsg.Clone()); } break; } case MessageTypes.News: { if (CanStore <NewsMessage>(default)) { _newsBuffer.Add((NewsMessage)message.Clone()); } break; }
/// <summary> /// Process message. /// </summary> /// <param name="message">Message.</param> public void ProcessMessage(Message message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } if (message.OfflineMode != MessageOfflineModes.None) { return; } switch (message.Type) { // in message case MessageTypes.Reset: { _subscriptionsById.Clear(); _ticksBuffer.Clear(); _level1Buffer.Clear(); _candleBuffer.Clear(); _orderLogBuffer.Clear(); _orderBooksBuffer.Clear(); _transactionsBuffer.Clear(); _newsBuffer.Clear(); _positionChangesBuffer.Clear(); //SendOutMessage(new ResetMessage()); break; } case MessageTypes.OrderRegister: { var regMsg = (OrderRegisterMessage)message; //if (!CanStore<ExecutionMessage>(regMsg.SecurityId, ExecutionTypes.Transaction)) // break; // try - cause looped back messages from offline adapter _securityIds.TryAdd(regMsg.TransactionId, regMsg.SecurityId); _transactionsBuffer.Add(regMsg.SecurityId, new ExecutionMessage { ServerTime = DateTimeOffset.Now, ExecutionType = ExecutionTypes.Transaction, SecurityId = regMsg.SecurityId, TransactionId = regMsg.TransactionId, HasOrderInfo = true, OrderPrice = regMsg.Price, OrderVolume = regMsg.Volume, Currency = regMsg.Currency, PortfolioName = regMsg.PortfolioName, ClientCode = regMsg.ClientCode, BrokerCode = regMsg.BrokerCode, Comment = regMsg.Comment, Side = regMsg.Side, TimeInForce = regMsg.TimeInForce, ExpiryDate = regMsg.TillDate, Balance = regMsg.Volume, VisibleVolume = regMsg.VisibleVolume, LocalTime = regMsg.LocalTime, IsMarketMaker = regMsg.IsMarketMaker, IsMargin = regMsg.IsMargin, Slippage = regMsg.Slippage, IsManual = regMsg.IsManual, OrderType = regMsg.OrderType, UserOrderId = regMsg.UserOrderId, OrderState = OrderStates.Pending, Condition = regMsg.Condition?.Clone(), }); break; } case MessageTypes.OrderCancel: { var cancelMsg = (OrderCancelMessage)message; //if (!CanStore<ExecutionMessage>(cancelMsg.SecurityId, ExecutionTypes.Transaction)) // break; // try - cause looped back messages from offline adapter _securityIds.TryAdd(cancelMsg.TransactionId, cancelMsg.SecurityId); _transactionsBuffer.Add(cancelMsg.SecurityId, new ExecutionMessage { ServerTime = DateTimeOffset.Now, ExecutionType = ExecutionTypes.Transaction, SecurityId = cancelMsg.SecurityId, HasOrderInfo = true, TransactionId = cancelMsg.TransactionId, IsCancellation = true, OrderId = cancelMsg.OrderId, OrderStringId = cancelMsg.OrderStringId, OriginalTransactionId = cancelMsg.OriginalTransactionId, OrderVolume = cancelMsg.Volume, //Side = cancelMsg.Side, }); break; } case MessageTypes.MarketData: { var mdMsg = (MarketDataMessage)message; if (Enabled) { if (mdMsg.IsSubscribe) { _subscriptionsById.Add(mdMsg.TransactionId); } else { _subscriptionsById.Remove(mdMsg.OriginalTransactionId); } } break; } // out messages case MessageTypes.Level1Change: { var level1Msg = (Level1ChangeMessage)message; if (CanStore(level1Msg)) { _level1Buffer.Add(level1Msg.SecurityId, level1Msg.TypedClone()); } break; } case MessageTypes.QuoteChange: { var quotesMsg = (QuoteChangeMessage)message; if (CanStore(quotesMsg)) { _orderBooksBuffer.Add(quotesMsg.SecurityId, quotesMsg.TypedClone()); } break; } case MessageTypes.Execution: { var execMsg = (ExecutionMessage)message; DataBuffer <SecurityId, ExecutionMessage> buffer; var secId = execMsg.SecurityId; var execType = execMsg.ExecutionType; switch (execType) { case ExecutionTypes.Tick: buffer = _ticksBuffer; break; case ExecutionTypes.Transaction: { // some responses do not contains sec id if (secId.IsDefault() && !_securityIds.TryGetValue(execMsg.OriginalTransactionId, out secId)) { return; } buffer = _transactionsBuffer; break; } case ExecutionTypes.OrderLog: buffer = _orderLogBuffer; break; default: throw new ArgumentOutOfRangeException(nameof(message), execType, LocalizedStrings.Str1695Params.Put(message)); } //if (execType == ExecutionTypes.Transaction && execMsg.TransactionId == 0) // break; if (execType == ExecutionTypes.Transaction || CanStore(execMsg)) { buffer.Add(secId, execMsg.TypedClone()); } break; } case MessageTypes.News: { var newsMsg = (NewsMessage)message; if (CanStore(newsMsg)) { _newsBuffer.Add(newsMsg.TypedClone()); } break; } case MessageTypes.PositionChange: { var posMsg = (PositionChangeMessage)message; var secId = posMsg.SecurityId; //if (CanStore<PositionChangeMessage>(secId)) _positionChangesBuffer.Add(secId, posMsg.TypedClone()); break; } default: { if (message is CandleMessage candleMsg && candleMsg.State == CandleStates.Finished) { if (CanStore(candleMsg)) { _candleBuffer.Add(Tuple.Create(candleMsg.SecurityId, candleMsg.GetType(), candleMsg.Arg), candleMsg.TypedClone()); } } break; } } }
/// <inheritdoc /> protected override void OnSendInMessage(Message message) { switch (message.Type) { case MessageTypes.Reset: { ClearSubscriptions(); _ticksBuffer.Clear(); _level1Buffer.Clear(); _candleBuffer.Clear(); _orderLogBuffer.Clear(); _orderBooksBuffer.Clear(); _transactionsBuffer.Clear(); _newsBuffer.Clear(); _positionChangesBuffer.Clear(); //SendOutMessage(new ResetMessage()); break; } case MessageTypes.OrderRegister: var regMsg = (OrderRegisterMessage)message; //if (!CanStore<ExecutionMessage>(regMsg.SecurityId, ExecutionTypes.Transaction)) // break; // try - cause looped back messages from offline adapter _securityIds.TryAdd(regMsg.TransactionId, regMsg.SecurityId); _transactionsBuffer.Add(regMsg.SecurityId, new ExecutionMessage { ServerTime = DateTimeOffset.Now, ExecutionType = ExecutionTypes.Transaction, SecurityId = regMsg.SecurityId, TransactionId = regMsg.TransactionId, HasOrderInfo = true, OrderPrice = regMsg.Price, OrderVolume = regMsg.Volume, Currency = regMsg.Currency, PortfolioName = regMsg.PortfolioName, ClientCode = regMsg.ClientCode, BrokerCode = regMsg.BrokerCode, Comment = regMsg.Comment, Side = regMsg.Side, TimeInForce = regMsg.TimeInForce, ExpiryDate = regMsg.TillDate, Balance = regMsg.Volume, VisibleVolume = regMsg.VisibleVolume, LocalTime = regMsg.LocalTime, IsMarketMaker = regMsg.IsMarketMaker, IsMargin = regMsg.IsMargin, Slippage = regMsg.Slippage, IsManual = regMsg.IsManual, OrderType = regMsg.OrderType, UserOrderId = regMsg.UserOrderId, OrderState = OrderStates.Pending, Condition = regMsg.Condition?.Clone(), }); break; case MessageTypes.OrderCancel: var cancelMsg = (OrderCancelMessage)message; //if (!CanStore<ExecutionMessage>(cancelMsg.SecurityId, ExecutionTypes.Transaction)) // break; // try - cause looped back messages from offline adapter _securityIds.TryAdd(cancelMsg.TransactionId, cancelMsg.SecurityId); _transactionsBuffer.Add(cancelMsg.SecurityId, new ExecutionMessage { ServerTime = DateTimeOffset.Now, ExecutionType = ExecutionTypes.Transaction, SecurityId = cancelMsg.SecurityId, HasOrderInfo = true, TransactionId = cancelMsg.TransactionId, IsCancelled = true, OrderId = cancelMsg.OrderId, OrderStringId = cancelMsg.OrderStringId, OriginalTransactionId = cancelMsg.OriginalTransactionId, OrderVolume = cancelMsg.Volume, //Side = cancelMsg.Side, }); break; } base.OnSendInMessage(message); }
/// <summary> /// Process message. /// </summary> /// <param name="message">Message.</param> public void SendOutMessage(Message message) { if (message is null) { throw new ArgumentNullException(nameof(message)); } if (message.OfflineMode != MessageOfflineModes.None) { return; } switch (message.Type) { case MessageTypes.Level1Change: { var level1Msg = (Level1ChangeMessage)message; if (CanStore(level1Msg)) { _level1Buffer.Add(level1Msg.SecurityId, level1Msg.TypedClone()); } break; } case MessageTypes.QuoteChange: { var quotesMsg = (QuoteChangeMessage)message; if (CanStore(quotesMsg)) { _orderBooksBuffer.Add(quotesMsg.SecurityId, quotesMsg.TypedClone()); } break; } case MessageTypes.Execution: { var execMsg = (ExecutionMessage)message; DataBuffer <SecurityId, ExecutionMessage> buffer; var secId = execMsg.SecurityId; var execType = execMsg.ExecutionType; switch (execType) { case ExecutionTypes.Tick: buffer = _ticksBuffer; break; case ExecutionTypes.Transaction: buffer = _transactionsBuffer; break; case ExecutionTypes.OrderLog: buffer = _orderLogBuffer; break; default: throw new ArgumentOutOfRangeException(nameof(message), execType, LocalizedStrings.Str1695Params.Put(message)); } if (CanStore(execMsg)) { buffer.Add(secId, execMsg.TypedClone()); } break; } case MessageTypes.News: { var newsMsg = (NewsMessage)message; if (CanStore(newsMsg)) { _newsBuffer.Add(newsMsg.TypedClone()); } break; } case MessageTypes.PositionChange: { var posMsg = (PositionChangeMessage)message; var secId = posMsg.SecurityId; if (CanStore(posMsg)) { _positionChangesBuffer.Add(secId, posMsg.TypedClone()); } break; } default: { if (message is CandleMessage candleMsg && candleMsg.State == CandleStates.Finished) { if (CanStore(candleMsg)) { _candleBuffer.Add(Tuple.Create(candleMsg.SecurityId, candleMsg.GetType(), candleMsg.Arg), candleMsg.TypedClone()); } } break; } } }
/// <summary> /// Process message. /// </summary> /// <param name="message">Message.</param> public void SendInMessage(Message message) { if (message is null) { throw new ArgumentNullException(nameof(message)); } if (message.OfflineMode != MessageOfflineModes.None) { return; } ExecutionMessage ToExec(OrderRegisterMessage regMsg) => new ExecutionMessage { ServerTime = DateTimeOffset.Now, ExecutionType = ExecutionTypes.Transaction, SecurityId = regMsg.SecurityId, TransactionId = regMsg.TransactionId, OriginalTransactionId = regMsg.TransactionId, HasOrderInfo = true, OrderPrice = regMsg.Price, OrderVolume = regMsg.Volume, Currency = regMsg.Currency, PortfolioName = regMsg.PortfolioName, ClientCode = regMsg.ClientCode, BrokerCode = regMsg.BrokerCode, Comment = regMsg.Comment, Side = regMsg.Side, TimeInForce = regMsg.TimeInForce, ExpiryDate = regMsg.TillDate, Balance = regMsg.Volume, VisibleVolume = regMsg.VisibleVolume, LocalTime = regMsg.LocalTime, IsMarketMaker = regMsg.IsMarketMaker, IsMargin = regMsg.IsMargin, Slippage = regMsg.Slippage, IsManual = regMsg.IsManual, OrderType = regMsg.OrderType, UserOrderId = regMsg.UserOrderId, OrderState = OrderStates.Pending, Condition = regMsg.Condition?.Clone(), MinVolume = regMsg.MinOrderVolume, PositionEffect = regMsg.PositionEffect, PostOnly = regMsg.PostOnly, }; switch (message.Type) { case MessageTypes.Reset: { _subscriptionsById.Clear(); _ticksBuffer.Clear(); _level1Buffer.Clear(); _candleBuffer.Clear(); _orderLogBuffer.Clear(); _orderBooksBuffer.Clear(); _transactionsBuffer.Clear(); _newsBuffer.Clear(); _positionChangesBuffer.Clear(); //SendOutMessage(new ResetMessage()); break; } case MessageTypes.OrderRegister: { var regMsg = (OrderRegisterMessage)message; if (!CanStore(regMsg)) { break; } _transactionsBuffer.Add(regMsg.SecurityId, ToExec(regMsg)); break; } case MessageTypes.OrderReplace: { var replaceMsg = (OrderReplaceMessage)message; if (!CanStore(replaceMsg)) { break; } _transactionsBuffer.Add(replaceMsg.SecurityId, ToExec(replaceMsg)); break; } case MessageTypes.OrderPairReplace: { var pairMsg = (OrderPairReplaceMessage)message; if (!CanStore(pairMsg)) { break; } _transactionsBuffer.Add(pairMsg.Message1.SecurityId, ToExec(pairMsg.Message1)); _transactionsBuffer.Add(pairMsg.Message2.SecurityId, ToExec(pairMsg.Message2)); break; } //case MessageTypes.OrderCancel: //{ // var cancelMsg = (OrderCancelMessage)message; // //if (!CanStore(cancelMsg)) // // break; // _transactionsBuffer.Add(cancelMsg.SecurityId, new ExecutionMessage // { // ServerTime = DateTimeOffset.Now, // ExecutionType = ExecutionTypes.Transaction, // SecurityId = cancelMsg.SecurityId, // HasOrderInfo = true, // TransactionId = cancelMsg.TransactionId, // IsCancellation = true, // OrderId = cancelMsg.OrderId, // OrderStringId = cancelMsg.OrderStringId, // OriginalTransactionId = cancelMsg.OriginalTransactionId, // OrderVolume = cancelMsg.Volume, // //Side = cancelMsg.Side, // }); // break; //} case MessageTypes.MarketData: { var mdMsg = (MarketDataMessage)message; if (Enabled) { if (mdMsg.IsSubscribe) { _subscriptionsById.Add(mdMsg.TransactionId); } else { _subscriptionsById.Remove(mdMsg.OriginalTransactionId); } } break; } } }
/// <summary> /// Process <see cref="MessageAdapterWrapper.InnerAdapter"/> output message. /// </summary> /// <param name="message">The message.</param> protected override void OnInnerAdapterNewOutMessage(Message message) { switch (message.Type) { case MessageTypes.Level1Change: { var level1Msg = (Level1ChangeMessage)message.Clone(); _level1Buffer.Add(level1Msg.SecurityId, level1Msg); break; } case MessageTypes.QuoteChange: { var quotesMsg = (QuoteChangeMessage)message.Clone(); _orderBooksBuffer.Add(quotesMsg.SecurityId, quotesMsg); break; } case MessageTypes.Execution: { var execMsg = (ExecutionMessage)message.Clone(); DataBuffer <SecurityId, ExecutionMessage> buffer; switch (execMsg.ExecutionType) { case ExecutionTypes.Tick: buffer = _ticksBuffer; break; case ExecutionTypes.Transaction: buffer = _transactionsBuffer; break; case ExecutionTypes.OrderLog: buffer = _orderLogBuffer; break; default: throw new ArgumentOutOfRangeException(LocalizedStrings.Str1695Params.Put(execMsg.ExecutionType)); } buffer.Add(execMsg.SecurityId, execMsg); break; } case MessageTypes.CandlePnF: case MessageTypes.CandleRange: case MessageTypes.CandleRenko: case MessageTypes.CandleTick: case MessageTypes.CandleTimeFrame: case MessageTypes.CandleVolume: { var candleMsg = (CandleMessage)message.Clone(); _candleBuffer.Add(Tuple.Create(candleMsg.SecurityId, candleMsg.GetType(), candleMsg.Arg), candleMsg); break; } case MessageTypes.News: { _newsBuffer.Add((NewsMessage)message.Clone()); break; } //case MessageTypes.Position: // break; //case MessageTypes.Portfolio: // break; //case MessageTypes.PositionChange: // break; //case MessageTypes.PortfolioChange: // break; } base.OnInnerAdapterNewOutMessage(message); }
private static void processaArquivo(string zipName, Simple.Sqlite.SqliteDB db) { var zip = new LeitorZipTransparencia(zipName); //zip.ShouldProcessFile = n => n.Contains("_Cadastro"); zip.IgnoreFirstLine = true; zip.InicioLeituraArquivo += (s, a) => Console.WriteLine($"Lendo {a}"); var zipLines = zip.ReadLines(); var rows = CSVHelper.DelimiterSplit(zipLines, ';'); var buffer = new DataBuffer <AuxilioModel>(20000, data => { db.BulkInsert(data, addReplace: true); Console.WriteLine($"# Data Write "); }); foreach (var row in rows) { var cad = new AuxilioModel() { UF = row[1], Municipio = row[2].ToInt(0), NIS = row[4], CPF_6D = row[5], Nome = row[6], NIS_Responsavel = row[7], CPF_6D_Responsavel = row[8], Nome_Responsavel = row[9], Enquadramento = row[10], }; if (cad.NIS == "00000000000") { cad.NIS = null; } if (cad.NIS_Responsavel == "-2") { cad.NIS_Responsavel = null; } if (cad.Nome_Responsavel == "Não se aplica") { cad.Nome_Responsavel = null; } if (cad.NIS_Responsavel == cad.NIS) { cad.NIS_Responsavel = null; } if (cad.CPF_6D_Responsavel == cad.NIS) { cad.CPF_6D_Responsavel = null; } if (cad.Nome_Responsavel == cad.Nome) { cad.Nome_Responsavel = null; } qtd++; if (qtd % buffer.Quantity == 0) { Console.WriteLine($"{DateTime.Now.ToLongTimeString()} Processado: {qtd:N0} {cad.Nome}"); } if (uids.Contains(cad.Key())) { continue; } uids.Add(cad.Key()); buffer.Add(cad); } buffer.Flush(); }