1
1
using System ;
2
2
using System . IO ;
3
3
using System . Net ;
4
- using System . Collections . Generic ;
5
4
using JetBrains . Annotations ;
6
5
using PatchKit . Logging ;
7
6
using PatchKit . Network ;
@@ -15,25 +14,26 @@ public sealed class BaseHttpDownloader : IBaseHttpDownloader
15
14
{
16
15
private readonly ILogger _logger ;
17
16
18
- private static readonly ulong DefaultBufferSize = 5 * ( ulong ) Units . MB ;
17
+ private static readonly int BufferSize = 5 * ( int ) Units . MB ;
19
18
20
19
private readonly string _url ;
21
20
private readonly int _timeout ;
22
21
private readonly IHttpClient _httpClient ;
23
22
24
- private readonly ulong _bufferSize ;
25
23
private readonly byte [ ] _buffer ;
26
24
27
25
private bool _downloadHasBeenCalled ;
28
26
private BytesRange ? _bytesRange ;
29
27
28
+ public event DataAvailableHandler DataAvailable ;
29
+
30
30
public BaseHttpDownloader ( string url , int timeout ) :
31
- this ( url , timeout , new DefaultHttpClient ( ) , PatcherLogManager . DefaultLogger , DefaultBufferSize )
31
+ this ( url , timeout , new DefaultHttpClient ( ) , PatcherLogManager . DefaultLogger )
32
32
{
33
33
}
34
34
35
35
public BaseHttpDownloader ( [ NotNull ] string url , int timeout , [ NotNull ] IHttpClient httpClient ,
36
- [ NotNull ] ILogger logger , ulong bufferSize )
36
+ [ NotNull ] ILogger logger )
37
37
{
38
38
if ( string . IsNullOrEmpty ( url ) ) throw new ArgumentException ( "Value cannot be null or empty." , "url" ) ;
39
39
if ( timeout <= 0 ) throw new ArgumentOutOfRangeException ( "timeout" ) ;
@@ -45,8 +45,7 @@ public BaseHttpDownloader([NotNull] string url, int timeout, [NotNull] IHttpClie
45
45
_httpClient = httpClient ;
46
46
_logger = logger ;
47
47
48
- _bufferSize = bufferSize ;
49
- _buffer = new byte [ _bufferSize ] ;
48
+ _buffer = new byte [ BufferSize ] ;
50
49
51
50
ServicePointManager . ServerCertificateValidationCallback =
52
51
( sender , certificate , chain , errors ) => true ;
@@ -63,26 +62,13 @@ public void SetBytesRange(BytesRange? range)
63
62
}
64
63
}
65
64
66
- public void Download ( CancellationToken cancellationToken , [ NotNull ] DataAvailableHandler onDataAvailable )
67
- {
68
- if ( onDataAvailable == null )
69
- {
70
- throw new ArgumentNullException ( "onDataAvailable" ) ;
71
- }
72
-
73
- foreach ( DataPacket packet in ReadPackets ( cancellationToken ) )
74
- {
75
- onDataAvailable ( packet . Data , packet . Length ) ;
76
- }
77
- }
78
-
79
- public IEnumerable < DataPacket > ReadPackets ( CancellationToken cancellationToken )
65
+ public void Download ( CancellationToken cancellationToken )
80
66
{
81
67
try
82
68
{
83
69
_logger . LogDebug ( "Downloading..." ) ;
84
70
_logger . LogTrace ( "url = " + _url ) ;
85
- _logger . LogTrace ( "bufferSize = " + _bufferSize ) ;
71
+ _logger . LogTrace ( "bufferSize = " + BufferSize ) ;
86
72
_logger . LogTrace ( "bytesRange = " + ( _bytesRange . HasValue
87
73
? _bytesRange . Value . Start + "-" + _bytesRange . Value . End
88
74
: "(none)" ) ) ;
@@ -98,7 +84,37 @@ public IEnumerable<DataPacket> ReadPackets(CancellationToken cancellationToken)
98
84
ReadWriteTimeout = _timeout ,
99
85
} ;
100
86
101
- return ReadResponseStream ( request , cancellationToken ) ;
87
+ using ( var response = _httpClient . Get ( request ) )
88
+ {
89
+ cancellationToken . ThrowIfCancellationRequested ( ) ;
90
+
91
+ _logger . LogDebug ( "Received response from server." ) ;
92
+ _logger . LogTrace ( "statusCode = " + response . StatusCode ) ;
93
+
94
+ if ( Is2XXStatus ( response . StatusCode ) )
95
+ {
96
+ _logger . LogDebug ( "Successful response. Reading response stream..." ) ;
97
+
98
+ //TODO: Could response.ContentStream be null? Need to check it.
99
+
100
+ ReadResponseStream ( response . ContentStream , cancellationToken ) ;
101
+
102
+ _logger . LogDebug ( "Stream has been read." ) ;
103
+ }
104
+ else if ( Is4XXStatus ( response . StatusCode ) )
105
+ {
106
+ throw new DataNotAvailableException ( string . Format (
107
+ "Request data for {0} is not available (status: {1})" , _url , response . StatusCode ) ) ;
108
+ }
109
+ else
110
+ {
111
+ throw new ServerErrorException ( string . Format (
112
+ "Server has experienced some issues with request for {0} which resulted in {1} status code." ,
113
+ _url , response . StatusCode ) ) ;
114
+ }
115
+ }
116
+
117
+ _logger . LogDebug ( "Downloading finished." ) ;
102
118
}
103
119
catch ( WebException webException )
104
120
{
@@ -113,45 +129,14 @@ public IEnumerable<DataPacket> ReadPackets(CancellationToken cancellationToken)
113
129
}
114
130
}
115
131
116
- private IEnumerable < DataPacket > ReadResponseStream ( HttpGetRequest request , CancellationToken cancellationToken )
132
+ private void ReadResponseStream ( Stream responseStream , CancellationToken cancellationToken )
117
133
{
118
- using ( var response = _httpClient . Get ( request ) )
134
+ int bufferRead ;
135
+ while ( ( bufferRead = responseStream . Read ( _buffer , 0 , BufferSize ) ) > 0 )
119
136
{
120
137
cancellationToken . ThrowIfCancellationRequested ( ) ;
121
138
122
- _logger . LogDebug ( "Received response from server." ) ;
123
- _logger . LogTrace ( "statusCode = " + response . StatusCode ) ;
124
-
125
- if ( Is2XXStatus ( response . StatusCode ) )
126
- {
127
- _logger . LogDebug ( "Successful response. Reading response stream..." ) ;
128
-
129
- //TODO: Could response.ContentStream be null? Need to check it.
130
-
131
- var responseStream = response . ContentStream ;
132
- int bufferRead ;
133
- while ( ( bufferRead = responseStream . Read ( _buffer , 0 , ( int ) _bufferSize ) ) > 0 )
134
- {
135
- cancellationToken . ThrowIfCancellationRequested ( ) ;
136
-
137
- var dataPacket = new DataPacket { Data = _buffer , Length = bufferRead , } ;
138
-
139
- yield return dataPacket ;
140
- }
141
-
142
- _logger . LogDebug ( "Downloading finished." ) ;
143
- }
144
- else if ( Is4XXStatus ( response . StatusCode ) )
145
- {
146
- throw new DataNotAvailableException ( string . Format (
147
- "Request data for {0} is not available (status: {1})" , _url , response . StatusCode ) ) ;
148
- }
149
- else
150
- {
151
- throw new ServerErrorException ( string . Format (
152
- "Server has experienced some issues with request for {0} which resulted in {1} status code." ,
153
- _url , response . StatusCode ) ) ;
154
- }
139
+ OnDataAvailable ( _buffer , bufferRead ) ;
155
140
}
156
141
}
157
142
@@ -166,5 +151,11 @@ private static bool Is4XXStatus(HttpStatusCode statusCode)
166
151
{
167
152
return ( int ) statusCode >= 400 && ( int ) statusCode <= 499 ;
168
153
}
154
+
155
+ private void OnDataAvailable ( byte [ ] data , int length )
156
+ {
157
+ var handler = DataAvailable ;
158
+ if ( handler != null ) handler ( data , length ) ;
159
+ }
169
160
}
170
161
}
0 commit comments