예제 #1
0
        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);
            }
        }
예제 #2
0
 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");
 }
예제 #3
0
 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());
         }
 }
예제 #5
0
        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));
                }
            }
        }
예제 #6
0
 public MessageEntry(ChatMessageDto dto, string name, string message)
 {
     Dto     = dto;
     Name    = name;
     Message = message;
 }
예제 #7
0
 public ChatMessageReceivedEventArgs(ChatRoomViewModel viewModel, ChatMessageDto message)
 {
     ViewModel = viewModel;
     Message   = message;
 }