-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathBlueNetworkStream.cpp
More file actions
346 lines (313 loc) · 8.4 KB
/
Copy pathBlueNetworkStream.cpp
File metadata and controls
346 lines (313 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright © 2015 CCP ehf.
#include "StdAfx.h"
#include "BlueNetworkStream.h"
#include "IBlueResMan.h"
#include "BlueRemoteStream.h"
#include <BlueStatistics.h>
BlueNetworkStream::BlueNetworkStream()
:m_data( "BlueHttpStream::m_data" ),
m_blockWritePosition( 0 ),
m_blockReadPosition( 0 ),
m_readPosition( 0 ),
m_size( 0 ),
m_receivedSize( 0 ),
m_sizeAvailable( 0, 1 ),
m_dataMutex( "BlueHttpStream", "m_dataMutex" ),
m_dataAvailable( 0, 1 ),
m_backgroundCallId( 0 ),
m_state( UNINITIALIZED ),
m_transferResult( CURLE_OK )
{
}
BlueNetworkStream::~BlueNetworkStream()
{
Close();
}
BlueStdResult BlueNetworkStream::Open( const char* url )
{
Close();
m_state = OPENED;
m_url = url;
BeResMan->AddToQueue(
BRMQ_BACKGROUND,
&PerformTransferHelper,
this,
0,
&m_backgroundCallId );
return BlueStdResult( BLUE_STD_RESULT_OK );
}
void BlueNetworkStream::Close()
{
if( m_state == UNINITIALIZED )
{
return;
}
m_state = FINISHED;
m_dataAvailable.Signal();
BeResMan->CancelFromQueue( BRMQ_BACKGROUND, m_backgroundCallId );
m_url = "";
m_blockReadPosition = 0;
m_blockWritePosition = 0;
for( auto it = m_data.begin(); it != m_data.end(); ++it )
{
CCP_DELETE[] it->data;
}
m_data.clear();
m_readPosition = 0;
m_size = 0;
m_receivedSize = 0;
m_headers = "";
m_state = UNINITIALIZED;
}
ptrdiff_t BlueNetworkStream::Read( void* dest, ptrdiff_t count )
{
ptrdiff_t read = 0;
uint8_t* data = static_cast<uint8_t*>( dest );
while( count > 0 )
{
m_dataMutex.Acquire();
size_t block = size_t( m_readPosition / BLOCK_SIZE );
while( m_state == OPENED && ( m_data.size() <= block || ( m_data.size() == block + 1 && m_blockWritePosition == m_blockReadPosition ) ) )
{
m_dataMutex.Release();
m_dataAvailable.Wait();
m_dataMutex.Acquire();
}
ptrdiff_t availableSize = ( m_data.size() > block + 1 ? BLOCK_SIZE : m_blockWritePosition ) - m_blockReadPosition;
if( m_data.empty() )
{
count = 0;
}
else if( availableSize )
{
auto copySize = std::min( availableSize, count );
memcpy( data, m_data[block].data + m_blockReadPosition, copySize );
count -= copySize;
data += copySize;
read += copySize;
m_blockReadPosition += copySize;
m_readPosition += copySize;
if( m_blockReadPosition >= BLOCK_SIZE )
{
m_blockReadPosition = 0;
}
}
else if( m_state != OPENED )
{
count = 0;
}
m_dataAvailable.Signal();
m_dataMutex.Release();
}
return read;
}
ptrdiff_t BlueNetworkStream::Write( const void* source, size_t count )
{
return -1;
}
ptrdiff_t BlueNetworkStream::Seek( ptrdiff_t distance, SeekOrigin method )
{
switch( method )
{
case SO_CURRENT:
distance = m_readPosition + distance;
case SO_BEGIN:
if( distance < m_readPosition )
{
m_dataMutex.Acquire();
m_readPosition = distance;
m_blockReadPosition = m_readPosition % BLOCK_SIZE;
m_dataMutex.Release();
}
else
{
m_dataMutex.Acquire();
while( ptrdiff_t( m_receivedSize ) < distance )
{
m_dataMutex.Release();
m_dataAvailable.Wait();
m_dataMutex.Acquire();
}
m_readPosition = distance;
m_blockReadPosition = m_readPosition % BLOCK_SIZE;
m_dataMutex.Release();
}
m_dataAvailable.Signal();
return m_readPosition;
default:
return -1;
}
}
ptrdiff_t BlueNetworkStream::GetPosition()
{
return m_readPosition;
}
ptrdiff_t BlueNetworkStream::GetSize()
{
m_sizeAvailable.Wait();
return m_size;
}
bool BlueNetworkStream::LockData( void** data, size_t size )
{
return false;
}
bool BlueNetworkStream::UnlockData()
{
return false;
}
BlueStdResult BlueNetworkStream::ReadData( Be::Optional<size_t> size, PyObject*& contents )
{
if( m_state == UNINITIALIZED )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "I/O operation on closed file" );
}
if( size.IsAssigned() )
{
if( size == 0 )
{
contents = PyBytes_FromString("");
return BlueStdResult( BLUE_STD_RESULT_OK );
}
CcpMallocBuffer data( "BlueHttpStream::ReadData", size );
auto readSize = Read( data.get(), size );
if( m_transferResult != CURLE_OK )
{
return BlueStdResult( BLUE_STD_RESULT_IO_ERROR );
}
contents = PyBytes_FromStringAndSize( data.get(), readSize );
return BlueStdResult( BLUE_STD_RESULT_OK );
}
else
{
contents = PyBytes_FromString("");
while( true )
{
CcpMallocBuffer data( "BlueHttpStream::ReadData", 1024 );
auto read = Read( data.get(), ptrdiff_t( data.size() ) );
PyBytes_ConcatAndDel( &contents, PyBytes_FromStringAndSize( data.get(), read ) );
if ( !contents ) {
return BlueStdResult( BLUE_STD_RESULT_RUNTIME_ERROR );
}
if( read != 1024 )
{
break;
}
}
if( m_transferResult != CURLE_OK )
{
return BlueStdResult( BLUE_STD_RESULT_IO_ERROR );
}
return BlueStdResult( BLUE_STD_RESULT_OK );
}
}
BlueStdResult BlueNetworkStream::Tell( ptrdiff_t& position )
{
if( m_state == UNINITIALIZED )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "I/O operation on closed file" );
}
position = GetPosition();
return BlueStdResult( BLUE_STD_RESULT_OK );
}
void BlueNetworkStream::PerformTransfer()
{
CURL* connection = curl_easy_init();
curl_easy_setopt( connection, CURLOPT_FAILONERROR, 1 );
curl_easy_setopt( connection, CURLOPT_URL, m_url.c_str() );
curl_easy_setopt( connection, CURLOPT_WRITEFUNCTION, WriteMemoryCallback );
curl_easy_setopt( connection, CURLOPT_WRITEDATA, (void*)this );
curl_easy_setopt( connection, CURLOPT_HEADERFUNCTION, WriteHeaderCallback );
curl_easy_setopt( connection, CURLOPT_HEADERDATA, (void*)this );
#if LIBCURL_VERSION_MAJOR > 7 || LIBCURL_VERSION_MINOR >= 32
curl_easy_setopt( connection, CURLOPT_XFERINFOFUNCTION, ProgressCallback );
curl_easy_setopt( connection, CURLOPT_XFERINFODATA, (void*)this );
#else
curl_easy_setopt( connection, CURLOPT_PROGRESSFUNCTION, ProgressCallback );
curl_easy_setopt( connection, CURLOPT_PROGRESSDATA, (void*)this );
#endif
GetProxySettings( m_url.c_str(), connection );
m_transferResult = curl_easy_perform( connection );
m_size = m_receivedSize;
m_state = FINISHED;
m_sizeAvailable.Signal();
m_dataAvailable.Signal();
}
void BlueNetworkStream::PerformTransferHelper( void* context )
{
static_cast<BlueNetworkStream*>( context )->PerformTransfer();
}
size_t BlueNetworkStream::WriteMemoryCallback( void* contents, size_t size, size_t nmemb, void* context )
{
CCP_STATS_ZONE( __FUNCTION__ );
size_t realsize = size * nmemb;
auto self = static_cast<BlueNetworkStream*>( context );
self->ReceiveData( contents, realsize );
return realsize;
}
size_t BlueNetworkStream::WriteHeaderCallback( void* contents, size_t size, size_t nmemb, void* context )
{
CCP_STATS_ZONE( __FUNCTION__ );
size_t realsize = size * nmemb;
auto self = static_cast<BlueNetworkStream*>( context );
self->ReceiveHeaders( static_cast<char*>( contents ), realsize );
return realsize;
}
void BlueNetworkStream::ReceiveData( const void* data, size_t size )
{
size_t initialSize = size;
CcpAutoMutex lock( m_dataMutex );
auto begin = static_cast<const uint8_t*>( data );
while( size )
{
if( m_data.empty() )
{
Block newBlock;
newBlock.data = CCP_NEW( "BlueHttpStream::Block::data" ) uint8_t[BLOCK_SIZE];
m_data.push_back( newBlock );
m_blockWritePosition = 0;
}
auto blockSize = std::min( BLOCK_SIZE - m_blockWritePosition, size );
auto& block = m_data.back();
memcpy( block.data + m_blockWritePosition, begin, blockSize );
if( blockSize >= size )
{
m_blockWritePosition += size;
size = 0;
}
else
{
begin += blockSize;
size -= blockSize;
Block newBlock;
newBlock.data = CCP_NEW( "BlueHttpStream::Block::data" ) uint8_t[BLOCK_SIZE];
m_data.push_back( newBlock );
m_blockWritePosition = 0;
}
}
m_receivedSize += initialSize;
m_dataAvailable.Signal();
}
void BlueNetworkStream::ReceiveHeaders( const void* data, size_t size )
{
m_headers.append( static_cast<const char*>( data ), size );
auto found = m_headers.find( "Content-Length: " );
if( found != m_headers.npos )
{
if( sscanf_s( m_headers.c_str() + found, "%" CCP_SIZET_FORMAT, &size ) == 1 )
{
m_size = size;
m_sizeAvailable.Signal();
}
}
}
#if LIBCURL_VERSION_MAJOR > 7 || LIBCURL_VERSION_MINOR >= 32
int BlueNetworkStream::ProgressCallback( void *context, curl_off_t, curl_off_t, curl_off_t, curl_off_t )
{
return static_cast<BlueNetworkStream*>( context )->m_state != OPENED;
}
#else
int BlueNetworkStream::ProgressCallback( void *context, double, double, double ultotal, double ulnow )
{
return static_cast<BlueNetworkStream*>( context )->m_state != OPENED;
}
#endif