github.com/gogf/gf@v1.16.9/os/gproc/gproc_comm_send.go (about) 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 7 package gproc 8 9 import ( 10 "github.com/gogf/gf/errors/gerror" 11 "github.com/gogf/gf/internal/json" 12 "github.com/gogf/gf/net/gtcp" 13 "io" 14 ) 15 16 // Send sends data to specified process of given pid. 17 func Send(pid int, data []byte, group ...string) error { 18 msg := MsgRequest{ 19 SendPid: Pid(), 20 RecvPid: pid, 21 Group: defaultGroupNameForProcComm, 22 Data: data, 23 } 24 if len(group) > 0 { 25 msg.Group = group[0] 26 } 27 msgBytes, err := json.Marshal(msg) 28 if err != nil { 29 return err 30 } 31 var conn *gtcp.PoolConn 32 conn, err = getConnByPid(pid) 33 if err != nil { 34 return err 35 } 36 defer conn.Close() 37 // Do the sending. 38 var result []byte 39 result, err = conn.SendRecvPkg(msgBytes, gtcp.PkgOption{ 40 Retry: gtcp.Retry{ 41 Count: 3, 42 }, 43 }) 44 if len(result) > 0 { 45 response := new(MsgResponse) 46 if err = json.UnmarshalUseNumber(result, response); err == nil { 47 if response.Code != 1 { 48 err = gerror.New(response.Message) 49 } 50 } 51 } 52 // EOF is not really an error. 53 if err == io.EOF { 54 err = nil 55 } 56 return err 57 }