func()

in fragmenting_reader.go [187:254]


func (r *fragmentingReader) Close() error {
	last := r.state == fragmentingReadInLastArgument
	if r.err != nil {
		return r.err
	}

	if !r.state.isReadingArgument() {
		r.err = errNotReadingArgument
		return r.err
	}

	if len(r.curChunk) > 0 {
		// There was more data remaining in the chunk
		r.err = errMoreDataInArgument
		return r.err
	}

	// Several possibilities here:
	// 1. The caller thinks this is the last argument, but there are chunks in the current
	//    fragment or more fragments in this message
	//       - give them an error
	// 2. The caller thinks this is the last argument, and there are no more chunks and no more
	//    fragments
	//       - the stream is complete
	// 3. The caller thinks there are more arguments, and there are more chunks in this fragment
	//       - advance to the next chunk, this is the first chunk for the next argument
	// 4. The caller thinks there are more arguments, and there are no more chunks in this fragment,
	//    but there are more fragments in the message
	//       - retrieve the next fragment, confirm it has an empty chunk (indicating the end of the
	//         current argument), advance to the next check (which is the first chunk for the next arg)
	// 5. The caller thinks there are more arguments, but there are no more chunks or fragments available
	//      - give them an err
	if last {
		if len(r.remainingChunks) > 0 || r.hasMoreFragments {
			// We expect more arguments
			r.err = errExpectedMoreArguments
			return r.err
		}

		r.doneReading(nil)
		r.curFragment.done()
		r.curChunk = nil
		r.state = fragmentingReadComplete
		return nil
	}

	r.state = fragmentingReadWaitingForArgument

	// If there are more chunks in this fragment, advance to the next chunk.  This is the first chunk
	// for the next argument
	if len(r.remainingChunks) > 0 {
		r.curChunk, r.remainingChunks = r.remainingChunks[0], r.remainingChunks[1:]
		return nil
	}

	// If there are no more chunks in this fragment, and no more fragments, we have an issue
	if !r.hasMoreFragments {
		r.err = errNoMoreFragments
		return r.err
	}

	// There are no more chunks in this fragments, but more fragments - get the next fragment
	if r.err = r.recvAndParseNextFragment(false); r.err != nil {
		return r.err
	}

	return nil
}