Example #1
0
unsigned long iftonl(const char* interface)
{
    int pos;
    int len;
    unsigned long a;
    unsigned long b;
    unsigned long c;
    unsigned long d;
    unsigned long addr;
    unsigned nread;
    
    nread = 0;
    pos = 0;
    len = strlen(interface);
    
    a = BufferToUInt64(interface + pos, len - pos, &nread);
    if (nread == 0 || a > 255)
        return INADDR_NONE;
    
    pos += nread;
    if (interface[pos++] != '.')
        return INADDR_NONE;
    
    b = BufferToUInt64(interface + pos, len - pos, &nread);
    if (nread == 0 || b > 255)
        return INADDR_NONE;
    
    pos += nread;
    if (interface[pos++] != '.')
        return INADDR_NONE;

    c = BufferToUInt64(interface + pos, len - pos, &nread);
    if (nread == 0 || c > 255)
        return INADDR_NONE;
    
    pos += nread;
    if (interface[pos++] != '.')
        return INADDR_NONE;

    d = BufferToUInt64(interface + pos, len - pos, &nread);
    if (nread == 0 || d > 255)
        return INADDR_NONE;
    
    pos += nread;
    if (interface[pos] != '\0' &&
        interface[pos] != ':')
        return INADDR_NONE;
    
    addr = (d & 0xff) << 24 | (c & 0xff) << 16 | (b & 0xff) << 8 | (a & 0xff);
    return addr;
}
Example #2
0
uint64_t QuorumDatabase::GetUint64(const char* name)
{
    ReadBuffer  key(name);
    ReadBuffer  value;
    uint64_t    u64;
    unsigned    nread;
    int         ret;

    ret = paxosShard->Get(key, value);
    if (!ret)
        return false;

    nread = 0;
    u64 = BufferToUInt64(value.GetBuffer(), value.GetLength(), &nread);
    if (nread != value.GetLength())
    {
        Log_Trace();
        u64 = 0;
    }

    return u64;
}
Example #3
0
void MessageConnection::OnRead()
{
    bool            yield, closed;
    unsigned        pos, msglength, nread, msgbegin, msgend, required;
    uint64_t        start;
    Stopwatch       sw;
    ReadBuffer      msg;

    if (!readActive)
    {
        if (resumeRead.IsActive())
            STOP_FAIL(1, "Program bug: resumeRead.IsActive() should be false.");
        EventLoop::Add(&resumeRead);
        return;
    }

    sw.Start();

    Log_Trace("Read buffer: %B", tcpread.buffer);

    tcpread.requested = IO_READ_ANY;
    pos = 0;
    start = NowClock();
    yield = false;

    while(true)
    {
        msglength = BufferToUInt64(tcpread.buffer->GetBuffer() + pos,
                                   tcpread.buffer->GetLength() - pos, &nread);

        if (msglength > tcpread.buffer->GetSize() - NumDigits(msglength) - 1)
        {
            Log_Trace();
            required = msglength + NumDigits(msglength) + 1;
            if (required > MESSAGING_MAX_SIZE)
            {
                Log_Trace();
                OnClose();
                return;
            }
            tcpread.buffer->Allocate(required);
            break;
        }

        if (nread == 0 || (unsigned) tcpread.buffer->GetLength() - pos <= nread)
            break;

        if (tcpread.buffer->GetCharAt(pos + nread) != ':')
        {
            Log_Trace("Message protocol error");
            OnClose();
            return;
        }

        msgbegin = pos + nread + 1;
        msgend = pos + nread + 1 + msglength;

        if ((unsigned) tcpread.buffer->GetLength() < msgend)
        {
            // read more
            //tcpread.requested = msgend - pos;
            break;
        }

        msg.SetBuffer(tcpread.buffer->GetBuffer() + msgbegin);
        msg.SetLength(msglength);
        closed = OnMessage(msg);
        if (closed)
            return;

        pos = msgend;

        // if the user called Close() in OnMessageRead()
        if (state != CONNECTED)
            return;

        if (tcpread.buffer->GetLength() == msgend)
            break;

        if (NowClock() - start >= YIELD_TIME || !readActive)
        {
            // let other code run every YIELD_TIME msec
            yield = true;
            if (resumeRead.IsActive())
                STOP_FAIL(1, "Program bug: resumeRead.IsActive() should be false.");
            EventLoop::Add(&resumeRead);
            break;
        }
    }

    if (pos > 0)
    {
        memmove(tcpread.buffer->GetBuffer(), tcpread.buffer->GetBuffer() + pos,
                tcpread.buffer->GetLength() - pos);
        tcpread.buffer->Shorten(pos);
    }

    if (state == CONNECTED
            && !tcpread.active && !yield)
        IOProcessor::Add(&tcpread);

    sw.Stop();
    Log_Trace("time spent in OnRead(): %U", sw.Elapsed());
}