Logo Search packages:      
Sourcecode: sbnc version File versions  Download package

int CConnection::Read ( bool  DontProcess = false  )  [virtual, inherited]

Read

Called when data is available on the socket.

Parameters:
DontProcess determines whether to process the data

Implements CSocketEvents.

Reimplemented in CClientConnection.

Definition at line 255 of file Connection.cpp.

References CTrafficStats::AddInbound(), CConnection::IsSSL(), CConnection::m_Connected, CConnection::m_InboundTraffic, CConnection::m_InboundTrafficReset, CConnection::m_RecvQ, CConnection::m_Shutdown, CConnection::m_Socket, CConnection::m_SSL, CConnection::m_Traffic, CConnection::ProcessBuffer(), and CFIFOBuffer::Write().

Referenced by CClientConnection::Read().

                                      {
      int ReadResult;
      static char *Buffer = NULL;
      static int BufferSize = 0;
      socklen_t OptLenght = sizeof(BufferSize);

      m_Connected = true;

      if (m_Shutdown) {
            return 0;
      }

      if (BufferSize == 0 && safe_getsockopt(m_Socket, SOL_SOCKET, SO_RCVBUF, (char *)&BufferSize, &OptLenght) != 0) {
            BufferSize = 8192;
      }

      if (Buffer == NULL) {
            Buffer = (char *)malloc(BufferSize);
      }

      CHECK_ALLOC_RESULT(Buffer, malloc) {
            return -1;
      } CHECK_ALLOC_RESULT_END;

#ifdef USESSL
      if (IsSSL()) {
            ReadResult = SSL_read(m_SSL, Buffer, BufferSize);

            if (ReadResult < 0) {
                  switch (SSL_get_error(m_SSL, ReadResult)) {
                        case SSL_ERROR_WANT_WRITE:
                        case SSL_ERROR_WANT_READ:
                        case SSL_ERROR_NONE:
                        case SSL_ERROR_ZERO_RETURN:

                              return 0;
                        default:
                              return -1;
                  }
            }

            ERR_print_errors_fp(stdout);
      } else
#endif
            ReadResult = safe_recv(m_Socket, Buffer, BufferSize, 0);

      if (ReadResult > 0) {
            if (g_CurrentTime - m_InboundTrafficReset > 30) {
                  m_InboundTrafficReset = g_CurrentTime;
                  m_InboundTraffic = 0;
            }

            m_InboundTraffic += ReadResult;

            m_RecvQ->Write(Buffer, ReadResult);

            if (m_Traffic) {
                  m_Traffic->AddInbound(ReadResult);
            }
      } else {
            int ErrorCode;

            if (ReadResult == 0) {
                  return -1;
            }

            ErrorCode = safe_errno();

#ifdef _WIN32
            if (ErrorCode == WSAEWOULDBLOCK) {
                  return 0;
            }
#else
            if (ErrorCode == EAGAIN) {
                  return 0;
            }
#endif

#ifdef USESSL
            if (IsSSL()) {
                  SSL_shutdown(m_SSL);
            }
#endif

            return ErrorCode;
      }

      if (!DontProcess) {
            ProcessBuffer();
      }

      return 0;
}


Generated by  Doxygen 1.6.0   Back to index