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) // WriteChannel(data []byte) 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) RemoteAddr() net.Addr LocalAddr() net.Addr Close() error } // 服务请求连接 type ServerConnectFunc func(conn Connect)