func (p *Packets) ReadEOF() error {
	data, err := p.Next()
	if err != nil {
		return err
	}
	switch data[0] {
	case proto.EOF_PACKET:
		return nil
	case proto.ERR_PACKET:
		return p.ParseERR(data)
	default:
		return sqldb.NewSQLErrorf(sqldb.ER_MALFORMED_PACKET, "unexpected.eof.packet[%+v]", data)
	}
}
ReadEOF used to read the EOF packet.
package packet
import (
	"fmt"
	"net"
	"github.com/xelabs/go-mysqlstack/proto"
	"github.com/xelabs/go-mysqlstack/sqldb"
	"github.com/xelabs/go-mysqlstack/sqlparser/depends/common"
	querypb "github.com/xelabs/go-mysqlstack/sqlparser/depends/query"
	"github.com/xelabs/go-mysqlstack/sqlparser/depends/sqltypes"
)
const (
	PACKET_MAX_SIZE = (1<<24 - 1)  
)
type Packet struct {
	SequenceID byte
	Datas      []byte
}
type Packets struct {
	seq    uint8
	stream *Stream
}
func NewPackets(c net.Conn) *Packets {
	return &Packets{
		stream: NewStream(c, PACKET_MAX_SIZE),
	}
}
func (p *Packets) Next() ([]byte, error) {
	pkt, err := p.stream.Read()
	if err != nil {
		return nil, err
	}
	if pkt.SequenceID != p.seq {
		return nil, sqldb.NewSQLErrorf(sqldb.ER_MALFORMED_PACKET, "pkt.read.seq[%v]!=pkt.actual.seq[%v]", pkt.SequenceID, p.seq)
	}
	p.seq++
	return pkt.Datas, nil
}
func (p *Packets) Write(payload []byte) error {
	payLen := len(payload)
	pkt := common.NewBuffer(64)
	pkt.WriteU24(uint32(payLen))
	pkt.WriteU8(p.seq)
	pkt.WriteBytes(payload)
	if err := p.stream.Write(pkt.Datas()); err != nil {
		return err
	}
	p.seq++
	return nil
}
func (p *Packets) WriteCommand(command byte, payload []byte) error {
	p.seq = 0
	pkt := common.NewBuffer(64)
	payLen := len(payload)
	pkt.WriteU24(uint32(1 + payLen))
	pkt.WriteU8(p.seq)
	pkt.WriteU8(command)
	pkt.WriteBytes(payload)
	if err := p.stream.Write(pkt.Datas()); err != nil {
		return err
	}
	p.seq++
	return nil
}
func (p *Packets) ResetSeq() {
	p.seq = 0
}
func (p *Packets) ParseOK(data []byte) (*proto.OK, error) {
	return proto.UnPackOK(data)
}
func (p *Packets) WriteOK(affectedRows, lastInsertID uint64, flags uint16, warnings uint16) error {
	ok := &proto.OK{
		AffectedRows: affectedRows,
		LastInsertID: lastInsertID,
		StatusFlags:  flags,
		Warnings:     warnings,
	}
	return p.Write(proto.PackOK(ok))
}
func (p *Packets) ParseERR(data []byte) error {
	return proto.UnPackERR(data)
}
func (p *Packets) WriteERR(errorCode uint16, sqlState string, format string, args ...interface{}) error {
	e := &proto.ERR{
		ErrorCode:    errorCode,
		SQLState:     sqlState,
		ErrorMessage: fmt.Sprintf(format, args...),
	}
	return p.Write(proto.PackERR(e))
}
func (p *Packets) Append(rawdata []byte) error {
	pkt := common.NewBuffer(64)
	pkt.WriteU24(uint32(len(rawdata)))
	pkt.WriteU8(p.seq)
	pkt.WriteBytes(rawdata)
	if err := p.stream.Append(pkt.Datas()); err != nil {
		return err
	}
	p.seq++
	return nil
}
func (p *Packets) ReadOK() error {
	data, err := p.Next()
	if err != nil {
		return err
	}
	switch data[0] {
	case proto.OK_PACKET:
		return nil
	case proto.ERR_PACKET:
		return p.ParseERR(data)
	default:
		return sqldb.NewSQLErrorf(sqldb.ER_MALFORMED_PACKET, "unexpected.ok.packet[%+v]", data)
	}
}