| 383 | } |
| 384 | |
| 385 | func (mp *MsgParser) Next() (*Packet, bool) { |
| 386 | buf := mp.buffer |
| 387 | |
| 388 | for { |
| 389 | line, rest := mp.lineFrom(buf) |
| 390 | |
| 391 | if line != nil { |
| 392 | mp.buffer = rest |
| 393 | return parseLine(line), true |
| 394 | } |
| 395 | |
| 396 | if mp.done { |
| 397 | if len(rest) > 0 { |
| 398 | return parseLine(rest), false |
| 399 | } |
| 400 | return nil, false |
| 401 | } |
| 402 | |
| 403 | // for udp, each message independent |
| 404 | // for tcp, copy to front and append |
| 405 | // unless no '\n' in entire TCP_READ_SIZE |
| 406 | idx := 0 |
| 407 | if mp.partialReads && len(buf) < TCP_READ_SIZE { |
| 408 | idx = len(buf) |
| 409 | copy(mp.newbuf, buf) |
| 410 | } |
| 411 | buf = mp.newbuf |
| 412 | |
| 413 | n, err := mp.reader.Read(buf[idx:]) |
| 414 | buf = buf[:idx+n] |
| 415 | if err != nil { |
| 416 | if err != io.EOF { |
| 417 | log.Printf("ERROR: %s", err) |
| 418 | } |
| 419 | mp.done = true |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | func (mp *MsgParser) lineFrom(input []byte) ([]byte, []byte) { |
| 425 | split := bytes.SplitN(input, []byte("\n"), 2) |