Skip to content

Commit f435322

Browse files
authored
Merge pull request #92 from cnblogs/refactor-PooledSocket-ReadAsync
Refactor PooledSocket.ReadAsync method
2 parents e270539 + 24126af commit f435322

File tree

5 files changed

+25
-33
lines changed

5 files changed

+25
-33
lines changed

Enyim.Caching.Tests/MemcachedClientGetTests.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ public async Task GetValueOrCreateAsyncTest()
103103
{
104104
var key = "GetValueOrCreateAsyncTest_" + Guid.NewGuid();
105105
var posts1 = await _client.GetValueOrCreateAsync(
106-
key,
107-
10,
106+
key,
107+
10,
108108
async () => await GenerateValue());
109109
Assert.NotNull(posts1);
110110

@@ -129,6 +129,7 @@ private Task<IEnumerable<BlogPost>> GenerateValue()
129129
internal class BlogPost
130130
{
131131
public string Title { get; set; }
132+
public string Tags { get; set; }
132133
public string Body { get; set; }
133134
}
134135
}

Enyim.Caching/Enyim.Caching.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>EnyimMemcachedCore is a Memcached client library for .NET Core. Usage: Add services.AddEnyimMemcached(...) and app.UseEnyimMemcached() in Startup. Add IMemcachedClient into constructor.</Description>
5-
<VersionPrefix>2.1.8</VersionPrefix>
5+
<VersionPrefix>2.1.11</VersionPrefix>
66
<Authors>cnblogs.com</Authors>
77
<TargetFramework>netstandard2.0</TargetFramework>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

Enyim.Caching/Memcached/PooledSocket.cs

+15-26
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public PooledSocket(DnsEndPoint endpoint, TimeSpan connectionTimeout, TimeSpan r
3535

3636
var timeout = connectionTimeout == TimeSpan.MaxValue
3737
? Timeout.Infinite
38-
: (int) connectionTimeout.TotalMilliseconds;
38+
: (int)connectionTimeout.TotalMilliseconds;
3939

4040
var rcv = receiveTimeout == TimeSpan.MaxValue
4141
? Timeout.Infinite
42-
: (int) receiveTimeout.TotalMilliseconds;
42+
: (int)receiveTimeout.TotalMilliseconds;
4343

4444
socket.ReceiveTimeout = rcv;
4545
socket.SendTimeout = rcv;
@@ -234,41 +234,30 @@ public int ReadByteAsync()
234234
}
235235
}
236236

237-
// public async Task<byte[]> ReadBytesAsync(int count)
238-
// {
239-
// var buffer = new ArraySegment<byte>(new byte[count], 0, count);
240-
// await this.socket.ReceiveAsync(buffer, SocketFlags.None);
241-
// return buffer.Array;
242-
// }
243-
//
244-
public async Task<byte[]> ReadBytesAsync(int count)
237+
public async Task ReadAsync(byte[] buffer, int offset, int count)
245238
{
246239
this.CheckDisposed();
247-
try
240+
241+
int read = 0;
242+
int shouldRead = count;
243+
244+
while (read < count)
248245
{
249-
var buffer = new ArraySegment<byte>(new byte[count], 0, count);
250-
int read = 0;
251-
int offset = 0;
252-
int shouldRead = count;
253-
while (read < count)
246+
try
254247
{
255-
var currentRead = await inputStream.ReadAsync(buffer.Array, offset, shouldRead);
248+
int currentRead = await this.inputStream.ReadAsync(buffer, offset, shouldRead);
256249
if (currentRead < 1)
257-
{
258250
continue;
259-
}
260251

261252
read += currentRead;
262253
offset += currentRead;
263254
shouldRead -= currentRead;
264255
}
265-
266-
return buffer.Array;
267-
}
268-
catch (IOException)
269-
{
270-
this.isAlive = false;
271-
throw;
256+
catch (IOException)
257+
{
258+
this.isAlive = false;
259+
throw;
260+
}
272261
}
273262
}
274263

Enyim.Caching/Memcached/Protocol/Binary/BinaryResponse.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public unsafe bool Read(PooledSocket socket)
6464
if (dataLength > 0)
6565
{
6666
var data = new byte[dataLength];
67-
socket.Read(data, 0, dataLength);
67+
socket.Read(data, 0, dataLength);
6868

6969
this.Extra = new ArraySegment<byte>(data, 0, extraLength);
7070
this.Data = new ArraySegment<byte>(data, extraLength, data.Length - extraLength);
@@ -79,15 +79,17 @@ public async Task<bool> ReadAsync(PooledSocket socket)
7979

8080
if (!socket.IsAlive) return false;
8181

82-
var header = await socket.ReadBytesAsync(HeaderLength);
82+
var header = new byte[HeaderLength];
83+
await socket.ReadAsync(header, 0, header.Length);
8384

8485
int dataLength, extraLength;
8586

8687
DeserializeHeader(header, out dataLength, out extraLength);
8788

8889
if (dataLength > 0)
8990
{
90-
var data = await socket.ReadBytesAsync(dataLength);
91+
var data = new byte[dataLength];
92+
await socket.ReadAsync(data, 0, dataLength);
9193

9294
this.Extra = new ArraySegment<byte>(data, 0, extraLength);
9395
this.Data = new ArraySegment<byte>(data, extraLength, data.Length - extraLength);

EnyimCachingCore.sln

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{46CD2BBA-9
1717
EndProject
1818
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{9954C92E-50E2-477C-AC47-31C857C46370}"
1919
EndProject
20-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWebApp.IntegrationTests", "SampleWebApp.IntegrationTests\SampleWebApp.IntegrationTests.csproj", "{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}"
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleWebApp.IntegrationTests", "SampleWebApp.IntegrationTests\SampleWebApp.IntegrationTests.csproj", "{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}"
2121
EndProject
2222
Global
2323
GlobalSection(SolutionConfigurationPlatforms) = preSolution

0 commit comments

Comments
 (0)