github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/tcp/stream-net.go (about)

     1  /* For license and copyright information please see the LEGAL file in the code repository */
     2  
     3  package tcp
     4  
     5  import (
     6  	"net"
     7  	"time"
     8  )
     9  
    10  /*
    11  ********** net.Conn interface **********
    12  // TODO::: concurrency safe??
    13  */
    14  
    15  // Read is not concurrent safe. Use just by one goroutine.
    16  func (s *Socket) Read(b []byte) (n int, err error) {
    17  	err = s.checkSocket()
    18  	if err != nil {
    19  		return
    20  	}
    21  
    22  	if !s.recv.buf.Full() {
    23  		err = s.blockInSelect()
    24  	}
    25  	// TODO::: check above error
    26  	n, err = s.recv.buf.Read(b)
    27  	return
    28  }
    29  func (s *Socket) Write(b []byte) (n int, err error) {
    30  	n, err = s.Unmarshal(b)
    31  	return
    32  }
    33  func (s *Socket) Close() (err error) {
    34  	err = s.checkSocket()
    35  	if err != nil {
    36  		return
    37  	}
    38  
    39  	err = s.close()
    40  	return
    41  }
    42  func (s *Socket) LocalAddr() net.Addr {
    43  	var err = s.checkSocket()
    44  	if err != nil {
    45  		return nil
    46  	}
    47  	return &net.TCPAddr{
    48  		IP:   net.IP(s.connection.LocalAddr()),
    49  		Port: int(s.sourcePort),
    50  	}
    51  }
    52  func (s *Socket) RemoteAddr() net.Addr {
    53  	var err = s.checkSocket()
    54  	if err != nil {
    55  		return nil
    56  	}
    57  	return &net.TCPAddr{
    58  		IP:   net.IP(s.connection.RemoteAddr()),
    59  		Port: int(s.destinationPort),
    60  	}
    61  }
    62  func (s *Socket) SetDeadline(t time.Time) (err error) {
    63  	var d = untilTo(t)
    64  	s.SetTimeout(d)
    65  	return
    66  }
    67  func (s *Socket) SetReadDeadline(t time.Time) (err error) {
    68  	var d = untilTo(t)
    69  	err = s.SetReadTimeout(d)
    70  	return
    71  }
    72  func (s *Socket) SetWriteDeadline(t time.Time) (err error) {
    73  	var d = untilTo(t)
    74  	err = s.SetWriteTimeout(d)
    75  	return
    76  }