gproc_comm_receive.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gproc
  7. import (
  8. "fmt"
  9. "github.com/gogf/gf/internal/json"
  10. "net"
  11. "github.com/gogf/gf/container/gqueue"
  12. "github.com/gogf/gf/container/gtype"
  13. "github.com/gogf/gf/net/gtcp"
  14. "github.com/gogf/gf/os/gfile"
  15. "github.com/gogf/gf/os/glog"
  16. "github.com/gogf/gf/util/gconv"
  17. )
  18. var (
  19. // tcpListened marks whether the receiving listening service started.
  20. tcpListened = gtype.NewBool()
  21. )
  22. // Receive blocks and receives message from other process using local TCP listening.
  23. // Note that, it only enables the TCP listening service when this function called.
  24. func Receive(group ...string) *MsgRequest {
  25. // Use atomic operations to guarantee only one receiver goroutine listening.
  26. if tcpListened.Cas(false, true) {
  27. go receiveTcpListening()
  28. }
  29. var groupName string
  30. if len(group) > 0 {
  31. groupName = group[0]
  32. } else {
  33. groupName = defaultGroupNameForProcComm
  34. }
  35. queue := commReceiveQueues.GetOrSetFuncLock(groupName, func() interface{} {
  36. return gqueue.New(maxLengthForProcMsgQueue)
  37. }).(*gqueue.Queue)
  38. // Blocking receiving.
  39. if v := queue.Pop(); v != nil {
  40. return v.(*MsgRequest)
  41. }
  42. return nil
  43. }
  44. // receiveTcpListening scans local for available port and starts listening.
  45. func receiveTcpListening() {
  46. var listen *net.TCPListener
  47. // Scan the available port for listening.
  48. for i := defaultTcpPortForProcComm; ; i++ {
  49. addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:%d", i))
  50. if err != nil {
  51. continue
  52. }
  53. listen, err = net.ListenTCP("tcp", addr)
  54. if err != nil {
  55. continue
  56. }
  57. // Save the port to the pid file.
  58. if err := gfile.PutContents(getCommFilePath(Pid()), gconv.String(i)); err != nil {
  59. panic(err)
  60. }
  61. break
  62. }
  63. // Start listening.
  64. for {
  65. if conn, err := listen.Accept(); err != nil {
  66. glog.Error(err)
  67. } else if conn != nil {
  68. go receiveTcpHandler(gtcp.NewConnByNetConn(conn))
  69. }
  70. }
  71. }
  72. // receiveTcpHandler is the connection handler for receiving data.
  73. func receiveTcpHandler(conn *gtcp.Conn) {
  74. var (
  75. result []byte
  76. response MsgResponse
  77. )
  78. for {
  79. response.Code = 0
  80. response.Message = ""
  81. response.Data = nil
  82. buffer, err := conn.RecvPkg()
  83. if len(buffer) > 0 {
  84. // Package decoding.
  85. msg := new(MsgRequest)
  86. if err := json.UnmarshalUseNumber(buffer, msg); err != nil {
  87. //glog.Error(err)
  88. continue
  89. }
  90. if msg.RecvPid != Pid() {
  91. // Not mine package.
  92. response.Message = fmt.Sprintf("receiver pid not match, target: %d, current: %d", msg.RecvPid, Pid())
  93. } else if v := commReceiveQueues.Get(msg.Group); v == nil {
  94. // Group check.
  95. response.Message = fmt.Sprintf("group [%s] does not exist", msg.Group)
  96. } else {
  97. // Push to buffer queue.
  98. response.Code = 1
  99. v.(*gqueue.Queue).Push(msg)
  100. }
  101. } else {
  102. // Empty package.
  103. response.Message = "empty package"
  104. }
  105. if err == nil {
  106. result, err = json.Marshal(response)
  107. if err != nil {
  108. glog.Error(err)
  109. }
  110. if err := conn.SendPkg(result); err != nil {
  111. glog.Error(err)
  112. }
  113. } else {
  114. // Just close the connection if any error occurs.
  115. if err := conn.Close(); err != nil {
  116. glog.Error(err)
  117. }
  118. break
  119. }
  120. }
  121. }