Giter Site home page Giter Site logo

Comments (3)

cyfdecyf avatar cyfdecyf commented on June 18, 2024

Sigh, I don't remember the reason why I do not check the written bytes.

Maybe I mistaken File.Write's doc for TCPConn.Write. Or maybe I thought it should confirm to the io.Writer interface. Both of these two Write return a non-nil error if it returns n < len(p).

It would be awkward to use loop for every socket write, but I am not sure TCPConn.Write has the same guarantee now ...

I'll have further check on this issue.

from shadowsocks-go.

lixin9311 avatar lixin9311 commented on June 18, 2024

http://golang.org/src/net/fd_unix.go?h=Write#L317
line 331
if nn == len(p) {
break
}
If the write operation succeeds the n that TCPConn.Write returns should be equal to len(buf), otherwise a Non-nil err should be returned.
I think Google has already implemented the loop.

from shadowsocks-go.

cyfdecyf avatar cyfdecyf commented on June 18, 2024

Thanks @lixin9311 for pointing this out.

I was actually reading the Write function when posting the last reply, and should have read the function before when I tried to port Go's net package to Lua in luanet. But I just ignored the loop, symptom for working for too long these days ...

For future reference, I'll include the write function here.

func (fd *netFD) Write(p []byte) (nn int, err error) {
    if err := fd.writeLock(); err != nil {
        return 0, err
    }
    defer fd.writeUnlock()
    if err := fd.pd.PrepareWrite(); err != nil {
        return 0, &OpError{"write", fd.net, fd.raddr, err}
    }
    for {
        var n int
        n, err = syscall.Write(int(fd.sysfd), p[nn:])
        if n > 0 {
            nn += n
        }
        if nn == len(p) {
            break
        }
        if err == syscall.EAGAIN {
            if err = fd.pd.WaitWrite(); err == nil {
                continue
            }
        }
        if err != nil {
            n = 0
            break
        }
        if n == 0 {
            err = io.ErrUnexpectedEOF
            break
        }
    }
    if err != nil {
        err = &OpError{"write", fd.net, fd.raddr, err}
    }
    return nn, err
}

from shadowsocks-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.