func (c *Conn) PrettyID() string {
	var remote string
	if c.Peer != nil {
		remote = c.Peer.Id
	} else {
		remote = c.RemoteAddr().String()
	}
	return color.CyanString(remote)
}
PrettyID returns a terminal colored format of the connection ID.
package network
import (
	"encoding/binary"
	"errors"
	"math/rand"
	"net"
	"time"
	"github.com/fatih/color"
	"github.com/degdb/degdb/protocol"
)
var Timeout = errors.New("request timed-out")
func (s *Server) NewConn(c net.Conn) *Conn {
	return &Conn{
		Conn:             c,
		server:           s,
		expectedMessages: make(map[uint64]chan *protocol.Message),
	}
}
type Conn struct {
	Peer   *protocol.Peer
	Closed bool
	peerRequest        chan bool
	peerRequestRetries int
	server             *Server
	expectedMessages   map[uint64]chan *protocol.Message
	net.Conn
}
func (c *Conn) Send(m *protocol.Message) error {
	msg, err := m.Marshal()
	if err != nil {
		return err
	}
	packet := make([]byte, len(msg)+4)
	binary.BigEndian.PutUint32(packet, uint32(len(msg)))
	copy(packet[4:], msg)
	_, err = c.Conn.Write(packet)
	return err
}
func (c *Conn) Request(m *protocol.Message) (*protocol.Message, error) {
	m.Id = uint64(rand.Int63())
	m.ResponseRequired = true
	if err := c.Send(m); err != nil {
		return nil, err
	}
	timeout := make(chan bool, 1)
	go func() {
		time.Sleep(10 * time.Second)
		timeout <- true
	}()
	resp := make(chan *protocol.Message, 1)
	c.expectedMessages[m.Id] = resp
	var msg *protocol.Message
	var err error
	select {
	case msg = <-resp:
	case <-timeout:
		err = Timeout
	}
	delete(c.expectedMessages, m.Id)
	return msg, err
}
func (c *Conn) RespondTo(to *protocol.Message, resp *protocol.Message) error {
	resp.ResponseTo = to.Id
	return c.Send(resp)
}
func (c *Conn) Close() error {
	if c == nil {
		return nil
	}
	c.Closed = true
	if c.Conn != nil {
		return c.Conn.Close()
	}
	return nil
}