在GoLang UDP服务器中获取分段数据包

时间:2019-03-03 20:13:54

标签: sockets go udp goroutine datagram

我需要在其中实现一个udp侦听器,以从端口读取传入的数据包并将其解析为json。问题是我收到的数据包碎片化,因为它们是不完整的json,因此无法解析。

const BufferSize = 15214400
func udpListener(ip []byte, port int) {
  addr := net.UDPAddr{IP: ip, Port: port, Zone: ""}
  ServerConn, err := net.ListenUDP("udp", &addr)
  if err != nil {
    log.Panicln(err.Error())
  }
  ServerConn.SetReadBuffer(BufferSize)
  defer ServerConn.Close()
  buffer := make([]byte, BufferSize)
  for {
    buffLen, senderAddress, err := ServerConn.ReadFromUDP(buffer)
    if err != nil {
      log.Errorln("Error occurred while read data from UDP: " + err.Error())
      continue
    }
    go handleUdpDatagram(buffLen, buffer, senderAddress.String())
  }
}

func handleUdpDatagram(buffLen int, buffer []byte, senderAddress string) {
  var jsonData map[string]interface{}
  if err := json.Unmarshal(buffer[0:buffLen], &jsonData); err != nil {
    log.Errorln("Error occurred while parsing data: " + data)
    return
  }
}

数据包被切成随机的字节数,并在下次读取时发送。 切碎的数据包的大小不是恒定的,我可以将其他数据包的大小保持不变。

样本包:

{"_id":"5c7c366e29c0094fcb03306e","index":0,"guid":"04fbc039-7fb4-4b3f-9722-60a0617b5469","isActive":true,"balance":"$3,431.05","picture":"http://placehold.it/32x32","age":25,"eyeColor":"green","name":"Reynolds Rodgers","gender":"male","company":"BITENDREX","email":"reynoldsrodgers@bitendrex.com","phone":"+1 (905) 547-2873","address":"778 Mermaid Avenue, Draper, Northern Mariana Islands, 4983","about":"Qui ad velit ut irure eu veniam laborum nulla fugiat magna nostrud irure in laboris. Laborum commodo enim Lorem anim id. Ex cupidatat do ad labore cillum cillum aute cillum. Incididunt velit duis nostrud deserunt anim. Aliqua aute commodo magna in deserunt reprehenderit consectetur aliquip ea proident. Ullamco deserunt qui aliqua ad mollit commodo enim pariatur enim labore cillum. Eu deserunt quis adipisicing nisi sint magna proident voluptate fugiat eiusmod ut nostrud exercitation sit.\r\n","registered":"2018-11-14T06:24:10 -06:-30","latitude":-70.393932,"longitude":-95.507978,"tags":["esse","voluptate","eu","enim","ullamco","aute","velit"],"friends":[{"id":0,"name":"Wendi Graves"},{"id":1,"name":"Hogan Bean"},{"id":2,"name":"Garrison Bennett"}],"greeting":"Hello, Reynolds Rodgers! You have 2 unread messages.","favoriteFruit":"banana"}

我发送此数据包时,侦听器将其分为2个片段

time="2019-03-04T01:51:47+05:30" level=error msg="Error occurred while parsing data: im\",\"ullamco\",\"aute\",\"velit\"],\"friends\":[{\"id\":0,\"name\":\"Wendi Graves\"},{\"id\":1,\"name\":\"Hogan Bean\"},{\"id\":2,\"name\":\"Garrison Bennett\"}],\"greeting\":\"Hello, Reynolds Rodgers! You have 2 unread messages.\",\"favoriteFruit\":\"banana\"}\n\"male\",\"company\":\"BITENDREX\",\"email\":\"reynoldsrodgers@bitendrex.com\",\"phone\":\"+1 (905) 547-2873\",\"address\":\"778 Mermaid Avenue, Draper, Northern Mariana Islands, 4983\",\"about\":\"Qui ad velit ut irure eu veniam laborum nulla fugiat magna nostrud irure in laboris. Laborum commodo enim Lorem anim id. Ex cupidatat do ad labore cillum cillum aute cillum. Incididunt velit duis nostrud deserunt anim. Aliqua aute commodo magna in deserunt reprehenderit consectetur aliquip ea proident. Ullamco deserunt qui aliqua ad mollit commodo enim pariatur enim labore cillum. Eu deserunt quis adipisicing nisi sint magna proident voluptate fugiat eiusmod ut nostrud exercitation sit.\r\n\",\"registered\":\"2018-11-14T06:24:10 -06:-30\",\"latitude\":-70.393932,\"longitude\":-95.507978,\"tags\":[\"esse\",\"voluptate\",\"eu\",\"en error: invalid character 'i' looking for beginning of value"
time="2019-03-04T01:51:47+05:30" level=error msg="Error occurred while parsing data: im\",\"ullamco\",\"aute\",\"velit\"],\"friends\":[{\"id\":0,\"name\":\"Wendi Graves\"},{\"id\":1,\"name\":\"Hogan Bean\"},{\"id\":2,\"name\":\"Garrison Bennett\"}],\"greeting\":\"Hello, Reynolds Rodgers! You have 2 unread messages.\",\"favoriteFruit\":\"banana\"}\n error: invalid character 'i' looking for beginning of value"

0 个答案:

没有答案