Пример #1
0
 /// <summary>
 /// Initializes the websocket connection handling. Must not not be called on a UI thread. Must not be called on a task which holds the handle lock.
 /// </summary>
 private void InitNetwork()
 {
     try
     {
         Logger.LogTrace("InitNetwork() sync context = {0}", SynchronizationContext.Current);
         MessageReceiver = new SignalServiceMessageReceiver(LibUtils.ServiceConfiguration, new StaticCredentialsProvider(Store.Username, Store.Password, Store.SignalingKey, (int)Store.DeviceId), LibUtils.USER_AGENT);
         Task.Run(async() =>
         {
             try
             {
                 var pipe = await MessageReceiver.CreateMessagePipe(CancelSource.Token, new SignalWebSocketFactory());
                 Logger.LogTrace("Starting IncomingMessagesTask");
                 IncomingMessagesTask = Task.Run(() => new IncomingMessages(CancelSource.Token, pipe, MessageReceiver).HandleIncomingMessages());
                 Logger.LogTrace("Starting OutgoingMessagesTask");
                 OutgoingMessagesTask = Task.Run(() => new OutgoingMessages(CancelSource.Token, pipe, Store, this).HandleOutgoingMessages());
             }
             catch (OperationCanceledException)
             {
                 Logger.LogInformation("InitNetwork cancelled");
             }
             catch (Exception e)
             {
                 Logger.LogError("InitNetwork failed: {0}\n{1}", e.Message, e.StackTrace);
                 await HandleAuthFailure();
                 throw e;
             }
         });
     }
     catch (Exception e)
     {
         Logger.LogError($"InitNetwork() failed: {e.Message}\n{e.StackTrace}");
     }
 }
Пример #2
0
 private void InitNetwork()
 {
     MessageReceiver      = new SignalServiceMessageReceiver(CancelSource.Token, LibUtils.ServiceUrls, new StaticCredentialsProvider(Store.Username, Store.Password, Store.SignalingKey, (int)Store.DeviceId), LibUtils.USER_AGENT);
     Pipe                 = MessageReceiver.createMessagePipe();
     MessageSender        = new SignalServiceMessageSender(CancelSource.Token, LibUtils.ServiceUrls, Store.Username, Store.Password, (int)Store.DeviceId, new Store(), Pipe, null, LibUtils.USER_AGENT);
     IncomingMessagesTask = Task.Factory.StartNew(() => new IncomingMessages(CancelSource.Token, Pipe, this).HandleIncomingMessages(), TaskCreationOptions.LongRunning);
     OutgoingMessagesTask = Task.Factory.StartNew(() => new OutgoingMessages(CancelSource.Token, MessageSender, this).HandleOutgoingMessages(), TaskCreationOptions.LongRunning);
 }
Пример #3
0
        /// <summary>
        /// Initializes the websocket connection handling. Must not not be called on a UI thread. Must not be called on a task which holds the handle lock.
        /// </summary>
        private async Task InitNetwork()
        {
            try
            {
                MessageReceiver = new SignalServiceMessageReceiver(LibUtils.ServiceConfiguration, new StaticCredentialsProvider(Store.Username, Store.Password, Store.SignalingKey, (int)Store.DeviceId), LibUtils.USER_AGENT);
                Pipe            = await MessageReceiver.CreateMessagePipe(CancelSource.Token, new SignalWebSocketFactory());

                MessageSender        = new SignalServiceMessageSender(CancelSource.Token, LibUtils.ServiceConfiguration, Store.Username, Store.Password, (int)Store.DeviceId, new Store(), Pipe, null, LibUtils.USER_AGENT);
                IncomingMessagesTask = Task.Factory.StartNew(async() => await new IncomingMessages(CancelSource.Token, Pipe, MessageReceiver).HandleIncomingMessages(), TaskCreationOptions.LongRunning);
                OutgoingMessages     = new OutgoingMessages(CancelSource.Token, MessageSender, this);
                OutgoingMessagesTask = Task.Factory.StartNew(async() => await OutgoingMessages.HandleOutgoingMessages(), TaskCreationOptions.LongRunning);
            }
            catch (Exception e)
            {
                Logger.LogError("InitNetwork failed: {0}\n{1}", e.Message, e.StackTrace);
                await HandleAuthFailure();
            }
        }
Пример #4
0
 public IncomingMessages(CancellationToken token, SignalServiceMessagePipe pipe, SignalServiceMessageReceiver messageReceiver)
 {
     Token           = token;
     Pipe            = pipe;
     MessageReceiver = messageReceiver;
 }
 public async Task Init()
 {
     Debug.WriteLine("Init lock wait");
     using (await ActionInProgress.LockAsync(CancelSource.Token))
     {
         Debug.WriteLine("Init lock grabbed");
         Running      = true;
         CancelSource = new CancellationTokenSource();
         try
         {
             await Task.Run(async() =>
             {
                 SignalDBContext.FailAllPendingMessages();
                 List <SignalContact> contacts = SignalDBContext.GetAllContactsLocked();
                 List <SignalGroup> groups     = SignalDBContext.GetAllGroupsLocked();
                 await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                 {
                     try
                     {
                         int amountContacts = contacts.Count;
                         int amountGroups   = groups.Count;
                         int contactsIdx    = 0;
                         int groupsIdx      = 0;
                         while (contactsIdx < amountContacts || groupsIdx < amountGroups)
                         {
                             if (contactsIdx < amountContacts)
                             {
                                 SignalConversation contact = contacts[contactsIdx];
                                 if (groupsIdx < amountGroups)
                                 {
                                     SignalConversation group = groups[groupsIdx];
                                     if (contact.LastActiveTimestamp > group.LastActiveTimestamp)
                                     {
                                         contactsIdx++;
                                         AddThread(contact);
                                     }
                                     else
                                     {
                                         groupsIdx++;
                                         AddThread(group);
                                     }
                                 }
                                 else
                                 {
                                     contactsIdx++;
                                     AddThread(contact);
                                 }
                             }
                             else if (groupsIdx < amountGroups)
                             {
                                 SignalConversation group = groups[groupsIdx];
                                 groupsIdx++;
                                 AddThread(group);
                             }
                         }
                     }
                     catch (Exception e)
                     {
                         Debug.WriteLine(e.Message);
                         Debug.WriteLine(e.StackTrace);
                     }
                 });
                 MessageReceiver      = new SignalServiceMessageReceiver(CancelSource.Token, App.ServiceUrls, new StaticCredentialsProvider(App.Store.Username, App.Store.Password, App.Store.SignalingKey, (int)App.Store.DeviceId), App.USER_AGENT);
                 Pipe                 = MessageReceiver.createMessagePipe();
                 MessageSender        = new SignalServiceMessageSender(CancelSource.Token, App.ServiceUrls, App.Store.Username, App.Store.Password, (int)App.Store.DeviceId, new Store(), Pipe, null, App.USER_AGENT);
                 IncomingMessagesTask = Task.Factory.StartNew(HandleIncomingMessages, TaskCreationOptions.LongRunning);
                 OutgoingMessagesTask = Task.Factory.StartNew(HandleOutgoingMessages, TaskCreationOptions.LongRunning);
             });
         }
         catch (AuthorizationFailedException)
         {
             Debug.WriteLine("OWS server rejected our credentials - redirecting to StartPage");
             Running = false;
             CancelSource.Cancel();
             View.Frame.Navigate(typeof(StartPage));
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.Message);
             Debug.WriteLine(e.StackTrace);
         }
     }
     Debug.WriteLine("Init lock released");
 }