public void PackMessageSucceeds() { ICrypto crypto = IndyDotNet.Crypto.Factory.GetCrypto(_wallet); PackedMessage packedMessage = crypto.PackMessage(_senderDid, _senderDid, MESSAGE_TO_SEND); Assert.IsNotNull(packedMessage, "crypto.PackMessage failed to return PackMessage instance"); }
public void UnpackPackedMessageWithMultipleRecipientsSucceeds() { ICrypto crypto = IndyDotNet.Crypto.Factory.GetCrypto(_wallet); List <IDid> recipients = new List <IDid>(); IdentitySeed seed = new IdentitySeed() { Seed = "00000000000000000000000000000My2" }; recipients.Add(IndyDotNet.Did.Factory.CreateMyDid(_pool, _wallet, seed)); seed = new IdentitySeed() { Seed = "00000000000000000000000000000My3" }; recipients.Add(IndyDotNet.Did.Factory.CreateMyDid(_pool, _wallet, seed)); PackedMessage packedMessage = crypto.PackMessage(recipients, _senderDid, MESSAGE_TO_SEND); UnpackedMessage unpackedMessage = crypto.UnpackMessage(packedMessage); Assert.IsNotNull(unpackedMessage, $"did not get back an unpacked message"); Assert.AreEqual(MESSAGE_TO_SEND, unpackedMessage.Message, "unpacked message is not the same as what was sent"); }
public void UnpackPackedMessageSucceeds() { ICrypto crypto = IndyDotNet.Crypto.Factory.GetCrypto(_wallet); PackedMessage packedMessage = crypto.PackMessage(_senderDid, _senderDid, MESSAGE_TO_SEND); UnpackedMessage unpackedMessage = crypto.UnpackMessage(packedMessage); Assert.IsNotNull(unpackedMessage, $"did not get back an unpacked message"); Assert.AreEqual(MESSAGE_TO_SEND, unpackedMessage.Message, "unpacked message is not the same as what was sent"); }
public static T Unpack <T>(this PackedMessage msg, string sender = null) { if (!string.IsNullOrEmpty(sender) && msg.sender != sender) { return(default(T)); } if (msg == typeof(T)) { return(JsonUtility.FromJson <T>(msg.message)); } return(default(T)); }
public void PackMessageToTwoRecipientsSucceeds() { IdentitySeed seed = new IdentitySeed() { Seed = "00000000000000000000000000000My2" }; ICrypto crypto = IndyDotNet.Crypto.Factory.GetCrypto(_wallet); List <IDid> recipients = new List <IDid>(); recipients.Add(_senderDid); recipients.Add(IndyDotNet.Did.Factory.CreateMyDid(_pool, _wallet, seed)); PackedMessage packedMessage = crypto.PackMessage(recipients, _senderDid, MESSAGE_TO_SEND); Assert.IsNotNull(packedMessage, "crypto.PackMessage failed to return PackMessage instance"); }
/// <summary> /// Takes messages out of a queue and sends them to the server. /// </summary> public async Task ProcessSendQueue() { Logger.Log(Logger.Level.Debug, "Send process started"); // Create a variable to store messages to send PackedMessage SendData = null; try { //// Don't run this time if there aren't any messages to acknowledge //if (PendingAcks.Any()) //{ // // Take all the required acks out of the bag // var acks = new List<long>(); // while (PendingAcks.TryTake(out var ack)) acks.Add(ack); // Logger.Log(Logger.Level.Debug, $"Found {acks.Count} messages that need acknowledgement. Adding them to the payload."); // // Create a request that contains the list of acks // var Acks_Request = new RequestState(Schema.msgs_ack(new { msg_ids = acks })); // // Add the request to both the send queue (to be sent to the server) and // // the sent acks queue (in case we need to resend. We don't want to place // // in pending since there shouldn't be a response. // SendQueue.Add(Acks_Request); // SentAcks.Put(Acks_Request); //} // Get messages if possible if ((SendData = SendQueue.Get()) == null) { // If not, stop processing Logger.Log(Logger.Level.Debug, $"There are no requests to send."); return; } // Add all non-ack messages to a queue // Note: The queue contains messages that require a response // from the server. Acks do not and, if added, would sit forever // ToDo: add support for AddRange SendData.Batch .Where(x => (string)x.Request["_"] != "msgs_ack") .ToList().ForEach(x => PendingQueue[x.MessageID] = x); var encrypted = State.EncryptMessageData(SendData.Data); var decrypted = State.DecryptMessageData(encrypted, true); // Send the messages to the server await Connection.Send(encrypted); Logger.Log(Logger.Level.Debug, $"There are now {PendingQueue.Count} pending requests"); } catch (Exception ex) { var sad = new RequestFailedException("Sending the requests to Telegram failed", ex); Logger.Log(sad); // Determine if there were messages in context if (SendData != null) { // If so, fail them with the same exception Logger.Log(Logger.Level.Error, $"Failing the current requests\n\n{ex.Message}"); SendData.Batch.ToList().ForEach(x => x.Response.TrySetException(sad)); } } }