Пример #1
0
func parse(in_ io.Reader, out chan<- *gomem.MCRequest) {
	in := bufio.NewReader(in_)
	buf := make([]byte, gomem.HDR_LEN)

	for {
		b, err := in.Peek(1)
		if err != nil {
			if err == io.EOF {
				return
			}
			// TODO print error
			return
		}

		if b[0] == gomem.REQ_MAGIC {
			req := reqP.Get().(*gomem.MCRequest)
			req.Cas = 0
			req.Key = nil
			req.Body = nil
			req.Extras = nil
			req.Opcode = 0
			req.Opaque = 0
			_, err := req.Receive(in, buf)
			if err != nil {
				if err == io.EOF {
					reqP.Put(req)
					return
				}
				// TODO: print error
				continue
			}
			out <- req
		} else {
			// text protocol fallback
			cmd, err := in.ReadString('\n')
			if err != nil {
				if err == io.EOF {
					return
				}
				// TODO: print error
				return
			}

			reqs, err := text.ToMCRequest(cmd, in)
			if err != nil {
				// TODO: print error
				return
			}
			for _, req := range reqs {
				req.Opaque = 0xffffffff
				out <- &req
			}
		}
	}
	close(out)
}
Пример #2
0
func hlps(t *testing.T, cmd string, in []byte, exp []gomem.MCRequest) {
	var in_ bytes.Buffer
	in_.Write(in)

	reqs, err := text.ToMCRequest(cmd, &in_)
	if err != nil {
		t.Error(err)
		return
	}

	if len(exp) != len(reqs) {
		t.Errorf("expected %d request objects, got %d\n", len(exp), len(reqs))
	}

	for i := range exp {
		if !bytes.Equal(reqs[i].Bytes(), exp[i].Bytes()) {
			t.Errorf("\n[%d] expected:\n%+v\ngot:\n%+v", i, &exp[i], reqs[i])
		}
	}
}