TEST_FIXTURE(page_blob_test_base, page_blob_write_stream_maximum_execution_time) { std::chrono::seconds duration(10); azure::storage::blob_request_options options; options.set_maximum_execution_time(duration); auto stream = m_blob.open_write(512, 0, azure::storage::access_condition(), options, m_context); std::this_thread::sleep_for(duration); std::vector<uint8_t> buffer; buffer.resize(512); stream.streambuf().putn_nocopy(buffer.data(), buffer.size()).wait(); stream.close().wait(); }
void* wxDownloadText::Entry() { char c = 0; int bytesread = 0; m_downloading = true; wxDownloadEvent event( wxEVT_DOWNLOAD, GetId() ); event.SetEventObject( (wxObject *)this->This() ); event.set_url( m_url); event.set_status(wxDownloadEvent::DOWNLOAD_RUNNING); #if 0 // -- according to docs this is the way to go but i couldn't figure out // how to set the proxy... wxFileSystem fs; wxFSFile *file = fs.OpenFile( m_url ); if( 0 == file ) { event.SetDownLoadStatus(wxDownloadEvent::DOWNLOAD_FAIL); if(m_parent) m_parent->GetEventHandler()->ProcessEvent( event ); return 0; } wxStreamBuffer streambuf( *file->GetStream(), wxStreamBuffer::read ); size_t size = file->GetStream()->GetSize(); if( !size ) { // empty file received event.SetDownLoadStatus(wxDownloadEvent::DOWNLOAD_COMPLETE); if(m_parent) m_parent->GetEventHandler()->ProcessEvent( event ); return 0; } char *buf = new char[ size + 1 ]; size_t haveread = streambuf.Read( buf, size ); buf[size] = '\0'; delete file; char *ptr = buf; while(*ptr) { wxString add((char)*ptr, (size_t)1); m_strContent += add; ++ptr; } delete [] buf; event.SetDownLoadStatus(wxDownloadEvent::DOWNLOAD_COMPLETE); if(m_parent) m_parent->GetEventHandler()->ProcessEvent( event ); return 0; #else wxURL Url(m_url); ((wxProtocol &)Url.GetProtocol()).SetTimeout(100); if (Url.GetError() == wxURL_NOERR) { wxInputStream *pIn_Stream = NULL; /* #ifdef __WXMSW__ wxRegKey *pRegKey = new wxRegKey(wxT("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings")); if( pRegKey->Exists() && pRegKey->HasValue(wxT("ProxyEnable"))) { long lProxyEnable; pRegKey->QueryValue(wxT("ProxyEnable"), &lProxyEnable); if(lProxyEnable == 1 && pRegKey->HasValue(wxT("ProxyServer"))) { wxString strProxyAddress; pRegKey->QueryValue(wxT("ProxyServer"), strProxyAddress); Url.SetProxy(strProxyAddress); } } // FIXME if no proxy it doesn't work? ;) delete pRegKey; #endif */ if( !Prefs::get().var(wxT("net_proxy")).sval().empty() ) Url.SetProxy( Prefs::get().var(wxT("net_proxy")).sval()); pIn_Stream = Url.GetInputStream(); if(!pIn_Stream) wxLogDebug(wxT("Cannot get a stream!")); if(pIn_Stream) { if(pIn_Stream->GetSize() != 0xFFFFFFFF) event.set_filesize(pIn_Stream->GetSize()); wxInt64 nCount = 0; while ((bytesread = (int)(pIn_Stream->Read(&c, 1)).LastRead()) > 0 && m_downloading && !TestDestroy() ) { wxString add((char)c, (size_t)1); event.m_text += add; //m_strContent += add; nCount += bytesread; if (m_notify && (nCount%m_notifybytes) == 0 && nCount>=m_notifybytes) { event.set_status(wxDownloadEvent::DOWNLOAD_INPROGRESS); event.set_bytesdownloaded(nCount); m_parent->AddPendingEvent(event); //m_parent->GetEventHandler()->ProcessEvent( event ); } } delete pIn_Stream; event.set_status(wxDownloadEvent::DOWNLOAD_COMPLETE); if(m_parent) m_parent->AddPendingEvent(event); //m_parent->GetEventHandler()->ProcessEvent( event ); } else { event.set_status(wxDownloadEvent::DOWNLOAD_FAIL); if(m_parent) m_parent->AddPendingEvent(event); //m_parent->GetEventHandler()->ProcessEvent( event ); } } else { event.set_status(wxDownloadEvent::DOWNLOAD_FAIL); if(m_parent) m_parent->AddPendingEvent(event); //m_parent->GetEventHandler()->ProcessEvent( event ); } return 0; #endif }
TEST_FIXTURE(append_blob_test_base, append_blob_append) { const size_t file_buffer_size = 24 * 1024 * 1024 + 6; std::vector<uint8_t> file_buffer; file_buffer.resize(file_buffer_size); azure::storage::blob_request_options options; options.set_use_transactional_md5(false); utility::string_t md5_header; m_context.set_sending_request([&md5_header](web::http::http_request& request, azure::storage::operation_context) { if (!request.headers().match(web::http::header_names::content_md5, md5_header)) { md5_header.clear(); } }); m_blob.create_or_replace(azure::storage::access_condition(), options, m_context); int block_count = 0; // append stream (4M, 4M) const size_t buffer_offsets1[2] = { 0, 4 * 1024 * 1024}; for (uint16_t i = 0; i < 2; ++i) { std::vector<uint8_t> buffer; buffer.resize(4 * 1024 * 1024); fill_buffer_and_get_md5(buffer); std::copy(buffer.begin(), buffer.end(), file_buffer.begin() + buffer_offsets1[i]); auto stream = concurrency::streams::bytestream::open_istream(buffer); azure::storage::access_condition condition = azure::storage::access_condition::generate_if_append_position_equal_condition(buffer_offsets1[i]); m_blob.append_from_stream(stream, condition, options, m_context); CHECK_UTF8_EQUAL(utility::string_t(), md5_header); block_count++; CHECK_EQUAL(block_count, m_blob.properties().append_blob_committed_block_count()); } // append stream with length (2M, 2M, 2M) const size_t buffer_offsets2[3] = { 8 * 1024 * 1024, 10 * 1024 * 1024, 12 * 1024 * 1024 }; for (uint16_t i = 0; i < 3; ++i) { std::vector<uint8_t> buffer; buffer.resize(4 * 1024 * 1024); fill_buffer_and_get_md5(buffer); std::copy(buffer.begin(), buffer.begin() + 2 * 1024 * 1024, file_buffer.begin() + buffer_offsets2[i]); auto stream = concurrency::streams::bytestream::open_istream(buffer); m_blob.append_from_stream(stream, 2 * 1024 * 1024, azure::storage::access_condition(), options, m_context); CHECK_UTF8_EQUAL(utility::string_t(), md5_header); block_count++; CHECK_EQUAL(block_count, m_blob.properties().append_blob_committed_block_count()); } // append file (5M, 5M) const size_t buffer_offsets3[2] = { 14 * 1024 * 1024, 19 * 1024 * 1024 }; for (uint16_t i = 0; i < 2; ++i) { std::vector<uint8_t> buffer; buffer.resize(5 * 1024 * 1024); fill_buffer_and_get_md5(buffer); std::copy(buffer.begin(), buffer.end(), file_buffer.begin() + buffer_offsets3[i]); // create a temporary test file utility::string_t tmp_file_path = get_random_container_name(8); auto stream = concurrency::streams::file_stream<uint8_t>::open_ostream(tmp_file_path).get(); stream.streambuf().putn_nocopy(buffer.data(), buffer.size()).wait(); stream.close().wait(); // append from file m_blob.append_from_file(tmp_file_path, azure::storage::access_condition(), options, m_context); // remote the temporary test file std::remove(utility::conversions::to_utf8string(tmp_file_path).c_str()); block_count += 2; CHECK_EQUAL(block_count, m_blob.properties().append_blob_committed_block_count()); } // append text (1, 5) const size_t buffer_offsets4[2] = { 24 * 1024 * 1024, 24 * 1024 * 1024 + 1}; { utility::string_t text1 = U("1"); std::string text1_copy = utility::conversions::to_utf8string(text1); std::copy(text1_copy.begin(), text1_copy.end(), file_buffer.begin() + buffer_offsets4[0]); m_blob.append_text(text1); block_count++; CHECK_EQUAL(block_count, m_blob.properties().append_blob_committed_block_count()); utility::string_t text2 = U("test2"); std::string text2_copy = utility::conversions::to_utf8string(text2); std::copy(text2_copy.begin(), text2_copy.end(), file_buffer.begin() + buffer_offsets4[1]); m_blob.append_text(text2); block_count++; CHECK_EQUAL(block_count, m_blob.properties().append_blob_committed_block_count()); } // download the blob concurrency::streams::container_buffer<std::vector<uint8_t>> downloaded_blob; m_blob.download_to_stream(downloaded_blob.create_ostream(), azure::storage::access_condition(), options, m_context); CHECK_ARRAY_EQUAL(file_buffer, downloaded_blob.collection(), (int)file_buffer.size()); m_blob.delete_blob(); m_context.set_sending_request(std::function<void(web::http::http_request &, azure::storage::operation_context)>()); }
TCPStream::TCPStream(TCPSocket &server, bool throwflag, timeout_t to) : streambuf(), Socket(accept(server.getSocket(), NULL, NULL)), #ifdef OLD_IOSTREAM iostream() #else iostream((streambuf *)this) #endif ,bufsize(0) ,gbuf(NULL) ,pbuf(NULL) { tpport_t port; family = IPV4; #ifdef OLD_IOSTREAM init((streambuf *)this); #endif timeout = to; setError(throwflag); IPV4Host host = getPeer(&port); if(!server.onAccept(host, port)) { endSocket(); error(errConnectRejected); iostream::clear(ios::failbit | rdstate()); return; } segmentBuffering(server.getSegmentSize()); Socket::state = CONNECTED; } #ifdef CCXX_IPV6 TCPStream::TCPStream(TCPV6Socket &server, bool throwflag, timeout_t to) : streambuf(), Socket(accept(server.getSocket(), NULL, NULL)), #ifdef OLD_IOSTREAM iostream() #else iostream((streambuf *)this) #endif ,bufsize(0) ,gbuf(NULL) ,pbuf(NULL) { tpport_t port; family = IPV6; #ifdef OLD_IOSTREAM init((streambuf *)this); #endif timeout = to; setError(throwflag); IPV6Host host = getIPV6Peer(&port); if(!server.onAccept(host, port)) { endSocket(); error(errConnectRejected); iostream::clear(ios::failbit | rdstate()); return; } segmentBuffering(server.getSegmentSize()); Socket::state = CONNECTED; } #endif TCPStream::TCPStream(const IPV4Host &host, tpport_t port, unsigned size, bool throwflag, timeout_t to) : streambuf(), Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP), #ifdef OLD_IOSTREAM iostream(), #else iostream((streambuf *)this), #endif bufsize(0),gbuf(NULL),pbuf(NULL) { #ifdef OLD_IOSTREAM init((streambuf *)this); #endif family = IPV4; timeout = to; setError(throwflag); connect(host, port, size); } #ifdef CCXX_IPV6 TCPStream::TCPStream(const IPV6Host &host, tpport_t port, unsigned size, bool throwflag, timeout_t to) : streambuf(), Socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP), #ifdef OLD_IOSTREAM iostream(), #else iostream((streambuf *)this), #endif bufsize(0),gbuf(NULL),pbuf(NULL) { family = IPV6; #ifdef OLD_IOSTREAM init((streambuf *)this); #endif timeout = to; setError(throwflag); connect(host, port, size); } #endif TCPStream::~TCPStream() { #ifdef CCXX_EXCEPTIONS try { endStream(); } catch( ... ) { if ( ! std::uncaught_exception()) throw;}; #else endStream(); #endif } #ifdef HAVE_GETADDRINFO void TCPStream::connect(const char *target, unsigned mss) { char namebuf[128]; char *cp; struct addrinfo hint, *list = NULL, *next, *first; bool connected = false; snprintf(namebuf, sizeof(namebuf), "%s", target); cp = strrchr(namebuf, '/'); if(!cp) cp = strrchr(namebuf, ':'); if(!cp) { endStream(); connectError(); return; } *(cp++) = 0; memset(&hint, 0, sizeof(hint)); hint.ai_family = family; hint.ai_socktype = SOCK_STREAM; hint.ai_protocol = IPPROTO_TCP; if(getaddrinfo(namebuf, cp, &hint, &list) || !list) { endStream(); connectError(); return; } first = list; #ifdef TCP_MAXSEG if(mss) setsockopt(so, IPPROTO_TCP, TCP_MAXSEG, (char *)&mss, sizeof(mss)); #endif while(list) { if(!::connect(so, list->ai_addr, (socklen_t)list->ai_addrlen)) { connected = true; break; } next = list->ai_next; list = next; } freeaddrinfo(first); if(!connected) { endStream(); connectError(); return; } segmentBuffering(mss); Socket::state = CONNECTED; }