package conn import ( "fmt" "net" ) // 消息类型 type MsgType byte const ( PingMsg MsgType = iota RequestMsg ResponseMsg ) func (t MsgType) String() string { switch t { case PingMsg: return "PingMsg" case RequestMsg: return "RequestMsg" case ResponseMsg: return "ResponseMsg" default: return fmt.Sprintf("Unknown MsgType (%d)", t) } } // 连接接口,代表一个原始的连接 type Connect interface { WriteAuthInfo(channel string, auth []byte) (err error) ReadAuthInfo() (proto string, version uint8, channel string, auth []byte, err error) WriteRequest(id uint16, cmd string, data []byte) error WriteResponse(id uint16, state uint8, data []byte) error WritePing(id uint16) error ReadMessage(deadline int) (msgType MsgType, id uint16, cmd string, state uint8, data []byte, err error) RemoteIP() net.IP LocalIP() net.IP Close() error } // 服务请求连接 type ServerConnectFunc func(conn Connect) // addrToIP attempts to extract a net.IP from a net.Addr func AddrToIP(addr net.Addr) net.IP { switch v := addr.(type) { case *net.TCPAddr: return v.IP case *net.UDPAddr: return v.IP case *net.IPAddr: return v.IP default: // Handle other potential net.Addr implementations or return nil return nil } }