SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLIOReaderFetch Class Reference

SLIOStream implementation for downloading files from a web server. More...

#include <SLIOFetch.h>

Inheritance diagram for SLIOReaderFetch:
[legend]

Public Member Functions

 SLIOReaderFetch (std::string url)
 
 ~SLIOReaderFetch ()
 
- Public Member Functions inherited from SLIOReaderMemory
 SLIOReaderMemory (std::string path)
 
size_t read (void *buffer, size_t size)
 
size_t tell ()
 
bool seek (size_t offset, Origin origin)
 
size_t size ()
 
- Public Member Functions inherited from SLIOStream
virtual ~SLIOStream ()=default
 
virtual size_t write (const void *buffer, size_t size)
 
virtual void flush ()
 

Static Public Member Functions

static bool exists (std::string url)
 

Additional Inherited Members

- Public Types inherited from SLIOStream
enum  Origin { IOO_beg , IOO_cur , IOO_end }
 
- Protected Attributes inherited from SLIOReaderMemory
std::string _path
 
size_t _position
 

Detailed Description

SLIOStream implementation for downloading files from a web server.

The constructor downloads the file via HTTP and stores it in memory. When downloading, a loading screen is displayed to the user because it blocks the entire application.

Definition at line 24 of file SLIOFetch.h.

Constructor & Destructor Documentation

◆ SLIOReaderFetch()

SLIOReaderFetch::SLIOReaderFetch ( std::string  url)

Definition at line 34 of file SLIOFetch.cpp.

35  : SLIOReaderMemory(url)
36 {
37  assert(pthread_self() != 0 && "Fetching is not allowed on the main thread");
38 
39  std::cout << "FETCH \"" << url << "\"" << std::endl;
40 
41  emscripten_fetch_attr_t attr;
42  emscripten_fetch_attr_init(&attr);
43  std::strcpy(attr.requestMethod, "GET");
44  attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS;
45  emscripten_fetch_t* fetch = emscripten_fetch(&attr, url.c_str());
46 
47  int status = fetch->status;
48  size_t size = (size_t)fetch->totalBytes;
49  std::cout << "STATUS: " << status << ", SIZE: " << size << std::endl;
50 
51  if (status == 200)
52  {
53  SLIOMemory::set(_path, std::vector<char>(fetch->data, fetch->data + size));
54  }
55 
56  emscripten_fetch_close(fetch);
57 }
SLIOReaderMemory(std::string path)
Definition: SLIOMemory.cpp:39
std::string _path
Definition: SLIOMemory.h:37
void set(std::string path, const std::vector< char > &data)
Definition: SLIOMemory.cpp:29

◆ ~SLIOReaderFetch()

SLIOReaderFetch::~SLIOReaderFetch ( )

Definition at line 59 of file SLIOFetch.cpp.

60 {
62 }
void clear(std::string path)
Definition: SLIOMemory.cpp:34

Member Function Documentation

◆ exists()

bool SLIOReaderFetch::exists ( std::string  url)
static

Definition at line 19 of file SLIOFetch.cpp.

20 {
21  assert(pthread_self() != 0 && "Fetching is not allowed on the main thread");
22 
23  emscripten_fetch_attr_t attr;
24  emscripten_fetch_attr_init(&attr);
25  std::strcpy(attr.requestMethod, "HEAD");
26  attr.attributes = EMSCRIPTEN_FETCH_SYNCHRONOUS;
27  emscripten_fetch_t* fetch = emscripten_fetch(&attr, url.c_str());
28  bool exists = fetch->status == 200;
29  emscripten_fetch_close(fetch);
30 
31  return exists;
32 }
static bool exists(std::string url)
Definition: SLIOFetch.cpp:19

The documentation for this class was generated from the following files: