almost there
This commit is contained in:
@@ -1,41 +1,58 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Transactions;
|
||||
using System.Xml.Schema;
|
||||
|
||||
namespace Tello_Drone
|
||||
{
|
||||
internal class UdpClientWrapper : IUdpClientWrapper
|
||||
internal class UdpClientWrapper : IUdpClientWrapper
|
||||
{
|
||||
private readonly UdpClient _client;
|
||||
private const string LocalHost = "127.0.0.1";
|
||||
private readonly int _port = 8889;
|
||||
private IPEndPoint _ipEndPoint;
|
||||
private int retryCount = 0;
|
||||
|
||||
private IPEndPoint _ipEndPoint;
|
||||
private IDroneConstants _droneConstants;
|
||||
private IConsoleLogger _consoleLogger;
|
||||
|
||||
public UdpClientWrapper(long ipAddress, int port)
|
||||
|
||||
public UdpClientWrapper(IConsoleLogger consoleLogger)
|
||||
{
|
||||
_ipEndPoint = new IPEndPoint(ipAddress, port);
|
||||
_client = new UdpClient(LocalHost, _port);
|
||||
|
||||
_consoleLogger = consoleLogger;
|
||||
_droneConstants = new DroneConstants();
|
||||
consoleLogger.Log("Enter Drone IPAddress:");
|
||||
_droneConstants.DroneIPAddress = Console.ReadLine();
|
||||
consoleLogger.Log("Enter Drone Port Number:");
|
||||
_droneConstants.DronePortNumber = Convert.ToInt32(Console.ReadLine());
|
||||
_client = new UdpClient(_droneConstants.DroneIPAddress, _droneConstants.DronePortNumber);
|
||||
_ipEndPoint = new IPEndPoint(IPAddress.Parse(_droneConstants.DroneIPAddress), _droneConstants.DronePortNumber);
|
||||
}
|
||||
public bool TrySend(string message, int timeOut)
|
||||
{
|
||||
if(retryCount < 3 )
|
||||
{
|
||||
bool successfullFlag = false;
|
||||
while(retryCount < 3 && successfullFlag == false)
|
||||
{
|
||||
_client.Send(Encoding.UTF8.GetBytes(message), 1, _ipEndPoint.Address.ToString(), _ipEndPoint.Port);
|
||||
var timer = new Stopwatch();
|
||||
timer.Start();
|
||||
while (timer.ElapsedMilliseconds <= timeOut)
|
||||
_client.Client.ReceiveTimeout = 1_500;
|
||||
_client.Send(Encoding.UTF8.GetBytes(message), 1);
|
||||
|
||||
try
|
||||
{
|
||||
var bytes = _client.Receive(ref _ipEndPoint);
|
||||
var returnMessage = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
|
||||
if (returnMessage == "Ok") return true;
|
||||
if (returnMessage == "Ok")
|
||||
successfullFlag = true;
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
_consoleLogger.Log("SHIT BROKE");
|
||||
}
|
||||
retryCount++;
|
||||
}
|
||||
return false;
|
||||
return successfullFlag;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
Reference in New Issue
Block a user