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

void ChatCore::prepareDatagram ( unsigned char  dtgrm_type,
quint32  dest_ip,
const QString &  msg = "",
unsigned long  chnnl_id = 0,
unsigned long  dtgrm_id = 0,
unsigned long  dtgrm_num = 0 
)

Generiruent paket

Parameters:
dtgrm_type - tip paketa iz enum DataType
msg_len - dlina soobscheniya
msg - soobschenie
chnnl_id - id kanala v kotoryi posylaetsya soobschenie
dtgrm_id - id paketa(dlya fragmentirovannyh paketov)
dtgrm_num - nomer paketa(dlya fragmentirovannyh paketov)
parametrs_len - dlina massiva parametrs
parametrs - razlichnye dopolnitel'nye parametry(ne yavlyaetsya C-strokoi). format: [char*(C-stroka) par1_name][Ushort par1_len][char[par1_len] par1]... [char*(C-stroka) parN_name][Ushort parN_len][char[parN_len] parN]

Definition at line 111 of file chatcore.cpp.

References UserInfo::compName(), Preferences::ip(), m_data, m_header, m_largeDtgrm, m_outputBuffer, and UserInfo::nickname().

Referenced by slot_rejectReceiving().

{
  m_largeDtgrm = false;

  quint32 src_ip  = m_prefs->ip();

  QByteArray msgData      = msg                 .toUtf8();
  QByteArray nameData     = m_myInfo->nickname().toUtf8();
  QByteArray compNameData = m_myInfo->compName().toUtf8();

  int   i;
  int   pr_len = 0; // protocol_len
  int   unl    = nameData    .size(); // user_name_len
  int   cnl    = compNameData.size(); // comp_name_len
  int*  pr     = (int*)Globals::Protocol;
  char* buf;

  for(i = 0; i < 18; i++)
    pr_len += pr[i];

  m_outputBufferSize = pr_len + unl + cnl + msgData.size() + m_parametrs.size();

  qDebug("[ChatCore::prepareDatagram]: pr_len = %d, unl = %d, cnl = %d, msgData.size() = %d, m_parametrs.size() = %d", pr_len, unl, cnl, msgData.size(), m_parametrs.size());

  m_headerSize = pr_len + unl + cnl;

  m_header = (char*)realloc(m_header, m_headerSize);
  assert(NULL != m_header);

  // 4 + 4 - dlya razmerov esli paket budet fragmented
  m_dataSize     = msgData.size() + m_parametrs.size() + 4 + 4;
  m_data         = (char*)realloc(m_data, m_dataSize);
  assert(NULL   != m_data);

  if(m_outputBufferSize > MAX_PACKET_LEN)
    m_largeDtgrm = true;

  buf = m_header;

  strncpy   (buf          , Globals::ProgramID, pr[0] );
  catUS2str (buf += pr[ 0], Globals::VersionID        );
  catUS2str (buf += pr[ 1], Globals::ProtocolVersionID);
  catULL2str(buf += pr[ 2], dest_ip          );
  catULL2str(buf += pr[ 3], src_ip           );
           *(buf += pr[ 4]) = dtgrm_type;
  catUL2str (buf += pr[ 5], dtgrm_id         );
  catUL2str (buf += pr[ 6], dtgrm_num        );
  catULL2str(buf += pr[ 7], (quint64)time(NULL));
  catUL2str (buf += pr[ 8], chnnl_type       );
           *(buf += pr[ 9]) = cnl;
           *(buf += pr[10]) = unl;
  catUL2str (buf += pr[11], msgData.size  () );
  catUL2str (buf += pr[12], m_parametrs.size() );
  strncpy   (buf += pr[13], compNameData, cnl);
  strncpy   (buf += cnl   , nameData    , unl);

  buf = m_data;

  if(m_largeDtgrm)
  {
    catUL2str(buf     , msgData  .size());
    catUL2str(buf += 4, m_parametrs.size());
    buf += 4;
  }

  memcpy   (buf, msgData.data(), msgData.size());

  if(!m_parametrs.isEmpty())
     memcpy (buf += msgData.size(), m_parametrs.data(), m_parametrs.size());

  if(!m_largeDtgrm)
  {
    memcpy(m_outputBuffer               , m_header, m_headerSize  );
    memcpy(m_outputBuffer + m_headerSize, m_data  , m_dataSize - 8); // -8, t.k. paket ne fragmented
    m_outputBufferSize = m_headerSize + m_dataSize - 8;
  }
}


Generated by  Doxygen 1.6.0   Back to index