github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/tcp/socket-net.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package tcp
     4  
     5  import (
     6  	"net"
     7  	"time"
     8  
     9  	"../protocol"
    10  )
    11  
    12  /*
    13  ********** net.Conn interface **********
    14  // TODO::: concurrency??
    15  */
    16  
    17  // Read is not concurrent safe. Use just by one goroutine.
    18  func (s *Socket) Read(b []byte) (n int, err error) {
    19  	err = s.checkSocket()
    20  	if err != nil {
    21  		return
    22  	}
    23  
    24  	if !s.recv.buf.Full() {
    25  		err = s.blockInSelect()
    26  	}
    27  	n, err = s.recv.buf.Read(b)
    28  	return
    29  }
    30  func (s *Socket) Write(b []byte) (n int, err error) {
    31  	err = s.checkSocket()
    32  	if err != nil {
    33  		return
    34  	}
    35  
    36  	for len(b) > 0 {
    37  		select {
    38  		case <-s.writeTimer.Signal():
    39  			// err =
    40  			return
    41  		default:
    42  			var sendNumber int
    43  			sendNumber, err = s.sendPayload(b)
    44  			if err != nil {
    45  				return
    46  			}
    47  			n += sendNumber
    48  			b = b[sendNumber:]
    49  		}
    50  	}
    51  	return
    52  }
    53  func (s *Socket) Close() (err error) {
    54  	err = s.checkSocket()
    55  	if err != nil {
    56  		return
    57  	}
    58  
    59  	err = s.close()
    60  	return
    61  }
    62  func (s *Socket) LocalAddr() net.Addr  { return nil }
    63  func (s *Socket) RemoteAddr() net.Addr { return nil }
    64  func (s *Socket) SetDeadline(t time.Time) (err error) {
    65  	var d protocol.Duration
    66  	if !t.IsZero() {
    67  		d = protocol.Duration(time.Until(t))
    68  		if d == 0 {
    69  			d = -1 // don't confuse deadline right now with no deadline
    70  		}
    71  	}
    72  	s.SetTimeout(d)
    73  	return
    74  }
    75  func (s *Socket) SetReadDeadline(t time.Time) (err error) {
    76  	var d protocol.Duration
    77  	if !t.IsZero() {
    78  		d = protocol.Duration(time.Until(t))
    79  		if d == 0 {
    80  			d = -1 // don't confuse deadline right now with no deadline
    81  		}
    82  	}
    83  	err = s.SetReadTimeout(d)
    84  	return
    85  }
    86  func (s *Socket) SetWriteDeadline(t time.Time) (err error) {
    87  	var d protocol.Duration
    88  	if !t.IsZero() {
    89  		d = protocol.Duration(time.Until(t))
    90  		if d == 0 {
    91  			d = -1 // don't confuse deadline right now with no deadline
    92  		}
    93  	}
    94  	err = s.SetWriteTimeout(d)
    95  	return
    96  }