func (rcv *TCP) Listen(addr *net.TCPAddr) error {
	return rcv.StartFunc(func() error {

		tcpListener, err := net.ListenTCP("tcp", addr)
		if err != nil {
			return err
		}

		rcv.Go(func(exit chan bool) {
			<-exit
			tcpListener.Close()
		})

		handler := rcv.HandleConnection
		if rcv.isFraming {
			handler = rcv.handleFraming
		}

		if rcv.buffer != nil {
			originalOut := rcv.out

			rcv.Go(func(exit chan bool) {
				for {
					select {
					case <-exit:
						return
					case p := <-rcv.buffer:
						originalOut(p)
					}
				}
			})

			rcv.out = func(p *points.Points) {
				rcv.buffer <- p
			}
		}

		rcv.Go(func(exit chan bool) {
			defer tcpListener.Close()

			for {

				conn, err := tcpListener.Accept()
				if err != nil {
					if strings.Contains(err.Error(), "use of closed network connection") {
						break
					}
					rcv.logger.Warn("failed to accept connection",
						zap.Error(err),
					)
					continue
				}

				rcv.Go(func(exit chan bool) {
					handler(conn)
				})
			}

		})

		rcv.listener = tcpListener

		return nil
	})
}
func (rcv *TCP) Listen(addr *net.TCPAddr) error {
	return rcv.StartFunc(func() error {

		tcpListener, err := net.ListenTCP("tcp", addr)
		if err != nil {
			return err
		}

		rcv.Go(func(exit chan bool) {
			<-exit
			tcpListener.Close()
		})

		handler := rcv.HandleConnection
		if rcv.isFraming {
			handler = rcv.handleFraming
		}

		if rcv.buffer != nil {
			originalOut := rcv.out

			rcv.Go(func(exit chan bool) {
				for {
					select {
					case <-exit:
						return
					case p := <-rcv.buffer:
						originalOut(p)
					}
				}
			})

			rcv.out = func(p *points.Points) {
				rcv.buffer <- p
			}
		}

		rcv.Go(func(exit chan bool) {
			defer tcpListener.Close()

			for {

				conn, err := tcpListener.Accept()
				if err != nil {
					if strings.Contains(err.Error(), "use of closed network connection") {
						break
					}
					rcv.logger.Warn("failed to accept connection",
						zap.Error(err),
					)
					continue
				}

				rcv.Go(func(exit chan bool) {
					handler(conn)
				})
			}

		})

		rcv.listener = tcpListener

		return nil
	})
}
func ( rcv * TCP ) Listen ( addr * net . TCPAddr ) error { return rcv . StartFunc ( func ( ) error { tcpListener , err := net . ListenTCP ( " " , addr ) if err != nil { return err } rcv . Go ( func ( exit chan bool ) { <- exit tcpListener . Close ( ) } ) handler := rcv . HandleConnection if rcv . isFraming { handler = rcv . handleFraming } if rcv . buffer != nil { originalOut := rcv . out rcv . Go ( func ( exit chan bool ) { for { select { case <- exit : return case p := <- rcv . buffer : originalOut ( p ) } } } ) rcv . out = func ( p * points . Points ) { rcv . buffer <- p } } rcv . Go ( func ( exit chan bool ) { defer tcpListener . Close ( ) for { conn , err := tcpListener . Accept ( ) if err != nil { if strings . Contains ( err . Error ( ) , " " ) { break } rcv . logger . Warn ( " " , zap . Error ( err ) , ) continue } rcv . Go ( func ( exit chan bool ) { handler ( conn ) } ) } } ) rcv . listener = tcpListener return nil } ) }
// Listen bind port. Receive messages and send to out channel
Listen bind port . Receive messages and send to out channel