Hey there, I think got a big problem here with a tcp server routine, I use a custom scanner to find CRLF, I tried both “\r\n” and as byte 0x0D, 0x0A, it works, the problem is that sometimes I get packages with 0D0A in the middle of the package. Below a packet with 0D0A in the middle;
78 78 1f 12 11 02 0d 0a 04 1e c9 01 c5 3d 4a 05 4d b6 8c 00 38 14 02 d4 06 0c 5a 00 7a 79 00 12 ff 0e 0d 0a
And below my scanner code;
// dropCR drops a terminal \r from the data.
func dropCR(data []byte) []byte {
if len(data) > 0 && data[len(data)-1] == '\r' {
return data[0 : len(data)-1]
}
return data
}
func ScanCRLF(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.Index(data, []byte{'\r', '\n'}); i >= 0 {
// We have a full newline-terminated line.
return i + 2, dropCR(data[0:i]), nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), dropCR(data), nil
}
// Request more data.
return 0, nil, nil
}
Below the usage;
func (c *Client) listen() {
reader := bufio.NewReader(c.conn)
scanner := bufio.NewScanner(reader)
scanner.Split(ScanCRLF)
for scanner.Scan() {
c.Server.onNewMessage(c, strings.ToUpper(hex.EncodeToString(scanner.Bytes())+"0d0a"))
}
}
Any one knows a way to fix it?
Thanks all…