Asher
Asher
CC#
Created by Tycho on 12/18/2023 in #help
MonoGame - inheritance implementation.
as for your code, wouldn't change anything but again, not a game dev so take that with a grain of salt
19 replies
CC#
Created by Tycho on 12/18/2023 in #help
MonoGame - inheritance implementation.
and that would give you alot of opportunity to learn how overriding works, how you pass references of a base class and possibly need to convert them to their original type etc etc
19 replies
CC#
Created by Tycho on 12/18/2023 in #help
MonoGame - inheritance implementation.
if it's a base class you'd also add health maybe speed unless they're all stationary
19 replies
CC#
Created by Tycho on 12/18/2023 in #help
MonoGame - inheritance implementation.
interface IEnemy
{
void DoAttack(); // this would be implemented for each enemy with their distinct attack
}
interface IEnemy
{
void DoAttack(); // this would be implemented for each enemy with their distinct attack
}
just to give an example
19 replies
CC#
Created by Tycho on 12/18/2023 in #help
MonoGame - inheritance implementation.
I'm not a game dev but I'd assume most games use some sort of base class system to define for example enemies, maybe an interface that you need to implement
19 replies
CC#
Created by Tycho on 12/18/2023 in #help
MonoGame - inheritance implementation.
in general from what I know most libraries or engines when creating some base class will expect you to override certain functions but if you really want to get a grasp on inheritence I'd argue you should be making the base class with an actual use case
19 replies
CC#
Created by Tycho on 12/18/2023 in #help
MonoGame - inheritance implementation.
is Game a class from a library you're using? or did you create it?
19 replies
CC#
Created by Asher on 12/18/2023 in #help
Unable to cancel async operation using CancellationToken
@xtreit tried this approach, it seems to still hang for some reason, the RecieveAsync itself just completely (but asynchronously) hangs everything
5 replies
CC#
Created by Asher on 12/18/2023 in #help
Unable to cancel async operation using CancellationToken
I am really going crazy trying to cancel this operation if a result is not recieved within 5 seconds, does anyone know why I'm unable to stop this operation?
5 replies
CC#
Created by Asher on 12/18/2023 in #help
Unable to cancel async operation using CancellationToken
This is the code particularly:
CancellationTokenSource cts = new CancellationTokenSource();

ValueTask<int> numBytesReceived = sock.ReceiveAsync(res, SocketFlags.None, cts.Token);
cts.CancelAfter(5000);
await numBytesReceived;
Array.Resize(ref res, numBytesReceived.Result);
CancellationTokenSource cts = new CancellationTokenSource();

