Esempio n. 1
0
Error
Serial::recv(void *msg_, size_t len_)
{
    char *msg = static_cast<char *> (msg_);
    int cc;
    Error err;

    if (!isConnected())
        return (ERR_NC);

    lockSend();
    while (len_ > 0) {
        cc = read(connection, msg, len_);
        if (cc <= 0) {
            if (errno != EWOULDBLOCK) {
                unlockSend();
                err = (Error) errno;
                return (err);
            }
            else
                cc = 0;
        }
        msg += cc;
        len_ -= cc;
    }
    unlockSend();
    return (0);
}
Esempio n. 2
0
bool NetHandler::sendIntSizedFdString(int fd, const string& str)
{
    // send to world
    //unsigned short len = htons(str.length());
    unsigned int ulen = (unsigned int) str.length();
    lockSend(); // we use this to lock the send procedure
    sendFdString(fd, (const char*) (&ulen), sizeof (ulen));
    bool succ = sendFdString(fd, str.c_str(), str.length());
    unlockSend();
    return succ;
}
Esempio n. 3
0
bool NetHandler::sendSizedFdString(int fd, const string& str)
{
    // send to web
    unsigned int len = str.length();
    char buf[10];
    sprintf(buf, "%05X", len);
    lockSend(); // we use this to lock the send procedure
    sendFdString(fd, buf, strlen(buf));
    bool succ = sendFdString(fd, str.c_str(), str.length());
    unlockSend();
    return succ;
}
Esempio n. 4
0
bool Connection::send( Packet& packet, const void* data,
                       const uint64_t dataSize )
{
    if( dataSize == 0 )
        return send( packet );

    if( dataSize <= 8 ) // fits in existing packet
    {
#ifndef NDEBUG // fill all eight bytes in debug to keep valgrind happy
        *(uint64_t*)((char*)(&packet) + packet.size - 8) = 0;
#endif
        memcpy( (char*)(&packet) + packet.size - 8, data, dataSize );
        return send( packet );
    }
    // else

    const uint64_t headerSize  = packet.size - 8;
    const uint64_t size        = headerSize + dataSize;
    if( size > EQ_ASSEMBLE_THRESHOLD )
    {
        // OPT: lock the connection and use two send() to avoid big memcpy
        packet.size = size;

        lockSend();
        const bool ret = ( send( &packet, headerSize, true ) &&
                           send( data,    dataSize,   true ));
        unlockSend();
        return ret;
    }
    // else

    char* buffer = (char*)alloca( size );

    memcpy( buffer,              &packet, headerSize );
    memcpy( buffer + headerSize, data,    dataSize );

    ((Packet*)buffer)->size = size;
    return send( buffer, size );
}