@@ -203,9 +203,9 @@ public async Task<bool> Acquire(CoreDispatcher d, ISignalFrontend w) //TODO wrap
203
203
{
204
204
LikelyHasValidStore = true ;
205
205
}
206
- var initNetwork = Task . Run ( ( ) =>
206
+ var initNetwork = Task . Run ( async ( ) =>
207
207
{
208
- InitNetwork ( ) ;
208
+ await InitNetwork ( ) ;
209
209
} ) ;
210
210
var recoverDownloadsTask = Task . Run ( ( ) =>
211
211
{
@@ -246,19 +246,35 @@ public async Task Reacquire()
246
246
GlobalResetEvent . Reset ( ) ;
247
247
LibsignalDBContext . ClearSessionCache ( ) ;
248
248
Instance = this ;
249
- await Task . Run ( ( ) =>
249
+ await Task . Run ( async ( ) =>
250
250
{
251
251
List < Task > tasks = new List < Task > ( ) ;
252
252
foreach ( var f in Frames )
253
253
{
254
254
var conversations = GetConversations ( ) ;
255
- tasks . Add ( f . Key . RunTaskAsync ( ( ) =>
255
+ var taskCompletionSource = new TaskCompletionSource < bool > ( ) ;
256
+ await f . Key . RunAsync ( CoreDispatcherPriority . Normal , ( ) =>
256
257
{
257
- f . Value . ReplaceConversationList ( conversations ) ;
258
- } ) ) ;
258
+ try
259
+ {
260
+ f . Value . ReplaceConversationList ( conversations ) ;
261
+ }
262
+ catch ( Exception e )
263
+ {
264
+ Logger . LogError ( "Reacquire() ReplaceConversationList() failed: {0}\n {1}" , e . Message , e . StackTrace ) ;
265
+ }
266
+ finally
267
+ {
268
+ taskCompletionSource . SetResult ( false ) ;
269
+ }
270
+ } ) ;
271
+ tasks . Add ( taskCompletionSource . Task ) ;
259
272
}
260
- Task . WaitAll ( tasks . ToArray ( ) ) ;
261
- RecoverDownloads ( ) . Wait ( ) ;
273
+ foreach ( var t in tasks )
274
+ {
275
+ await t ;
276
+ }
277
+ await RecoverDownloads ( ) ;
262
278
Store = LibsignalDBContext . GetSignalStore ( ) ;
263
279
if ( Store != null )
264
280
{
@@ -267,9 +283,9 @@ await Task.Run(() =>
267
283
} ) ;
268
284
if ( LikelyHasValidStore )
269
285
{
270
- var initNetworkTask = Task . Run ( ( ) =>
286
+ var initNetworkTask = Task . Run ( async ( ) =>
271
287
{
272
- InitNetwork ( ) ;
288
+ await InitNetwork ( ) ;
273
289
} ) ;
274
290
}
275
291
Running = true ;
@@ -429,14 +445,14 @@ public async Task SendBlockedMessage()
429
445
public void StartAttachmentDownload ( SignalAttachment sa )
430
446
{
431
447
//TODO lock, check if already downloading, start a new download if not exists
432
- Task . Run ( ( ) =>
448
+ Task . Run ( async ( ) =>
433
449
{
434
450
try
435
451
{
436
452
Logger . LogTrace ( "StartAttachmentDownload() locking" ) ;
437
453
SemaphoreSlim . Wait ( CancelSource . Token ) ;
438
454
Logger . LogTrace ( "StartAttachmentDownload() locked" ) ;
439
- TryScheduleAttachmentDownload ( sa ) ;
455
+ await TryScheduleAttachmentDownload ( sa ) ;
440
456
}
441
457
catch ( Exception e )
442
458
{
@@ -844,12 +860,12 @@ private List<SignalConversation> GetConversations()
844
860
/// <summary>
845
861
/// 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.
846
862
/// </summary>
847
- private void InitNetwork ( )
863
+ private async Task InitNetwork ( )
848
864
{
849
865
try
850
866
{
851
- MessageReceiver = new SignalServiceMessageReceiver ( CancelSource . Token , LibUtils . ServiceConfiguration , new StaticCredentialsProvider ( Store . Username , Store . Password , Store . SignalingKey , ( int ) Store . DeviceId ) , LibUtils . USER_AGENT , null ) ;
852
- Pipe = MessageReceiver . CreateMessagePipe ( ) ;
867
+ MessageReceiver = new SignalServiceMessageReceiver ( LibUtils . ServiceConfiguration , new StaticCredentialsProvider ( Store . Username , Store . Password , Store . SignalingKey , ( int ) Store . DeviceId ) , LibUtils . USER_AGENT , null ) ;
868
+ Pipe = await MessageReceiver . CreateMessagePipe ( CancelSource . Token ) ;
853
869
MessageSender = new SignalServiceMessageSender ( CancelSource . Token , LibUtils . ServiceConfiguration , Store . Username , Store . Password , ( int ) Store . DeviceId , new Store ( ) , Pipe , null , LibUtils . USER_AGENT ) ;
854
870
IncomingMessagesTask = Task . Factory . StartNew ( async ( ) => await new IncomingMessages ( CancelSource . Token , Pipe , MessageReceiver ) . HandleIncomingMessages ( ) , TaskCreationOptions . LongRunning ) ;
855
871
OutgoingMessages = new OutgoingMessages ( CancelSource . Token , MessageSender , this ) ;
@@ -858,7 +874,7 @@ private void InitNetwork()
858
874
catch ( Exception e )
859
875
{
860
876
Logger . LogError ( "InitNetwork failed: {0}\n {1}" , e . Message , e . StackTrace ) ;
861
- HandleAuthFailure ( ) ;
877
+ await HandleAuthFailure ( ) ;
862
878
}
863
879
}
864
880
@@ -885,27 +901,27 @@ private async Task HandleAuthFailure()
885
901
}
886
902
}
887
903
888
- private void TryScheduleAttachmentDownload ( SignalAttachment attachment )
904
+ private async Task TryScheduleAttachmentDownload ( SignalAttachment attachment )
889
905
{
890
906
if ( Downloads . Count < 100 )
891
907
{
892
908
if ( attachment . Status != SignalAttachmentStatus . Finished && ! Downloads . ContainsKey ( attachment . Id ) )
893
909
{
894
910
SignalServiceAttachmentPointer attachmentPointer = attachment . ToAttachmentPointer ( ) ;
895
911
IStorageFolder localFolder = ApplicationData . Current . LocalFolder ;
896
- IStorageFile tmpDownload = Task . Run ( async ( ) =>
912
+ IStorageFile tmpDownload = await Task . Run ( async ( ) =>
897
913
{
898
914
return await ApplicationData . Current . LocalCacheFolder . CreateFileAsync ( @"Attachments\" + attachment . Id + ".cipher" , CreationCollisionOption . ReplaceExisting ) ;
899
- } ) . Result ;
915
+ } ) ;
900
916
BackgroundDownloader downloader = new BackgroundDownloader ( ) ;
901
917
downloader . SetRequestHeader ( "Content-Type" , "application/octet-stream" ) ;
902
918
// this is the recommended way to call CreateDownload
903
919
// see https://docs.microsoft.com/en-us/uwp/api/windows.networking.backgroundtransfer.backgrounddownloader#Methods
904
- DownloadOperation download = downloader . CreateDownload ( new Uri ( RetrieveAttachmentUrl ( attachmentPointer ) ) , tmpDownload ) ;
920
+ DownloadOperation download = downloader . CreateDownload ( new Uri ( await MessageReceiver . RetrieveAttachmentDownloadUrl ( CancelSource . Token , attachmentPointer ) ) , tmpDownload ) ;
905
921
attachment . Guid = download . Guid . ToString ( ) ;
906
922
SignalDBContext . UpdateAttachmentGuid ( attachment ) ;
907
923
Downloads . Add ( attachment . Id , download ) ;
908
- Task . Run ( async ( ) =>
924
+ var downloadSuccessfulHandler = Task . Run ( async ( ) =>
909
925
{
910
926
Logger . LogInformation ( "Waiting for download {0}({1})" , attachment . SentFileName , attachment . Id ) ;
911
927
var t = await download . StartAsync ( ) ;
@@ -947,11 +963,6 @@ private async Task HandleSuccessfullDownload(SignalAttachment attachment, IStora
947
963
}
948
964
}
949
965
950
- private string RetrieveAttachmentUrl ( SignalServiceAttachmentPointer pointer )
951
- {
952
- return MessageReceiver . RetrieveAttachmentDownloadUrl ( pointer ) ;
953
- }
954
-
955
966
private void DecryptAttachment ( SignalServiceAttachmentPointer pointer , Stream ciphertextFileStream , Stream plaintextFileStream )
956
967
{
957
968
byte [ ] buf = new byte [ 32 ] ;
0 commit comments