chronoxor / NetCoreServer

Ultra fast and low latency asynchronous socket server & client C# .NET Core library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution
https://chronoxor.github.io/NetCoreServer
MIT License
2.63k stars 550 forks source link

Buffer-size in the 'OnReceived' methods can get too long #290

Closed Mufanza closed 5 months ago

Mufanza commented 5 months ago

Hi, First of all, a big thank You to Chronoxor & all the contributors for this amazing library - I've been using it for years and I love it!

Here's one thing that was always perplexing me: The 'Receive' and 'OnReceived' methods of the TcpSession class have the following signatures:

(byte[] buffer, long offset, long size)

However, how can the offset and size parameters be of a long type, if the buffer is just a regular array? The maximum length of an array is an int, not a long! So what happens if the methods receive either the 'offset' or the 'size' argument with a value larger than Int.MaxValue? I'd expect one of the following: Either the method fails with an 'OverflowException' Or the extra value will be truncated, resulting in a loss of data.

Is there a real risk of this happening? I wonder if the argument types could be changed from long to int, or if the method could call itself recursively on a truncated buffer if the size/offset get too big?

chronoxor commented 5 months ago

long offset, long size was introduced after we migrated our CppServer library to C#, there we use size_t which is 64bit. You should never get values bigger that Int.MaxValue.

Mufanza commented 5 months ago

Ah, okay - and thanks for the swift reply! Would it make sense to change the parameter type from float to int? Or does it break some kind of backwards compatibility?

In any case, if this is known and there's no danger of overflowing the maximum value then I guess there's no issue - so I'm closing this

chronoxor commented 5 months ago

Yes mostly for backward compatibility. Maybe in some next major release I'll refactor this.