MCPcopy Create free account
hub / github.com/XTLS/Go / Read

Method Read

conn.go:1525–1586  ·  view source on GitHub ↗

Read reads data from the connection. As Read calls Handshake, in order to prevent indefinite blocking a deadline must be set for both Read and Write before Read is called when the handshake has not yet completed. See SetDeadline, SetReadDeadline, and SetWriteDeadline.

(b []byte)

Source from the content-addressed store, hash-verified

1523// has not yet completed. See SetDeadline, SetReadDeadline, and
1524// SetWriteDeadline.
1525func (c *Conn) Read(b []byte) (int, error) {
1526 if c.DirectIn {
1527 return c.conn.Read(b)
1528 }
1529
1530 if c.DirectPre {
1531 if c.input.Len() != 0 {
1532 n, _ := c.input.Read(b)
1533 return n, nil
1534 }
1535 if c.rawInput.Len() != 0 {
1536 n, _ := c.rawInput.Read(b)
1537 return n, nil
1538 }
1539 c.DirectIn = true
1540 if c.SHOW {
1541 fmt.Println(c.MARK, "DirectIn = true")
1542 }
1543 return c.conn.Read(b)
1544 }
1545
1546 if err := c.Handshake(); err != nil {
1547 return 0, err
1548 }
1549 if len(b) == 0 {
1550 // Put this after Handshake, in case people were calling
1551 // Read(nil) for the side effect of the Handshake.
1552 return 0, nil
1553 }
1554
1555 c.in.Lock()
1556 defer c.in.Unlock()
1557
1558 for c.input.Len() == 0 {
1559 if err := c.readRecord(); err != nil {
1560 return 0, err
1561 }
1562 for c.hand.Len() > 0 {
1563 if err := c.handlePostHandshakeMessage(); err != nil {
1564 return 0, err
1565 }
1566 }
1567 }
1568
1569 n, _ := c.input.Read(b)
1570
1571 // If a close-notify alert is waiting, read it so that we can return (n,
1572 // EOF) instead of (n, nil), to signal to the HTTP response reading
1573 // goroutine that the connection is now closed. This eliminates a race
1574 // where the HTTP response reading goroutine would otherwise not observe
1575 // the EOF until its next read, by which time a client goroutine might
1576 // have already tried to reuse the HTTP connection for a new request.
1577 // See https://golang.org/cl/76400046 and https://golang.org/issue/3514
1578 if n != 0 && c.input.Len() == 0 && c.rawInput.Len() > 0 &&
1579 recordType(c.rawInput.Bytes()[0]) == recordTypeAlert {
1580 if err := c.readRecord(); err != nil {
1581 return n, err // will be io.EOF on closeNotify
1582 }

Callers 3

ReadMethod · 0.45
sendSessionTicketsMethod · 0.45
expandLabelMethod · 0.45

Calls 4

HandshakeMethod · 0.95
readRecordMethod · 0.95
recordTypeTypeAlias · 0.85

Tested by

no test coverage detected