Example #1
0
//---------------------------------------------------------------------------
void TcpReceiveMessage(void)
{
  int rl;
  TSyslogdTcpConn * c;
  MSocket * p;
  for(int i=0; i<tcp_cons->Count; i++)
  {
    c = (TSyslogdTcpConn *)tcp_cons->Items[i];
    p = c->Socket;

    if( p->Poll(true, false, false, 0) ) // wait 0 msec
    {
      rl = p->ReadLength();

      if( rl > 0 )
      {
        // Ready to receive rl bytes
        if( p->Read(c->GetBufferSize(rl), rl) )
        {
          c->DataSize += rl;
        }
        else
        {
          if( p->bytes > 0 && p->bytes < rl )
          {
            c->DataSize += p->bytes;
            WriteToLogError("WARNING\tTCP received %ld, wait %ld, error=%ld", p->bytes, rl, p->errcode);
          }
          else
          {
            if( p->errcode==WSAESHUTDOWN )
            {
              WriteToLogError("WARNING\tTCP shutdown from %s", p->GetRemoteAddrPort().c_str());
              TcpDeleteConnection(i--);
              continue;
            }
            else
            {
              WriteToLogError("ERROR\tTCP read(rl=%ld) from %s\tbytes=%ld error=%ld",
                p->GetRemoteAddrPort().c_str(), rl, p->bytes, p->errcode);
            }
          }
        }
      }
      else // Readed 0 bytes !
      {
        BYTE b;
        if( ! p->Read(&b, 1) )
        {
          if( p->errcode==0 && p->bytes==0 )
          {
            // Good disconnect
            TcpDeleteConnection(i--);
            continue;
          }
          else
          {
            WriteToLogError("ERROR\tTCP error from %s: %s [%d]",
              p->GetRemoteAddrPort().c_str(), p->GetErrorMessageEng().c_str(), p->errcode);
            TcpDeleteConnection(i--);
            continue;
          }
        }
      }
    } // Poll
    if( c->DataSize > 0 )
    {
      int i, start;
      for(i=start=0; i<c->DataSize; i++)
      {
        // find end of message
        if( c->Data[i]==0 || c->Data[i]=='\n' || c->Data[i]=='\r' )
        {
          if( start < i )
          {
            // process messages

            // NULL terminate syslog message
            c->Data[i] = 0;

            WriteToLogRawMessage((char *)(c->Data + start));

            TSyslogMessage sm;
            sm.FromStringSyslogd((char *)(c->Data + start), i - start, &c->Socket->destAddr);
            if( ProcessMessageRules(&sm) )
            {
              // option is not set: "Ignore (do not save to the default file "syslog")"
              TStorageFile * sf = fdb->Get(0);
              if( sf )
                if( ! sf->Save( sm.ToString() ) )
                  WriteToLogError("ERROR\tSave message to file: %s", sf->GetFileName().c_str());
            }
          }
          start = i + 1;
        }
      }
      // half of syslog message in c->Data ?
      if( start < i )
      {
        // Save to buffer start
        memmove(c->Data, c->Data + start, i - start);
        c->DataSize = i - start;
      }
      else
      {
        c->DataSize = 0;
      }
    }
  } // End for
}