コード例 #1
0
// Saves the data to a file
void Utils::saveData2File(const std::string& fname,
                          const BinaryData& data)
{
    int fd = open(fname.c_str(),  O_WRONLY|O_APPEND|O_CREAT);
    if (fd < 0)
    {
        throw Exception(__FILE__, __LINE__, "Error %d in open(): %s",
                             errno, strerror(errno));
    }

    try
    {
        size_t wcount = write(fd, data.toVoid(), data.size());
        if (wcount != data.size())
        {
            throw Exception(__FILE__, __LINE__, "Error %d in write(): %s",
                                 errno, strerror(errno));
        }
    }
    catch(...)
    {
        close(fd);
        throw;
    }
    close(fd);

    // change permsiisons to rw for owner
    if (chmod (fname.c_str(), S_IRUSR|S_IWUSR) < 0)
    {
        throw Exception(__FILE__, __LINE__, "Error %d in chmod(): %s",
                             errno, strerror(errno));
    }
}
コード例 #2
0
// Recieves a data portion
void TCPSocket::recv(BinaryData& data)
{
    BOOST_ASSERT(m_sock.getDescriptor() >= 0);
    BOOST_ASSERT(data.empty() == false);

    try
    {
        int count =
            ::recv(m_sock.getDescriptor(), data.toVoid(), data.size(), 0);
        if (count < 0)
        {
            int saved_errno = errno;
            if (saved_errno == EPIPE)
            {
                throw ClosedConnection(__FILE__, __LINE__,
                                            getPeerName());
            }
            else
            {
                throw Exception(__FILE__, __LINE__,
                                     "Error %d in recv(): %s",
                                     saved_errno,
                                     strerror(saved_errno));
            }
        }
        else if (count == 0)
        {
            throw ClosedConnection(__FILE__, __LINE__,
                                        getPeerName());
        }
        else if (count < static_cast<int>(data.size()))
        {
            data.resize(count);
        }

        m_rater.updateInput(static_cast<size_t>(count));
    }
    catch(...)
    {
        disconnect();
        throw;
    }
}