conn.go 985 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package conn
  2. import (
  3. "fmt"
  4. "net"
  5. )
  6. // 消息类型
  7. type MsgType byte
  8. const (
  9. PingMsg MsgType = iota
  10. RequestMsg
  11. ResponseMsg
  12. )
  13. func (t MsgType) String() string {
  14. switch t {
  15. case PingMsg:
  16. return "PingMsg"
  17. case RequestMsg:
  18. return "RequestMsg"
  19. case ResponseMsg:
  20. return "ResponseMsg"
  21. default:
  22. return fmt.Sprintf("Unknown MsgType (%d)", t)
  23. }
  24. }
  25. // 连接接口,代表一个原始的连接
  26. type Connect interface {
  27. WriteAuthInfo(channel string, auth []byte) (err error)
  28. ReadAuthInfo() (proto string, version uint8, channel string, auth []byte, err error)
  29. // WriteChannel(data []byte) error
  30. WriteRequest(id uint16, cmd string, data []byte) error
  31. WriteResponse(id uint16, state uint8, data []byte) error
  32. WritePing(id uint16) error
  33. ReadMessage(deadline int) (msgType MsgType, id uint16, cmd string, state uint8, data []byte, err error)
  34. RemoteAddr() net.Addr
  35. LocalAddr() net.Addr
  36. Close() error
  37. }
  38. // 服务请求连接
  39. type ServerConnectFunc func(conn Connect)