// Called from the scheduler thread. Doesn't throw. // Returns true if the message indicates a fill of one of our orders. public bool OnMessage(TimestampedMsg <IMessageIn> msg) { try { return(msg.Value.Visit(new MessageHandler(this, msg.Received))); } catch (Exception e) { _log.Error(e, "Unable to handle incoming message. Expect inconsistencies in order states."); return(false); } }
void Notify(TimestampedMsg <ArraySegment <byte> > message) { if (message == null) { _log.Info("IN: <ERROR>"); } else { _log.Info("IN: {0}", DecodeForLogging(message.Value)); } try { OnMessage?.Invoke(message); } catch (Exception e) { _log.Warn(e, "Error while handling a message from ClientWebSocket"); } }
void OnMessage(TimestampedMsg <IMessageIn> msg) { Condition.Requires(msg, "msg").IsNotNull(); string channel = Channels.FromMessage(msg.Value); Turnstile <IMessageIn, IMessageOut> turnstile; lock (_monitor) { if (!_channels.TryGetValue(channel, out turnstile)) { // Market data, ping or somesuch. return; } } turnstile.OnReply(msg); }
// Called from the scheduler thread. void PollOne(Currency currency, CoinType coin) { foreach (var elem in _restClient.FuturePositions(currency, coin)) { var update = new FuturePositionsUpdate() { Currency = currency, CoinType = coin, FutureType = elem.Key, Positions = elem.Value, }; var msg = new TimestampedMsg <FuturePositionsUpdate>() { Received = DateTime.UtcNow, Value = update, }; try { OnFuturePositions?.Invoke(msg); } catch (Exception e) { _log.Warn(e, "Ignoring exception from OnFuturePositions"); } } }
void PublishUpdate(IMessageIn msg, Order order, Fill fill, bool finished) { Condition.Requires(order.Callback, "order.Callback").IsNotNull(); var update = new TimestampedMsg <OrderUpdate>() { Received = _received, Value = new OrderUpdate() { Time = msg.Time, OrderId = order.OrderId, Unfilled = order.Unfilled, Fill = fill, Finished = finished, } }; var cb = order.Callback; if (finished) { order.Callback = null; } try { cb(update); } catch (Exception e) { _log.Warn(e, "Ignoring exception from order callback"); } }
async void ReadLoop() { byte[] message = null; var buffer = new ArraySegment <byte>(new byte[128 << 10]); while (true) { WebSocketReceiveResult res = null; // If we aren't getting anything in 30 seconds, presume the connection broken. using (var cancel = new CancellationTokenSource(TimeSpan.FromSeconds(30))) { Task <WebSocketReceiveResult> t = null; lock (_monitor) { if (_state != State.Connected) { break; } try { t = _socket.ReceiveAsync(buffer, cancel.Token); } catch (Exception e) { _log.Warn(e, "Unable to read from ClientWebSocket"); } } if (t != null) { try { res = await t; } catch (Exception e) { // Don't spam logs with errors if we are in the process of disconnecting. if (Connected) { _log.Warn(e, "Unable to read from ClientWebSocket"); } } } } if (res == null) { if (Connected) { Notify(null); } break; } if (res.CloseStatus.HasValue) { _log.Info("The remote side is closing connection: {0}, {1}", res.CloseStatus, res.CloseStatusDescription); } if (res.Count > 0) { Append(ref message, buffer.Array, buffer.Offset, res.Count); } if (res.EndOfMessage && message != null) { var incoming = new TimestampedMsg <ArraySegment <byte> >() { Received = DateTime.UtcNow, Value = new ArraySegment <byte>(message), }; Notify(incoming); message = null; } } _log.Info("Stopped reading data from ClientWebSocket"); }
void OnMessage(TimestampedMsg <IMessageIn> msg) { Condition.Requires(msg, "msg").IsNotNull(); Condition.Requires(msg.Value, "msg.Value").IsNotNull(); msg.Value.Visit(new MessageHandler(this, msg.Received)); }
void OnMessage(TimestampedMsg <WebSocket.IMessageIn> msg) { Condition.Requires(msg, "msg").IsNotNull(); Condition.Requires(msg.Value, "msg.Value").IsNotNull(); Condition.Requires(msg.Value.ProductId, "msg.Value.ProductId").IsNotNullOrEmpty(); Condition.Requires(_products.ContainsKey(msg.Value.ProductId)); bool myFill = _orderManager.OnMessage(msg); OrderBookBuilder book = _products[msg.Value.ProductId]; OrderBookDelta delta = null; Trade trade = null; bool ok = false; try { ok = book.OnOrderUpdate(msg.Value, out delta, out trade); } catch (Exception e) { _log.Error(e, "Unable to process order update"); } if (ok) { if (!myFill && trade != null && trade.Size > 0m) { try { OnTrade?.Invoke( msg.Value.ProductId, new TimestampedMsg <Trade>() { Received = msg.Received, Value = trade }); } catch (Exception e) { _log.Warn(e, "Ignoring exception from OnTrade"); } } if (delta != null && (delta.Bids.Any() || delta.Asks.Any())) { try { OnOrderBook?.Invoke( msg.Value.ProductId, new TimestampedMsg <OrderBookDelta>() { Received = msg.Received, Value = delta }); } catch (Exception e) { _log.Warn(e, "Ignoring exception from OnOrderBook"); } } } else { RefreshOrderBook(msg.Value.ProductId, book); } }