I’ve been experimenting with asynchronous programming in Unity for handling network communication recently, but I’ve encountered a tricky issue. I’m using the asynchronous method Socket.ReceiveAsync to receive data and I want to be able to cancel this operation using a CancellationTokenSource. However, no matter what I try, I can’t seem to cancel the ReceiveAsync operation successfully.
Here’s a simplified version of my code:
private async void Start()
{
IPAddress ipAddress = IPAddress.Parse(ip);
IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, port);
var socket = new Socket(remoteEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
Memory<byte> test = new Memory<byte>(new byte[2048]);
var tokenSource = new CancellationTokenSource();
InputSystem.onAnyButtonPress.Call(x =>
{
if (x is KeyControl keyboardCtl && keyboardCtl.keyCode == Key.E)
{
tokenSource.Cancel();
Debug.Log("token cancel");
}
});
Debug.Log("start receive");
try
{
int count = await socket.ReceiveAsync(test, SocketFlags.None, tokenSource.Token);
Debug.Log($"end receive : {count}");
}
catch (OperationCanceledException)
{
Debug.Log("ReceiveAsync cancelled");
}
}
In this code snippet, I expect the ReceiveAsync operation to be cancelled when a certain button (e.g., the E key on the keyboard) is pressed. However, no matter what I try, I can’t seem to trigger the cancellation operation.
I suspect the issue may lie with the Socket.ReceiveAsync method, as it doesn’t seem to respond to the CancellationTokenSource passed to it. I expect the ReceiveAsync method to throw an OperationCanceledException when the cancellation token is triggered, but instead, it continues to block the thread