-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
gproc_comm_send.go
58 lines (54 loc) · 1.26 KB
/
gproc_comm_send.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gproc
import (
"io"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/net/gtcp"
)
// Send sends data to specified process of given pid.
func Send(pid int, data []byte, group ...string) error {
msg := MsgRequest{
SenderPid: Pid(),
ReceiverPid: pid,
Group: defaultGroupNameForProcComm,
Data: data,
}
if len(group) > 0 {
msg.Group = group[0]
}
msgBytes, err := json.Marshal(msg)
if err != nil {
return err
}
var conn *gtcp.PoolConn
conn, err = getConnByPid(pid)
if err != nil {
return err
}
defer conn.Close()
// Do the sending.
var result []byte
result, err = conn.SendRecvPkg(msgBytes, gtcp.PkgOption{
Retry: gtcp.Retry{
Count: 3,
},
})
if len(result) > 0 {
response := new(MsgResponse)
if err = json.UnmarshalUseNumber(result, response); err == nil {
if response.Code != 1 {
err = gerror.New(response.Message)
}
}
}
// EOF is not really an error.
if err == io.EOF {
err = nil
}
return err
}