Esempio n. 1
0
        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();
            }
        }