All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WebRequestServiceBackendImpl.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include "Web.h"
7 #include "Tasks.h"
8 #include "Helpers.h"
9 #include "Web.h"
10 #include "BidirectionalWebIOBus.h"
11 #include "WebMessages.h"
12 #include "WebIOConfig.h"
13 
14 #include <curl.h>
15 #include <uv.h>
16 
17 #include <string>
18 #include <deque>
19 #include <list>
20 #include <vector>
21 
22 namespace Eegeo
23 {
24  namespace Web
25  {
26  // we use this to marry up a curl socket & a uv poll handle
28  {
29  // uv_poll_t must be the first field; do not re-order or it'll break everything!
30  uv_poll_t pollHandle;
31  curl_socket_t sockfd;
32  void* data; // expect this to be an instance of WebRequestServiceBackendImpl
33  };
34 
35  // pimpl class to avoid pulling in various headers & causing include chaos
37  {
38  public:
40 
42 
43  bool Update();
44  private:
45  void StartWebRequest(InFlightWebRequest* request, Eegeo::Web::WebRequestHandle* requestHandle);
46  void IssueWebLoadRequests();
47  void SendResponse(InFlightWebRequest& completedWebRequest);
48  void SendFallBackToCacheMessage(const TWebRequestId requestId);
49  void CheckMultiInfo();
50 
51  void ResetHandle(WebRequestHandle* pWebRequestHandle);
52 
53  void OnWebPauseMessage(const Eegeo::Web::WebPauseMessage& message);
54  void OnWebResumeMessage(const Eegeo::Web::WebResumeMessage& message);
55  void OnWebRequestLoadMessage(const Eegeo::Web::WebRequestLoadMessage& message);
56  void OnWebRequestCancelMessage(const Eegeo::Web::WebRequestCancelMessage& message);
57  void OnWebStopWorkMessage(const Eegeo::Web::WebStopWorkMessage& message);
58 
59  // libcurl callbacks
60  static int CurlSocketCallbackWrapper(CURL* easy, curl_socket_t s, int action, void* userp, void* socketp);
61  int CurlSocketCallBack(CURL* easy, curl_socket_t s, int action, void* socketp);
62 
63  static int CurlTimeoutCallbackWrapper(CURLM* multi, long timeout_ms, void* userp);
64  int CurlTimeoutCallback(CURLM* multi, long timeout_ms);
65 
66  // libuv callbacks
67  // (static) callback that is invoked by libuv when changes are detected on a socket it is monitoring.
68  static void UVCallbackPollWrapper(uv_poll_t* req, int status, int events);
69  void UVCallbackPoll(AppCurlContext* pAppCurlContext, int status, int events);
70 
71  // (static) callback invoked by libuv when a timeout occurs
72  static void UVCallbackTimeoutWrapper(uv_timer_t* handle);
73  void UVCallbackTimeout(uv_timer_t* handle);
74 
75  const WebIOConfig m_config;
76  int m_threadSleepDurationMilliseconds;
77  Eegeo::Web::TMessageBus& m_webMessageBus;
78 
79  CURLM * m_curlMultiHandle;
80  WebRequestHandlePool* m_pWebLoadHandlePool;
81  std::vector<WebRequestHandle*> m_inFlightWebRequestHandles;
82  std::list<InFlightWebRequest*> m_pendingWebRequests;
83 
84  int m_stillRunning;
85  bool m_webLoadPoolExhaustedLastFrame;
86 
87  uv_timer_t m_uvTimer;
88  uv_loop_t* m_pUvLoop;
89 
95 
96  bool m_continueRunning;
97  };
98  }
99 }