1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package door
- import (
- "log"
- "sync/atomic"
- "syscall"
- )
- func Reader(d *Door) {
- // handle syscall.Handle, readerChannel *chan byte
- var handle syscall.Handle = syscall.Handle(d.Config.Comm_handle)
- defer func() {
- log.Printf("~Reader")
- if d.ReaderCanClose {
- d.wg.Done()
- }
- }()
- defer func() {
- if err := recover(); err != nil {
- log.Printf("Reader: %#v\n", err)
- }
- }()
- var buffer []byte = make([]byte, 1)
- WSA_Buffer := syscall.WSABuf{Len: 1, Buf: &buffer[0]}
- read := uint32(0)
- flags := uint32(0)
- for {
- err := syscall.WSARecv(handle, &WSA_Buffer, 1, &read, &flags, nil, nil)
- if err != nil {
- log.Printf("Reader ERR: %#v\n", err)
- close(d.readerChannel)
- if !d.Disconnect() {
- log.Println("Reader close writerChannel")
- atomic.StoreInt32(&d.Disconnected, 1)
- d.closeChannel <- struct{}{}
- return
- }
- break
- }
- if read == 1 {
- d.readerChannel <- buffer[0]
- } else {
- log.Printf("READ FAILED %d\n", read)
- close(d.readerChannel)
- if !d.Disconnect() {
- log.Println("Reader close writerChannel")
- atomic.StoreInt32(&d.Disconnected, 1)
- d.closeChannel <- struct{}{}
- return
- }
- return
- }
- }
- }
|