C ++从iostream故障位集继承

时间:2019-05-16 01:27:59

标签: c++ inheritance iostream winsock2

从哪里开始?我想是开始。

我是套接字编程的新手。我想试一试。 After research,我发现似乎没有标准的C ++ oop套接字库(there were some third party libraries,但我还是决定自己创建一个,因为那样吧,自从无论如何,这只是一个个人项目),所以我决定自己做。

我决定将新库与iostream集成。当然,this probably isn't a good idea usually,但我知道1)我有一个不错的理由,2)无论如何,这只是一个学术练习。至少学会如何做到这一点也无妨。

因此,I did a little digging提出了以下设计:

class NetworkStream : public std::iostream {
    public: 
        // class network streambuff
        // should handle the actual writing
        class NetworkBuff : public std::streambuf {
            private:
                SOCKET socket; // I'm working with winsock2, btw
            public:
                NetworkBuff(SOCKET s);

            protected:
                // out

                virtual std::streamsize xsputn(const char* buffer, std::streamsize size);

                virtual std::streamsize overflow(char buffer);

                virtual int underflow();

                virtual std::streamsize xsgetn(char* buffer, std::streamsize size);

        };

        // constructors
        NetworkStream(SOCKET socket); // sets up the socket in NetworkBuff
        virtual ~NetworkStream();

    }; // end network stream

该NetworkStream对象应该处理网络上的实际读取和写入。连接由更高级别的代码维护(因此,这是指从NetworkStream继承的对象)。该代码有效(据我所知,无论如何),并且与我的问题无关,因此我省略了它。我只是提到它,以便您了解我的设计。

无论如何,我的新流对象的实际实现看起来像这样:

// class network streambuff
// should handle the actual writing

//constructor
NetworkStream::NetworkBuff::NetworkBuff(SOCKET s) {
    socket = s;
}

// out

std::streamsize NetworkStream::NetworkBuff::xsputn(const char* buffer, std::streamsize size) {
    // well, let's send the data
    int result = send(socket,buffer,static_cast<int>(size),0);

    // if that didn't work, throw an error
    if(result == SOCKET_ERROR) throw("Send Failure: " + WSAGetLastError()); 

    // NOTE: I realized after I wrote this that this throw may be useless, 
    // since I think iostream catches any errors thrown at this level, but 
    // just in case

    // and pass through to streambuff, because, well, it can't hurt
    return std::streambuf::xsputn(buffer, size);
}

// basically do the same thing as before...
std::streamsize NetworkStream::NetworkBuff::overflow(char buffer) {
    // well, let's send the data
    int result = send(socket,buffer,sizeof(buffer),0);

    // if that didn't work, throw an error
    if(result == SOCKET_ERROR) throw("Send Failure: " + WSAGetLastError()); 

    // and pass through to streambuff, because, well, it can't hurt
    return std::streambuf::overflow(buffer);
}

据我所知,这就像一种魅力。我逐步调试程序,然后调用xsputn()。所以我可以做这样的事情:

std::string data = "Hello, world!";
networkstream << data;

它被调用。我认为它发送成功。它不会单步执行调试器,结果没有错误。但是,我尚未对其进行全面测试,因为它是我的接收功能无法正常工作:

std::streamsize NetworkStream::NetworkBuff::xsgetn(char* buffer, std::streamsize size) {
    // well, let's read the data
    int result = recv(socket,buffer,static_cast<int>(size),0);

    // if that didn't work, throw an error
    if(result == SOCKET_ERROR) throw("Receive Failure: " + WSAGetLastError());

    // Now this I think is wrong, specifically comparing against SOCKET_ERROR.
    // That's not my problem, though. My problem is that this function seems to 
    // never get called, so a wrong comparison doesn't matter yet anyway

    // and pass through to streambuff, because, well, it can't hurt
    return std::streambuf::xsgetn(buffer, size);
}

// Now this guy, I'm pretty extra sure is probably wrong, but it's what I got. I 
// couldn't find a good example of using underflow, so I did my best from the 
// research I did find
int NetworkStream::NetworkBuff::underflow() {
    // well, let's read the data
    int result = recv(socket,gptr(),sizeof(*gptr()),0);

    // if that didn't work, throw an error
    if(result == SOCKET_ERROR) throw("Recieve Failure: " + WSAGetLastError());

    // and pass through to streambuff, because, well, it can't hurt
    return std::streambuf::underflow();
}

它可以很好地编译。但是,当我尝试使用它时:

std::string data = "";
networkstream >> data; // should wait for data on the network

似乎没有任何假装。不,实际上,根据我的调试器的说法,它似乎设置了一个失败位,忽略了我实际上重载的函数,并且像什么都没有发生一样继续执行。

因此,在一个段落中,我的问题是:我的下溢/ xsgetn函数到底出了什么错,导致其失败并从本质上忽略了我的代码?我确定我做错了什么,但是究竟是什么?

1 个答案:

答案 0 :(得分:0)

事实证明,我不能只是通过。那不是streambuf的设计方式。

据我所知(如果我错了,请告诉我),streambuf实际上不辜负它的名字。它应该是流的缓冲区。

无论如何,对我来说还不清楚的是streambuf,虽然它应该处理缓冲,但是它没有实际的内部方式来保存或处理数据。这就是超负荷的全部要点。您应该处理缓冲。您不能通过(再说一次,如果我错了,请纠正)到父母。那不是它的设计方式。

您需要的(或者我想,我需要的)是下溢的良好实现,并且可以正确实现。幸运的是,经过更多的挖掘,我找到了一个。长话短说,我是这样的:

// Shoutout to http://www.voidcn.com/article/p-vjnlygmc-gy.html where I found out
// how to do this proper
std::streambuf::int_type NetworkStream::NetworkBuff::underflow() {
    const int readsize = 30;

    // first, check to make sure the buffer is not exausted:
    if(gptr() < egptr()) {
        return traits_type::to_int_type(*gptr());
    }

    // Now, let's read...

    // btw, inputbuffer is a data member that is a std::string
    // clear the buffer
    inputbuffer.clear();
    inputbuffer.resize(readsize);

    // let's read
    int bytesread = recv(socket,&inputbuffer[0],static_cast<int>(readsize)); 

    // return the end of file if we read no bytes
    if(bytesread == 0) {
        return traits_type::eof();
    }

    // set the pointers for the buffer...
    setg(&inputbuffer[0],&inputbuffer[0],&inputbuffer[readsize-1]);

    // finally, let's return the data type
    return traits_type::to_int_type(*gptr());

}