summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/connection.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2014-09-18 21:52:10 +0000
committerGeorge Hazan <george.hazan@gmail.com>2014-09-18 21:52:10 +0000
commit6d932bfaf11e4699355fedc45e28b353b8877130 (patch)
tree78d3f6741cf9762d2c6b97ed1f5bc472e850e15f /protocols/WhatsApp/src/connection.cpp
parent4dd774d667df90583315c04696537b6397f6fc02 (diff)
merge into trunk
git-svn-id: http://svn.miranda-ng.org/main/trunk@10515 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'protocols/WhatsApp/src/connection.cpp')
-rw-r--r--protocols/WhatsApp/src/connection.cpp138
1 files changed, 58 insertions, 80 deletions
diff --git a/protocols/WhatsApp/src/connection.cpp b/protocols/WhatsApp/src/connection.cpp
index 5fc08ec1dc..a76ad89522 100644
--- a/protocols/WhatsApp/src/connection.cpp
+++ b/protocols/WhatsApp/src/connection.cpp
@@ -2,35 +2,28 @@
void WhatsAppProto::ChangeStatus(void*)
{
- if (m_iDesiredStatus != ID_STATUS_OFFLINE && m_iStatus == ID_STATUS_OFFLINE)
- {
+ if (m_iDesiredStatus != ID_STATUS_OFFLINE && m_iStatus == ID_STATUS_OFFLINE) {
ResetEvent(update_loop_lock_);
ForkThread(&WhatsAppProto::sentinelLoop, this);
ForkThread(&WhatsAppProto::stayConnectedLoop, this);
}
- else if (m_iStatus == ID_STATUS_INVISIBLE && m_iDesiredStatus == ID_STATUS_ONLINE)
- {
- if (this->connection != NULL)
- {
+ else if (m_iStatus == ID_STATUS_INVISIBLE && m_iDesiredStatus == ID_STATUS_ONLINE) {
+ if (this->connection != NULL) {
this->connection->sendAvailableForChat();
m_iStatus = ID_STATUS_ONLINE;
- ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE) m_iStatus, ID_STATUS_INVISIBLE);
+ ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)m_iStatus, ID_STATUS_INVISIBLE);
}
}
- else if (m_iStatus == ID_STATUS_ONLINE && m_iDesiredStatus == ID_STATUS_INVISIBLE)
- {
- if (this->connection != NULL)
- {
+ else if (m_iStatus == ID_STATUS_ONLINE && m_iDesiredStatus == ID_STATUS_INVISIBLE) {
+ if (this->connection != NULL) {
this->connection->sendClose();
m_iStatus = ID_STATUS_INVISIBLE;
- SetAllContactStatuses( ID_STATUS_OFFLINE, true );
- ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE) m_iStatus, ID_STATUS_ONLINE);
+ SetAllContactStatuses(ID_STATUS_OFFLINE, true);
+ ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)m_iStatus, ID_STATUS_ONLINE);
}
}
- else if (m_iDesiredStatus == ID_STATUS_OFFLINE)
- {
- if (this->conn != NULL)
- {
+ else if (m_iDesiredStatus == ID_STATUS_OFFLINE) {
+ if (this->conn != NULL) {
SetEvent(update_loop_lock_);
this->conn->forceShutdown();
debugLogA("Forced shutdown");
@@ -42,58 +35,50 @@ void WhatsAppProto::stayConnectedLoop(void*)
{
bool error = true;
std::string cc, in, pass;
- DBVARIANT dbv = {0};
+ DBVARIANT dbv = { 0 };
- if ( !getString(WHATSAPP_KEY_CC, &dbv))
- {
+ if (!getString(WHATSAPP_KEY_CC, &dbv)) {
cc = dbv.pszVal;
db_free(&dbv);
error = cc.empty();
}
- if (error)
- {
- NotifyEvent(m_tszUserName,TranslateT("Please enter a country-code."),NULL,WHATSAPP_EVENT_CLIENT);
+ if (error) {
+ NotifyEvent(m_tszUserName, TranslateT("Please enter a country-code."), NULL, WHATSAPP_EVENT_CLIENT);
return;
}
error = true;
- if ( !getString(WHATSAPP_KEY_LOGIN, &dbv))
- {
+ if (!getString(WHATSAPP_KEY_LOGIN, &dbv)) {
in = dbv.pszVal;
db_free(&dbv);
error = in.empty();
}
- if (error)
- {
- NotifyEvent(m_tszUserName,TranslateT("Please enter a phone-number without country code."),NULL,WHATSAPP_EVENT_CLIENT);
+ if (error) {
+ NotifyEvent(m_tszUserName, TranslateT("Please enter a phone-number without country code."), NULL, WHATSAPP_EVENT_CLIENT);
return;
}
this->phoneNumber = cc + in;
this->jid = this->phoneNumber + "@s.whatsapp.net";
error = true;
- if ( !getString(WHATSAPP_KEY_NICK, &dbv))
- {
+ if (!getString(WHATSAPP_KEY_NICK, &dbv)) {
this->nick = dbv.pszVal;
db_free(&dbv);
error = nick.empty();
}
- if (error)
- {
+ if (error) {
NotifyEvent(m_tszUserName, TranslateT("Please enter a nickname."), NULL, WHATSAPP_EVENT_CLIENT);
return;
}
error = true;
- if ( !getString(WHATSAPP_KEY_PASS, &dbv))
- {
+ if (!getString(WHATSAPP_KEY_PASS, &dbv)) {
pass = dbv.pszVal;
db_free(&dbv);
error = pass.empty();
}
- if (error)
- {
- NotifyEvent(m_tszUserName,TranslateT("Please enter a password."),NULL,WHATSAPP_EVENT_CLIENT);
+ if (error) {
+ NotifyEvent(m_tszUserName, TranslateT("Please enter a password."), NULL, WHATSAPP_EVENT_CLIENT);
return;
}
@@ -105,41 +90,36 @@ void WhatsAppProto::stayConnectedLoop(void*)
this->conn = NULL;
- while (true)
- {
- if (connection != NULL)
- {
- if (connection->getLogin() == NULL && login != NULL)
- {
+ while (true) {
+ if (connection != NULL) {
+ if (connection->getLogin() == NULL && login != NULL) {
delete login;
login = NULL;
}
delete connection;
connection = NULL;
}
- if (this->conn != NULL)
- {
+ if (this->conn != NULL) {
delete this->conn;
this->conn = NULL;
}
desiredStatus = this->m_iDesiredStatus;
- if (desiredStatus == ID_STATUS_OFFLINE || error)
- {
+ if (desiredStatus == ID_STATUS_OFFLINE || error) {
debugLogA("Set status to offline");
- SetAllContactStatuses( ID_STATUS_OFFLINE, true );
+ SetAllContactStatuses(ID_STATUS_OFFLINE, true);
this->ToggleStatusMenuItems(false);
int prevStatus = this->m_iStatus;
this->m_iStatus = ID_STATUS_OFFLINE;
- ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE) m_iStatus, prevStatus);
+ ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)m_iStatus, prevStatus);
break;
}
debugLogA("Connecting...");
this->m_iStatus = ID_STATUS_CONNECTING;
- ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE) ID_STATUS_OFFLINE, m_iStatus);
+ ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)ID_STATUS_OFFLINE, m_iStatus);
- CODE_BLOCK_TRY
+ try {
unsigned passLen;
ptrA passBin((char*)mir_base64_decode(pass.c_str(), &passLen));
std::string password(passBin, passLen), resource = ACCOUNT_RESOURCE;
@@ -148,13 +128,13 @@ void WhatsAppProto::stayConnectedLoop(void*)
portNumber = 443, resource += "-443";
else
portNumber = 5222, resource += "-5222";
-
+
this->conn = new WASocketConnection("c.whatsapp.net", portNumber);
connection = new WAConnection(&this->connMutex, this, this);
login = new WALogin(connection, new BinTreeNodeReader(connection, conn, WAConnection::dictionary, WAConnection::DICTIONARY_LEN),
- new BinTreeNodeWriter(connection, conn, WAConnection::dictionary, WAConnection::DICTIONARY_LEN, &writerMutex),
- "s.whatsapp.net", this->phoneNumber, resource, password, nick);
+ new BinTreeNodeWriter(connection, conn, WAConnection::dictionary, WAConnection::DICTIONARY_LEN, &writerMutex),
+ "s.whatsapp.net", this->phoneNumber, resource, password, nick);
std::vector<unsigned char>* nextChallenge = login->login(*this->challenge);
delete this->challenge;
@@ -167,27 +147,31 @@ void WhatsAppProto::stayConnectedLoop(void*)
debugLogA("Set status to online");
this->m_iStatus = desiredStatus;
- ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE )m_iStatus, ID_STATUS_CONNECTING);
+ ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)m_iStatus, ID_STATUS_CONNECTING);
this->ToggleStatusMenuItems(true);
ForkThread(&WhatsAppProto::ProcessBuddyList, this);
// #TODO Move out of try block. Exception is expected on disconnect
bool cont = true;
- while (cont == true)
- {
+ while (cont == true) {
this->lastPongTime = time(NULL);
cont = connection->read();
}
debugLogA("Exit from read-loop");
-
- CODE_BLOCK_CATCH(WAException)
+ }
+ catch (WAException &e) {
+ debugLogA("Exception: %s", e.what());
error = true;
- CODE_BLOCK_CATCH(exception)
+ }
+ catch (exception &e) {
+ debugLogA("Exception: %s", e.what());
error = true;
- CODE_BLOCK_CATCH_UNKNOWN
+ }
+ catch (...) {
+ debugLogA("Unknown exception");
error = true;
- CODE_BLOCK_END
+ }
}
debugLogA("Break out from loop");
}
@@ -196,30 +180,23 @@ void WhatsAppProto::sentinelLoop(void*)
{
int delay = MAX_SILENT_INTERVAL;
int quietInterval;
- while (WaitForSingleObjectEx(update_loop_lock_, delay * 1000, true) == WAIT_TIMEOUT)
- {
- if (this->m_iStatus != ID_STATUS_OFFLINE && this->connection != NULL && this->m_iDesiredStatus == this->m_iStatus)
- {
+ while (WaitForSingleObjectEx(update_loop_lock_, delay * 1000, true) == WAIT_TIMEOUT) {
+ if (this->m_iStatus != ID_STATUS_OFFLINE && this->connection != NULL && this->m_iDesiredStatus == this->m_iStatus) {
// #TODO Quiet after pong or tree read?
quietInterval = difftime(time(NULL), this->lastPongTime);
- if (quietInterval >= MAX_SILENT_INTERVAL)
- {
- CODE_BLOCK_TRY
+ if (quietInterval >= MAX_SILENT_INTERVAL) {
+ try {
debugLogA("send ping");
this->lastPongTime = time(NULL);
this->connection->sendPing();
- CODE_BLOCK_CATCH(exception)
- CODE_BLOCK_END
- }
- else
- {
- delay = MAX_SILENT_INTERVAL - quietInterval;
+ }
+ catch (exception &e) {
+ debugLogA("Exception: %s", e.what());
+ }
}
+ else delay = MAX_SILENT_INTERVAL - quietInterval;
}
- else
- {
- delay = MAX_SILENT_INTERVAL;
- }
+ else delay = MAX_SILENT_INTERVAL;
}
ResetEvent(update_loop_lock_);
debugLogA("Exiting sentinel loop");
@@ -228,9 +205,10 @@ void WhatsAppProto::sentinelLoop(void*)
void WhatsAppProto::onPing(const std::string& id)
{
if (this->isOnline()) {
- CODE_BLOCK_TRY
+ try {
debugLogA("Sending pong with id %s", id.c_str());
this->connection->sendPong(id);
+ }
CODE_BLOCK_CATCH_ALL
}
}