Skip to content

Commit

Permalink
cache proxy on platform iphone
Browse files Browse the repository at this point in the history
  • Loading branch information
garryyan committed Jul 7, 2017
1 parent e9b34eb commit a9ab981
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 3 deletions.
112 changes: 110 additions & 2 deletions mars/app/app_logic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@
*/

#include "mars/app/app_logic.h"

#include <TargetConditionals.h>

#include "mars/comm/xlogger/xlogger.h"
#include "mars/comm/bootrun.h"
#include "mars/comm/thread/mutex.h"
#include "mars/comm/thread/lock.h"
#include "mars/comm/thread/thread.h"
#include "mars/comm/dns/dns.h"
#include "mars/baseevent/baseprjevent.h"

namespace mars {
namespace app {
Expand All @@ -42,9 +51,101 @@ void SetCallback(Callback* const callback) {
}

#ifndef ANDROID

static mars::comm::ProxyInfo sg_proxyInfo;
static bool sg_gotProxy = false;
static Mutex sg_slproxymutex;
static Thread sg_slproxyThread;
static uint64_t sg_slporxytimetick = gettickcount();
static int sg_slproxycount = 0;

static void __ClearProxyInfo() {
ScopedLock lock(sg_slproxymutex);
sg_slporxytimetick = gettickcount();
sg_slproxycount = 0;
sg_gotProxy = false;
sg_proxyInfo.type = mars::comm::kProxyNone;
}

static void __GetProxyInfo(const std::string& _host, uint64_t _timetick) {
xinfo_function(TSF"timetick:%_, host:%_", _timetick, _host);

mars::comm::ProxyInfo proxy_info;
if (!sg_callback->GetProxyInfo(_host, proxy_info)) {
ScopedLock lock(sg_slproxymutex);
if (_timetick != sg_slporxytimetick) {
return;
}
++ sg_slproxycount;
return;
}

ScopedLock lock(sg_slproxymutex);
if (_timetick != sg_slporxytimetick) {
return;
}

++ sg_slproxycount;

sg_proxyInfo = proxy_info;

if (mars::comm::kProxyNone == sg_proxyInfo.type || !sg_proxyInfo.ip.empty() || sg_proxyInfo.host.empty()) {
sg_gotProxy = true;
return;
}

std::string host = sg_proxyInfo.host;
lock.unlock();

static DNS s_dns;
std::vector<std::string> ips;
s_dns.GetHostByName(host, ips);

if (ips.empty()) {
return;
}

lock.lock();
sg_proxyInfo.ip = ips.front();
sg_gotProxy = true;

}

static void __InitbindBaseprjevent() {
GetSignalOnNetworkChange().connect(&__ClearProxyInfo);
}


#if TARGET_OS_IPHONE
BOOT_RUN_STARTUP(__InitbindBaseprjevent);
#endif

mars::comm::ProxyInfo GetProxyInfo(const std::string& _host) {
xassert2(sg_callback != NULL);
return sg_callback->GetProxyInfo(_host);

#if !TARGET_OS_IPHONE
mars::comm::ProxyInfo proxy_info;
sg_callback->GetProxyInfo(_host, proxy_info)
return proxy_info;
#endif

if (sg_gotProxy) {
return sg_proxyInfo;
}

ScopedLock lock(sg_slproxymutex, false);
if (!lock.timedlock(500)) return mars::comm::ProxyInfo();

if (sg_slproxycount < 3 || 5 * 1000 > gettickspan(sg_slporxytimetick)) {
sg_slproxyThread.start(boost::bind(&__GetProxyInfo, _host, sg_slporxytimetick));
}

if (sg_gotProxy) {
return sg_proxyInfo;
}

return mars::comm::ProxyInfo();

}

std::string GetAppFilePath() {
Expand Down Expand Up @@ -76,7 +177,14 @@ void SetCallback(Callback* const callback) {

DeviceInfo GetDeviceInfo() {
xassert2(sg_callback != NULL);
return sg_callback->GetDeviceInfo();

static DeviceInfo device_info;
if (!device_info.devicename.empty() || !device_info.devicetype.empty()) {
return device_info;
}

device_info = sg_callback->GetDeviceInfo();
return device_info;
}


Expand Down
2 changes: 1 addition & 1 deletion mars/app/app_logic.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace app {
public:
virtual ~Callback() {};

virtual mars::comm::ProxyInfo GetProxyInfo(const std::string& _host) { return mars::comm::ProxyInfo(); }
virtual bool GetProxyInfo(const std::string& _host, mars::comm::ProxyInfo& _proxy_info) { return false; }

virtual std::string GetAppFilePath() = 0;

Expand Down

0 comments on commit a9ab981

Please sign in to comment.