public async Task <List <SignalServiceEnvelope> > RetrieveMessagesAsync(IMessagePipeCallback callback, CancellationToken?token = null) { if (token == null) { token = CancellationToken.None; } List <SignalServiceEnvelope> results = new List <SignalServiceEnvelope>(); List <SignalServiceEnvelopeEntity> entities = await socket.GetMessagesAsync(token); foreach (SignalServiceEnvelopeEntity entity in entities) { SignalServiceEnvelope envelope; if (entity.HasSource() && entity.SourceDevice > 0) { SignalServiceAddress address = new SignalServiceAddress(UuidUtil.ParseOrNull(entity.SourceUuid), entity.SourceE164); envelope = new SignalServiceEnvelope((int)entity.Type, address, (int)entity.SourceDevice, (int)entity.Timestamp, entity.Message, entity.Content, entity.ServerTimestamp, entity.ServerUuid); } else { envelope = new SignalServiceEnvelope((int)entity.Type, (int)entity.Timestamp, entity.Message, entity.Content, entity.ServerTimestamp, entity.ServerUuid); } await callback.OnMessageAsync(envelope); results.Add(envelope); if (envelope.HasUuid()) { await socket.AcknowledgeMessageAsync(envelope.GetUuid(), token); } else { await socket.AcknowledgeMessageAsync(entity.SourceE164 !, entity.Timestamp, token); } } return(results); }