void CancelOrder(IReader <IMessageIn> reader, IWriter <IMessageOut> writer, Product product, long orderId) { var req = new CancelOrderRequest() { Product = product, OrderId = orderId }; writer.Send(req); var deadline = DateTime.UtcNow + CancelTimeout; while (true) { TimestampedMsg <IMessageIn> resp; if (!reader.PeekWithTimeout(DateTime.UtcNow - deadline, out resp)) { throw new Exception("Timed out waiting for response to our order cancellation request"); } var reply = resp.Value as CancelOrderResponse; if (reply != null) { if (reply.Error.HasValue && reply.Error.Value != ErrorCode.OrderDoesNotExist1 && reply.Error.Value != ErrorCode.OrderDoesNotExist2) { throw new Exception(String.Format("Unable to cancel order {0}: {1}", orderId, reply)); } // We need to consume the response so that it doesn't confuse Gateway. reader.Consume(); break; } reader.Skip(); } }
void Subscribe(IReader <IMessageIn> reader, IWriter <IMessageOut> writer, IMessageOut req, bool consumeFirst) { writer.Send(req); string channel = WebSocket.Channels.FromMessage(req); var deadline = DateTime.UtcNow + SubscribeTimeout; while (true) { TimestampedMsg <IMessageIn> resp; if (!reader.PeekWithTimeout(DateTime.UtcNow - deadline, out resp)) { throw new Exception(String.Format( "Timed out waiting for response to our subscription request: ({0}) {1}", req.GetType(), req)); } if (channel == WebSocket.Channels.FromMessage(resp.Value)) { if (resp.Value.Error.HasValue) { throw new Exception(String.Format( "Exchange returned error to our subscription request. Request: ({0}) {1}. Response: ({2}) {3}", req.GetType(), req, resp.Value.GetType(), resp.Value)); } if (consumeFirst) { reader.Consume(); } break; } reader.Skip(); } }