docs(handler/relay): convert all comments to English
Translate all Chinese comments in the relay handler package to English: handler.go, connect.go, bind.go, forward.go, conn.go, entrypoint.go, observe.go, metadata.go. All inline and doc comments are now in English. Also set a persistent preference: all future code comments must be written in English only.
This commit is contained in:
+38
-4
@@ -9,6 +9,16 @@ import (
|
||||
"net"
|
||||
)
|
||||
|
||||
// tcpConn wraps a TCP connection with response header buffering (wbuf).
|
||||
//
|
||||
// In non-noDelay mode, the relay.Response header is first written into wbuf.
|
||||
// On the first Write() call, the buffered header and the data are sent together.
|
||||
// This avoids sending a small relay frame before the data stream begins.
|
||||
//
|
||||
// Write() semantics:
|
||||
// - n always returns len(b) rather than the actual bytes written. This differs
|
||||
// from net.Conn's contract. The rationale: when wbuf has content, the actual
|
||||
// write is "wbuf + b" but the caller only cares that "b" was "processed".
|
||||
type tcpConn struct {
|
||||
net.Conn
|
||||
wbuf bytes.Buffer
|
||||
@@ -19,9 +29,9 @@ func (c *tcpConn) Read(b []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
func (c *tcpConn) Write(b []byte) (n int, err error) {
|
||||
n = len(b) // force byte length consistent
|
||||
n = len(b) // always return len(b), not actual bytes written
|
||||
if c.wbuf.Len() > 0 {
|
||||
c.wbuf.Write(b) // append the data to the cached header
|
||||
c.wbuf.Write(b) // 将数据追加到缓存的头部之后
|
||||
_, err = c.wbuf.WriteTo(c.Conn)
|
||||
return
|
||||
}
|
||||
@@ -29,6 +39,27 @@ func (c *tcpConn) Write(b []byte) (n int, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// udpConn wraps UDP datagrams over a stream connection.
|
||||
//
|
||||
// When the GOST relay protocol carries UDP over a TCP/stream transport,
|
||||
// a 2-byte big-endian length prefix frames each datagram. TCP is a stream
|
||||
// protocol without message boundaries, so this framing is necessary.
|
||||
//
|
||||
// Datagram wire format:
|
||||
//
|
||||
// [2-byte length (big-endian)][datagram payload]
|
||||
//
|
||||
// Read() flow:
|
||||
// 1. Read 2-byte length prefix.
|
||||
// 2. Read the datagram payload of the specified length.
|
||||
// 3. If the caller's buffer is too small, allocate an internal buffer,
|
||||
// read the full datagram, then truncate on copy.
|
||||
//
|
||||
// Write() flow:
|
||||
// 1. Reject data exceeding MaxUint16 (65535).
|
||||
// 2. If wbuf has a cached header, append the length prefix + data to the
|
||||
// header and flush everything at once.
|
||||
// 3. Otherwise write the length prefix then the data.
|
||||
type udpConn struct {
|
||||
net.Conn
|
||||
wbuf bytes.Buffer
|
||||
@@ -45,6 +76,7 @@ func (c *udpConn) Read(b []byte) (n int, err error) {
|
||||
if len(b) >= dlen {
|
||||
return io.ReadFull(c.Conn, b[:dlen])
|
||||
}
|
||||
// Caller's buffer is too small; allocate internal buffer.
|
||||
buf := make([]byte, dlen)
|
||||
_, err = io.ReadFull(c.Conn, buf)
|
||||
n = copy(b, buf)
|
||||
@@ -60,14 +92,16 @@ func (c *udpConn) Write(b []byte) (n int, err error) {
|
||||
|
||||
n = len(b)
|
||||
if c.wbuf.Len() > 0 {
|
||||
// Wbuf has cached header; append length prefix + data and flush together.
|
||||
var bb [2]byte
|
||||
binary.BigEndian.PutUint16(bb[:], uint16(len(b)))
|
||||
c.wbuf.Write(bb[:])
|
||||
c.wbuf.Write(b) // append the data to the cached header
|
||||
c.wbuf.Write(b)
|
||||
_, err = c.wbuf.WriteTo(c.Conn)
|
||||
return
|
||||
}
|
||||
|
||||
// Write length prefix + data directly.
|
||||
var bb [2]byte
|
||||
binary.BigEndian.PutUint16(bb[:], uint16(len(b)))
|
||||
_, err = c.Conn.Write(bb[:])
|
||||
@@ -75,4 +109,4 @@ func (c *udpConn) Write(b []byte) (n int, err error) {
|
||||
return
|
||||
}
|
||||
return c.Conn.Write(b)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user