1
0
mirror of https://github.com/oliverbooth/VpSharp synced 2024-11-22 19:18:47 +00:00

Code cleanup

This commit is contained in:
Oliver Booth 2022-11-30 19:00:40 +00:00
parent eef5dcf1c2
commit 6a16ef6097
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634

View File

@ -7,10 +7,10 @@ namespace VpSharp.Internal;
internal sealed class Connection : IDisposable internal sealed class Connection : IDisposable
{ {
private readonly object _lockObject; private readonly object _lockObject;
private readonly List<byte[]> _readyBuffers = new();
private readonly Socket _socket; private readonly Socket _socket;
private byte[] _pendingBuffer; private byte[] _pendingBuffer;
private readonly List<byte[]> _readyBuffers = new();
private Timer? _timer; private Timer? _timer;
private nint _vpConnection; private nint _vpConnection;
@ -24,7 +24,7 @@ internal sealed class Connection : IDisposable
public void BeforeDestroy() public void BeforeDestroy()
{ {
// ReSharper disable once InconsistentlySynchronizedField // ReSharper disable once InconsistentlySynchronizedField
_vpConnection = nint.Zero; _vpConnection = 0;
} }
public int Connect(string host, ushort port) public int Connect(string host, ushort port)
@ -52,12 +52,12 @@ internal sealed class Connection : IDisposable
connection._socket.BeginReceive(connection._pendingBuffer, 0, 1024, SocketFlags.None, ReceiveCallback, connection._socket.BeginReceive(connection._pendingBuffer, 0, 1024, SocketFlags.None, ReceiveCallback,
connection); connection);
connection.Notify(NetworkNotification.Connect, (int) NetworkReturnCode.Success); connection.Notify(NetworkNotification.Connect, (int)NetworkReturnCode.Success);
} }
} }
catch (SocketException) catch (SocketException)
{ {
connection?.Notify(NetworkNotification.Connect, (int) NetworkReturnCode.ConnectionError); connection?.Notify(NetworkNotification.Connect, (int)NetworkReturnCode.ConnectionError);
} }
} }
@ -109,7 +109,7 @@ internal sealed class Connection : IDisposable
{ {
lock (_lockObject) lock (_lockObject)
{ {
if (_vpConnection != IntPtr.Zero) if (_vpConnection != 0)
{ {
_ = Native.vp_net_notify(_vpConnection, (int)notification, rc); _ = Native.vp_net_notify(_vpConnection, (int)notification, rc);
} }
@ -182,7 +182,7 @@ internal sealed class Connection : IDisposable
connection._pendingBuffer = new byte[1024]; connection._pendingBuffer = new byte[1024];
} }
if (connection._vpConnection != nint.Zero) if (connection._vpConnection != 0)
{ {
if (bytesRead > 0) if (bytesRead > 0)
{ {
@ -241,19 +241,11 @@ internal sealed class Connection : IDisposable
public int Timeout(int seconds) public int Timeout(int seconds)
{ {
if (seconds < 0) _timer = seconds < 0 ? null : new Timer(HandleTimeout, this, seconds * 1000, System.Threading.Timeout.Infinite);
{
_timer = null;
}
else
{
_timer = new Timer(HandleTimeout, this, seconds * 1000, global::System.Threading.Timeout.Infinite);
}
return 0; return 0;
} }
public static int TimeoutNative(IntPtr ptr, int seconds) public static int TimeoutNative(nint ptr, int seconds)
{ {
if (GCHandle.FromIntPtr(ptr).Target is Connection connection) if (GCHandle.FromIntPtr(ptr).Target is Connection connection)
{ {