type.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. ReadAuthInfo() (proto string, version uint8, channel string, auth []byte, err error)
  34. ReadMessage(deadline int) (msgType MsgType, id uint16, cmd string, state uint8, data []byte, err error)
  35. ReadRawPackage(deadline int) (buf []byte, recycle bool, err error)
  36. WriteRawPackage(raw []byte, recycle bool) (err error)
  37. WriteAuthInfo(channel string, auth []byte) (err error)
  38. WriteRequest(id uint16, cmd string, data []byte) error
  39. WriteResponse(id uint16, state uint8, data []byte) error
  40. WritePing(id uint16) error
  41. RemoteIP() net.IP
  42. LocalIP() net.IP
  43. Close() error
  44. }
  45. // 服务请求连接
  46. type ServerConnectFunc func(conn Connect)