summaryrefslogtreecommitdiff
path: root/protocols/VKontakte/src
diff options
context:
space:
mode:
Diffstat (limited to 'protocols/VKontakte/src')
-rw-r--r--protocols/VKontakte/src/vk_proto.h12
-rw-r--r--protocols/VKontakte/src/vk_queue.cpp15
-rw-r--r--protocols/VKontakte/src/vk_thread.cpp81
3 files changed, 100 insertions, 8 deletions
diff --git a/protocols/VKontakte/src/vk_proto.h b/protocols/VKontakte/src/vk_proto.h
index d6a6a3259e..23e3144b81 100644
--- a/protocols/VKontakte/src/vk_proto.h
+++ b/protocols/VKontakte/src/vk_proto.h
@@ -98,6 +98,13 @@ struct CVkProto : public PROTO<CVkProto>
void RetrieveFriends();
void OnReceiveFriends(NETLIBHTTPREQUEST*, void*);
+ void RetrievePollingInfo();
+ void OnReceivePollingInfo(NETLIBHTTPREQUEST*, void*);
+
+ void __cdecl PollingThread(void*);
+ void PollServer();
+ void OnReceivePolling(NETLIBHTTPREQUEST*, void*);
+
int SetServerStatus(int);
__forceinline bool IsOnline() const { return m_bOnline; }
@@ -144,8 +151,11 @@ private:
bool m_bOnline;
- HANDLE m_hNetlibUser, m_hNetlibConn;
+ HANDLE m_hNetlibConn;
ptrA m_szAccessToken, m_myUserId;
ptrT m_defaultGroup;
UINT_PTR m_timer;
+
+ ptrA m_pollingServer, m_pollingKey, m_pollingTs;
+ HANDLE m_pollingConn;
};
diff --git a/protocols/VKontakte/src/vk_queue.cpp b/protocols/VKontakte/src/vk_queue.cpp
index e81bc61b0c..0e204109f2 100644
--- a/protocols/VKontakte/src/vk_queue.cpp
+++ b/protocols/VKontakte/src/vk_queue.cpp
@@ -72,21 +72,22 @@ bool CVkProto::PushAsyncHttpRequest(int iRequestType, LPCSTR szUrl, bool bSecure
pReq->flags |= NLHRF_SSL;
CMStringA url;
- if (nParams > 0) {
+ if (*szUrl == '/') {
url = VK_API_URL;
url += szUrl;
- for (int i=0; i < nParams; i++) {
- url.AppendChar((i == 0) ? '?' : '&');
- url += pParams[i].szName;
- url.AppendChar('=');
- url += ptrA( mir_urlEncode(pParams[i].szValue));
- }
pReq->nlc = m_hNetlibConn;
}
else {
url = szUrl;
pReq->flags |= NLHRF_REMOVEHOST | NLHRF_SMARTREMOVEHOST;
}
+
+ for (int i=0; i < nParams; i++) {
+ url.AppendChar((i == 0) ? '?' : '&');
+ url += pParams[i].szName;
+ url.AppendChar('=');
+ url += ptrA( mir_urlEncode(pParams[i].szValue));
+ }
pReq->requestType = iRequestType;
pReq->szUrl = mir_strdup(url);
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;
+}