Exemplo n.º 1
0
        public static void ProcessQueue()
        {
            if (!_isHandlingRequest && _packets.Count > 0)
            {
                lock (_syncRoot)
                {
                    if (_sasProvider == null)
                    {
                        _sasProvider = new SharedAccessSignatureTokenProvider(CommonConfiguration.Instance.BackboneConfiguration.IssuerName,
                                                                              CommonConfiguration.Instance.BackboneConfiguration.IssuerSecret,
                                                                              new TimeSpan(1, 0, 0));
                    }

                    _isHandlingRequest = true;
                    var packet = _packets.Dequeue();

                    var content = Encoding.Default.GetBytes(DatacontractSerializerHelper.Serialize <GamePacket>(packet));

                    using (WebClient webClient = new WebClient())
                    {
                        var token = _sasProvider.GetToken(CommonConfiguration.Instance.BackboneConfiguration.GetRealm(), "POST", new TimeSpan(1, 0, 0));
                        webClient.Headers[HttpRequestHeader.Authorization] = token.TokenValue;

                        // add the properties
                        var collection = new NameValueCollection();
                        collection.Add(GamePacket.VERSION, GamePacket.Namespace);
                        webClient.Headers.Add(collection);

                        webClient.UploadDataCompleted += WebClient_UploadDataCompleted;
                        webClient.UploadDataAsync(new Uri(CommonConfiguration.Instance.BackboneConfiguration.GetServiceMessagesAddress(packet.Queue)), "POST", content);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void ListenForMessages()
        {
            if (_isRunning != null && !_isRunning.IsCompleted)
            {
                return;
            }

            var address = new Uri(CommonConfiguration.Instance.BackboneConfiguration.GetServiceSubscriptionsAddress(Queue, CommonConfiguration.Instance.PlayerId));

            try
            {
                WebRequest request = WebRequest.Create(address);

                if (_sasProvider == null)
                {
                    _sasProvider = new SharedAccessSignatureTokenProvider(CommonConfiguration.Instance.BackboneConfiguration.IssuerName,
                                                                          CommonConfiguration.Instance.BackboneConfiguration.IssuerSecret,
                                                                          new TimeSpan(1, 0, 0));
                }

                var token = _sasProvider.GetToken(CommonConfiguration.Instance.BackboneConfiguration.GetRealm(), "POST", new TimeSpan(1, 0, 0));
                request.Headers[HttpRequestHeader.Authorization] = token.TokenValue;
                request.Method = "DELETE";
                RequestState rs = new RequestState();
                rs.Request = request;

                _isRunning = request.BeginGetResponse(new AsyncCallback(RespCallback), rs);
            }
            catch (WebException ex)
            {
                // if the server has not created a topic yet for the client then a 404 error will be returned so do not report
                if (!ex.Message.Contains("The remote server returned an error: (404) Not Found"))
                {
                    if (OnListenError != null)
                    {
                        OnListenError.Invoke(this, new System.IO.ErrorEventArgs(ex));
                    }
                }
            }
            catch (Exception ex)
            {
                if (OnListenError != null)
                {
                    OnListenError.Invoke(this, new System.IO.ErrorEventArgs(ex));
                }
            }
        }