5
5
using System . Linq ;
6
6
using System . Net ;
7
7
using System . Net . Sockets ;
8
- using System . Reflection ;
9
- using System . Runtime . InteropServices ;
10
8
using System . Text ;
11
9
using System . Threading ;
12
10
using System . Threading . Tasks ;
@@ -36,12 +34,12 @@ public PooledSocket(DnsEndPoint endpoint, TimeSpan connectionTimeout, TimeSpan r
36
34
socket . NoDelay = true ;
37
35
38
36
var timeout = connectionTimeout == TimeSpan . MaxValue
39
- ? Timeout . Infinite
40
- : ( int ) connectionTimeout . TotalMilliseconds ;
37
+ ? Timeout . Infinite
38
+ : ( int ) connectionTimeout . TotalMilliseconds ;
41
39
42
40
var rcv = receiveTimeout == TimeSpan . MaxValue
43
41
? Timeout . Infinite
44
- : ( int ) receiveTimeout . TotalMilliseconds ;
42
+ : ( int ) receiveTimeout . TotalMilliseconds ;
45
43
46
44
socket . ReceiveTimeout = rcv ;
47
45
socket . SendTimeout = rcv ;
@@ -62,7 +60,8 @@ private void ConnectWithTimeout(Socket socket, DnsEndPoint endpoint, int timeout
62
60
//Workaround for https://github.com/dotnet/corefx/issues/26840
63
61
if ( ! IPAddress . TryParse ( endpoint . Host , out var address ) )
64
62
{
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 ) ;
66
65
if ( address == null )
67
66
throw new ArgumentException ( String . Format ( "Could not resolve host '{0}'." , endpoint . Host ) ) ;
68
67
args . RemoteEndPoint = new IPEndPoint ( address , endpoint . Port ) ;
@@ -105,7 +104,9 @@ public void Reset()
105
104
if ( available > 0 )
106
105
{
107
106
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 ) ;
109
110
110
111
byte [ ] data = new byte [ available ] ;
111
112
@@ -140,8 +141,13 @@ public void Destroy()
140
141
141
142
~ PooledSocket ( )
142
143
{
143
- try { this . Dispose ( true ) ; }
144
- catch { }
144
+ try
145
+ {
146
+ this . Dispose ( true ) ;
147
+ }
148
+ catch
149
+ {
150
+ }
145
151
}
146
152
147
153
protected void Dispose ( bool disposing )
@@ -153,8 +159,13 @@ protected void Dispose(bool disposing)
153
159
try
154
160
{
155
161
if ( socket != null )
156
- try { this . socket . Dispose ( ) ; }
157
- catch { }
162
+ try
163
+ {
164
+ this . socket . Dispose ( ) ;
165
+ }
166
+ catch
167
+ {
168
+ }
158
169
159
170
if ( this . inputStream != null )
160
171
this . inputStream . Dispose ( ) ;
@@ -223,11 +234,42 @@ public int ReadByteAsync()
223
234
}
224
235
}
225
236
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
+ //
226
244
public async Task < byte [ ] > ReadBytesAsync ( int count )
227
245
{
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
+ }
231
273
}
232
274
233
275
/// <summary>
@@ -343,6 +385,7 @@ public bool ReceiveAsync(AsyncIOArgs p)
343
385
}
344
386
345
387
#region [ License information ]
388
+
346
389
/* ************************************************************
347
390
*
348
391
* Copyright (c) 2010 Attila Kisk? enyim.com
@@ -360,4 +403,5 @@ public bool ReceiveAsync(AsyncIOArgs p)
360
403
* limitations under the License.
361
404
*
362
405
* ************************************************************/
363
- #endregion
406
+
407
+ #endregion
0 commit comments