input_windows.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package door
  2. import (
  3. "log"
  4. "sync/atomic"
  5. "syscall"
  6. )
  7. func Reader(d *Door) {
  8. // handle syscall.Handle, readerChannel *chan byte
  9. var handle syscall.Handle = syscall.Handle(d.Config.Comm_handle)
  10. defer func() {
  11. log.Printf("~Reader")
  12. if d.ReaderCanClose {
  13. d.wg.Done()
  14. }
  15. }()
  16. defer func() {
  17. if err := recover(); err != nil {
  18. log.Printf("Reader: %#v\n", err)
  19. }
  20. }()
  21. var buffer []byte = make([]byte, 1)
  22. WSA_Buffer := syscall.WSABuf{Len: 1, Buf: &buffer[0]}
  23. read := uint32(0)
  24. flags := uint32(0)
  25. for {
  26. err := syscall.WSARecv(handle, &WSA_Buffer, 1, &read, &flags, nil, nil)
  27. if err != nil {
  28. log.Printf("Reader ERR: %#v\n", err)
  29. close(d.readerChannel)
  30. if !d.Disconnect() {
  31. log.Println("Reader close writerChannel")
  32. atomic.StoreInt32(&d.Disconnected, 1)
  33. d.closeChannel <- struct{}{}
  34. return
  35. }
  36. break
  37. }
  38. if read == 1 {
  39. d.readerChannel <- buffer[0]
  40. } else {
  41. log.Printf("READ FAILED %d\n", read)
  42. close(d.readerChannel)
  43. if !d.Disconnect() {
  44. log.Println("Reader close writerChannel")
  45. atomic.StoreInt32(&d.Disconnected, 1)
  46. d.closeChannel <- struct{}{}
  47. return
  48. }
  49. return
  50. }
  51. }
  52. }