From d4c204ca301117519e21ec50ecfb565d400e2cda Mon Sep 17 00:00:00 2001
From: George Hazan <george.hazan@gmail.com>
Date: Fri, 11 Oct 2013 22:41:40 +0000
Subject: VK: - message sending - fix for hangup on exit

git-svn-id: http://svn.miranda-ng.org/main/trunk@6454 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
---
 protocols/VKontakte/src/vk_proto.cpp | 57 +++++++++++++++++++++++++++++++-----
 protocols/VKontakte/src/vk_proto.h   |  3 ++
 2 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/protocols/VKontakte/src/vk_proto.cpp b/protocols/VKontakte/src/vk_proto.cpp
index 78ca6fbdc4..2ef53906ea 100644
--- a/protocols/VKontakte/src/vk_proto.cpp
+++ b/protocols/VKontakte/src/vk_proto.cpp
@@ -19,7 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
 
 CVkProto::CVkProto(const char *szModuleName, const TCHAR *ptszUserName) :
 	PROTO<CVkProto>(szModuleName, ptszUserName),
-	m_arRequestsQueue(10)
+	m_arRequestsQueue(10),
+	m_msgId(1)
 {
 	InitQueue();
 
@@ -62,8 +63,6 @@ int CVkProto::OnPreShutdown(WPARAM wParam, LPARAM lParam)
 {
 	m_bTerminated = true;
 	SetEvent(m_evRequestsQueue);
-	if (m_hPollingThread)
-		TerminateThread(m_hPollingThread, 0);
 	return 0;
 }
 
@@ -109,6 +108,53 @@ int CVkProto::RecvMsg(HANDLE hContact, PROTORECVEVENT *pre)
 
 //////////////////////////////////////////////////////////////////////////////
 
+struct TFakeAckParams
+{
+	__inline TFakeAckParams(HANDLE _hContact, int _msgid) :
+		hContact(_hContact), msgid(_msgid)
+		{}
+
+	HANDLE hContact;
+	int msgid;
+};
+
+void CVkProto::SendMsgAck(void *param)
+{
+	TFakeAckParams *ack = (TFakeAckParams*)param;
+	Sleep(100);
+	ProtoBroadcastAck(ack->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)ack->msgid, 0);
+	delete ack;
+}
+
+int CVkProto::SendMsg(HANDLE hContact, int flags, const char *msg)
+{ 
+	ptrA szID( getStringA(hContact, "ID"));
+	if (szID == NULL)
+		return 0;
+
+	ptrA szMsg;
+	if (flags & PREF_UTF)
+		szMsg = mir_strdup(msg);
+	else if (flags & PREF_UNICODE)
+		msg = mir_utf8encodeW((wchar_t*)&msg[strlen(msg)+1]);
+	else
+		msg = mir_utf8encode(msg);
+
+	HttpParam params[] = {
+		{ "type", "0" },
+		{ "uid",  szID },
+		{ "message", szMsg },
+		{ "access_token", m_szAccessToken }
+	};
+	PushAsyncHttpRequest(REQUEST_GET, "/method/messages.send.json", true, NULL, SIZEOF(params), params);
+
+	ULONG msgId = ::InterlockedIncrement(&m_msgId);
+	ForkThread(&CVkProto::SendMsgAck, new TFakeAckParams(hContact, msgId));
+	return msgId;
+}
+
+//////////////////////////////////////////////////////////////////////////////
+
 int CVkProto::SetStatus(int iNewStatus)
 {
 	if (m_iDesiredStatus == iNewStatus)
@@ -193,11 +239,6 @@ int CVkProto::AuthDeny(HANDLE hDbEvent, const PROTOCHAR *reason)
 	return 1;
 }
 
-int CVkProto::SendMsg(HANDLE hContact, int flags, const char *msg)
-{ 
-	return 0;
-}
-
 int CVkProto::UserIsTyping(HANDLE hContact, int type)
 { 
 	return 0;
diff --git a/protocols/VKontakte/src/vk_proto.h b/protocols/VKontakte/src/vk_proto.h
index 3f5d8df4e0..c8275ae6f1 100644
--- a/protocols/VKontakte/src/vk_proto.h
+++ b/protocols/VKontakte/src/vk_proto.h
@@ -152,6 +152,8 @@ private:
 
 	void   SetAllContactStatuses(int status);
 
+	void   __cdecl SendMsgAck(void *param);
+
 	bool   m_bOnline;
 
 	HANDLE m_hNetlibConn;
@@ -161,4 +163,5 @@ private:
 
 	ptrA   m_pollingServer, m_pollingKey, m_pollingTs;
 	HANDLE m_hPollingThread;
+	ULONG  m_msgId;
 };
-- 
cgit v1.2.3