| 88 | } |
| 89 | |
| 90 | func WriteMessage(sink *comm.ZeroCopySink, msg Message) { |
| 91 | pstart := sink.Size() |
| 92 | sink.NextBytes(common.MSG_HDR_LEN) // can not save the buf, since it may reallocate in sink |
| 93 | msg.Serialization(sink) |
| 94 | pend := sink.Size() |
| 95 | total := pend - pstart |
| 96 | payLen := total - common.MSG_HDR_LEN |
| 97 | |
| 98 | sink.BackUp(total) |
| 99 | buf := sink.NextBytes(total) |
| 100 | checksum := common.Checksum(buf[common.MSG_HDR_LEN:]) |
| 101 | hdr := newMessageHeader(msg.CmdType(), uint32(payLen), checksum) |
| 102 | |
| 103 | sink.BackUp(total) |
| 104 | writeMessageHeaderInto(sink, hdr) |
| 105 | sink.NextBytes(payLen) |
| 106 | } |
| 107 | |
| 108 | func ReadMessage(reader io.Reader) (Message, uint32, error) { |
| 109 | hdr, err := readMessageHeader(reader) |