private async Task <byte[]> SendCommandAndGetDataAsync(RatCommand command) { IsEnabled = false; var buffer = new byte[Common.BufferSize]; _client = new Socket(SocketType.Stream, ProtocolType.Tcp); try { await _client.ConnectAsync(new IPEndPoint(IPAddress.Parse(AddressTextBox.Text), Common.Port)); _client.Send(new[] { (byte)command }); _client.Receive(buffer); return(buffer); } catch (SocketException ex) { if (ex.SocketErrorCode == SocketError.ConnectionRefused) { Dispatcher.Invoke(() => MessageBox.Show("Cannot connect", "Error", MessageBoxButton.OK, MessageBoxImage.Error)); } } finally { Dispatcher.Invoke(() => IsEnabled = true); } return(null); }
private async void SendCommand(RatCommand command, IEnumerable <byte> data = null) { IsEnabled = false; _client = new Socket(SocketType.Stream, ProtocolType.Tcp); try { await _client.ConnectAsync(new IPEndPoint(IPAddress.Parse(AddressTextBox.Text), Common.Port)); var commandId = new[] { (byte)command }; _client.Send(data == null ? commandId : commandId.Concat(data).ToArray()); } catch (SocketException ex) { if (ex.SocketErrorCode == SocketError.ConnectionRefused) { Dispatcher.Invoke(() => MessageBox.Show("Cannot connect", "Error", MessageBoxButton.OK, MessageBoxImage.Error)); } } finally { Dispatcher.Invoke(() => IsEnabled = true); } }