ValueTask<int> numBytesReceived = sock.ReceiveAsync(res, SocketFlags.None, cts.Token);
cts.CancelAfter(5000);
await numBytesReceived;
Array.Resize(ref res, numBytesReceived.Result);
5 replies
CC#
Created by occluder on 12/8/2023 in #help
✅ Blazor's @onclick isn't firing
I don't believe you need the preventDefault
21 replies
CC#
Created by Asher on 11/11/2023 in #help
UdpClient doesn't receive data with ReceiveAsync but does with BeginReceive
I might take a look at the .net library source to see exactly how ReceiveAsync works so I might get a clue or try to use Socket instead of UdpClient thats like my other 2 options right now but I can't for the life of me figure out why this hangs
14 replies
CC#
Created by Asher on 11/11/2023 in #help
UdpClient doesn't receive data with ReceiveAsync but does with BeginReceive
I can't run it multiple times though
14 replies
CC#
Created by Asher on 11/11/2023 in #help
UdpClient doesn't receive data with ReceiveAsync but does with BeginReceive
It does, basically the one with BeginReceive & EndReceive is my current working version
14 replies
CC#
Created by Asher on 11/11/2023 in #help
UdpClient doesn't receive data with ReceiveAsync but does with BeginReceive
any knows why this might hang?
14 replies
CC#
Created by Asher on 11/11/2023 in #help
UdpClient doesn't receive data with ReceiveAsync but does with BeginReceive
I find it particularly weird that ReceiveAsync does not take in an endpoint as a parameter like BeginReceive does but there is no overload where it takes such a parameter
14 replies
CC#
Created by Asher on 11/11/2023 in #help
UdpClient doesn't receive data with ReceiveAsync but does with BeginReceive
no exceptions thrown either
14 replies
CC#
Created by Asher on 11/11/2023 in #help
UdpClient doesn't receive data with ReceiveAsync but does with BeginReceive
the weird thing is, ReceiveAsync does not respect my Cancellation Token, it just hangs forever.
14 replies
CC#
Created by Asher on 11/11/2023 in #help
UdpClient doesn't receive data with ReceiveAsync but does with BeginReceive
ReceiveAsync:
private async Task<byte[]?> ExecuteUdpRequest2(Uri uri, byte[] message)
{
byte[]? data = null;
if (uri == null) throw new ArgumentNullException(nameof(uri));
if (message == null) throw new ArgumentNullException(nameof(message));

int port = await Torrent.PortList.AwaitOpenPort();

IPEndPoint ep = new IPEndPoint(IPAddress.Any, port);

try
{
using UdpClient udpClient = new(ep);

Logger.Log($"Listening on : {ep}");

udpClient.Client.SendTimeout = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
udpClient.Client.ReceiveTimeout = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;

Logger.Log($"sending message to {uri.Host}, returning.", source: "UdpRequest");

int numBytesSent = await udpClient.SendAsync(message, message.Length, uri.Host, uri.Port);
Logger.Log($"Sent: {numBytesSent}", source: "UdpRequest");

UdpReceiveResult? res;
//var res = udpClient.BeginReceive(null, null);
try
{
CancellationTokenSource cts = new CancellationTokenSource();

var timer = new Timer(state => cts.Cancel(), null, 5000, Timeout.Infinite);
res = await udpClient.ReceiveAsync(cts.Token);
}
catch (OperationCanceledException ex)
{
res = null;
}
// begin recieve right after request
if (res != null && res?.Buffer != null && res?.Buffer.Length > 0)
{
Logger.Log($"Recieved message from endpoint, returning.", source: "UdpRequest");

data = res?.Buffer;
}
else
{
Logger.Log($"No Bytes Recieved from UdpRequest", source: "UdpRequest");
// here the client just times out.
}
}
catch (SocketException ex)
{
Logger.Log($"Failed UDP tracker message to {uri} for torrent {Torrent.InfoHash}: {ex.Message}");
}
Torrent.PortList.SetPortUnused(port);
return data;
}
private async Task<byte[]?> ExecuteUdpRequest2(Uri uri, byte[] message)
{
byte[]? data = null;
if (uri == null) throw new ArgumentNullException(nameof(uri));
if (message == null) throw new ArgumentNullException(nameof(message));

int port = await Torrent.PortList.AwaitOpenPort();

IPEndPoint ep = new IPEndPoint(IPAddress.Any, port);

try
{
using UdpClient udpClient = new(ep);

Logger.Log($"Listening on : {ep}");

udpClient.Client.SendTimeout = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
udpClient.Client.ReceiveTimeout = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;

Logger.Log($"sending message to {uri.Host}, returning.", source: "UdpRequest");

int numBytesSent = await udpClient.SendAsync(message, message.Length, uri.Host, uri.Port);
Logger.Log($"Sent: {numBytesSent}", source: "UdpRequest");

UdpReceiveResult? res;
//var res = udpClient.BeginReceive(null, null);
try
{
CancellationTokenSource cts = new CancellationTokenSource();

var timer = new Timer(state => cts.Cancel(), null, 5000, Timeout.Infinite);
res = await udpClient.ReceiveAsync(cts.Token);
}
catch (OperationCanceledException ex)
{
res = null;
}
// begin recieve right after request
if (res != null && res?.Buffer != null && res?.Buffer.Length > 0)
{
Logger.Log($"Recieved message from endpoint, returning.", source: "UdpRequest");

data = res?.Buffer;
}
else
{
Logger.Log($"No Bytes Recieved from UdpRequest", source: "UdpRequest");
// here the client just times out.
}
}
catch (SocketException ex)
{
Logger.Log($"Failed UDP tracker message to {uri} for torrent {Torrent.InfoHash}: {ex.Message}");
}
Torrent.PortList.SetPortUnused(port);
return data;
}
14 replies
CC#
Created by Asher on 11/11/2023 in #help
UdpClient doesn't receive data with ReceiveAsync but does with BeginReceive
BeginReceive:
private async Task<byte[]?> ExecuteUdpRequest(Uri uri, byte[] message)
{
if (uri == null) throw new ArgumentNullException(nameof(uri));
if (message == null) throw new ArgumentNullException(nameof(message));

byte[]? data = null;
IPEndPoint any = new(IPAddress.Any, ListenPort);

try
{
using (UdpClient udpClient = new UdpClient())
{
udpClient.Client.SendTimeout = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
udpClient.Client.ReceiveTimeout = (int)TimeSpan.FromSeconds(15).TotalMilliseconds;

Logger.Log($"sending message to {uri.Host}, returning.", source: "UdpRequest");

int numBytesSent = await udpClient.SendAsync(message, message.Length, uri.Host, uri.Port);
Logger.Log($"Sent: {numBytesSent}", source: "UdpRequest");

var res = udpClient.BeginReceive(null, null);
//data = udpClient.EndReceive(res, ref any);
// begin recieve right after request
if (res.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5)))
{
Logger.Log($"Recieved message from endpoint, returning.", source: "UdpRequest");

#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
data = udpClient.EndReceive(res, ref any);
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
}
else
{
Logger.Log($"No Bytes Recieved from UdpRequest", source: "UdpRequest");
// here the client just times out.
}
}
}
catch(SocketException ex)
{
Logger.Log($"Failed UDP tracker message to {uri} for torrent {Torrent.InfoHash}: {ex.Message}");
}

return data;
}
}
private async Task<byte[]?> ExecuteUdpRequest(Uri uri, byte[] message)
{
if (uri == null) throw new ArgumentNullException(nameof(uri));
if (message == null) throw new ArgumentNullException(nameof(message));

byte[]? data = null;
IPEndPoint any = new(IPAddress.Any, ListenPort);

try
{
using (UdpClient udpClient = new UdpClient())
{
udpClient.Client.SendTimeout = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
udpClient.Client.ReceiveTimeout = (int)TimeSpan.FromSeconds(15).TotalMilliseconds;

Logger.Log($"sending message to {uri.Host}, returning.", source: "UdpRequest");

int numBytesSent = await udpClient.SendAsync(message, message.Length, uri.Host, uri.Port);
Logger.Log($"Sent: {numBytesSent}", source: "UdpRequest");

var res = udpClient.BeginReceive(null, null);
//data = udpClient.EndReceive(res, ref any);
// begin recieve right after request
if (res.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5)))
{
Logger.Log($"Recieved message from endpoint, returning.", source: "UdpRequest");

#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
data = udpClient.EndReceive(res, ref any);
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
}
else
{
Logger.Log($"No Bytes Recieved from UdpRequest", source: "UdpRequest");
// here the client just times out.
}
}
}
catch(SocketException ex)
{
Logger.Log($"Failed UDP tracker message to {uri} for torrent {Torrent.InfoHash}: {ex.Message}");
}

return data;
}
}
14 replies