TCP message lost when host my C# on (AWS) cloud server

Viewed 38

My dotnet/c# server is running very well for my Unity game on my local machine. But when I host it on an online cloud server (AWS EC2 instance) it will result in not all TCP messages arriving at the client side.

Server side method: git: github.com/IRiViI/TCPmreServer

public void SendData(Packet _packet)
        {
            try
            {
                if (socket != null)
                {
                    stream.BeginWrite(_packet.ToArray(), 0, _packet.Length(), null, null);
                }
            }
            catch (Exception _ex)
            {
                Console.WriteLine($"Error sending data to player {id} via TCP: {_ex}");
            }
        }

Client side method:git: github.com/IRiViI/TCPmreClient

  private void ReceiveCallback(IAsyncResult _result){
  try {
      int _byteLength = stream.EndRead(_result);
      if (_byteLength <= 0){
          client.Disconnect();
          return;
      }

      byte[] _data = new byte[_byteLength];
      Array.Copy(receiveBuffer, _data, _byteLength);
      receivedData.Reset(HandleData(_data)); 
      stream.BeginRead(receiveBuffer, 0, dataBufferSize, ReceiveCallback, null);
  } catch(Exception _ex) {
      Disconnect();
      Debug.Log(_ex);
  }
}

private bool HandleData(byte[] _data){
int _packetLength = 0;
receivedData.SetBytes(_data);

if(receivedData.UnreadLength() >= 4){
    _packetLength = receivedData.ReadInt();
    if (_packetLength <= 0){
        return true;
    }
}
while (_packetLength > 0 && _packetLength <= receivedData.UnreadLength()){
    byte[] _packetBytes = receivedData.ReadBytes(_packetLength);
    ThreadManager.ExecuteOnMainThread(() => {
        using (Packet _packet = new Packet(_packetBytes)){
            int _packetId = _packet.ReadInt();
            client.packetHandlers[_packetId](_packet);
        }
    });    
    _packetLength = 0;
    if(receivedData.UnreadLength() >= 4){
        _packetLength = receivedData.ReadInt();
        if (_packetLength <= 0){
            return true;
        }
    }
}
if (_packetLength <= 1){
    return true;
}
return false;
}

It only happens when I send many (100+) messages at the same time. below 100 it usally goes well.

I think the issue might be AWS related (firewall and such). But I would expect to get some error messages when a TCP message could not be send. I'm not able to find any errors. I would have expect this behaviour of UDP but not of TCP.

My question is: How can I increase the amount of messages that can be send or have some message not received callback inorder to handle failed TCP messages.

Update --

The problem seems to be on the client side. I tested my server code on my server at home and the same problem occures as when running my server code on an EC2 AWS instance.

The problem occures when there are multiple tcp packets handled.

At the moment when the tcp handler breaks, I get a packetsLength of 16777216 (which equals 2^24).

if(receivedData.UnreadLength() >= 4){
_packetLength = receivedData.ReadInt();
if (_packetLength <= 0){
    return true;
}
Console.WriteLine($"_{_packetLength}");

}

0 Answers
Related