Merge pull request #4314 from MartijnKaijser/beta1
[vuplus_xbmc] / xbmc / filesystem / DllLibCurl.h
1 #pragma once
2 /*
3  *      Copyright (C) 2005-2013 Team XBMC
4  *      http://xbmc.org
5  *
6  *  This Program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This Program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with XBMC; see the file COPYING.  If not, see
18  *  <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #include "DynamicDll.h"
23 #include "threads/CriticalSection.h"
24
25 /* put types of curl in namespace to avoid namespace pollution */
26 namespace XCURL
27 {
28   #define CURL CURL_HANDLE
29   #include <curl/curl.h>
30   #undef CURL
31
32   class DllLibCurlInterface
33   {
34   public:
35     virtual ~DllLibCurlInterface() {}
36     virtual CURLcode global_init(long flags)=0;
37     virtual void global_cleanup(void)=0;
38     virtual CURL_HANDLE * easy_init(void)=0;
39     //virtual CURLcode easy_setopt(CURL_HANDLE *handle, CURLoption option, ...)=0;
40     virtual CURLcode easy_perform(CURL_HANDLE * handle )=0;
41     virtual CURLcode easy_pause(CURL_HANDLE * handle, int bitmask )=0;
42     virtual void easy_reset(CURL_HANDLE * handle)=0;
43     //virtual CURLcode easy_getinfo(CURL_HANDLE *curl, CURLINFO info, ... )=0;
44     virtual void easy_cleanup(CURL_HANDLE * handle )=0;
45     virtual CURL_HANDLE *easy_duphandle(CURL_HANDLE *handle )=0;
46     virtual CURLM * multi_init(void)=0;
47     virtual CURLMcode multi_add_handle(CURLM *multi_handle, CURL_HANDLE *easy_handle)=0;
48     virtual CURLMcode multi_perform(CURLM *multi_handle, int *running_handles)=0;
49     virtual CURLMcode multi_remove_handle(CURLM *multi_handle, CURL_HANDLE *easy_handle)=0;
50     virtual CURLMcode multi_fdset(CURLM *multi_handle, fd_set *read_fd_set, fd_set *write_fd_set, fd_set *exc_fd_set, int *max_fd)=0;
51     virtual CURLMcode multi_timeout(CURLM *multi_handle, long *timeout)=0;
52     virtual CURLMsg*  multi_info_read(CURLM *multi_handle, int *msgs_in_queue)=0;
53     virtual void multi_cleanup(CURL_HANDLE * handle )=0;
54     virtual struct curl_slist* slist_append(struct curl_slist *, const char *)=0;
55     virtual void  slist_free_all(struct curl_slist *)=0;
56   };
57
58   class DllLibCurl : public DllDynamic, DllLibCurlInterface
59   {
60     DECLARE_DLL_WRAPPER(DllLibCurl, DLL_PATH_LIBCURL)
61     DEFINE_METHOD1(CURLcode, global_init, (long p1))
62     DEFINE_METHOD0(void, global_cleanup)
63     DEFINE_METHOD0(CURL_HANDLE *, easy_init)
64     DEFINE_METHOD_FP(CURLcode, easy_setopt, (CURL_HANDLE *p1, CURLoption p2, ...))
65     DEFINE_METHOD1(CURLcode, easy_perform, (CURL_HANDLE * p1 ))
66     DEFINE_METHOD2(CURLcode, easy_pause, (CURL_HANDLE * p1, int p2 ))
67     DEFINE_METHOD1(void, easy_reset, (CURL_HANDLE * p1 ))
68     DEFINE_METHOD_FP(CURLcode, easy_getinfo, (CURL_HANDLE *p1, CURLINFO p2, ... ))
69     DEFINE_METHOD1(void, easy_cleanup, (CURL_HANDLE * p1))
70     DEFINE_METHOD1(CURL_HANDLE *, easy_duphandle, (CURL_HANDLE * p1))
71     DEFINE_METHOD0(CURLM *, multi_init)
72     DEFINE_METHOD2(CURLMcode, multi_add_handle, (CURLM *p1, CURL_HANDLE *p2))
73     DEFINE_METHOD2(CURLMcode, multi_perform, (CURLM *p1, int *p2))
74     DEFINE_METHOD2(CURLMcode, multi_remove_handle, (CURLM *p1, CURL_HANDLE *p2))
75     DEFINE_METHOD5(CURLMcode, multi_fdset, (CURLM *p1, fd_set *p2, fd_set *p3, fd_set *p4, int *p5))
76     DEFINE_METHOD2(CURLMcode, multi_timeout, (CURLM *p1, long *p2))
77     DEFINE_METHOD2(CURLMsg*,  multi_info_read, (CURLM *p1, int *p2))
78     DEFINE_METHOD1(void, multi_cleanup, (CURLM *p1))
79     DEFINE_METHOD2(struct curl_slist*, slist_append, (struct curl_slist * p1, const char * p2))
80     DEFINE_METHOD1(void, slist_free_all, (struct curl_slist * p1))
81     DEFINE_METHOD1(const char *, easy_strerror, (CURLcode p1))
82 #if defined(HAS_CURL_STATIC)
83     DEFINE_METHOD1(void, crypto_set_id_callback, (unsigned long (*p1)(void)))
84     DEFINE_METHOD1(void, crypto_set_locking_callback, (void (*p1)(int, int, const char *, int)))
85 #endif
86     BEGIN_METHOD_RESOLVE()
87       RESOLVE_METHOD_RENAME(curl_global_init, global_init)
88       RESOLVE_METHOD_RENAME(curl_global_cleanup, global_cleanup)
89       RESOLVE_METHOD_RENAME(curl_easy_init, easy_init)
90       RESOLVE_METHOD_RENAME(curl_easy_strerror, easy_strerror)
91       RESOLVE_METHOD_RENAME_FP(curl_easy_setopt, easy_setopt)
92       RESOLVE_METHOD_RENAME(curl_easy_perform, easy_perform)
93       RESOLVE_METHOD_RENAME(curl_easy_pause, easy_pause)
94       RESOLVE_METHOD_RENAME(curl_easy_reset, easy_reset)
95       RESOLVE_METHOD_RENAME_FP(curl_easy_getinfo, easy_getinfo)
96       RESOLVE_METHOD_RENAME(curl_easy_cleanup, easy_cleanup)
97       RESOLVE_METHOD_RENAME(curl_easy_duphandle, easy_duphandle)
98       RESOLVE_METHOD_RENAME(curl_multi_init, multi_init)
99       RESOLVE_METHOD_RENAME(curl_multi_add_handle, multi_add_handle)
100       RESOLVE_METHOD_RENAME(curl_multi_perform, multi_perform)
101       RESOLVE_METHOD_RENAME(curl_multi_remove_handle, multi_remove_handle)
102       RESOLVE_METHOD_RENAME(curl_multi_fdset, multi_fdset)
103       RESOLVE_METHOD_RENAME(curl_multi_timeout, multi_timeout)
104       RESOLVE_METHOD_RENAME(curl_multi_info_read, multi_info_read)
105       RESOLVE_METHOD_RENAME(curl_multi_cleanup, multi_cleanup)
106       RESOLVE_METHOD_RENAME(curl_slist_append, slist_append)
107       RESOLVE_METHOD_RENAME(curl_slist_free_all, slist_free_all)
108 #if defined(HAS_CURL_STATIC)
109       RESOLVE_METHOD_RENAME(CRYPTO_set_id_callback, crypto_set_id_callback)
110       RESOLVE_METHOD_RENAME(CRYPTO_set_locking_callback, crypto_set_locking_callback)
111 #endif
112     END_METHOD_RESOLVE()
113
114   };
115
116   class DllLibCurlGlobal : public DllLibCurl
117   {
118   public:
119     /* extend interface with buffered functions */
120     void easy_aquire(const char *protocol, const char *hostname, CURL_HANDLE** easy_handle, CURLM** multi_handle);
121     void easy_release(CURL_HANDLE** easy_handle, CURLM** multi_handle);
122     void easy_duplicate(CURL_HANDLE* easy, CURLM* multi, CURL_HANDLE** easy_out, CURLM** multi_out);
123     CURL_HANDLE* easy_duphandle(CURL_HANDLE* easy_handle);
124     void CheckIdle();
125
126     /* overloaded load and unload with reference counter */
127     virtual bool Load();
128     virtual void Unload();
129
130     /* structure holding a session info */
131     typedef struct SSession
132     {
133       unsigned int  m_idletimestamp;  // timestamp of when this object when idle
134       CStdString    m_protocol;
135       CStdString    m_hostname;
136       bool          m_busy;
137       CURL_HANDLE*  m_easy;
138       CURLM*        m_multi;
139     } SSession;
140
141     typedef std::vector<SSession> VEC_CURLSESSIONS;
142
143     VEC_CURLSESSIONS m_sessions;
144     CCriticalSection m_critSection;
145   };
146 }
147
148 extern XCURL::DllLibCurlGlobal g_curlInterface;