/// <summary> /// Performs a get synchronously /// </summary> /// <param name="id">The ID of the file to get</param> /// <param name="hash">The hash of the files contents</param> /// <returns>The result of the get operation</returns> public UnityCacheClientGetResult Get(Guid id, string hash) { UnityCacheClientGetResult result = new UnityCacheClientGetResult(); result.Id = id; result.Hash = hash; byte[] command = Encoding.ASCII.GetBytes("g"); this.stream.WriteByte(command[0]); UnityCacheUtilities.SendIdAndHashOnStream(this.stream, id, hash); this.stream.Read(command, 0, command.Length); string strResult = Encoding.ASCII.GetString(command); if (strResult == "-") { result.Result = CacheResult.CacheMiss; // Read and toss the hash since we don't need it UnityCacheUtilities.ReadGuid(this.stream); UnityCacheUtilities.ReadHash(this.stream); } else if (strResult == "+") { result.Result = CacheResult.CacheHit; // Read the length of the file byte[] buffer = new byte[16]; this.stream.Read(buffer, 0, 16); ulong bytesToBeRead = UnityCacheUtilities.GetAsciiBytesAsUInt64(buffer); // Read the ID and hash. Toss this, we don't need it UnityCacheUtilities.ReadGuid(this.stream); UnityCacheUtilities.ReadHash(this.stream); // Read the reply from the server buffer = new byte[bytesToBeRead]; result.Data = buffer; int offset = 0; while (bytesToBeRead > 0) { int len = (bytesToBeRead > (ulong)this.streamBlockSize) ? this.streamBlockSize : (int)bytesToBeRead; ulong bytesReturned = (ulong)this.stream.Read(buffer, offset, len); bytesToBeRead -= (ulong)bytesReturned; offset += (int)bytesReturned; } } return(result); }
/// <summary> /// Performs a get synchronously /// </summary> /// <param name="id">The ID of the file to get</param> /// <param name="hash">The hash of the files contents</param> /// <returns>The result of the get operation</returns> public UnityCacheClientGetResult Get(Guid id, string hash) { UnityCacheClientGetResult result = new UnityCacheClientGetResult(); result.Id = id; result.Hash = hash; byte[] command = Encoding.ASCII.GetBytes("g"); this.stream.WriteByte(command[0]); UnityCacheUtilities.SendIdAndHashOnStream(this.stream, id, hash); this.stream.Read(command, 0, command.Length); string strResult = Encoding.ASCII.GetString(command); if (strResult == "-") { result.Result = CacheResult.CacheMiss; // Read and toss the hash since we don't need it UnityCacheUtilities.ReadGuid(this.stream); UnityCacheUtilities.ReadHash(this.stream); } else if (strResult == "+") { result.Result = CacheResult.CacheHit; // Read the length of the file byte[] buffer = new byte[16]; this.stream.Read(buffer, 0, 16); ulong bytesToBeRead = UnityCacheUtilities.GetAsciiBytesAsUInt64(buffer); // Read the ID and hash. Toss this, we don't need it UnityCacheUtilities.ReadGuid(this.stream); UnityCacheUtilities.ReadHash(this.stream); // Read the reply from the server buffer = new byte[bytesToBeRead]; result.Data = buffer; int offset = 0; while (bytesToBeRead > 0) { int len = (bytesToBeRead > (ulong)this.streamBlockSize) ? this.streamBlockSize : (int)bytesToBeRead; ulong bytesReturned = (ulong)this.stream.Read(buffer, offset, len); bytesToBeRead -= (ulong)bytesReturned; offset += (int)bytesReturned; } } return result; }