Network/HTTP.h

Sat, 08 Mar 2014 13:55:46 +0200

author
larukedi
date
Sat, 08 Mar 2014 13:55:46 +0200
changeset 401
85c7261e590a
parent 199
e6acf22bd64f
permissions
-rw-r--r--

added LICENSE and README.md files

150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
1 #pragma once
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
2 #include <string>
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
3 #include "../Utils/EventChain.h"
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
4 #include <fstream>
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
5 #include "../Engine/GGEMain.h"
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
6
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
7 namespace gge { namespace network {
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
8 namespace http {
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
9 void Initialize();
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
10
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
11 std::string BlockingGetText(const std::string &URL);
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
12
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
13 class Error : public std::exception {
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
14 public:
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
15
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
16 enum Code {
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
17 NoError = 0,
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
18 BadURL = 3,
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
19 HostResolutionFailed = 6,
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
20 ConnectionFailed = 7,
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
21 AccessDenied = 9,
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
22 OutOfMemory = 27,
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
23 LoginError = 67,
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
24 PageNotFound =404,
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
25 Unknown = -1,
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
26 };
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
27
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
28
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
29 Error(const std::string &message="", Code error=NoError) : message(message), error(error)
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
30 { }
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
31
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
32 virtual const char* what() const throw() {
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
33 return message.c_str();
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
34 }
199
e6acf22bd64f * Merged with 3.1 branch
cemkalyoncu
parents: 153
diff changeset
35
e6acf22bd64f * Merged with 3.1 branch
cemkalyoncu
parents: 153
diff changeset
36 virtual ~Error() throw() {}
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
37
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
38 std::string message;
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
39 Code error;
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
40 };
199
e6acf22bd64f * Merged with 3.1 branch
cemkalyoncu
parents: 153
diff changeset
41
e6acf22bd64f * Merged with 3.1 branch
cemkalyoncu
parents: 153
diff changeset
42 int threadfncall nonblockingop(void *);
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
43
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
44 class Nonblocking {
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
45 friend int threadfncall nonblockingop(void *);
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
46 public:
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
47
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
48 Nonblocking();
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
49 ~Nonblocking() {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
50 gge::Main.BeforeRenderEvent.Unregister(token);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
51 }
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
52
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
53 utils::EventChain<Nonblocking, std::string&> TextTransferComplete;
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
54 //Given vector is temporary, it will be destroyed after used
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
55 //you may swap its data if you need it
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
56 utils::EventChain<Nonblocking, std::vector<Byte>&> DataTransferComplete;
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
57 utils::EventChain<Nonblocking> FileTransferComplete;
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
58 utils::EventChain<Nonblocking, Error> TransferError;
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
59
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
60 void GetText(const std::string &URL);
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
61 void GetData(const std::string &URL);
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
62 void GetData(const std::string &URL, std::vector<Byte> &vec);
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
63 void GetFile(const std::string &URL, const std::string &filename);
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
64 void GetStream(const std::string &URL, std::ostream &stream);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
65
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
66
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
67 bool IsRunning() const { return isrunning; }
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
68
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
69 protected:
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
70 void* curl;
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
71 bool isrunning;
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
72 std::string tempstr;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
73 std::vector<Byte> *tempvec;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
74 std::ofstream tempfile;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
75 Error err;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
76 utils::EventChain<GGEMain>::Token token;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
77
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
78 void stream(const std::string &URL, std::ostream &stream);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
79 void getdata(const std::string &URL, std::vector<Byte> &vec);
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
80
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
81 enum {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
82 None,
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
83 Text,
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
84 Data,
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
85 OwnedData,
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
86 File,
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
87 Stream
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
88 } currentoperation;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
89 };
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
90 }
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
91
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
92 }}
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
93

mercurial