Чистка кода от мусора
This commit is contained in:
@ -1,67 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace DroneClient
|
||||
{
|
||||
internal class Drone
|
||||
{
|
||||
public struct DataOut
|
||||
internal class Drone
|
||||
{
|
||||
public float AccX, AccY, AccZ;
|
||||
public float GyrX, GyrY, GyrZ;
|
||||
public float PosX, PosY;
|
||||
public float LaserRange;
|
||||
public struct DataOut
|
||||
{
|
||||
public float AccX, AccY, AccZ;
|
||||
public float GyrX, GyrY, GyrZ;
|
||||
public float PosX, PosY;
|
||||
public float LaserRange;
|
||||
}
|
||||
|
||||
public struct DataIn
|
||||
{
|
||||
public float MotorUL, MotorUR, MotorDL, MotorDR;
|
||||
}
|
||||
|
||||
public static byte[] getBytes(object data)
|
||||
{
|
||||
int size = Marshal.SizeOf(data);
|
||||
byte[] arr = new byte[size];
|
||||
|
||||
IntPtr ptr = IntPtr.Zero;
|
||||
try
|
||||
{
|
||||
ptr = Marshal.AllocHGlobal(size);
|
||||
Marshal.StructureToPtr(data, ptr, true);
|
||||
Marshal.Copy(ptr, arr, 0, size);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeHGlobal(ptr);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
public static object fromBytes(byte[] arr, Type type)
|
||||
{
|
||||
object mem = new object();
|
||||
|
||||
int size = Marshal.SizeOf(type);
|
||||
IntPtr ptr = IntPtr.Zero;
|
||||
try
|
||||
{
|
||||
ptr = Marshal.AllocHGlobal(size);
|
||||
|
||||
Marshal.Copy(arr, 0, ptr, size);
|
||||
|
||||
mem = Marshal.PtrToStructure(ptr, type);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeHGlobal(ptr);
|
||||
}
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public struct DataIn
|
||||
{
|
||||
public float MotorUL, MotorUR, MotorDL, MotorDR;
|
||||
}
|
||||
|
||||
public static byte[] getBytes(object data)
|
||||
{
|
||||
int size = Marshal.SizeOf(data);
|
||||
byte[] arr = new byte[size];
|
||||
|
||||
IntPtr ptr = IntPtr.Zero;
|
||||
try
|
||||
{
|
||||
ptr = Marshal.AllocHGlobal(size);
|
||||
Marshal.StructureToPtr(data, ptr, true);
|
||||
Marshal.Copy(ptr, arr, 0, size);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeHGlobal(ptr);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
public static object fromBytes(byte[] arr, Type type)
|
||||
{
|
||||
object mem = new object();
|
||||
|
||||
int size = Marshal.SizeOf(type);
|
||||
IntPtr ptr = IntPtr.Zero;
|
||||
try
|
||||
{
|
||||
ptr = Marshal.AllocHGlobal(size);
|
||||
|
||||
Marshal.Copy(arr, 0, ptr, size);
|
||||
|
||||
mem = Marshal.PtrToStructure(ptr, type);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeHGlobal(ptr);
|
||||
}
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,108 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
|
||||
namespace DroneSimulator
|
||||
{
|
||||
internal class NetClient
|
||||
{
|
||||
public class ConnectData
|
||||
internal class NetClient
|
||||
{
|
||||
public bool Connect;
|
||||
public class ConnectData
|
||||
{
|
||||
public bool Connect;
|
||||
|
||||
public Socket? Server;
|
||||
public Socket? Server;
|
||||
}
|
||||
|
||||
public class ReceiveData
|
||||
{
|
||||
public byte[]? Buffer;
|
||||
public int Size;
|
||||
|
||||
public Socket? Server;
|
||||
}
|
||||
|
||||
private class ServerData
|
||||
{
|
||||
public const int size = 1024;
|
||||
public byte[] buffer = new byte[size];
|
||||
}
|
||||
|
||||
private bool Connected = false;
|
||||
private Socket? ServerSocket = null;
|
||||
private ServerData DataServer = new ServerData();
|
||||
|
||||
public delegate void ClientCallback(object o);
|
||||
|
||||
private ClientCallback? ConnectionCallback;
|
||||
private ClientCallback? ReceiveCallback;
|
||||
|
||||
public enum ClientState { Error, Connected, Stop };
|
||||
|
||||
public ClientState Connect(string Addr, int Port, ClientCallback Connection, ClientCallback Receive)
|
||||
{
|
||||
if (Connected)
|
||||
{
|
||||
try { ServerSocket?.Shutdown(SocketShutdown.Both); } catch { }
|
||||
ServerSocket?.Close();
|
||||
Connected = false;
|
||||
return ClientState.Stop;
|
||||
}
|
||||
|
||||
ConnectionCallback = Connection;
|
||||
ReceiveCallback = Receive;
|
||||
|
||||
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(Addr), Port);
|
||||
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
try { ServerSocket.Connect(ep); }
|
||||
catch { ServerSocket.Close(); return ClientState.Error; }
|
||||
|
||||
Connected = true;
|
||||
|
||||
ConnectionCallback(new ConnectData { Connect = true, Server = ServerSocket });
|
||||
|
||||
ReceiveData receiveData = new ReceiveData { Buffer = DataServer.buffer, Size = ServerData.size, Server = ServerSocket };
|
||||
|
||||
try { ServerSocket.BeginReceive(DataServer.buffer, 0, ServerData.size, 0, new AsyncCallback(ReadCallback), receiveData); }
|
||||
catch { }
|
||||
|
||||
return ClientState.Connected;
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
try { ServerSocket?.Shutdown(SocketShutdown.Both); } catch { }
|
||||
ServerSocket?.Close();
|
||||
Connected = false;
|
||||
}
|
||||
|
||||
public void ReadCallback(IAsyncResult ar)
|
||||
{
|
||||
ReceiveData cd = (ReceiveData)ar.AsyncState;
|
||||
if (cd == null) return;
|
||||
|
||||
int bytes = 0;
|
||||
try { bytes = ServerSocket.EndReceive(ar); } catch { }
|
||||
|
||||
if (bytes == 0)
|
||||
{
|
||||
ServerSocket?.Close();
|
||||
|
||||
Connected = false;
|
||||
|
||||
if (ServerSocket != null) ConnectionCallback(new ConnectData { Connect = false, Server = null });
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
ReceiveCallback(new ReceiveData { Buffer = cd.Buffer, Size = bytes, Server = ServerSocket });
|
||||
|
||||
try { ServerSocket?.BeginReceive(cd.Buffer, 0, ServerData.size, 0, new AsyncCallback(ReadCallback), cd); }
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
public class ReceiveData
|
||||
{
|
||||
public byte[]? Buffer;
|
||||
public int Size;
|
||||
|
||||
public Socket? Server;
|
||||
}
|
||||
|
||||
private class ServerData
|
||||
{
|
||||
public const int size = 1024;
|
||||
public byte[] buffer = new byte[size];
|
||||
}
|
||||
|
||||
private bool Connected = false;
|
||||
private Socket? ServerSocket = null;
|
||||
private ServerData DataServer = new ServerData();
|
||||
|
||||
public delegate void ClientCallback(object o);
|
||||
|
||||
private ClientCallback? ConnectionCallback;
|
||||
private ClientCallback? ReceiveCallback;
|
||||
|
||||
public enum ClientState { Error, Connected, Stop };
|
||||
|
||||
public ClientState Connect(string Addr, int Port, ClientCallback Connection, ClientCallback Receive)
|
||||
{
|
||||
if (Connected)
|
||||
{
|
||||
try { ServerSocket?.Shutdown(SocketShutdown.Both); } catch { }
|
||||
ServerSocket?.Close();
|
||||
Connected = false;
|
||||
return ClientState.Stop;
|
||||
}
|
||||
|
||||
ConnectionCallback = Connection;
|
||||
ReceiveCallback = Receive;
|
||||
|
||||
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(Addr), Port);
|
||||
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
try { ServerSocket.Connect(ep); }
|
||||
catch { ServerSocket.Close(); return ClientState.Error; }
|
||||
|
||||
Connected = true;
|
||||
|
||||
ConnectionCallback(new ConnectData { Connect = true, Server = ServerSocket });
|
||||
|
||||
ReceiveData receiveData = new ReceiveData { Buffer = DataServer.buffer, Size = ServerData.size, Server = ServerSocket };
|
||||
|
||||
try { ServerSocket.BeginReceive(DataServer.buffer, 0, ServerData.size, 0, new AsyncCallback(ReadCallback), receiveData); }
|
||||
catch { }
|
||||
|
||||
return ClientState.Connected;
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
try { ServerSocket?.Shutdown(SocketShutdown.Both); } catch { }
|
||||
ServerSocket?.Close();
|
||||
Connected = false;
|
||||
}
|
||||
|
||||
public void ReadCallback(IAsyncResult ar)
|
||||
{
|
||||
ReceiveData cd = (ReceiveData)ar.AsyncState;
|
||||
if (cd == null) return;
|
||||
|
||||
int bytes = 0;
|
||||
try { bytes = ServerSocket.EndReceive(ar); } catch { }
|
||||
|
||||
if (bytes == 0)
|
||||
{
|
||||
ServerSocket?.Close();
|
||||
|
||||
Connected = false;
|
||||
|
||||
if (ServerSocket != null) ConnectionCallback(new ConnectData { Connect = false, Server = null });
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
ReceiveCallback(new ReceiveData { Buffer = cd.Buffer, Size = bytes, Server = ServerSocket });
|
||||
|
||||
try { ServerSocket?.BeginReceive(cd.Buffer, 0, ServerData.size, 0, new AsyncCallback(ReadCallback), cd); }
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user