func()

in lib/ec2macossystemmonitor/relayd.go [194:234]


func (relay *SerialRelay) StartRelay(logger *Logger, relayStatus *StatusLogBuffer) {
	// Accept new connections, dispatching them to relayServer in a goroutine.
	for {
		err := relay.setListenerDeadline(time.Now().Add(SocketTimeout))
		if err != nil {
			logger.Fatal("Unable to set deadline on socket:", err)
		}

		socCon, err := relay.listener.Accept()
		// Look for signal to exit, otherwise keep going, check the error only if we aren't supposed to shutdown
		select {
		case <-relay.ReadyToClose:
			logger.Info("[relayd] requested to shutdown")
			// Clean up resources manually
			relay.CleanUp()
			// Return to stop the connections from continuing
			return
		default:
			// If ReadyToClose has not been sent, then check for errors, handle timeouts, otherwise process
			if err != nil {
				if er, ok := err.(net.Error); ok && er.Timeout() {
					// This is just a timeout, break the loop and go to the top to start listening again
					continue
				} else {
					// This is some other error, for Accept(), its a fatal error if we can't Accept()
					logger.Fatal("Unable to start accepting on socket:", err)
				}
			}

		}

		// Write the date to the relay
		written, err := relay.serialConnection.RelayData(socCon)
		if err != nil {
			logger.Errorf("Failed to send data: %s\n", err)
		}

		// Increment the counter
		atomic.AddInt64(&relayStatus.Written, int64(written))
	}
}