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

void MsnSocketTcp::slotSocketDataReceived (  )  [private, slot]

Called when data is received from the server.

This method parses the incoming raw data for the syntax used by the MSN protocol. When a command is only partially received, it's buffered until the whole command is received by subsequent dataReceived() calls.

Messages are then signalled back to MsnConnection which perform the parsing. Only PNG commands are handled internally.

Definition at line 419 of file msnsockettcp.cpp.

References MsnSocketBase::isErrorCommand(), and MsnSocketBase::isPayloadCommand().

Referenced by MsnSocketTcp().

{
  bool        gotPayloadCommand  = false;
  bool        hasCompletePayload = false;
  QByteArray  rawCommandLine;
  QByteArray  payloadData;
  QStringList command;
  QString     commandLine;

  while( true )
  {
    // Read the next command if we're not waiting for a payload.
    if( nextPayloadSize_ == 0 )
    {
      // Stop when socket didn't receive a full line yet (Qt does the buffering, thanks Qt4!)
      if( ! socket_->canReadLine() )
      {
        break;
      }

      // Read the next line.
      rawCommandLine = socket_->readLine();

      // Parse the command, split in separate fields
      commandLine = QString::fromUtf8( rawCommandLine.data(), rawCommandLine.size() );
      command     = commandLine.trimmed().split(" ");

      // See if it's a payload command or an error
      if( isErrorCommand( command[0] ) )
      {
        // If the command contains a payload length parameter, also parse it
        gotPayloadCommand = ( command.size() > 2 );
      }
      else
      {
        gotPayloadCommand = isPayloadCommand( command[0] );
      }

      if( gotPayloadCommand )
      {
        nextPayloadSize_    = command[ command.count() - 1 ].toInt();  // Last arg is payload length.
        nextPayloadCommand_ = command;
      }
    }


    // See if we can read the payload
    if( nextPayloadSize_ > 0 )
    {
      // Check if the full payload is received.
      // Otherwise, just wait until "dataReceived" is called again.
      hasCompletePayload = ( socket_->bytesAvailable() >= nextPayloadSize_ );
      if( ! hasCompletePayload )
      {
        break;
      }

      // Full payload received.
      payloadData = socket_->read( nextPayloadSize_ );

      // Copy state from previous loop if needed.
      if( ! gotPayloadCommand )
      {
        gotPayloadCommand = true;
        command           = nextPayloadCommand_;
      }

      // Reset state for next loop.
      nextPayloadSize_ = 0;
    }
    else
    {
      payloadData.clear();
    }


    // Response from the internal ping timer.
    if( command[0] == "QNG" )
    {
      pingReceived_ = true;
      missedPings_  = 0;
    }
    else // All other data
    {
      emit dataReceived( command, payloadData );
    }
  }
}


Generated by  Doxygen 1.6.0   Back to index