comment non-bloquing read

This commit is contained in:
xdrm-brackets 2018-05-04 07:34:58 +02:00
parent ab351de382
commit b713011e7b
2 changed files with 15 additions and 14 deletions

View File

@ -21,7 +21,7 @@ func main(){
// if receive message -> send it back // if receive message -> send it back
sender <- &msg sender <- &msg
// sender <- nil sender <- nil
} }

View File

@ -131,21 +131,21 @@ func (c *client) reader(){
} }
/* (2) Wait for available data */ /* (2) Wait for available data */
c.io.sock.SetReadDeadline(time.Now().Add(time.Microsecond*10)) // c.io.sock.SetReadDeadline(time.Now().Add(10*time.Microsecond))
_, err := c.io.reader.Peek(1) // _, err := c.io.reader.Peek(1)
// timeout -> continune checking // // timeout -> continune checking
if neterr, ok := err.(net.Error); ok && neterr.Timeout() { // if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
continue // time.Sleep(10*time.Microsecond)
// continue
// }
} // // another error -> stop reading
// if err != nil {
// break
// }
// another error -> stop reading // c.io.sock.SetReadDeadline(time.Time{}) // remove timeout
if err != nil {
break
}
c.io.sock.SetReadDeadline(time.Time{}) // remove timeout
/* (3) Parse message */ /* (3) Parse message */
msg, err := readMessage(c.io.reader) msg, err := readMessage(c.io.reader)
@ -238,7 +238,7 @@ func (c *client) close(status MessageError){
Data: make([]byte, 8), Data: make([]byte, 8),
} }
binary.BigEndian.PutUint16(msg.Data, uint16(status)) binary.BigEndian.PutUint16(msg.Data, uint16(status))
msg.Data = append(msg.Data, []byte("Closing")...) msg.Data = append(msg.Data, []byte(" close")...)
msg.Size = uint( len(msg.Data) ) msg.Size = uint( len(msg.Data) )
/* Send message */ /* Send message */
@ -247,6 +247,7 @@ func (c *client) close(status MessageError){
} }
/* (2) Close socket */ /* (2) Close socket */
c.io.sock.SetReadDeadline(time.Now())
time.Sleep(time.Second * 3) time.Sleep(time.Second * 3)
c.io.sock.Close() c.io.sock.Close()