Skip to content

Commit bcd83ef

Browse files
committed
Async await everything
1 parent fe51d8a commit bcd83ef

File tree

7 files changed

+195
-180
lines changed

7 files changed

+195
-180
lines changed

libsignal-service-dotnet/SignalServiceAccountManager.cs

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public SignalServiceAccountManager(SignalServiceConfiguration configuration,
5252
/// Construct a SignalServiceAccountManager for linking as a slave device
5353
/// </summary>
5454
/// <param name="configuration">The URL configuration for the Signal Service</param>
55-
/// <param name="token">The cancellation token for the ProvisioningSocket</param>
5655
/// <param name="userAgent">A string which identifies the client software</param>
5756
public SignalServiceAccountManager(SignalServiceConfiguration configuration, string userAgent)
5857
{
@@ -66,28 +65,32 @@ public SignalServiceAccountManager(SignalServiceConfiguration configuration, str
6665
///
6766
/// </summary>
6867
/// <param name="pin"></param>
69-
public void SetPin(string pin)
68+
public async Task SetPin(string pin)
7069
{
7170
if (pin != null)
72-
PushServiceSocket.SetPin(pin);
71+
{
72+
await PushServiceSocket.SetPin(pin);
73+
}
7374
else
74-
PushServiceSocket.RemovePin();
75+
{
76+
await PushServiceSocket.RemovePin();
77+
}
7578
}
7679

7780
/// <summary>
7881
/// Register/Unregister a Google Cloud Messaging registration ID.
7982
/// </summary>
8083
/// <param name="gcmRegistrationId">The GCM id to register. A call with an absent value will unregister.</param>
8184
/// <returns></returns>
82-
public void SetGcmId(May<string> gcmRegistrationId)// throws IOException
85+
public async Task SetGcmId(May<string> gcmRegistrationId)// throws IOException
8386
{
8487
if (gcmRegistrationId.HasValue)
8588
{
86-
this.PushServiceSocket.RegisterGcmId(gcmRegistrationId.ForceGetValue());
89+
await PushServiceSocket.RegisterGcmId(gcmRegistrationId.ForceGetValue());
8790
}
8891
else
8992
{
90-
this.PushServiceSocket.UnregisterGcmId();
93+
await PushServiceSocket.UnregisterGcmId();
9194
}
9295
}
9396

@@ -96,19 +99,19 @@ public void SetGcmId(May<string> gcmRegistrationId)// throws IOException
9699
/// an SMS verification code to this Signal user.
97100
/// </summary>
98101
/// <returns></returns>
99-
public void RequestSmsVerificationCode()// throws IOException
102+
public async Task RequestSmsVerificationCode()// throws IOException
100103
{
101-
this.PushServiceSocket.CreateAccount(false);
104+
await PushServiceSocket.CreateAccount(false);
102105
}
103106

104107
/// <summary>
105108
/// Request a Voice verification code. On success, the server will
106109
/// make a voice call to this Signal user.
107110
/// </summary>
108111
/// <returns></returns>
109-
public void RequestVoiceVerificationCode()// throws IOException
112+
public async Task RequestVoiceVerificationCode()// throws IOException
110113
{
111-
this.PushServiceSocket.CreateAccount(true);
114+
await PushServiceSocket.CreateAccount(true);
112115
}
113116

114117
/// <summary>
@@ -124,10 +127,10 @@ public void RequestVoiceVerificationCode()// throws IOException
124127
/// <param name="fetchesMessages">True if the client does not support GCM</param>
125128
/// <param name="pin"></param>
126129
/// <returns></returns>
127-
public void VerifyAccountWithCode(string verificationCode, string signalingKey,
130+
public async Task VerifyAccountWithCode(string verificationCode, string signalingKey,
128131
uint signalProtocolRegistrationId, bool fetchesMessages, string pin)
129132
{
130-
this.PushServiceSocket.VerifyAccountCode(verificationCode, signalingKey,
133+
await PushServiceSocket.VerifyAccountCode(verificationCode, signalingKey,
131134
signalProtocolRegistrationId, fetchesMessages, pin);
132135
}
133136

@@ -142,9 +145,9 @@ public void VerifyAccountWithCode(string verificationCode, string signalingKey,
142145
/// <param name="fetchesMessages">True if the client does not support GCM</param>
143146
/// <param name="pin"></param>
144147
/// <returns></returns>
145-
public void SetAccountAttributes(string signalingKey, uint signalProtocolRegistrationId, bool fetchesMessages, string pin)
148+
public async Task SetAccountAttributes(string signalingKey, uint signalProtocolRegistrationId, bool fetchesMessages, string pin)
146149
{
147-
this.PushServiceSocket.SetAccountAttributes(signalingKey, signalProtocolRegistrationId, fetchesMessages, pin);
150+
await PushServiceSocket.SetAccountAttributes(signalingKey, signalProtocolRegistrationId, fetchesMessages, pin);
148151
}
149152

150153
/// <summary>
@@ -155,48 +158,48 @@ public void SetAccountAttributes(string signalingKey, uint signalProtocolRegistr
155158
/// <param name="signedPreKey">The client's signed prekey.</param>
156159
/// <param name="oneTimePreKeys">The client's list of one-time prekeys.</param>
157160
/// <returns></returns>
158-
public bool SetPreKeys(IdentityKey identityKey, SignedPreKeyRecord signedPreKey, IList<PreKeyRecord> oneTimePreKeys)//throws IOException
161+
public async Task<bool> SetPreKeys(IdentityKey identityKey, SignedPreKeyRecord signedPreKey, IList<PreKeyRecord> oneTimePreKeys)//throws IOException
159162
{
160-
this.PushServiceSocket.RegisterPreKeys(identityKey, signedPreKey, oneTimePreKeys);
163+
await PushServiceSocket.RegisterPreKeys(identityKey, signedPreKey, oneTimePreKeys);
161164
return true;
162165
}
163166

164167
/// <summary>
165168
///
166169
/// </summary>
167170
/// <returns>The server's count of currently available (eg. unused) prekeys for this user.</returns>
168-
public int GetPreKeysCount()// throws IOException
171+
public async Task<int> GetPreKeysCount()// throws IOException
169172
{
170-
return this.PushServiceSocket.GetAvailablePreKeys();
173+
return await PushServiceSocket.GetAvailablePreKeys();
171174
}
172175

173176
/// <summary>
174177
/// Set the client's signed prekey.
175178
/// </summary>
176179
/// <param name="signedPreKey">The client's new signed prekey.</param>
177-
public void SetSignedPreKey(SignedPreKeyRecord signedPreKey)// throws IOException
180+
public async Task SetSignedPreKey(SignedPreKeyRecord signedPreKey)// throws IOException
178181
{
179-
this.PushServiceSocket.SetCurrentSignedPreKey(signedPreKey);
182+
await PushServiceSocket.SetCurrentSignedPreKey(signedPreKey);
180183
}
181184

182185
/// <summary>
183186
///
184187
/// </summary>
185188
/// <returns>The server's view of the client's current signed prekey.</returns>
186-
public SignedPreKeyEntity GetSignedPreKey()// throws IOException
189+
public async Task<SignedPreKeyEntity> GetSignedPreKey()// throws IOException
187190
{
188-
return this.PushServiceSocket.GetCurrentSignedPreKey();
191+
return await PushServiceSocket.GetCurrentSignedPreKey();
189192
}
190193

191194
/// <summary>
192195
/// Checks whether a contact is currently registered with the server
193196
/// </summary>
194197
/// <param name="e164number">The contact to check.</param>
195198
/// <returns>An optional ContactTokenDetails, present if registered, absent if not.</returns>
196-
public May<ContactTokenDetails> GetContact(string e164number)// throws IOException
199+
public async Task<May<ContactTokenDetails>> GetContact(string e164number)// throws IOException
197200
{
198201
string contactToken = CreateDirectoryServerToken(e164number, true);
199-
ContactTokenDetails contactTokenDetails = this.PushServiceSocket.GetContactTokenDetails(contactToken);
202+
ContactTokenDetails contactTokenDetails = await PushServiceSocket.GetContactTokenDetails(contactToken);
200203

201204
if (contactTokenDetails != null)
202205
{
@@ -211,10 +214,10 @@ public May<ContactTokenDetails> GetContact(string e164number)// throws IOExcepti
211214
/// </summary>
212215
/// <param name="e164numbers">The contacts to check.</param>
213216
/// <returns>A list of ContactTokenDetails for the registered users.</returns>
214-
public List<ContactTokenDetails> GetContacts(IList<string> e164numbers)
217+
public async Task<List<ContactTokenDetails>> GetContacts(IList<string> e164numbers)
215218
{
216219
IDictionary<string, string> contactTokensMap = CreateDirectoryServerTokenMap(e164numbers);
217-
List<ContactTokenDetails> activeTokens = this.PushServiceSocket.RetrieveDirectory(contactTokensMap.Keys);
220+
List<ContactTokenDetails> activeTokens = await PushServiceSocket.RetrieveDirectory(contactTokensMap.Keys);
218221

219222
foreach (ContactTokenDetails activeToken in activeTokens)
220223
{
@@ -242,9 +245,9 @@ public async Task<string> GetNewDeviceUuid(CancellationToken token)
242245
/// Called by an already verified device.
243246
/// </summary>
244247
/// <returns>A verification code (String of 6 digits)</returns>
245-
public string GetNewDeviceVerificationCode()// throws IOException
248+
public async Task<string> GetNewDeviceVerificationCode()// throws IOException
246249
{
247-
return this.PushServiceSocket.GetNewDeviceVerificationCode();
250+
return await PushServiceSocket.GetNewDeviceVerificationCode();
248251
}
249252

250253
/// <summary>
@@ -302,7 +305,7 @@ public async Task<int> FinishNewDeviceRegistration(CancellationToken token, Sign
302305
/// <param name="identityKeyPair"></param>
303306
/// <param name="profileKey"></param>
304307
/// <param name="code"></param>
305-
public void AddDevice(string deviceIdentifier,
308+
public async Task AddDevice(string deviceIdentifier,
306309
ECPublicKey deviceKey,
307310
IdentityKeyPair identityKeyPair,
308311
byte[] profileKey,
@@ -323,25 +326,25 @@ public void AddDevice(string deviceIdentifier,
323326
}
324327

325328
byte[] ciphertext = cipher.encrypt(message);
326-
this.PushServiceSocket.SendProvisioningMessage(deviceIdentifier, ciphertext);
329+
await PushServiceSocket.SendProvisioningMessage(deviceIdentifier, ciphertext);
327330
}
328331

329332
/// <summary>
330333
/// TODO
331334
/// </summary>
332335
/// <returns></returns>
333-
public List<DeviceInfo> GetDevices()
336+
public async Task<List<DeviceInfo>> GetDevices()
334337
{
335-
return this.PushServiceSocket.GetDevices();
338+
return await PushServiceSocket.GetDevices();
336339
}
337340

338341
/// <summary>
339342
/// TODO
340343
/// </summary>
341344
/// <param name="deviceId"></param>
342-
public void RemoveDevice(long deviceId)
345+
public async Task RemoveDevice(long deviceId)
343346
{
344-
this.PushServiceSocket.RemoveDevice(deviceId);
347+
await PushServiceSocket.RemoveDevice(deviceId);
345348
}
346349

347350
/// <summary>
@@ -358,26 +361,26 @@ public async Task<TurnServerInfo> GetTurnServerInfo(CancellationToken token)
358361
/// </summary>
359362
/// <param name="key"></param>
360363
/// <param name="name"></param>
361-
public void SetProfileName(byte[] key, string name)
364+
public async Task SetProfileName(byte[] key, string name)
362365
{
363366
if (name == null) name = "";
364367
string ciphertextName = Base64.EncodeBytesWithoutPadding(new ProfileCipher(key).EncryptName(Encoding.Unicode.GetBytes(name), ProfileCipher.NAME_PADDED_LENGTH));
365-
PushServiceSocket.SetProfileName(ciphertextName);
368+
await PushServiceSocket.SetProfileName(ciphertextName);
366369
}
367370

368371
/// <summary>
369372
/// TODO
370373
/// </summary>
371374
/// <param name="key"></param>
372375
/// <param name="avatar"></param>
373-
public void SetProfileAvatar(byte[] key, StreamDetails avatar)
376+
public async Task SetProfileAvatar(byte[] key, StreamDetails avatar)
374377
{
375378
ProfileAvatarData profileAvatarData = null;
376379
if (avatar != null)
377380
{
378381
profileAvatarData = new ProfileAvatarData(avatar.InputStream, avatar.Length, avatar.ContentType, new ProfileCipherOutputStreamFactory(key));
379382
}
380-
PushServiceSocket.SetProfileAvatar(profileAvatarData);
383+
await PushServiceSocket.SetProfileAvatar(profileAvatarData);
381384
}
382385

383386
/// <summary>

libsignal-service-dotnet/SignalServiceMessagePipe.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ internal SignalServiceMessagePipe(CancellationToken token, SignalWebSocketConnec
3333
this.Token = token;
3434
this.Websocket = websocket;
3535
this.CredentialsProvider = credentialsProvider;
36-
this.Websocket.Connect(token).Wait();
36+
}
37+
38+
/// <summary>
39+
/// Connect message pipe.
40+
/// </summary>
41+
/// <returns>Task</returns>
42+
public async Task Connect()
43+
{
44+
Logger.LogTrace("Connecting to message pipe");
45+
await Websocket.Connect(Token);
3746
}
3847

3948
/// <summary>
@@ -79,7 +88,7 @@ public async Task ReadBlocking(IMessagePipeCallback callback)
7988
/// </summary>
8089
/// <param name="list"></param>
8190
/// <returns></returns>
82-
public SendMessageResponse Send(OutgoingPushMessageList list)
91+
public async Task<SendMessageResponse> Send(OutgoingPushMessageList list)
8392
{
8493
Logger.LogTrace("Send()");
8594
WebSocketRequestMessage requestmessage = new WebSocketRequestMessage()
@@ -92,7 +101,7 @@ public SendMessageResponse Send(OutgoingPushMessageList list)
92101
requestmessage.Headers.Add("content-type:application/json");
93102
Logger.LogDebug("Sending message {0}", requestmessage.Id);
94103
var t = Websocket.SendRequest(requestmessage);
95-
t.Wait();
104+
await t;
96105
if (t.IsCompleted)
97106
{
98107
var response = t.Result;
@@ -115,7 +124,7 @@ public SendMessageResponse Send(OutgoingPushMessageList list)
115124
/// </summary>
116125
/// <param name="address"></param>
117126
/// <returns></returns>
118-
public SignalServiceProfile GetProfile(SignalServiceAddress address)
127+
public async Task<SignalServiceProfile> GetProfile(SignalServiceAddress address)
119128
{
120129
Logger.LogTrace("GetProfile()");
121130
WebSocketRequestMessage requestMessage = new WebSocketRequestMessage()
@@ -126,7 +135,7 @@ public SignalServiceProfile GetProfile(SignalServiceAddress address)
126135
};
127136

128137
var t = Websocket.SendRequest(requestMessage);
129-
t.Wait();
138+
await t;
130139
if (t.IsCompleted)
131140
{
132141
var response = t.Result;

libsignal-service-dotnet/SignalServiceMessageReceiver.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public SignalServiceMessageReceiver(CancellationToken token, SignalServiceConfig
6060
/// </summary>
6161
/// <param name="address"></param>
6262
/// <returns></returns>
63-
public SignalServiceProfile RetrieveProfile(SignalServiceAddress address)
63+
public async Task<SignalServiceProfile> RetrieveProfile(SignalServiceAddress address)
6464
{
65-
return Socket.RetrieveProfile(address);
65+
return await Socket.RetrieveProfile(address);
6666
}
6767

6868
/// <summary>
@@ -88,9 +88,9 @@ public Stream RetrieveProfileAvatar(string path, FileStream destination, byte[]
8888
/// <param name="tmpCipherDestination">The temporary destination for this attachment before decryption</param>
8989
/// <param name="maxSizeBytes">The maximum size for this attachment (not yet implemented)</param>
9090
/// <param name="listener">An optional listener (may be null) to receive callbacks on download progress.</param>
91-
public Stream RetrieveAttachment(SignalServiceAttachmentPointer pointer, Stream tmpCipherDestination, int maxSizeBytes, IProgressListener listener)
91+
public async Task<Stream> RetrieveAttachment(SignalServiceAttachmentPointer pointer, Stream tmpCipherDestination, int maxSizeBytes, IProgressListener listener)
9292
{
93-
Socket.RetrieveAttachment(pointer.Relay, pointer.Id, tmpCipherDestination, maxSizeBytes);
93+
await Socket.RetrieveAttachment(pointer.Relay, pointer.Id, tmpCipherDestination, maxSizeBytes);
9494
tmpCipherDestination.Position = 0;
9595
return AttachmentCipherInputStream.CreateFor(tmpCipherDestination, pointer.Size != null ? pointer.Size.Value : 0, pointer.Key, pointer.Digest);
9696
}
@@ -100,9 +100,9 @@ public Stream RetrieveAttachment(SignalServiceAttachmentPointer pointer, Stream
100100
/// </summary>
101101
/// <param name="pointer">The pointer to the attachment</param>
102102
/// <returns></returns>
103-
public string RetrieveAttachmentDownloadUrl(SignalServiceAttachmentPointer pointer)
103+
public async Task<string> RetrieveAttachmentDownloadUrl(SignalServiceAttachmentPointer pointer)
104104
{
105-
return Socket.RetrieveAttachmentDownloadUrl(pointer.Relay, pointer.Id);
105+
return await Socket.RetrieveAttachmentDownloadUrl(pointer.Relay, pointer.Id);
106106
}
107107

108108
/// <summary>
@@ -111,16 +111,18 @@ public string RetrieveAttachmentDownloadUrl(SignalServiceAttachmentPointer point
111111
/// Callers must call <see cref="SignalServiceMessagePipe.Shutdown()"/> when finished with the pipe.
112112
/// </summary>
113113
/// <returns>A SignalServiceMessagePipe for receiving Signal Service messages.</returns>
114-
public SignalServiceMessagePipe CreateMessagePipe()
114+
public async Task<SignalServiceMessagePipe> CreateMessagePipe()
115115
{
116116
SignalWebSocketConnection webSocket = new SignalWebSocketConnection(Token, Urls.SignalServiceUrls[0].Url, CredentialsProvider, UserAgent, ConnectivityListener);
117-
return new SignalServiceMessagePipe(Token, webSocket, CredentialsProvider);
117+
var messagePipe = new SignalServiceMessagePipe(Token, webSocket, CredentialsProvider);
118+
await messagePipe.Connect();
119+
return messagePipe;
118120
}
119121

120122
public async Task<List<SignalServiceEnvelope>> RetrieveMessages(IMessagePipeCallback callback)
121123
{
122124
List<SignalServiceEnvelope> results = new List<SignalServiceEnvelope>();
123-
List<SignalServiceEnvelopeEntity> entities = Socket.GetMessages();
125+
List<SignalServiceEnvelopeEntity> entities = await Socket.GetMessages();
124126

125127
foreach (SignalServiceEnvelopeEntity entity in entities)
126128
{
@@ -132,7 +134,7 @@ public async Task<List<SignalServiceEnvelope>> RetrieveMessages(IMessagePipeCall
132134
await callback.OnMessage(envelope);
133135
results.Add(envelope);
134136

135-
Socket.AcknowledgeMessage(entity.Source, entity.Timestamp);
137+
await Socket.AcknowledgeMessage(entity.Source, entity.Timestamp);
136138
}
137139
return results;
138140
}

0 commit comments

Comments
 (0)