Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http_conn.cpp中write()好像有问题 #38

Closed
luchao2424631502 opened this issue Dec 19, 2020 · 5 comments
Closed

http_conn.cpp中write()好像有问题 #38

luchao2424631502 opened this issue Dec 19, 2020 · 5 comments

Comments

@luchao2424631502
Copy link

bool http_conn::write()
{
    int temp = 0;

    if (bytes_to_send == 0)
    {
        modfd(m_epollfd, m_sockfd, EPOLLIN, m_TRIGMode);
        init();
        return true;
    }

    //int mv0len = m_iv[0].iov_len;

    while (1)
    {
        temp = writev(m_sockfd, m_iv, m_iv_count);

        if (temp < 0)
        {
            if (errno == EAGAIN)
            {
                modfd(m_epollfd, m_sockfd, EPOLLOUT, m_TRIGMode);
                return true;
            }
            unmap();
            return false;
        }

        bytes_have_send += temp;
        bytes_to_send -= temp;

        if (bytes_have_send >= m_iv[0].iov_len)
        {
            m_iv[0].iov_len = 0;
            m_iv[1].iov_base = m_file_address + (bytes_have_send - m_write_idx);
            m_iv[1].iov_len = bytes_to_send;
            //printf("1: >= \n");
        }
        else
        {
            m_iv[0].iov_base = m_write_buf + bytes_have_send;
            m_iv[0].iov_len = m_iv[0].iov_len - bytes_have_send;
            //printf("2: < \n");
        }

        // if (bytes_have_send >= mv0len)
        // {
        //     m_iv[0].iov_len = 0;
        //     m_iv[1].iov_base = m_file_address + (bytes_have_send - m_write_idx);
        //     m_iv[1].iov_len = bytes_to_send;
        // }
        // else 
        // {
        //     m_iv[0].iov_base = m_write_buf + bytes_have_send;
        //     m_iv[0].iov_len = m_iv[0].iov_len - temp;
        // }

        if (bytes_to_send <= 0)
        {
            unmap();
            modfd(m_epollfd, m_sockfd, EPOLLIN, m_TRIGMode);

            if (m_linger)
            {
                init();
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

使用作者的wirtev后的处理方式跑,图片视屏传输正常,但是printf("2\n")没有执行过,也就是writev每次都把m_iv[0]的内容一次发送完毕,但是看作者的if(bytes_have_send >= m_iv[0].iov_len) else{}这里的else逻辑明显有问题,只是因为如上原因没有执行过,
假设writev第一次没有发出iov0的全部内容if (bytes_have_send >= m_iv[0].iov_len)不执行,则第二论的while()的if (bytes_have_send >= m_iv[0].iov_len)这里的m_iv[0].iov_len明显不是iov0的全部长度,按照博主的判断流程这里应该修改我注视的代码,不知道博主怎么认为的.

@Ventery
Copy link

Ventery commented Apr 18, 2021

确实,我看到这里也一头雾水。搜了下就进来了,我手头代码照你的改改先试试

@luchao2424631502
Copy link
Author

确实,我看到这里也一头雾水。搜了下就进来了,我手头代码照你的改改先试试

ok

@Ventery
Copy link

Ventery commented Apr 21, 2021

确实,我看到这里也一头雾水。搜了下就进来了,我手头代码照你的改改先试试

ok

改好了现在可以正常传输图片了。
每次writev后需要调整缓冲区,原作者没有处理好这个问题。可能是因为他的套接字缓冲设置的比较大才一次能写完吧,那就查不到这个错误了

经过测试确实是socket写缓冲太小了,改大一点就ok。不过改缓冲大小也占用内存资源,一般都是分多次传输比较好

@luchao2424631502
Copy link
Author

确实,我看到这里也一头雾水。搜了下就进来了,我手头代码照你的改改先试试

ok

改好了现在可以正常传输图片了。
每次writev后需要调整缓冲区,原作者没有处理好这个问题。可能是因为他的套接字缓冲设置的比较大才一次能写完吧,那就查不到这个错误了

经过测试确实是socket写缓冲太小了,改大一点就ok。不过改缓冲大小也占用内存资源,一般都是分多次传输比较好

我改的正确吗?时间太长记不清楚了

@qinguoyi
Copy link
Owner

确实,我看到这里也一头雾水。搜了下就进来了,我手头代码照你的改改先试试

ok

改好了现在可以正常传输图片了。 每次writev后需要调整缓冲区,原作者没有处理好这个问题。可能是因为他的套接字缓冲设置的比较大才一次能写完吧,那就查不到这个错误了

经过测试确实是socket写缓冲太小了,改大一点就ok。不过改缓冲大小也占用内存资源,一般都是分多次传输比较好

如有空,提交个mr看下,tks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants