github.com/sagernet/sing-box@v1.9.0-rc.20/common/interrupt/conn.go (about)

     1  package interrupt
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/sagernet/sing/common/x/list"
     7  )
     8  
     9  /*type GroupedConn interface {
    10  	MarkAsInternal()
    11  }
    12  
    13  func MarkAsInternal(conn any) {
    14  	if groupedConn, isGroupConn := common.Cast[GroupedConn](conn); isGroupConn {
    15  		groupedConn.MarkAsInternal()
    16  	}
    17  }*/
    18  
    19  type Conn struct {
    20  	net.Conn
    21  	group   *Group
    22  	element *list.Element[*groupConnItem]
    23  }
    24  
    25  /*func (c *Conn) MarkAsInternal() {
    26  	c.element.Value.internal = true
    27  }*/
    28  
    29  func (c *Conn) Close() error {
    30  	c.group.access.Lock()
    31  	defer c.group.access.Unlock()
    32  	c.group.connections.Remove(c.element)
    33  	return c.Conn.Close()
    34  }
    35  
    36  func (c *Conn) ReaderReplaceable() bool {
    37  	return true
    38  }
    39  
    40  func (c *Conn) WriterReplaceable() bool {
    41  	return true
    42  }
    43  
    44  func (c *Conn) Upstream() any {
    45  	return c.Conn
    46  }
    47  
    48  type PacketConn struct {
    49  	net.PacketConn
    50  	group   *Group
    51  	element *list.Element[*groupConnItem]
    52  }
    53  
    54  /*func (c *PacketConn) MarkAsInternal() {
    55  	c.element.Value.internal = true
    56  }*/
    57  
    58  func (c *PacketConn) Close() error {
    59  	c.group.access.Lock()
    60  	defer c.group.access.Unlock()
    61  	c.group.connections.Remove(c.element)
    62  	return c.PacketConn.Close()
    63  }
    64  
    65  func (c *PacketConn) ReaderReplaceable() bool {
    66  	return true
    67  }
    68  
    69  func (c *PacketConn) WriterReplaceable() bool {
    70  	return true
    71  }
    72  
    73  func (c *PacketConn) Upstream() any {
    74  	return c.PacketConn
    75  }