/// <summary> /// Event handler to be executed when send button is clicked. /// /// New outbound message is constructed using the configured chat name and /// chat message from the message input field. Message is send to the other device. /// </summary> private async void sendButton_Click(object sender, RoutedEventArgs e) { Message m = new Message() { Name = _dataContext.Settings.Name, Text = messageInput.Text, Direction = Message.DirectionValue.Out }; messageInput.Text = ""; _dataContext.Messages.Add(m); await _dataContext.Communication.SendMessageAsync(m); scrollToLast(); }
/// <summary> /// Listens to incoming transmissions. /// /// PeerName property is updates if a chat name is received from the other device. /// MessageReceived action will be invoked when a message from another device has been received. /// ConnectionInterrupted action will be invoked if connection to another device breaks. /// </summary> private async Task ListenAsync() { try { while (true) { await GuaranteedLoadAsync(sizeof(UInt32)); uint version = _reader.ReadUInt32(); if (version == 0) { await GuaranteedLoadAsync(sizeof(UInt32)); uint operation = _reader.ReadUInt32(); switch (operation) { case 0: // name { await GuaranteedLoadAsync(sizeof(UInt32)); uint length = _reader.ReadUInt32(); await GuaranteedLoadAsync(length); string name = _reader.ReadString(length); PeerName = name; } break; case 1: // message { await GuaranteedLoadAsync(sizeof(UInt32)); uint length = _reader.ReadUInt32(); await GuaranteedLoadAsync(length); string text = _reader.ReadString(length); Message m = new Message() { Name = PeerName, Text = text, Direction = Message.DirectionValue.In }; if (MessageReceived != null) { MessageReceived(m); } } break; } } else { throw new Exception("Protocol version mismatch"); } } } catch (Exception ex) { if (ConnectionInterrupted != null) { ConnectionInterrupted(); } } }
/// <summary> /// Event handler to be executed when a new inbound message has been received. /// /// Message is stored to the DataContext.Messages. /// </summary> /// <param name="m"></param> private void MessageReceived(Message m) { Deployment.Current.Dispatcher.BeginInvoke(() => { _dataContext.Messages.Add(m); }); }
/// <summary> /// Sends a chat message to the other device. /// </summary> /// <param name="m">Message to send</param> public async Task SendMessageAsync(Message m) { try { if (m.Text.Length > 0) { _writer.WriteUInt32(0); // protocol version _writer.WriteUInt32(1); // operation identifier uint length = _writer.MeasureString(m.Text); _writer.WriteUInt32(length); _writer.WriteString(m.Text); await _writer.StoreAsync(); } } catch (Exception) { if (ConnectionInterrupted != null) { ConnectionInterrupted(); } } }
/// <summary> /// Event handler to be executed when send button is clicked. /// /// New outbound message is constructed using the configured chat name and /// chat message from the message input field. Message is send to the other device. /// </summary> private async void sendButton_Click(object sender, EventArgs e) { Message m = new Message() { Name = _dataContext.Settings.Name, Text = messageInput.Text, Direction = Message.DirectionValue.Out }; // If we are transferring an image, load it into the message from the temporary image file if (m_ImageReadyToTransfer) { m.ImageName = string.Format("{0}.jpg", Guid.NewGuid()); // assign a unique image name await m.ConvertImageToByteArrayAsync(m_TempImageName); // load up the temporary image into the message's byte array await m.SaveByteArrayImageToFileSystemAsync(false); // save a local copy of the image, and do not clean up the byte array } // Clean up input values messageInput.Text = ""; messageImage.Source = null; messageImage.Visibility = System.Windows.Visibility.Collapsed; m_ImageReadyToTransfer = false; _dataContext.Messages.Add(m); await _dataContext.Communication.SendMessageAsync(m); scrollToLast(); }
/// <summary> /// Event handler to be executed when a new inbound message has been received. /// /// Message is stored to the DataContext.Messages. /// </summary> /// <param name="m"></param> private void MessageReceived(Message m) { Deployment.Current.Dispatcher.BeginInvoke(async () => { await m.SaveByteArrayImageToFileSystemAsync(); _dataContext.Messages.Add(m); }); }
/// <summary> /// Sends a chat message to the other device. /// </summary> /// <param name="m">Message to send</param> public async Task SendMessageAsync(Message m) { try { //if (m.Text.Length > 0) if (m != null) { _writer.WriteUInt32(0); // protocol version _writer.WriteUInt32(1); // operation identifier //uint length = _writer.MeasureString(m.Text); //_writer.WriteUInt32(length); //_writer.WriteString(m.Text); IBuffer buf = ConvertMessageToByteArray(m).AsBuffer(); _writer.WriteUInt32(buf.Length); _writer.WriteBuffer(buf); await _writer.StoreAsync(); } } catch (Exception) { if (ConnectionInterrupted != null) { ConnectionInterrupted(); } } }
private byte[] ConvertMessageToByteArray(Message msg) { if (msg == null) { return null; } using (MemoryStream ms = new MemoryStream()) { Serializer.Serialize<Message>(ms, msg); return ms.ToArray(); } }