Skip to content

Commit 512418a

Browse files
committed
Merge pull request #646 from fodinabor/HTTPFetcher
Adding HTTPFetcher to fetch files from the WWW via HTTP, fixing getTh…
2 parents 1b256f1 + ed8addc commit 512418a

File tree

39 files changed

+455
-60
lines changed

39 files changed

+455
-60
lines changed

Assets/Templates/C++/Linux/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CC=g++
2-
CFLAGS=-I../../Core/Dependencies/include -I../../Core/Dependencies/include/AL -I../../Core/include -I../../Modules/include -I../../Modules/Dependencies/include -I../../Modules/Dependencies/include/bullet
2+
CFLAGS=-I../../Core/Dependencies/include -I../../Core/Dependencies/include/AL -I../../Core/Dependencies/include/freetype2 -I../../Core/include -I../../Modules/include -I../../Modules/Dependencies/include -I../../Modules/Dependencies/include/bullet
33
LDFLAGS=-lrt -ldl -lpthread ../../Core/lib/libPolycore.a ../../Core/Dependencies/lib/libfreetype.a ../../Core/Dependencies/lib/liblibvorbisfile.a ../../Core/Dependencies/lib/liblibvorbis.a ../../Core/Dependencies/lib/liblibogg.a ../../Core/Dependencies/lib/libopenal.so ../../Core/Dependencies/lib/libphysfs.a ../../Core/Dependencies/lib/libpng15.a ../../Core/Dependencies/lib/libz.a -lGL -lGLU -lSDL ../../Modules/lib/libPolycode2DPhysics.a ../../Modules/Dependencies/lib/libBox2D.a ../../Modules/lib/libPolycode3DPhysics.a ../../Modules/Dependencies/lib/libBulletDynamics.a ../../Modules/Dependencies/lib/libBulletCollision.a ../../Modules/Dependencies/lib/libLinearMath.a -lX11
44

55
default:

Assets/Templates/C++/Windows/Polycode.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<PolycodeLibsRelease>$(PolycodeCoreLibsRelease);$(PolycodeDependLibsRelease);$(PolycodeWinLibsRelease)</PolycodeLibsRelease>
1414
</PropertyGroup>
1515
<PropertyGroup>
16-
<IncludePath>$(PolycodeDir)Core\include;$(PolycodeDir)Core\Dependencies\include;$(PolycodeDir)Core\PolycodeView;$(PolycodeDir)Core\Dependencies\include\AL;$(IncludePath)</IncludePath>
16+
<IncludePath>$(PolycodeDir)Core\include;$(PolycodeDir)Core\Dependencies\include;$(PolycodeDir)Core\PolycodeView;$(PolycodeDir)Core\Dependencies\include\AL;$(PolycodeDir)Core\Dependencies\include\freetype2;$(IncludePath)</IncludePath>
1717
</PropertyGroup>
1818
<PropertyGroup>
1919
<LibraryPath>$(PolycodeDir)Core\lib;$(PolycodeDir)Core\Dependencies\lib;$(PolycodeDir)Modules\lib;$(PolycodeDir)Modules\Dependencies\lib;$(LibraryPath)</LibraryPath>

CMake/ExternalFreetype.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SET(freetype_CMAKE_ARGS
99
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
1010
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
1111
-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
12+
-DCMAKE_DEBUG_POSTFIX=_d
1213
)
1314

1415
EXTERNALPROJECT_ADD(freetype

Core/Contents/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ SET(polycore_SRCS
8080
Source/PolyPeer.cpp
8181
Source/PolyClient.cpp
8282
Source/PolyServer.cpp
83+
Source/PolyHTTPFetcher.cpp
8384
Source/PolyRay.cpp
8485
Source/PolySceneSprite.cpp
8586
Source/PolySceneEntityInstance.cpp
@@ -169,6 +170,7 @@ SET(polycore_HDRS
169170
Include/PolyClient.h
170171
Include/PolyServer.h
171172
Include/PolyServerWorld.h
173+
Include/PolyHTTPFetcher.h
172174
Include/PolyRay.h
173175
Include/PolySceneSprite.h
174176
Include/PolySceneEntityInstance.h
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Copyright (C) 2015 by Joachim Meyer
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
*/
22+
23+
#pragma once
24+
#include <string.h>
25+
26+
#include "PolyGlobals.h"
27+
#include "PolyThreaded.h"
28+
29+
#define HTTP_VERSION "HTTP/1.1"
30+
#define DEFAULT_USER_AGENT "Polycode HTTP Fetcher/1.0"
31+
#define DEFAULT_PAGE_BUF_SIZE 2048
32+
33+
namespace Polycode {
34+
35+
class HTTPFetcherEvent : public Event {
36+
public:
37+
HTTPFetcherEvent() { contentSize = 0; errorCode = 0; data = NULL; storedInFile = false; }
38+
~HTTPFetcherEvent(){}
39+
40+
//If storedInFile: data is the file path, else: data contains all the fetched data
41+
char* data;
42+
//Error code: contains either the errno / WSAError code or the HTTP error code or the HTTPFetcher error code
43+
int errorCode;
44+
45+
//Has the data been saved to a file or is it shipped with this event?
46+
bool storedInFile;
47+
//Size of the HTTP reply
48+
unsigned long contentSize;
49+
50+
static const int EVENTBASE_SOCKETEVENT = 0x500;
51+
static const int EVENT_HTTP_ERROR = EVENTBASE_SOCKETEVENT + 2;
52+
static const int EVENT_HTTP_DATA_RECEIVED = EVENTBASE_SOCKETEVENT + 3;
53+
};
54+
55+
/**
56+
* A utility to download a file from the WWW through HTTP. It is threaded (and therefor non blocking).
57+
* If you want to use the data you might add an EventListener for the HTTPFetcherEvent::EVENT_HTTP_DATA_RECEIVED event code.
58+
*/
59+
class HTTPFetcher : public Threaded {
60+
public:
61+
/*
62+
* Connects to a host and fetches a file given in the param
63+
* @param address Full path including the hostname (Domain or IP) and protocol (http://) aswell as the path to the file on the server
64+
* @param saveToPath true if you want the file to be directly saved, false if you just want the data as char array
65+
* @param savePath Path String where the file should be saved to
66+
*/
67+
HTTPFetcher(String address, bool saveToPath = false, String savePath = "");
68+
~HTTPFetcher();
69+
70+
String getData();
71+
72+
/*
73+
* Fetches a file given in the param
74+
* @param pathToFile Path String to the new file to fetch from the same host. Without leading "/"
75+
* @param saveToPath true if you want the file to be directly saved, false if you just want the data as char array
76+
* @param savePath Path String where the file should be saved to
77+
*/
78+
void fetchFile(String pathToFile, bool saveToPath = false, String savePath = "");
79+
80+
//The received data is more or less than the HTTP header told us it should be
81+
static const int HTTPFETCHER_ERROR_WRONG_SIZE = 0x10F00;
82+
83+
bool storeInFile;
84+
85+
private:
86+
int s;
87+
String address;
88+
String bodyReturn;
89+
String path;
90+
String host;
91+
String protocol;
92+
String savePath;
93+
94+
bool createSocket();
95+
void updateThread();
96+
};
97+
}

Core/Contents/Include/Polycode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
#include "PolyServer.h"
8787
#include "PolyServerWorld.h"
8888
#include "PolySocket.h"
89+
#include "PolyHTTPFetcher.h"
8990
#include "PolyRay.h"
9091
#include "PolySceneSprite.h"
9192
#include "PolySceneEntityInstance.h"

0 commit comments

Comments
 (0)