Bugfixes for SSL sockets.

This commit is contained in:
Gunnar Beutner 2012-07-16 11:44:11 +02:00
parent dd26fd46f5
commit 6ebb1bf192
4 changed files with 52 additions and 21 deletions

View File

@ -316,12 +316,8 @@ void Socket::ReadThreadProc(void)
return; return;
} }
if (FD_ISSET(fd, &readfds)) { if (FD_ISSET(fd, &readfds))
if (!m_Connected)
m_Connected = true;
HandleReadable(); HandleReadable();
}
if (FD_ISSET(fd, &exceptfds)) if (FD_ISSET(fd, &exceptfds))
HandleException(); HandleException();
@ -340,7 +336,7 @@ void Socket::WriteThreadProc(void)
FD_ZERO(&writefds); FD_ZERO(&writefds);
while (!WantsToWrite() && m_Connected) { while (!WantsToWrite()) {
m_WriteCV.timed_wait(lock, boost::posix_time::seconds(1)); m_WriteCV.timed_wait(lock, boost::posix_time::seconds(1));
if (GetFD() == INVALID_SOCKET) if (GetFD() == INVALID_SOCKET)
@ -368,16 +364,22 @@ void Socket::WriteThreadProc(void)
return; return;
} }
if (FD_ISSET(fd, &writefds)) { if (FD_ISSET(fd, &writefds))
if (!m_Connected)
m_Connected = true;
HandleWritable(); HandleWritable();
} }
} }
}
mutex& Socket::GetMutex(void) const mutex& Socket::GetMutex(void) const
{ {
return m_Mutex; return m_Mutex;
} }
void Socket::SetConnected(bool connected)
{
m_Connected = connected;
}
bool Socket::IsConnected(void) const
{
return m_Connected;
}

View File

@ -53,6 +53,9 @@ protected:
void SetFD(SOCKET fd); void SetFD(SOCKET fd);
SOCKET GetFD(void) const; SOCKET GetFD(void) const;
void SetConnected(bool connected);
bool IsConnected(void) const;
int GetError(void) const; int GetError(void) const;
static int GetLastSocketError(void); static int GetLastSocketError(void);
void HandleSocketError(const exception& ex); void HandleSocketError(const exception& ex);

View File

@ -120,10 +120,14 @@ void TcpClient::HandleWritable(void)
rc = send(GetFD(), (const char *)data, count, 0); rc = send(GetFD(), (const char *)data, count, 0);
if (rc <= 0) { if (rc <= 0) {
SetConnected(false);
HandleSocketError(SocketException("send() failed", GetError())); HandleSocketError(SocketException("send() failed", GetError()));
return; return;
} }
SetConnected(true);
m_SendQueue->Read(NULL, rc); m_SendQueue->Read(NULL, rc);
} }
} }
@ -182,10 +186,14 @@ void TcpClient::HandleReadable(void)
return; return;
if (rc <= 0) { if (rc <= 0) {
SetConnected(false);
HandleSocketError(SocketException("recv() failed", GetError())); HandleSocketError(SocketException("recv() failed", GetError()));
return; return;
} }
SetConnected(true);
m_RecvQueue->Write(data, rc); m_RecvQueue->Write(data, rc);
} }

View File

@ -118,7 +118,13 @@ void TlsClient::HandleReadable(void)
for (;;) { for (;;) {
char data[1024]; char data[1024];
int rc = SSL_read(m_SSL.get(), data, sizeof(data)); int rc;
if (IsConnected()) {
rc = SSL_read(m_SSL.get(), data, sizeof(data));
} else {
rc = SSL_do_handshake(m_SSL.get());
}
if (rc <= 0) { if (rc <= 0) {
switch (SSL_get_error(m_SSL.get(), rc)) { switch (SSL_get_error(m_SSL.get(), rc)) {
@ -137,7 +143,10 @@ void TlsClient::HandleReadable(void)
} }
} }
if (IsConnected())
m_RecvQueue->Write(data, rc); m_RecvQueue->Write(data, rc);
else
SetConnected(true);
} }
post_event: post_event:
@ -156,6 +165,9 @@ void TlsClient::HandleWritable(void)
size_t count; size_t count;
for (;;) { for (;;) {
int rc;
if (IsConnected()) {
count = m_SendQueue->GetAvailableBytes(); count = m_SendQueue->GetAvailableBytes();
if (count == 0) if (count == 0)
@ -166,7 +178,10 @@ void TlsClient::HandleWritable(void)
m_SendQueue->Peek(data, count); m_SendQueue->Peek(data, count);
int rc = SSL_write(m_SSL.get(), (const char *)data, count); rc = SSL_write(m_SSL.get(), (const char *)data, count);
} else {
rc = SSL_do_handshake(m_SSL.get());
}
if (rc <= 0) { if (rc <= 0) {
switch (SSL_get_error(m_SSL.get(), rc)) { switch (SSL_get_error(m_SSL.get(), rc)) {
@ -185,7 +200,10 @@ void TlsClient::HandleWritable(void)
} }
} }
if (IsConnected())
m_SendQueue->Read(NULL, rc); m_SendQueue->Read(NULL, rc);
else
SetConnected(true);
} }
} }