jeudi 3 décembre 2015

Why is this function false, from TCP server session class member socket_.is_Open

Observation

I built a demo application according to this server example using ASIO after I used C++11 std to replace everything originally in boost. The server can show that class member tcp_session::start() is called only after the client connects which is good indication that the server accepts the connection from the client.

However, I saw nothing received by handle_read while the clients sends a lot of data. Finally, I found that the stopped() is always true, which means the socket_.isOpen is false.

Question:

So I wonder whether I need to do anything to set the socket_ to be isOpen in start(), or the socket_ should be "Open" automatically (means connected)? Or is my understanding or assumption wrong?

I am using VS2015 and test on localhost.

Here are some relevant code:

class tcp_session : public subscriber, public  std::enable_shared_from_this<tcp_session> {
public:
void start() {
    std::cout<<"started"<<std::endl;
    channel_.join(shared_from_this());
    start_read();
    input_deadline_.async_wait(
        std::bind(&tcp_session::check_deadline, shared_from_this(), &input_deadline_)
        );
    await_output();

    output_deadline_.async_wait(
        std::bind(&tcp_session::check_deadline, shared_from_this(), &output_deadline_)
    );
}
private:    
bool stopped() const {
    return !socket_.is_open();// weird that it is still not open
}
void handle_read(const asio::error_code& ec) {
    if (stopped()) // it thinks it stopped and returned without processing
        return;

Aucun commentaire:

Enregistrer un commentaire