public async Task Send(IdentityHash destination, ChatMessageDto messageDto) { ChatRoomContext chatroomContext; if (!chatRoomTable.TryLookup(destination, out chatroomContext)) { throw new InvalidStateException("Cannot send message to undiscovered chatroom/user."); } var messageDtoBytes = CampfireChatSerializer.GetBytes(messageDto); if (chatroomContext.IsUnicast) { Console.WriteLine("Unicast messaging"); await client.UnicastAsync(destination, messageDtoBytes); } else if (destination.Bytes.Any(v => v != 0)) { Console.WriteLine("Multicast messaging"); await client.MulticastAsync(destination, messageDtoBytes); } else { Console.WriteLine("Broadcast messaging"); await client.BroadcastAsync(messageDtoBytes); } }
public void SendMessage(ChatMessageDto message) { // This will write to Merkle Tree which will trigger message received. Console.WriteLine("Entering send message 3"); chatMessageSender.Send(ChatroomIdentityHash, message).Forget(); Console.WriteLine("Exiting send message 3"); }
public void SendMessage(ChatMessageContentType contentType, byte[] contentRaw) { lock (synchronization) { Console.WriteLine("Entering send message 2"); var message = new ChatMessageDto { SequenceNumber = Interlocked.Increment(ref outboundMessageSequenceNumber), LogicalClock = IncrementThenCaptureCurrentLogicalClock(), FriendlySenderName = campfireChatClient.LocalFriendlyName, ContentType = ChatMessageContentType.Text, ContentRaw = contentRaw }; Console.WriteLine("Made message"); SendMessage(message); Console.WriteLine("Exiting send message 2"); } }
public static byte[] GetBytes(ChatMessageDto data) { using (var ms = new MemoryStream()) using (var writer = new BinaryWriter(ms)) { writer.Write((uint)ChatDtoTypeId.Message); writer.Write((int)data.SequenceNumber); writer.Write((int)data.LogicalClock.Count); foreach (var kvp in data.LogicalClock) { writer.Write(kvp.Key.Bytes.ToArray()); writer.Write((int)kvp.Value); } writer.Write((uint)data.ContentType); writer.Write((int)data.ContentRaw.Length); writer.Write(data.ContentRaw, 0, data.ContentRaw.Length); writer.WriteLengthPrefixedUtf8String(data.FriendlySenderName); return(ms.ToArray()); } }
public void HandleMessageReceived(ChatMessageDto message) { lock (synchronization) { messages.Add(message, message); foreach (var kvp in message.LogicalClock) { int storedCounter; if (!currentLogicalClock.TryGetValue(kvp.Key, out storedCounter) || storedCounter < kvp.Value) { currentLogicalClock[kvp.Key] = kvp.Value; } } foreach (var viewModel in viewModels) { viewModel.MessageReceivedCallback(this, new ChatMessageReceivedEventArgs(viewModel, message)); } } }
public MessageEntry(ChatMessageDto dto, string name, string message) { Dto = dto; Name = name; Message = message; }
public ChatMessageReceivedEventArgs(ChatRoomViewModel viewModel, ChatMessageDto message) { ViewModel = viewModel; Message = message; }