summaryrefslogtreecommitdiff
path: root/protocols/VKontakte/src/vk_thread.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2013-10-11 14:01:25 +0000
committerGeorge Hazan <george.hazan@gmail.com>2013-10-11 14:01:25 +0000
commit773421e8ba17637d994088c393d406226b516a30 (patch)
tree130c0bf529e30f493ae4f2abbe1230d31fecdeed /protocols/VKontakte/src/vk_thread.cpp
parent3fc8b6f686262e8a595fc10b2bd947526ca77bdc (diff)
- m_hNetlibUser moved to PROTO_INTERFACE;
- unified protocol loggers git-svn-id: http://svn.miranda-ng.org/main/trunk@6435 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'protocols/VKontakte/src/vk_thread.cpp')
-rw-r--r--protocols/VKontakte/src/vk_thread.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/protocols/VKontakte/src/vk_thread.cpp b/protocols/VKontakte/src/vk_thread.cpp
index 4ef6be5c89..d191f70b2c 100644
--- a/protocols/VKontakte/src/vk_thread.cpp
+++ b/protocols/VKontakte/src/vk_thread.cpp
@@ -207,6 +207,7 @@ void CVkProto::OnReceiveMyInfo(NETLIBHTTPREQUEST *reply, void*)
RetrieveUserInfo(m_myUserId);
RetrieveFriends();
+ RetrievePollingInfo();
}
/////////////////////////////////////////////////////////////////////////////////////////
@@ -315,3 +316,83 @@ void CVkProto::OnReceiveFriends(NETLIBHTTPREQUEST *reply, void*)
if (szValue && *szValue) setTString(hContact, "Phone1", szValue);
}
}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
+void CVkProto::RetrievePollingInfo()
+{
+ Netlib_Logf(m_hNetlibUser, "CVkProto::RetrievePollingInfo");
+
+ HttpParam param = { "access_token", m_szAccessToken };
+ PushAsyncHttpRequest(REQUEST_GET, "/method/messages.getLongPollServer.json", true, &CVkProto::OnReceivePollingInfo, 1, &param);
+}
+
+void CVkProto::OnReceivePollingInfo(NETLIBHTTPREQUEST *reply, void*)
+{
+ Netlib_Logf(m_hNetlibUser, "CVkProto::OnReceivePollingInfo %d", reply->resultCode);
+ if (reply->resultCode != 200)
+ return;
+
+ JSONROOT pRoot(reply->pData);
+ if ( !CheckJsonResult(pRoot))
+ return;
+
+ JSONNODE *pResponse = json_get(pRoot, "response");
+ if (pResponse == NULL)
+ return;
+
+ m_pollingTs = mir_t2a( ptrT( json_as_string( json_get(pResponse, "ts"))));
+ m_pollingKey = mir_t2a( ptrT( json_as_string( json_get(pResponse, "key"))));
+ m_pollingServer = mir_t2a( ptrT( json_as_string( json_get(pResponse, "server"))));
+ if (m_pollingTs != NULL && m_pollingKey != NULL && m_pollingServer != NULL)
+ ForkThread(&CVkProto::PollingThread, 0);
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
+void CVkProto::PollServer()
+{
+ Netlib_Logf(m_hNetlibUser, "CVkProto::PollServer");
+
+ HttpParam params[] = {
+ { "act", "a_check" },
+ { "key", m_pollingKey },
+ { "ts", m_pollingTs },
+ { "wait", "25" },
+ { "access_token", m_szAccessToken }
+ };
+ PushAsyncHttpRequest(REQUEST_GET, m_pollingServer, true, &CVkProto::OnReceivePolling, SIZEOF(params), params);
+}
+
+void CVkProto::OnReceivePolling(NETLIBHTTPREQUEST *reply, void*)
+{
+ Netlib_Logf(m_hNetlibUser, "CVkProto::OnReceivePolling %d", reply->resultCode);
+ if (reply->resultCode != 200)
+ return;
+
+ JSONROOT pRoot(reply->pData);
+ if ( !CheckJsonResult(pRoot))
+ return;
+
+ JSONNODE *pResponse = json_get(pRoot, "response");
+ if (pResponse != NULL)
+ m_pollingTs = mir_t2a( ptrT( json_as_string( json_get(pResponse, "ts"))));
+}
+
+void CVkProto::PollingThread(void*)
+{
+ NETLIBOPENCONNECTION nloc = { sizeof(nloc) };
+ nloc.flags = NLOCF_SSL | NLOCF_HTTP | NLOCF_V2;
+ nloc.szHost = VK_API_URL;
+ nloc.wPort = 443;
+ nloc.timeout = 60*1000;
+ m_pollingConn = (HANDLE)CallService(MS_NETLIB_OPENCONNECTION, (WPARAM)m_hNetlibUser, (LPARAM)&nloc);
+ if (m_pollingConn == NULL)
+ return;
+
+ while (!m_bTerminated)
+ PollServer();
+
+ CallService(MS_NETLIB_CLOSEHANDLE, (WPARAM)m_pollingConn, 0);
+ m_pollingConn = NULL;
+}