From 8209ab9c983dbf4621e78faa91b582f4d404bb38 Mon Sep 17 00:00:00 2001 From: Clemens Zeidler Date: Mon, 28 Feb 2011 23:16:57 +0000 Subject: [PATCH] Hopefully handle cases when the connection to the server is lost. This should trigger a reconnect. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40748 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../imap/IMAPInboundProtocol.cpp | 8 +++++- .../imap/imap_lib/IMAPProtocol.cpp | 27 +++++++++++++++---- .../imap/imap_lib/IMAPProtocol.h | 3 +++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/add-ons/mail_daemon/inbound_protocols/imap/IMAPInboundProtocol.cpp b/src/add-ons/mail_daemon/inbound_protocols/imap/IMAPInboundProtocol.cpp index df945bc81f..49929aeee3 100644 --- a/src/add-ons/mail_daemon/inbound_protocols/imap/IMAPInboundProtocol.cpp +++ b/src/add-ons/mail_daemon/inbound_protocols/imap/IMAPInboundProtocol.cpp @@ -398,7 +398,13 @@ IMAPInboundProtocol::Reconnect() bool IMAPInboundProtocol::IsConnected() { - return fIsConnected; + if (!fIsConnected) + return false; + if (fIMAPMailbox.IsConnected()) + return true; + + Disconnect(); + return false; } diff --git a/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/IMAPProtocol.cpp b/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/IMAPProtocol.cpp index 8ebebbc44b..44ca668ec1 100644 --- a/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/IMAPProtocol.cpp +++ b/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/IMAPProtocol.cpp @@ -101,7 +101,7 @@ ConnectionReader::_GetNextDataBunch(BString& line, bigtime_t timeout, } int nReaded = fServerConnection->Read(buffer, maxNewLength); - if (nReaded < 0) + if (nReaded <= 0) return B_ERROR; fStringBuffer.SetTo(buffer, nReaded); @@ -135,7 +135,8 @@ IMAPProtocol::IMAPProtocol() fServerConnection(&fOwnServerConnection), fConnectionReader(fServerConnection), fCommandId(0), - fStopNow(0) + fStopNow(0), + fIsConnected(false) { } @@ -146,7 +147,8 @@ IMAPProtocol::IMAPProtocol(IMAPProtocol& connection) fServerConnection(connection.fServerConnection), fConnectionReader(fServerConnection), fCommandId(0), - fStopNow(0) + fStopNow(0), + fIsConnected(false) { } @@ -203,6 +205,7 @@ IMAPProtocol::Connect(const char* server, const char* username, if (status != B_OK) return status; + fIsConnected = true; return B_OK; } @@ -211,10 +214,18 @@ status_t IMAPProtocol::Disconnect() { ProcessCommand("LOGOUT"); + fIsConnected = false; return fOwnServerConnection.Disconnect(); } +bool +IMAPProtocol::IsConnected() +{ + return fIsConnected; +} + + ConnectionReader& IMAPProtocol::GetConnectionReader() { @@ -281,8 +292,11 @@ IMAPProtocol::SendCommand(const char* command, int32 commandId) TRACE("_SendCommand: %s\n", cmd); int commandLength = strlen(cmd); - if (fServerConnection->Write(cmd, commandLength) != commandLength) + if (fServerConnection->Write(cmd, commandLength) != commandLength) { + // we might lost the connection, clear the connection state + Disconnect(); return B_ERROR; + } fOngoingCommands.push_back(commandId); return B_OK; @@ -301,7 +315,10 @@ IMAPProtocol::HandleResponse(int32 commandId, bigtime_t timeout) if (status != B_OK) { if (status != B_TIMED_OUT) TRACE("S:read error %s", line.String()); - break; + // we might lost the connection, clear the connection state + TRACE("Disconnect\n"); + Disconnect(); + return status; } //TRACE("S: %s", line.String()); diff --git a/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/IMAPProtocol.h b/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/IMAPProtocol.h index 4419f53605..e23a255603 100644 --- a/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/IMAPProtocol.h +++ b/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/IMAPProtocol.h @@ -75,6 +75,7 @@ public: const char* username, const char* password, bool useSSL = true, int32 port = -1); status_t Disconnect(); + bool IsConnected(); ConnectionReader& GetConnectionReader(); status_t SendRawCommand(const char* command); @@ -118,6 +119,8 @@ private: BString fCommandError; vint32 fStopNow; + + bool fIsConnected; };