Пример #1
0
static shared_ptr<PixelTransferBuffer> addressToPTB(const NetAddress& addr) {
    std::string str = std::string("HTTP://") + addr.ipString() + ":" + format("%d", addr.port());
    debugAssert(str.size() <= 28);
    QRcode* qrcode = encode(str.c_str(), (int)str.size());

    const int N = qrcode->width;
    shared_ptr<PixelTransferBuffer> buffer = CPUPixelTransferBuffer::create(N, N, ImageFormat::L8());
    
    unorm8* dst = reinterpret_cast<unorm8*>(buffer->mapWrite());
    const unsigned char* src = qrcode->data;

    const unorm8 BLACK = unorm8::fromBits(0x00);
    const unorm8 WHITE = unorm8::fromBits(0xFF);

    for (int i = 0; i < N * N; ++i) {
        // The bytes are in the opposite convention
        dst[i] = (src[i] & 1) ? BLACK : WHITE;
    }
    
    dst = NULL;
    buffer->unmap();
    QRcode_free(qrcode);

    return buffer;
}
Пример #2
0
//-----------------------------------------------------------------------------
bool TCPSocket::connect(const NetAddress& destination)
{
    int sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (sd <= 0)
    {
        os::printf("Failed to open socket\n");
        m_socket = 0;

        return false;
    }

    m_socket = sd;

    sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_addr.s_addr =  htonl(destination.address());
    address.sin_port = htons(destination.port());

    if (::connect(sd, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
    {
        os::printf("Failed to connect socket\n");
        close();

        return false;
    }

    return true;
}