func (c *Client) ReadPacket() (packet.Packet, error) {
	var l uint32
	if err := binary.Read(c.conn, binary.LittleEndian, &l); err != nil {
		return nil, err
	}
	data := make([]byte, l)
	read, err := c.conn.Read(data)
	if err != nil {
		return nil, err
	}
	if read != int(l) {
		return nil, fmt.Errorf("expected %v bytes, got %v", l, read)
	}
	buf := bytes.NewBuffer(data)
	header := &packet.Header{}
	if err := header.Read(buf); err != nil {
		return nil, err
	}
	pk, ok := c.pool[header.PacketID]
	if !ok {
		return nil, fmt.Errorf("unknown packet %v", header.PacketID)
	}
	pk.Unmarshal(protocol.NewReader(buf, 0))
	if buf.Len() > 0 {
		return nil, fmt.Errorf("still have %v bytes unread", buf.Len())
	}
	return pk, nil
}
ReadPacket reads a packet from the connection and returns it. The client is expected to prefix the packet
payload with 4 bytes for the length of the payload.
package socket
import (
	"bytes"
	"encoding/binary"
	"fmt"
	"github.com/paroxity/portal/server"
	"github.com/paroxity/portal/socket/packet"
	"github.com/sandertv/gophertunnel/minecraft/protocol"
	"github.com/sirupsen/logrus"
	"net"
	"sync"
)
type Client struct {
	conn net.Conn
	pool packet.Pool
	sendMu sync.Mutex
	hdr    *packet.Header
	buf    *bytes.Buffer
	name       string
	clientType uint8
	extraData  map[string]interface{}
}
func NewClient(conn net.Conn) *Client {
	return &Client{
		conn: conn,
		pool: packet.NewPool(),
		buf:  bytes.NewBuffer(make([]byte, 0, 4096)),
		hdr:  &packet.Header{},
		extraData: make(map[string]interface{}),
	}
}
func (c *Client) Name() string {
	return c.name
}
func (c *Client) Close() error {
	logrus.Debugf("Socket connection \"%s\" closed\n", c.name)
	switch c.clientType {
	case packet.ClientTypeServer:
		if name, ok := c.extraData["group"]; ok {
			g, _ := server.GroupFromName(name.(string))
			s, _ := g.Server(c.name)
			server_setConn(s, nil)
		}
	}
	return c.conn.Close()
}