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.75k stars 575 forks source link

SSL Message Framing #38

Closed developervariety closed 4 years ago

developervariety commented 4 years ago

I'm experiencing an issue with my implementation of message framing. I've read the other issue (#3) and practically did the same thing, but the server tends to crash at random times.

This is the code from my server.

private void SendData(string json) {
 byte[] data = Serialization.GroBuf.Serialize(json);

 byte[] sizeInfo = new byte[4];
 sizeInfo[0] = (byte) data.Length;
 sizeInfo[1] = (byte)(data.Length >> 8);
 sizeInfo[2] = (byte)(data.Length >> 16);
 sizeInfo[3] = (byte)(data.Length >> 24);

 SendAsync(sizeInfo);
 SendAsync(data);
}

private long _totalRead;
private bool _receivingMessage;
private int _messageSize;
private byte[] _fullMessage;
protected override async void OnReceived(byte[] buffer, long offset, long size) {
 if (!_receivingMessage && size == 4) {
  _messageSize |= buffer[offset + 0];
  _messageSize |= buffer[offset + 1] << 8;
  _messageSize |= buffer[offset + 2] << 16;
  _messageSize |= buffer[offset + 3] << 24;
  _receivingMessage = true;
  _fullMessage = new byte[_messageSize];
 } else if (_messageSize != 0) {
  Array.Copy(buffer, offset, _fullMessage, _totalRead, size); // System.ArgumentException: Destination array was not long enough. Check the destination index, length, and the array's lower bounds. (Parameter 'destinationArray')
  _totalRead += size;
  if (_messageSize != _totalRead) return;

  await HandleData(_fullMessage);

  _fullMessage = null;
  _messageSize = 0;
  _receivingMessage = false;
  _totalRead = 0;
 }
}

Any help is appreciated, thank you.

TailyFair commented 3 years ago

How did you resolved this issue? Was issue in framework or in application code?

developervariety commented 3 years ago

Hey @TailyFair, I'm sorry but I do not use this project anymore. I use WatsonTcp for all my needs, not to bash this amazing project.

sojournercntl commented 3 years ago

https://github.com/chronoxor/NetCoreServer/issues/117#issue-812162444