Ejemplo n.º 1
0
        public async Task <ConnectionState> Connect(uint maxAttempts = 0)
        {
            await Initialize();

            _reconnectOnFailure = true;
            _handshake          = new Handshake(this);
            var  attemptsLeft = maxAttempts;
            uint attempts     = 1;

            while (maxAttempts == 0 || attemptsLeft > 0)
            {
                _config.RemoteEndpoint = await _handshake.Shake();

                if (_config.RemoteEndpoint != null)
                {
                    await Connector.Connect();

                    return(Connector.ConnectionState);
                }

                var delay = attempts;
                if (delay > Config.MaxConnectionCooldown)
                {
                    delay = Config.MaxConnectionCooldown;
                }
                _logger.Warning($"Failed to connect, delaying for {delay} seconds");
                await Task.Delay(TimeSpan.FromSeconds(delay));

                if (attemptsLeft > 0)
                {
                    attemptsLeft--;
                }
                attempts++;
            }
            _logger.Warning("Failed to connect within the allotted connection attempt limit.");
            OnConnectionFailed();
            return(ConnectionState.Disconnected);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Write the specified data.
        /// </summary>
        /// <param name="data">RootObject to serialize and send</param>
        /// <param name="allowQueue">Whether to allow the data to be added to the queue</param>
        public virtual async Task Write(JObject data, bool allowQueue = true)
        {
            if ((!Connected() || EnableQueue) && allowQueue)
            {
                lock (_queueLock)
                {
                    if (_queue == null)
                    {
                        _queue = new JObject
                        {
                            new JProperty("msg", _msgId.Next),
                            new JProperty("responses", new JArray()),
                            new JProperty("requests", new JArray())
                        };
                    }

                    if (data["responses"] != null)
                    {
                        foreach (var resp in data["responses"].Value <JArray>())
                        {
                            ((JArray)_queue["responses"]).Add(resp);
                        }
                    }

                    if (data["requests"] != null)
                    {
                        foreach (var req in data["requests"].Value <JArray>())
                        {
                            ((JArray)_queue["requests"]).Add(req);
                        }
                    }

                    if (data["ack"] != null)
                    {
                        _queue["ack"] = data["ack"];
                    }

                    if (!_hasQueueEvent)
                    {
                        // Set flag to queue flush
                        _hasQueueEvent = true;
                    }
                }
                if (_hasQueueEvent)
                {
                    await TriggerQueueFlush();
                }
            }

            if (data["msg"] == null)
            {
                data["msg"] = _msgId.Next;
            }

            if (data["requests"] != null && data["requests"].Value <JArray>().Count == 0)
            {
                data.Remove("requests");
            }

            if (data["responses"] != null && data["responses"].Value <JArray>().Count == 0)
            {
                data.Remove("responses");
            }

            try
            {
                await WriteData(DataSerializer.Serialize(data));
            }
            catch (WebSocketException e)
            {
                _logger.Warning("Failed to send message, reconnecting.");
                _logger.Warning(e.StackTrace);
                await Disconnect();
            }
        }
Ejemplo n.º 3
0
 public new static void Warning(string message)
 {
     Singleton.Warning(message);
 }
Ejemplo n.º 4
0
 public static void SNError(object message, params object[] argv)
 {
     BaseLogger.Warning(message, argv);
 }