I’ve spent weeks looking through my code for a big memory leak that’s been crashing game servers. And I’ve pulled out all of my code and was left with this… NetworkTransport.Send()!! one of the most commonly used functions in the game is eating memory roughly at the same rate that data is sent… which leads me to think that maybe the sent data isn’t being released.
Maybe I’m worng, so please let me know if this code leaks for you as well. I’m really just frustrated at the time wasted if one of the most important functions wasn’t smoke tested. Thanks.
public class test : MonoBehaviour {
int m_MyHostId;
int m_PeerId;
byte error;
bool connected = false;
void Start()
{
NetworkTransport.Init ();
ConnectionConfig myConfig = new ConnectionConfig();
myConfig.AddChannel(QosType.Reliable);
HostTopology myTopology = new HostTopology(myConfig, 10); //up to 10 connection allowed
m_MyHostId = NetworkTransport.AddHost(myTopology, 9999);
m_PeerId = NetworkTransport.Connect(m_MyHostId, "127.0.0.1", 9999, 0, out error);
if ((NetworkError)error != NetworkError.Ok)
{
Debug.LogError("Network error is occurred: " + (NetworkError)error);
}
}
void Update()
{
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
if (connected) {
for (int i = 0; i < 2; i++) {
NetworkTransport.Send (m_MyHostId, m_PeerId, 0, new byte[500], 500, out error);
if ((NetworkError)error != NetworkError.Ok)
{
Debug.LogError("Network error is occurred: " + (NetworkError)error);
}
}
}
while (true) {
NetworkEventType recData = NetworkTransport.Receive (out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
if ((NetworkError)error != NetworkError.Ok) {
Debug.LogError ("Network error is occurred: " + (NetworkError)error);
break;
}
if (recData == NetworkEventType.Nothing) {
break;
}
switch (recData) {
case NetworkEventType.ConnectEvent:
Debug.Assert (connectionId == m_PeerId, "Success");
connected = true;
break;
case NetworkEventType.DataEvent:
Debug.Log ("Received Data");
break;
case NetworkEventType.DisconnectEvent:
Debug.Assert (connectionId == m_PeerId, "Failure");
connected = false;
break;
default:
break;
}
}
}
}