func()

in image/resources/knfsd-fsidd/socket.go [284:346]


func (c *connection) Serve() {
	defer func() {
		err := c.Close()
		c.s.trackConnection(c, false)
		if err != nil && !isClosed(err) {
			log.Warn.Printf("[%d] %s", c.id, err)
		}
		log.Debug.Printf("[%d] connection closed", c.id)
	}()

	log.Debug.Printf("[%d] received connection", c.id)

	// Make the buffer 1 byte larger than the max allowed packet length to
	// detect truncated packets. If we read > PacketMaxLength bytes then the
	// packet would have been truncated if the buffer was PacketMaxLength.
	buf := make([]byte, PacketMaxLength+1)

	ctx, cancel := context.WithCancel(context.Background())
	ctx = log.WithID(ctx, c.id)
	c.cancel = cancel

	// Check inShutdown as r.Scan could have another command buffered.
	// There is a race condition between setting setting inShutdown, closing
	// the connection and scan returning the next command but that's ok, as
	// the command will have the grace period to complete its work before being
	// terminated.
	for !c.shuttingDown() {
		n, err := c.rw.Read(buf)
		if err != nil {
			switch {
			case isClosed(err):
				// connection closed
				log.Debug.Printf("[%d] received EOF", c.id)
				return

			default:
				// Non-recoverable error, reset the connection.
				log.Error.Printf("[%d] read error: %s", c.id, err)
				return
			}
		}

		if n > PacketMaxLength {
			// message was truncated, return an error to the client
			log.Warn.Printf("[%d] message truncated, ignoring", c.id)
			c.writeError("message truncated")
			continue
		}

		line := string(buf[0:n])
		log.Debug.Printf("[%d] => %q", c.id, line)

		cmd, arg, _ := cut(line, " ")
		cmd = strings.ToUpper(cmd)

		err = c.execute(ctx, cmd, arg)
		if err != nil {
			log.Error.Printf("[%d] error executing command: %s", c.id, err)
			// Non-recoverable error, reset the connection.
			return
		}
	}
}