xbmc
DllLibCurl.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "threads/CriticalSection.h"
12 
13 #include <stdio.h>
14 #include <string>
15 #include <sys/time.h>
16 #include <sys/types.h>
17 #include <type_traits>
18 #include <vector>
19 
20 #define CURL CURL_HANDLE
21 #include <curl/curl.h>
22 #undef CURL
23 
24 namespace XCURL
25 {
26 
28 {
29 public:
30  virtual ~DllLibCurl() = default;
31 
32  CURLcode global_init(long flags);
33  void global_cleanup();
34  CURL_HANDLE* easy_init();
35  template<typename... Args>
36  CURLcode easy_setopt(CURL_HANDLE* handle, CURLoption option, Args... args)
37  {
38  return curl_easy_setopt(handle, option, std::forward<Args>(args)...);
39  }
40  CURLcode easy_perform(CURL_HANDLE* handle);
41  CURLcode easy_pause(CURL_HANDLE* handle, int bitmask);
42  void easy_reset(CURL_HANDLE* handle);
43  template<typename... Args>
44  CURLcode easy_getinfo(CURL_HANDLE* curl, CURLINFO info, Args... args)
45  {
46  return curl_easy_getinfo(curl, info, std::forward<Args>(args)...);
47  }
48  void easy_cleanup(CURL_HANDLE* handle);
49  virtual CURL_HANDLE* easy_duphandle(CURL_HANDLE* handle);
50  CURLM* multi_init(void);
51  CURLMcode multi_add_handle(CURLM* multi_handle, CURL_HANDLE* easy_handle);
52  CURLMcode multi_perform(CURLM* multi_handle, int* running_handles);
53  CURLMcode multi_remove_handle(CURLM* multi_handle, CURL_HANDLE* easy_handle);
54  CURLMcode multi_fdset(CURLM* multi_handle,
55  fd_set* read_fd_set,
56  fd_set* write_fd_set,
57  fd_set* exc_fd_set,
58  int* max_fd);
59  CURLMcode multi_timeout(CURLM* multi_handle, long* timeout);
60  CURLMsg* multi_info_read(CURLM* multi_handle, int* msgs_in_queue);
61  CURLMcode multi_cleanup(CURLM* handle);
62  curl_slist* slist_append(curl_slist* list, const char* to_append);
63  void slist_free_all(curl_slist* list);
64  const char* easy_strerror(CURLcode code);
65 };
66 
68 {
69 public:
72  /* extend interface with buffered functions */
73  void easy_acquire(const char* protocol,
74  const char* hostname,
75  CURL_HANDLE** easy_handle,
76  CURLM** multi_handle);
77  void easy_release(CURL_HANDLE** easy_handle, CURLM** multi_handle);
78  void easy_duplicate(CURL_HANDLE* easy,
79  const CURLM* multi,
80  CURL_HANDLE** easy_out,
81  CURLM** multi_out);
82  CURL_HANDLE* easy_duphandle(CURL_HANDLE* easy_handle) override;
83  void CheckIdle();
84 
85  /* overloaded load and unload with reference counter */
86 
87  /* structure holding a session info */
88  typedef struct SSession
89  {
90  std::chrono::time_point<std::chrono::steady_clock>
91  m_idletimestamp; // timestamp of when this object when idle
92  std::string m_protocol;
93  std::string m_hostname;
94  bool m_busy;
95  CURL_HANDLE* m_easy;
96  CURLM* m_multi;
97  } SSession;
98 
99  typedef std::vector<SSession> VEC_CURLSESSIONS;
100 
101  VEC_CURLSESSIONS m_sessions;
102  CCriticalSection m_critSection;
103 };
104 } // namespace XCURL
105 
106 extern XCURL::DllLibCurlGlobal g_curlInterface;
Definition: DllLibCurl.cpp:17
Definition: DllLibCurl.h:27
Definition: DllLibCurl.h:67
Definition: DllLibCurl.h:88
Definition: inftrees.h:24