Toemsel / Network

C# Network Library

Home Page:https://push-force.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting 'The operation was canceled' in InvokeWork()

asusralis opened this issue · comments

at System.Threading.CancellationToken.ThrowOperationCanceledException()
at Network.Connection.InvokeWork() in C:\Users\Kyle\Desktop\tol\Network\Network\Connection.cs:line 680

Followed by:

System.ObjectDisposedException
HResult=0x80131622
Message=Cannot access a disposed object.
Object name: 'System.Net.Sockets.Socket'.
Source=System.Net.Sockets
StackTrace:
at System.Net.Sockets.Socket.get_Available()
at System.Net.Sockets.TcpClient.get_Available()
at Network.TcpConnection.ReadBytes(Int32 amount) in
TcpConnection.cs:line 153

The client is in Unity while the server is running in .Net Core 2.0. I'm on the no bluetooth branch. All the exceptions are on the server. It seems some data is passed between the two (including the massive initial packet I send) before this happens.

I found the problem. On the client a message was creating a Unity object, but you obviously can only do that on the main thread. My solution was to add the received data in a concurrent queue and, on the main thread, handle them there.

I didn't realize this was happening until I connected my network library debugger and not the unity project debugger. Sorry!

No problem at all. Btw: the main power of this lib relies on the possibility to send and receive objects. RawData is one option to transmit data. However, I strongly suggest to use objects, in order to increase the maintainability and to reduce the required client logic. I would use RawData for quick and dirty things only; and objects for everything else.

Regarding the concurrent queue. Using the Dispatcher isn't an option?