type.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. ProxyConnectMsg
  13. ProxyResultMsg
  14. )
  15. func (t MsgType) String() string {
  16. switch t {
  17. case PingMsg:
  18. return "PingMsg"
  19. case RequestMsg:
  20. return "RequestMsg"
  21. case ResponseMsg:
  22. return "ResponseMsg"
  23. case ProxyConnectMsg:
  24. return "ProxyConnectMsg"
  25. case ProxyResultMsg:
  26. return "ProxyResultMsg"
  27. default:
  28. return fmt.Sprintf("Unknown MsgType (%d)", t)
  29. }
  30. }
  31. // 连接接口,代表一个原始的连接
  32. type Connect interface {
  33. // io.ReadWriteCloser
  34. WriteAuthInfo(channel string, auth []byte) (err error)
  35. ReadAuthInfo() (proto string, version uint8, channel string, auth []byte, err error)
  36. WriteRequest(id uint16, cmd string, data []byte) error
  37. WriteResponse(id uint16, state uint8, data []byte) error
  38. WritePing(id uint16) error
  39. ReadMessage(deadline int) (msgType MsgType, id uint16, cmd string, state uint8, data []byte, err error)
  40. RemoteIP() net.IP
  41. LocalIP() net.IP
  42. Close() error
  43. }
  44. // 服务请求连接
  45. type ServerConnectFunc func(conn Connect)