message.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package mhayaFacade
  2. import (
  3. "strings"
  4. "time"
  5. cconst "github.com/mhaya/const"
  6. cerr "github.com/mhaya/error"
  7. cstring "github.com/mhaya/extend/string"
  8. cproto "github.com/mhaya/net/proto"
  9. )
  10. type (
  11. Message struct {
  12. BuildTime int64 // message build time(ms)
  13. PostTime int64 // post to actor time(ms)
  14. Source string // 来源actor path
  15. Target string // 目标actor path
  16. targetPath *ActorPath // 目标actor path对象
  17. FuncName string // 请求调用的函数名
  18. Session *cproto.Session // session of gateway
  19. Args interface{} // 请求的参数
  20. Err error // 返回的错误
  21. ClusterReply IRespond // 返回消息的接口
  22. IsCluster bool // 是否为集群消息
  23. ChanResult chan interface{} //
  24. }
  25. IRespond interface {
  26. Respond(data []byte) error
  27. }
  28. // ActorPath = NodeID . ActorID
  29. // ActorPath = NodeID . ActorID . ChildID
  30. ActorPath struct {
  31. NodeID string
  32. ActorID string
  33. ChildID string
  34. }
  35. )
  36. //var (
  37. // messagePool = &sync.Pool{
  38. // New: func() interface{} {
  39. // return new(Message)
  40. // },
  41. // }
  42. //)
  43. func GetMessage() Message {
  44. msg := Message{
  45. BuildTime: time.Now().UnixMilli(),
  46. }
  47. return msg
  48. }
  49. //func (p *Message) Recycle() {
  50. // p.BuildTime = 0
  51. // p.PostTime = 0
  52. // p.Source = ""
  53. // p.Target = ""
  54. // p.targetPath = nil
  55. // p.FuncName = "_"
  56. // p.Session = nil
  57. // p.Args = nil
  58. // p.Err = nil
  59. // p.ClusterReply = nil
  60. // p.ChanResult = nil
  61. // p.IsCluster = false
  62. // messagePool.Put(p)
  63. //}
  64. func (p *Message) TargetPath() *ActorPath {
  65. if p.targetPath == nil {
  66. p.targetPath, _ = ToActorPath(p.Target)
  67. }
  68. return p.targetPath
  69. }
  70. func (p *Message) IsReply() bool {
  71. return p.ClusterReply != nil
  72. }
  73. func (p *ActorPath) IsChild() bool {
  74. return p.ChildID != ""
  75. }
  76. func (p *ActorPath) IsParent() bool {
  77. return p.ChildID == ""
  78. }
  79. // String
  80. func (p *ActorPath) String() string {
  81. return NewChildPath(p.NodeID, p.ActorID, p.ChildID)
  82. }
  83. func NewActorPath(nodeID, actorID, childID string) *ActorPath {
  84. return &ActorPath{
  85. NodeID: nodeID,
  86. ActorID: actorID,
  87. ChildID: childID,
  88. }
  89. }
  90. func NewChildPath(nodeID, actorID, childID interface{}) string {
  91. if childID == "" {
  92. return NewPath(nodeID, actorID)
  93. }
  94. return cstring.ToString(nodeID) + cconst.DOT + cstring.ToString(actorID) + cconst.DOT + cstring.ToString(childID)
  95. }
  96. func NewPath(nodeID, actorID interface{}) string {
  97. return cstring.ToString(nodeID) + cconst.DOT + cstring.ToString(actorID)
  98. }
  99. func ToActorPath(path string) (*ActorPath, error) {
  100. if path == "" {
  101. return nil, cerr.ActorPathError
  102. }
  103. p := strings.Split(path, cconst.DOT)
  104. pLen := len(p)
  105. if pLen == 2 {
  106. return NewActorPath(p[0], p[1], ""), nil
  107. }
  108. if len(p) == 3 {
  109. return NewActorPath(p[0], p[1], p[2]), nil
  110. }
  111. return nil, cerr.ActorPathError
  112. }