/// <summary>Disconnects from the server and cleans up the UDP connection.</summary> private void Disconnect() { Active.Disconnect(); endPoint = null; socket = null; }
/// <summary>Disconnects from the server and cleans up the TCP connection.</summary> private void Disconnect() { Active.Disconnect(); stream = null; receivedData = null; receiveBuffer = null; socket = null; }
/// <summary>Receives incoming UDP data.</summary> private void ReceiveCallback(IAsyncResult _result) { try { byte[] _data = socket.EndReceive(_result, ref endPoint); socket.BeginReceive(ReceiveCallback, null); if (_data.Length < 4) { Active.Disconnect(); return; } HandleData(_data); } catch { Disconnect(); } }
/// <summary>Reads incoming data from the stream.</summary> private void ReceiveCallback(IAsyncResult _result) { try { int _byteLength = stream.EndRead(_result); if (_byteLength <= 0) { Active.Disconnect(); return; } byte[] _data = new byte[_byteLength]; Array.Copy(receiveBuffer, _data, _byteLength); receivedData.Reset(HandleData(_data)); // Reset receivedData if all data was handled stream.BeginRead(receiveBuffer, 0, dataBufferSize, ReceiveCallback, null); } catch { Disconnect(); } }