WriteTCP write a proto to TCP writer.
(wr *bufio.Writer)
| 95 | |
| 96 | // WriteTCP write a proto to TCP writer. |
| 97 | func (p *Proto) WriteTCP(wr *bufio.Writer) (err error) { |
| 98 | var ( |
| 99 | buf []byte |
| 100 | packLen int32 |
| 101 | ) |
| 102 | if p.Op == OpRaw { |
| 103 | // write without buffer, job concact proto into raw buffer |
| 104 | _, err = wr.WriteRaw(p.Body) |
| 105 | return |
| 106 | } |
| 107 | packLen = _rawHeaderSize + int32(len(p.Body)) |
| 108 | if buf, err = wr.Peek(_rawHeaderSize); err != nil { |
| 109 | return |
| 110 | } |
| 111 | binary.BigEndian.PutInt32(buf[_packOffset:], packLen) |
| 112 | binary.BigEndian.PutInt16(buf[_headerOffset:], int16(_rawHeaderSize)) |
| 113 | binary.BigEndian.PutInt16(buf[_verOffset:], int16(p.Ver)) |
| 114 | binary.BigEndian.PutInt32(buf[_opOffset:], p.Op) |
| 115 | binary.BigEndian.PutInt32(buf[_seqOffset:], p.Seq) |
| 116 | if p.Body != nil { |
| 117 | _, err = wr.Write(p.Body) |
| 118 | } |
| 119 | return |
| 120 | } |
| 121 | |
| 122 | // WriteTCPHeart write TCP heartbeat with room online. |
| 123 | func (p *Proto) WriteTCPHeart(wr *bufio.Writer, online int32) (err error) { |