Esempio n. 1
0
File: BoSH.cs Progetto: shyrat/xmpp
 private void ExtractBody(body resp)
 {
     if (null != resp)
     {
         foreach (var element in resp.Elements<XElement>())
         {
             _manager.Events.NewTag(this, Tag.Get(element));
         }
     }
 }
Esempio n. 2
0
File: BoSH.cs Progetto: shyrat/xmpp
        private void CombineBody(body body)
        {
            int counter = _manager.Settings.QueryCount;

            while (!_tagQueue.IsEmpty)
            {
                XElement tag;
                if (_tagQueue.TryDequeue(out tag))
                {
                    body.Add(tag);
                    if (--counter == 0)
                    {
                        break;
                    }
                }
            }
        }
Esempio n. 3
0
File: BoSH.cs Progetto: shyrat/xmpp
        private body CombineBody()
        {
            var body = new body
            {
                sid = _sid,
                rid = Interlocked.Increment(ref _rid),
                from = _manager.Settings.Id,
                to = _manager.Settings.Id.Server
            };

            CombineBody(body);

            return body;
        }
Esempio n. 4
0
File: BoSH.cs Progetto: shyrat/xmpp
        private void SendSessionTerminationRequest()
        {
            var body = new body
            {
                sid = _sid,
                rid = Interlocked.Increment(ref _rid),
                type = "terminate"
            };

            CombineBody(body);

            SendRequest(body);
        }
Esempio n. 5
0
File: BoSH.cs Progetto: shyrat/xmpp
        private void SendSessionCreationRequest()
        {
            var body = new body
            {
                rid = _rid,
                wait = Wait,
                hold = Hold,
                from = _manager.Settings.Id.Bare,
                to = _manager.Settings.Id.Server
            };

            var resp = SendRequest(body);

            if (null != resp)
            {
                IsConnected = true;

                _manager.Events.Connected(this);

                _sid = resp.sid;
                _requests = resp.requests;

                _connectionsCounter = new SemaphoreSlim(_requests.Value, _requests.Value);

                var payload = resp.Element<tags.streams.features>(tags.streams.Namespace.features);

                _manager.State = new ServerFeaturesState(_manager);
                _manager.State.Execute(payload);
            }
        }
Esempio n. 6
0
File: BoSH.cs Progetto: shyrat/xmpp
        private body SendRequest(body body)
        {
            using (var req = new HttpRequestMessage
                            {
                                RequestUri = new Uri(_manager.Settings.Hostname),
                                Method = new HttpMethod("POST"),
                                Content = new HttpStringContent(body, UnicodeEncoding.Utf8),
                            })
            {
                req.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("text/xml")
                {
                    CharSet = "utf-8",
                };

                try
                {
                    using (var resp = _client.SendRequestAsync(req).AsTask().Result)
                    {
                        if (resp.IsSuccessStatusCode)
                        {
                            var data = resp.Content.ReadAsStringAsync().AsTask().Result;

                            Interlocked.Exchange(ref _retryCounter, 0);

                            return Tag.Get(data) as body;
                        }

                        ConnectionError(
                            ErrorType.ConnectToServerFailed,
                            ErrorPolicyType.Reconnect,
                            string.Format(
                                "Connection error: Status: {0} Reason Phrase: {1}",
                                resp.StatusCode,
                                resp.ReasonPhrase),
                            body);
                    }
                }
                catch (AggregateException e)
                {
                    if (!(e.InnerException is TaskCanceledException))
                    {
                        ConnectionError(
                            ErrorType.ConnectToServerFailed,
                            ErrorPolicyType.Reconnect,
                            e.Message,
                            body);
                    }
                }

                return null;
            }
        }
Esempio n. 7
0
File: BoSH.cs Progetto: shyrat/xmpp
        private void SendRestartRequest()
        {
            var body = new body
            {
                sid = _sid,
                rid = Interlocked.Increment(ref _rid),
                restart = true,
                to = _manager.Settings.Id.Server
            };

            var resp = SendRequest(body);
            if (null != resp)
            {
                var payload = resp.Element<tags.streams.features>(tags.streams.Namespace.features);

                _manager.State = new ServerFeaturesState(_manager);
                _manager.State.Execute(payload);
            }
        }
Esempio n. 8
0
File: BoSH.cs Progetto: shyrat/xmpp
        private void ConnectionError(ErrorType type, ErrorPolicyType policy, string cause, body body)
        {
            if (_disconnecting.IsSet)
            {
                return;
            }

            if (string.IsNullOrEmpty(body.sid) || Interlocked.Increment(ref _retryCounter) >= MaxRetry)
            {
                if (!_connectionError.IsSet)
                {
                    _connectionError.Set();

                    _manager.Events.Error(this, type, policy, cause);
                }
            }
            else
            {
                foreach (var item in body.Elements())
                {
                    _tagQueue.Enqueue(item);
                }
            }
        }