Commit 8e640def authored by Manu's avatar Manu 🏅
Browse files

Implement waveOut sender logic

parent 06aba6fe
......@@ -13,9 +13,9 @@ namespace DirectoTalk.Sender
private WaveIn waveSource = null;
public CallingMachine(string ipToCall)
public CallingMachine(string destinationAddress, int destinationPort)
{
NetworkHandler = new NetworkHandler(ipToCall);
NetworkHandler = new NetworkHandler(destinationAddress, destinationPort);
}
public void StartCall()
......
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace DirectoTalk.Sender
{
public class NetworkHandler : IDisposable
{
private readonly HttpClient httpClient = new HttpClient();
private readonly HttpClient _HttpClient = new HttpClient();
public NetworkHandler(string destinationAddress)
private readonly string _DestinationAddress;
private readonly int _DestinationPort;
private readonly UdpClient _UdpClient;
public NetworkHandler(string destinationAddress, int destinationPort)
{
httpClient.BaseAddress = new Uri(destinationAddress);
_DestinationAddress = destinationAddress;
_DestinationPort = destinationPort;
_UdpClient = new UdpClient();
}
public async Task<SendResult> SendAudio(byte[] buffer)
{
ByteArrayContent binaryContent = new ByteArrayContent(buffer);
try
{
_ = Task.Run(async () => await SendUdpPacket(buffer).ConfigureAwait(false));
return new SendResult("OK", "packet on the way");
}
catch (Exception ex)
{
return new SendResult("Error", ex.Message);
}
}
/// <summary>
/// Asynchronously sends a UDP package with the given content.
/// </summary>
/// <param name="sourcePort"></param>
/// <param name="destinationAddress"></param>
/// <param name="destinationPort"></param>
/// <param name="data"></param>
private async Task SendUdpPacket(byte[] data)
{
await _UdpClient.SendAsync(data, data.Length, _DestinationAddress, _DestinationPort).ConfigureAwait(false);
}
private async Task<SendResult> SendTcpPacket(byte[] data)
{
ByteArrayContent binaryContent = new ByteArrayContent(data);
binaryContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
using (var httpRequest = new HttpRequestMessage())
......@@ -24,19 +57,19 @@ namespace DirectoTalk.Sender
httpRequest.Content = binaryContent;
try
{
HttpResponseMessage response = await httpClient.SendAsync(httpRequest).ConfigureAwait(false);
HttpResponseMessage response = await _HttpClient.SendAsync(httpRequest).ConfigureAwait(false);
return new SendResult(response.StatusCode.ToString(), await response.Content.ReadAsStringAsync().ConfigureAwait(false));
}
catch (Exception ex)
{
return new SendResult("XXX", $"{ex.Message} {ex.InnerException?.Message}");
return new SendResult("Err", $"{ex.Message} {ex.InnerException?.Message}");
}
}
}
public void Dispose()
{
httpClient.Dispose();
_HttpClient.Dispose();
}
}
}
\ No newline at end of file
......@@ -21,8 +21,8 @@
<TextBox Margin="10" Padding="5" Text="127.0.0.1"></TextBox>
</DockPanel>
<DockPanel Grid.Row="0" Grid.Column="1" >
<Label Margin="10" Padding="5">Unused:</Label>
<TextBox Margin="10" Padding="5"></TextBox>
<Label Margin="10" Padding="5">Destination port:</Label>
<TextBox Margin="10" Padding="5" Text="{Binding DestinationPort}"></TextBox>
</DockPanel>
<Button Grid.Row="1" Grid.Column="0" Margin="10" Padding="5" Click="CallButton_Click" IsEnabled="{Binding IsCallEnabled}">TRANSMIT VOICE</Button>
<Button Grid.Row="1" Grid.Column="1" Margin="10" Padding="5" Click="HangUpButton_Click" IsEnabled="{Binding IsHangUpEnabled}">HANG UP</Button>
......
......@@ -8,20 +8,34 @@ namespace DirectoTalk.Sender.ViewModels
public TalkViewModel()
{
CallingMachine = new CallingMachine(IpToCall);
CallingMachine = new CallingMachine(DestinationAddress, DestinationPort);
}
private string ipToCall = "https://127.0.0.1";
public string IpToCall
private string destinationAddress = "127.0.0.1";
public string DestinationAddress
{
get
{
return ipToCall;
return destinationAddress;
}
set
{
ipToCall = value;
OnPropertyChanged(nameof(IpToCall));
destinationAddress = value;
OnPropertyChanged(nameof(DestinationAddress));
}
}
private int destinationPort = 42000;
public int DestinationPort
{
get
{
return destinationPort;
}
set
{
destinationPort = value;
OnPropertyChanged(nameof(DestinationPort));
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment