我正在嘗試實現(xiàn)我自己的 beanstalkd 客戶端作為一種學習方式。https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt目前,我正在bufio讀取由 . 分隔的一行數據\n。res, err := this.reader.ReadLine('\n')當我發(fā)送單個命令并讀取單行響應時,這很好:INSERTED %d\r\n但是當我嘗試保留作業(yè)時發(fā)現(xiàn)困難,因為作業(yè)正文可能是多行,因此,我不能使用\n分隔符。有沒有辦法讀入緩沖區(qū)直到CRLF?例如,當我發(fā)送reserve命令時。我的預期反應如下:RESERVED <id> <bytes>\r\n<data>\r\n但數據可能包含\n,所以我需要閱讀直到\r\n.<bytes>或者 - 有沒有一種方法可以讀取上面示例響應中指定的特定字節(jié)數?目前,我已經(刪除了錯誤處理):func (this *Bean) receiveLine() (string, error) { res, err := this.reader.ReadString('\n') return res, err}func (this *Bean) receiveBody(numBytesToRead int) ([]byte, error) { res, err := this.reader.ReadString('\r\n') // What to do here to read to CRLF / up to number of expected bytes? return res, err}func (this *Bean) Reserve() (*Job, error) { this.send("reserve\r\n") res, err := this.receiveLine() var jobId uint64 var bodylen int _, err = fmt.Sscanf(res, "RESERVED %d %d\r\n", &jobId, &bodylen) body, err := this.receiveBody(bodylen) job := new(Job) job.Id = jobId job.Body = body return job, nil}
- 0 回答
- 0 關注
- 512 瀏覽
添加回答
舉報
0/150
提交
取消