diff --git a/OpenSim/Capabilities/Handlers/FetchInventory/FetchInventory2Handler.cs b/OpenSim/Capabilities/Handlers/FetchInventory/FetchInventory2Handler.cs index 0f9ff4ac6f..807f97a400 100644 --- a/OpenSim/Capabilities/Handlers/FetchInventory/FetchInventory2Handler.cs +++ b/OpenSim/Capabilities/Handlers/FetchInventory/FetchInventory2Handler.cs @@ -27,7 +27,6 @@ using System.Net; using System.Reflection; -using System.Text; using OpenMetaverse; using OpenMetaverse.StructuredData; using OpenSim.Framework; diff --git a/OpenSim/Data/MySQL/MySQLAssetData.cs b/OpenSim/Data/MySQL/MySQLAssetData.cs index 8569c903a6..36a89f246e 100644 --- a/OpenSim/Data/MySQL/MySQLAssetData.cs +++ b/OpenSim/Data/MySQL/MySQLAssetData.cs @@ -324,7 +324,7 @@ namespace OpenSim.Data.MySQL metadata.CreatorID = dbReader["CreatorID"].ToString(); // Current SHA1s are not stored/computed. - metadata.SHA1 = new byte[] { }; + metadata.SHA1 = Array.Empty(); retList.Add(metadata); } diff --git a/OpenSim/Data/MySQL/MySQLXAssetData.cs b/OpenSim/Data/MySQL/MySQLXAssetData.cs index 0b69ca4f17..13c72433bb 100644 --- a/OpenSim/Data/MySQL/MySQLXAssetData.cs +++ b/OpenSim/Data/MySQL/MySQLXAssetData.cs @@ -231,27 +231,20 @@ namespace OpenSim.Data.MySQL asset.Description, asset.ID, asset.Description.Length, assetDescription.Length); } + byte[] data; if (m_enableCompression) { - MemoryStream outputStream = new MemoryStream(); - - using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress, false)) - { -// Console.WriteLine(WebUtil.CopyTo(new MemoryStream(asset.Data), compressionStream, int.MaxValue)); - // We have to close the compression stream in order to make sure it writes everything out to the underlying memory output stream. - compressionStream.Close(); - byte[] compressedData = outputStream.ToArray(); - asset.Data = compressedData; - } + using MemoryStream inMs = new(asset.Data); + using MemoryStream outputStream = new(); + using GZipStream compressionStream = new(outputStream, CompressionMode.Compress); + inMs.CopyTo(compressionStream); + compressionStream.Flush(); + data = outputStream.ToArray(); } + else + data = asset.Data; - byte[] hash; - using (HashAlgorithm hasher = SHA256.Create()) - hash = hasher.ComputeHash(asset.Data); - -// m_log.DebugFormat( -// "[XASSET DB]: Compressed data size for {0} {1}, hash {2} is {3}", -// asset.ID, asset.Name, hash, compressedData.Length); + byte[] hash = SHA256.HashData(data); try { @@ -297,7 +290,7 @@ namespace OpenSim.Data.MySQL dbcon)) { cmd.Parameters.AddWithValue("?Hash", hash); - cmd.Parameters.AddWithValue("?Data", asset.Data); + cmd.Parameters.AddWithValue("?Data", data); cmd.ExecuteNonQuery(); } } diff --git a/OpenSim/Framework/AssetLoader/Filesystem/AssetLoaderFileSystem.cs b/OpenSim/Framework/AssetLoader/Filesystem/AssetLoaderFileSystem.cs index 317ca8866b..4987eeff93 100644 --- a/OpenSim/Framework/AssetLoader/Filesystem/AssetLoaderFileSystem.cs +++ b/OpenSim/Framework/AssetLoader/Filesystem/AssetLoaderFileSystem.cs @@ -78,9 +78,8 @@ namespace OpenSim.Framework.AssetLoader.Filesystem if (fInfo.Exists) { FileStream fStream = new FileStream(path, FileMode.Open, FileAccess.Read); - byte[] idata = new byte[numBytes]; BinaryReader br = new BinaryReader(fStream); - idata = br.ReadBytes((int)numBytes); + byte[] idata = br.ReadBytes((int)numBytes); br.Close(); fStream.Close(); info.Data = idata; diff --git a/OpenSim/Framework/LLSDxmlEncode.cs b/OpenSim/Framework/LLSDxmlEncode.cs index 1c35068641..f38516880a 100644 --- a/OpenSim/Framework/LLSDxmlEncode.cs +++ b/OpenSim/Framework/LLSDxmlEncode.cs @@ -794,8 +794,8 @@ namespace OpenSim.Framework public static byte[] ulongToByteArray(ulong uLongValue) { - return new byte[8] - { + return + [ (byte)(uLongValue >> 56), (byte)(uLongValue >> 48), (byte)(uLongValue >> 40), @@ -804,18 +804,18 @@ namespace OpenSim.Framework (byte)(uLongValue >> 16), (byte)(uLongValue >> 8), (byte)uLongValue - }; + ]; } public static byte[] uintToByteArray(uint value) { - return new byte[4] - { + return + [ (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)value - }; + ]; } static readonly char[] base64Chars = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O', diff --git a/OpenSim/Framework/MultipartForm.cs b/OpenSim/Framework/MultipartForm.cs index 35cf82adb3..49acad7b97 100644 --- a/OpenSim/Framework/MultipartForm.cs +++ b/OpenSim/Framework/MultipartForm.cs @@ -119,7 +119,7 @@ namespace OpenSim.Framework // Copy the temporary stream to the network stream formDataStream.Seek(0, SeekOrigin.Begin); using (Stream requestStream = request.GetRequestStream()) - formDataStream.CopyStream(requestStream, (int)formDataStream.Length); + formDataStream.CopyTo(requestStream, (int)formDataStream.Length); } #endregion Stream Writing diff --git a/OpenSim/Framework/PrimitiveBaseShape.cs b/OpenSim/Framework/PrimitiveBaseShape.cs index fb9c416ec0..fee56919a8 100644 --- a/OpenSim/Framework/PrimitiveBaseShape.cs +++ b/OpenSim/Framework/PrimitiveBaseShape.cs @@ -1508,7 +1508,7 @@ namespace OpenSim.Framework return null; if (RenderMaterials.overrides is null || RenderMaterials.overrides.Length == 0) - return new byte[] { 0 }; // store so outdated viewer caches can be updated + return [0]; // store so outdated viewer caches can be updated int nentries = 0; for (int i = 0; i < RenderMaterials.overrides.Length; i++) @@ -1517,7 +1517,7 @@ namespace OpenSim.Framework nentries++; } if(nentries == 0) - return new byte[] { 0 }; + return [0]; osUTF8 sb = OSUTF8Cached.Acquire(); sb.Append((byte)nentries); diff --git a/OpenSim/Framework/TerrainData.cs b/OpenSim/Framework/TerrainData.cs index 2209203584..3eab9fd746 100644 --- a/OpenSim/Framework/TerrainData.cs +++ b/OpenSim/Framework/TerrainData.cs @@ -630,37 +630,34 @@ namespace OpenSim.Framework Array ret = null; try { - using (MemoryStream inp = new MemoryStream((2 * sizeof(Int32)) + (SizeX * SizeY * sizeof(float)))) + using (MemoryStream inp = new MemoryStream((2 * sizeof(int)) + (SizeX * SizeY * sizeof(float)))) { using (BinaryWriter bw = new BinaryWriter(inp)) { - bw.Write((Int32)SizeX); - bw.Write((Int32)SizeY); + bw.Write(SizeX); + bw.Write(SizeY); for (int yy = 0; yy < SizeY; yy++) for (int xx = 0; xx < SizeX; xx++) { - //bw.Write((float)m_heightmap[xx, yy]); bw.Write(MathF.Round(m_heightmap[xx, yy], 3, MidpointRounding.AwayFromZero)); } bw.Flush(); inp.Seek(0, SeekOrigin.Begin); - using (MemoryStream outputStream = new MemoryStream()) - { - using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress)) - { - inp.CopyStream(compressionStream, int.MaxValue); - compressionStream.Close(); - ret = outputStream.ToArray(); - } - } + using MemoryStream outputStream = new MemoryStream(); + using GZipStream compressionStream = new(outputStream, CompressionMode.Compress); + inp.CopyTo(compressionStream); + compressionStream.Flush(); + ret = outputStream.ToArray(); } } + m_log.Debug($"{LogHeader} V2DGzip {ret.Length} bytes"); + } + catch (Exception ex) + { + m_log.Error($"{LogHeader} V2DGzip error: {ex.Message}"); } - catch {} - - m_log.DebugFormat("{0} V2DGzip {1} bytes", LogHeader, ret.Length); return ret; } @@ -787,7 +784,6 @@ namespace OpenSim.Framework { using (GZipStream decompressionStream = new GZipStream(inputStream, CompressionMode.Decompress)) { - decompressionStream.Flush(); decompressionStream.CopyTo(outputStream); } } diff --git a/OpenSim/Framework/WebUtil.cs b/OpenSim/Framework/WebUtil.cs index 744e732e44..6911fdc91c 100644 --- a/OpenSim/Framework/WebUtil.cs +++ b/OpenSim/Framework/WebUtil.cs @@ -26,6 +26,7 @@ */ using System; +using System.Buffers; using System.Collections; using System.Collections.Specialized; using System.Globalization; @@ -804,18 +805,18 @@ namespace OpenSim.Framework /// public static int CopyStream(this Stream copyFrom, Stream copyTo, int maximumBytesToCopy) { - byte[] buffer = new byte[4096]; + byte[] buffer = ArrayPool.Shared.Rent(8196); int readBytes; int totalCopiedBytes = 0; - while ((readBytes = copyFrom.Read(buffer, 0, Math.Min(4096, maximumBytesToCopy))) > 0) + while ((readBytes = copyFrom.Read(buffer, 0, Math.Min(8196, maximumBytesToCopy))) > 0) { int writeBytes = Math.Min(maximumBytesToCopy, readBytes); copyTo.Write(buffer, 0, writeBytes); totalCopiedBytes += writeBytes; maximumBytesToCopy -= writeBytes; } - + ArrayPool.Shared.Return(buffer); return totalCopiedBytes; } diff --git a/OpenSim/Region/ClientStack/Linden/UDP/LLClientView.cs b/OpenSim/Region/ClientStack/Linden/UDP/LLClientView.cs index 6d8cfc7dd7..99dc3a18d0 100755 --- a/OpenSim/Region/ClientStack/Linden/UDP/LLClientView.cs +++ b/OpenSim/Region/ClientStack/Linden/UDP/LLClientView.cs @@ -8533,7 +8533,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP ) return true; - float qdelta = MathF.Abs(x.BodyRotation.Dot(m_thisAgentUpdateArgs.BodyRotation)); + float qdelta = MathF.Abs(x.BodyRotation.Dot(ref m_thisAgentUpdateArgs.BodyRotation)); return qdelta < QDELTABody; // significant if body rotation above(below cos) threshold } diff --git a/OpenSim/Region/CoreModules/ServiceConnectorsOut/Asset/RegionAssetConnectorModule.cs b/OpenSim/Region/CoreModules/ServiceConnectorsOut/Asset/RegionAssetConnectorModule.cs index b41ed78dd3..c9856ca211 100644 --- a/OpenSim/Region/CoreModules/ServiceConnectorsOut/Asset/RegionAssetConnectorModule.cs +++ b/OpenSim/Region/CoreModules/ServiceConnectorsOut/Asset/RegionAssetConnectorModule.cs @@ -515,7 +515,6 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset return null; } - private readonly string stringUUIDZero = UUID.Zero.ToString(); public string Store(AssetBase asset) { string id; @@ -527,7 +526,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset id = StoreForeign(asset); if (m_Cache != null) { - if (!string.IsNullOrEmpty(id) && !id.Equals(stringUUIDZero)) + if (!string.IsNullOrEmpty(id) && !id.Equals(UUID.ZeroString)) m_Cache.Cache(asset); } return id; diff --git a/OpenSim/Region/Framework/Scenes/ScenePresence.cs b/OpenSim/Region/Framework/Scenes/ScenePresence.cs index b03c38f445..2e0f759182 100644 --- a/OpenSim/Region/Framework/Scenes/ScenePresence.cs +++ b/OpenSim/Region/Framework/Scenes/ScenePresence.cs @@ -1649,7 +1649,7 @@ namespace OpenSim.Region.Framework.Scenes /// public void MakeChildAgent(ulong newRegionHandle) { - m_updateAgentReceivedAfterTransferEvent.Reset(); + m_updateAgentReceivedAfterTransferEvent?.Reset(); m_haveGroupInformation = false; m_gotCrossUpdate = false; m_crossingFlags = 0; @@ -1660,6 +1660,9 @@ namespace OpenSim.Region.Framework.Scenes m_log.DebugFormat("[SCENE PRESENCE]: Making {0} a child agent in {1} from root region {2}", Name, Scene.RegionInfo.RegionName, newRegionHandle); + if(disposed) + return; + // Reset the m_originRegionID as it has dual use as a flag to signal that the UpdateAgent() call orignating // from the source simulator has completed on a V2 teleport. lock (m_originRegionIDAccessLock) diff --git a/OpenSim/Region/PhysicsModules/ubOdeMeshing/Meshmerizer.cs b/OpenSim/Region/PhysicsModules/ubOdeMeshing/Meshmerizer.cs index 4591dfaa2d..02469acfa2 100644 --- a/OpenSim/Region/PhysicsModules/ubOdeMeshing/Meshmerizer.cs +++ b/OpenSim/Region/PhysicsModules/ubOdeMeshing/Meshmerizer.cs @@ -448,12 +448,7 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing using (MemoryStream inMs = new(primShape.SculptData, physOffset + 2 , physSize - 2)) // skip first 2 bytes in header { using DeflateStream decompressionStream = new(inMs, CompressionMode.Decompress); - byte[] readBuffer = ArrayPool.Shared.Rent(8192); - int readLen = 0; - - while ((readLen = decompressionStream.Read(readBuffer, 0, readBuffer.Length)) > 0) - outMs.Write(readBuffer, 0, readLen); - ArrayPool.Shared.Return(readBuffer); + decompressionStream.CopyTo(outMs); } outMs.Seek(0, SeekOrigin.Begin); decodedMeshOsd = OSDParser.DeserializeLLSDBinary(outMs); diff --git a/OpenSim/Services/FSAssetService/FSAssetService.cs b/OpenSim/Services/FSAssetService/FSAssetService.cs index 3e92227cac..363f2ebece 100644 --- a/OpenSim/Services/FSAssetService/FSAssetService.cs +++ b/OpenSim/Services/FSAssetService/FSAssetService.cs @@ -363,11 +363,7 @@ namespace OpenSim.Services.FSAssetService string GetSHA256Hash(byte[] data) { - byte[] hash; - using (SHA256 sha = SHA256.Create()) - hash = sha.ComputeHash(data); - - return BitConverter.ToString(hash).Replace("-", String.Empty); + return Util.SHA256Hash(data); } public string HashToPath(string hash)