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();
            }
        }
Esempio n. 2
0
        // `param` may be null (means empty).
        string AuthenticatedRequest(IMessageOut msg, IEnumerable <KV> param)
        {
            param = param ?? new KV[0];
            param = param.Append(new KV("api_key", _keys.ApiKey));
            // Signature is added last because its value depends on all other parameters.
            param = param.Append(new KV("sign", Authenticator.Sign(_keys, param)));
            string parameters = String.Join(",", param.Select(kv => String.Format("\"{0}\":\"{1}\"", kv.Key, kv.Value)));

            return(String.Format("{{\"event\":\"addChannel\",\"channel\":\"{0}\",\"parameters\":{{{1}}}}}",
                                 Channels.FromMessage(msg), parameters));
        }
Esempio n. 3
0
        // Action `done` will be called exactly once in the scheduler. Its argument will be null on timeout.
        public void Send(IMessageOut msg, Action <TimestampedMsg <IMessageIn> > done)
        {
            Condition.Requires(msg, "msg").IsNotNull();
            Condition.Requires(done, "done").IsNotNull();
            string channel = Channels.FromMessage(msg);
            Turnstile <IMessageIn, IMessageOut> turnstile;

            lock (_monitor)
            {
                if (!_channels.TryGetValue(channel, out turnstile))
                {
                    turnstile = new Turnstile <IMessageIn, IMessageOut>(_connection);
                    _channels.Add(channel, turnstile);
                }
            }
            turnstile.Send(msg, done);
        }
Esempio n. 4
0
 string UnauthenticatedRequest(IMessageOut msg)
 {
     // Example: {"event":"addChannel","channel":"ok_btcusd_future_depth_this_week_60"}.
     return(String.Format("{{\"event\":\"addChannel\",\"channel\":\"{0}\"}}",
                          Channels.FromMessage(msg)));
 }
 public ZipArchive(IMessageOut messageOut)
 {
     _messageOut = messageOut;
 }
Esempio n. 6
0
 public static string FromMessage(IMessageOut msg)
 {
     return(msg.Visit(new MessageChannel()));
 }