Guest User

Untitled

a guest
Jan 6th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. type Conn struct {
  2. wc io.WriteCloser
  3. tasks sync.WaitGroup
  4. // ... other internal state ...
  5. }
  6.  
  7. func NewConn(rwc io.ReadWriteCloser) *Conn {
  8. c := &Conn{wc: rwc}
  9. c.tasks.Add(1)
  10. go func() {
  11. defer c.tasks.Done()
  12. c.receive(rwc)
  13. }()
  14. return c
  15. }
  16.  
  17. func (c *Conn) receive(r io.Reader) {
  18. for {
  19. msg, err := decode(r)
  20. if err != nil {
  21. return
  22. }
  23. // ... Process message ...
  24. }
  25. }
  26.  
  27. func (c *Conn) Close() error {
  28. err := c.wc.Close()
  29. c.tasks.Wait()
  30. return err
  31. }
Add Comment
Please, Sign In to add comment