Skip to content

Commit e270539

Browse files
authored
Merge pull request #87 from cnblogs/hotfix-85
hotfix #85
2 parents 65321a4 + f230763 commit e270539

File tree

1 file changed

+59
-15
lines changed

1 file changed

+59
-15
lines changed

Enyim.Caching/Memcached/PooledSocket.cs

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
using System.Linq;
66
using System.Net;
77
using System.Net.Sockets;
8-
using System.Reflection;
9-
using System.Runtime.InteropServices;
108
using System.Text;
119
using System.Threading;
1210
using System.Threading.Tasks;
@@ -36,12 +34,12 @@ public PooledSocket(DnsEndPoint endpoint, TimeSpan connectionTimeout, TimeSpan r
3634
socket.NoDelay = true;
3735

3836
var timeout = connectionTimeout == TimeSpan.MaxValue
39-
? Timeout.Infinite
40-
: (int)connectionTimeout.TotalMilliseconds;
37+
? Timeout.Infinite
38+
: (int) connectionTimeout.TotalMilliseconds;
4139

4240
var rcv = receiveTimeout == TimeSpan.MaxValue
4341
? Timeout.Infinite
44-
: (int)receiveTimeout.TotalMilliseconds;
42+
: (int) receiveTimeout.TotalMilliseconds;
4543

4644
socket.ReceiveTimeout = rcv;
4745
socket.SendTimeout = rcv;
@@ -62,7 +60,8 @@ private void ConnectWithTimeout(Socket socket, DnsEndPoint endpoint, int timeout
6260
//Workaround for https://github.com/dotnet/corefx/issues/26840
6361
if (!IPAddress.TryParse(endpoint.Host, out var address))
6462
{
65-
address = Dns.GetHostAddresses(endpoint.Host).FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
63+
address = Dns.GetHostAddresses(endpoint.Host)
64+
.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
6665
if (address == null)
6766
throw new ArgumentException(String.Format("Could not resolve host '{0}'.", endpoint.Host));
6867
args.RemoteEndPoint = new IPEndPoint(address, endpoint.Port);
@@ -105,7 +104,9 @@ public void Reset()
105104
if (available > 0)
106105
{
107106
if (_logger.IsEnabled(LogLevel.Warning))
108-
_logger.LogWarning("Socket bound to {0} has {1} unread data! This is probably a bug in the code. InstanceID was {2}.", this.socket.RemoteEndPoint, available, this.InstanceId);
107+
_logger.LogWarning(
108+
"Socket bound to {0} has {1} unread data! This is probably a bug in the code. InstanceID was {2}.",
109+
this.socket.RemoteEndPoint, available, this.InstanceId);
109110

110111
byte[] data = new byte[available];
111112

@@ -140,8 +141,13 @@ public void Destroy()
140141

141142
~PooledSocket()
142143
{
143-
try { this.Dispose(true); }
144-
catch { }
144+
try
145+
{
146+
this.Dispose(true);
147+
}
148+
catch
149+
{
150+
}
145151
}
146152

147153
protected void Dispose(bool disposing)
@@ -153,8 +159,13 @@ protected void Dispose(bool disposing)
153159
try
154160
{
155161
if (socket != null)
156-
try { this.socket.Dispose(); }
157-
catch { }
162+
try
163+
{
164+
this.socket.Dispose();
165+
}
166+
catch
167+
{
168+
}
158169

159170
if (this.inputStream != null)
160171
this.inputStream.Dispose();
@@ -223,11 +234,42 @@ public int ReadByteAsync()
223234
}
224235
}
225236

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+
//
226244
public async Task<byte[]> ReadBytesAsync(int count)
227245
{
228-
var buffer = new ArraySegment<byte>(new byte[count], 0, count);
229-
await this.socket.ReceiveAsync(buffer, SocketFlags.None);
230-
return buffer.Array;
246+
this.CheckDisposed();
247+
try
248+
{
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)
254+
{
255+
var currentRead = await inputStream.ReadAsync(buffer.Array, offset, shouldRead);
256+
if (currentRead < 1)
257+
{
258+
continue;
259+
}
260+
261+
read += currentRead;
262+
offset += currentRead;
263+
shouldRead -= currentRead;
264+
}
265+
266+
return buffer.Array;
267+
}
268+
catch (IOException)
269+
{
270+
this.isAlive = false;
271+
throw;
272+
}
231273
}
232274

233275
/// <summary>
@@ -343,6 +385,7 @@ public bool ReceiveAsync(AsyncIOArgs p)
343385
}
344386

345387
#region [ License information ]
388+
346389
/* ************************************************************
347390
*
348391
* Copyright (c) 2010 Attila Kisk? enyim.com
@@ -360,4 +403,5 @@ public bool ReceiveAsync(AsyncIOArgs p)
360403
* limitations under the License.
361404
*
362405
* ************************************************************/
363-
#endregion
406+
407+
#endregion

0 commit comments

Comments
 (0)