public static void SendJsonCapsula(Context context, JsonCapsula toSend, long recepientId, long myUserId) { if (toSend == null) { return; } var recepient = context.Contacts .Where(u => u.PublicId == recepientId) .SingleOrDefault(); if (recepient == null) { throw new Exception($"User is not downloaded in local database."); } else if (recepient.Trusted != 1) { throw new Exception($"User {recepient.PublicId} ({recepient.UserName}) is not trusted."); } long?blobId = null; if (myUserId == recepientId) { var blobMessage = new BlobMessages() { SenderId = myUserId, PublicId = null, DoDelete = 0, Failed = 0 }; context.BlobMessages.Add(blobMessage); context.SaveChanges(); blobId = blobMessage.Id; PullMessageParser.ParseIJTypeMessage(context, toSend, myUserId, blobMessage.Id, myUserId); context.SaveChanges(); } context.ToSendMessages.Add(new ToSendMessages() { RecepientId = recepientId, BlobMessagesId = blobId, Blob = JsonEncoder.GetJsonEncoded(context, toSend, recepientId), Priority = toSend.Message.GetPriority() }); context.SaveChanges(); PushAction.Changed = true; }
public int Pull() { int changes = 0; ThrowExceptionIfNotConnected(); lock (mutex) { Log("Sending PULL command."); BinaryEncoder.SendCommand(stream, ConnectionCommand.PULL); #if (DEBUG) Log("Sending ClientPullCapsula."); #endif ClientPullCapsula clientCapsula; using (Context context = new Context(config)) { clientCapsula = new ClientPullCapsula() { AesKeysUserIds = context.Contacts .Where(u => u.ReceiveAesKey == null) .Select(u => u.PublicId) .ToArray() }; } TextEncoder.SendJson(stream, clientCapsula); ServerPullCapsula capsula = TextEncoder.ReadJson <ServerPullCapsula>(stream); #if (DEBUG) Log("Received ServerPullCapsula."); #endif changes += capsula.AesKeysUserIds.Count; changes += capsula.Messages.Count; using (Context context = new Context(config)) { #if (DEBUG) Log("Receiving and saving AES keys."); #endif foreach (var id in capsula.AesKeysUserIds) { var user = new UContact(context.Contacts.Where(con => con.PublicId == id).SingleOrDefault()); try { user.ReceiveAesKey = RSAEncoder.DecryptAndVerify(BinaryEncoder.ReceiveBytes(stream), MyCertificate, X509Certificate2Utils.ImportFromPem(user.PublicCertificate)); } catch (Exception ex) { Log($"Loading of Receive AESKey from {user.PublicId} has failed."); logger.LogException(this, ex); } PushOperations.Update(context, user, UserId, UserId); } context.SaveChanges(); #if (DEBUG) Log("Receiving and saving messages."); #endif foreach (PullMessage metaMessage in capsula.Messages) { BlobMessages metaBlob = new BlobMessages() { PublicId = metaMessage.PublicId, SenderId = metaMessage.SenderId, Failed = 0, DoDelete = 0 }; context.BlobMessages.Add(metaBlob); context.SaveChanges(); try { PullMessageParser.ParseEncryptedMessage(context, BinaryEncoder.ReceiveBytes(stream), metaBlob.SenderId, metaBlob.Id, UserId); } catch (Exception ex) { Log($"Loading of message {metaMessage.PublicId} has failed."); metaBlob.Failed = 1; logger.LogException(this, ex); } context.SaveChanges(); } } Log("Pull have been done."); } return(changes); }
public void Pull() { Log("Sending PULL command."); TextEncoder.SendCommand(stream, ConnectionCommand.PULL); PullCapsula capsula = TextEncoder.ReadPullCapsula(stream); #if (DEBUG) Log("Received PullCapsula."); #endif using (Context context = new Context(config)) { #if (DEBUG) Log("Saving new users."); #endif foreach (PullUser user in capsula.Users) { context.Contacts.Add(new Contacts { PublicId = user.UserId, PublicCertificate = user.PublicCertificate, UserName = user.UserName }); } context.SaveChanges(); #if (DEBUG) Log("Saving trusted contacts."); #endif context.Database.ExecuteSqlCommand("update CONTACTS set TRUSTED=0;"); context.SaveChanges(); foreach (var user in context.Contacts .Where(users => capsula.TrustedUserIds.Contains(users.PublicId))) { user.Trusted = 1; } context.SaveChanges(); #if (DEBUG) Log("Receiving and saving AES keys."); #endif foreach (var id in capsula.AesKeysUserIds) { var user = context.Contacts.Where(con => con.PublicId == id).SingleOrDefault(); user.ReceiveAesKey = RSAEncoder.Decrypt(BinaryEncoder.ReceiveBytes(stream), ClientCertificate); } context.SaveChanges(); #if (DEBUG) Log("Receiving and saving messages."); #endif foreach (PullMessage metaMessage in capsula.Messages) { BlobMessages metaBlob = new BlobMessages() { PublicId = metaMessage.PublicId, SenderId = metaMessage.SenderId, Failed = 0, DoDelete = 0 }; context.BlobMessages.Add(metaBlob); context.SaveChanges(); try { PullMessageParser.ParseEncryptedMessage(context, BinaryEncoder.ReceiveBytes(stream), metaBlob.SenderId, metaBlob.Id, UserId); } catch (Exception ex) { Log($"Loading of message {metaMessage.PublicId} has failed."); metaBlob.Failed = 1; logger.LogException(this, ex); } context.SaveChanges(); } } Log("Pull have been done."); }
private void Pull() { #if (DEBUG) Log("Pulling started."); Log("Receiving PushCapsula."); #endif PushCapsula capsula = TextEncoder.ReadJson <PushCapsula>(stream); PushResponse response = new PushResponse() { MessageIds = new List <long>() }; using (Context context = new Context(config)) { #if (DEBUG) Log($"Receiving and saving {capsula.PushMessages.Count} blobs."); #endif foreach (var pushMessage in capsula.PushMessages) { BlobMessages blobMessage = new BlobMessages() { Content = BinaryEncoder.ReceiveBytes(stream), RecepientId = pushMessage.RecepientId, SenderId = connectionInfo.UserId, Priority = pushMessage.Priority }; context.BlobMessages.Add(blobMessage); context.SaveChanges(); if (pushMessage.RecepientId == connectionInfo.UserId) { response.MessageIds.Add(blobMessage.Id); context.ClientsMessagesDownloaded.Add(new ClientsMessagesDownloaded() { BlobMessagesId = blobMessage.Id, ClientsId = connectionInfo.ClientId }); } context.SaveChanges(); } #if (DEBUG) Log($"Deleting {capsula.MessageToDeleteIds.Count} blobs."); #endif foreach (var toDeleteId in capsula.MessageToDeleteIds) { var toDelete = context.BlobMessages .Where(u => u.RecepientId == connectionInfo.UserId && u.Id == toDeleteId).SingleOrDefault(); if (toDelete != null) { context.BlobMessages.Remove(toDelete); } } context.SaveChanges(); } #if (DEBUG) Log("Sending push response."); #endif TextEncoder.SendJson(stream, response); #if (DEBUG) Log("Pulling ended."); #endif }