dimanche 24 décembre 2017

std::streambuf for tcp socket doesn't send data

I trying to write buffered std::streambuf for socket. I already wrote unbuffered std::streambuf. I don't understand why buffered streambuf doesn't work.

    Socket::StreamBuf::StreamBuf(const IO &io, tcp::Socket *socket) :
            socket(socket),
            // wb is std::vector<char>
            wb(io.writing)  // set size
    {
        // write
        char_type *buf = wb.data();
        setp(buf, buf + (wb.size() - 1));

    };

    int Socket::StreamBuf::overflow(int c) {
        if(c != traits_type::eof()) {
            *pptr() = c;
            pbump(1);

            if(sync() == -1) return traits_type::eof();
        }

        return c;
    }

    int Socket::StreamBuf::sync() {
        if(pptr() && pptr() > pbase()) {
            Int32 sz = Int32(pptr() - pbase());

            // int Socket::send(char *data, int size)
            // return sent bytes count
            if (socket->send(pbase(), sz) == sz) {
                pbump(-sz);
                return 0;
            }
        }

        return -1;
    }

Aucun commentaire:

Enregistrer un commentaire