本文整理汇总了Golang中net.UnixConn.File方法的典型用法代码示例。如果您正苦于以下问题:Golang UnixConn.File方法的具体用法?Golang UnixConn.File怎么用?Golang UnixConn.File使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.UnixConn
的用法示例。
在下文中一共展示了UnixConn.File方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: qmpCommander
func qmpCommander(handler chan QmpInteraction, conn *net.UnixConn, session *QmpSession, feedback chan QmpInteraction) {
glog.V(1).Info("Begin process command session")
for _, cmd := range session.commands {
msg, err := json.Marshal(*cmd)
if err != nil {
handler <- qmpFail("cannot marshal command", session.respond)
return
}
success := false
var qe *QmpError = nil
for repeat := 0; !success && repeat < 3; repeat++ {
if len(cmd.Scm) > 0 {
glog.V(1).Infof("send cmd with scm (%d bytes) (%d) %s", len(cmd.Scm), repeat+1, string(msg))
f, _ := conn.File()
fd := f.Fd()
syscall.Sendmsg(int(fd), msg, cmd.Scm, nil, 0)
} else {
glog.V(1).Infof("sending command (%d) %s", repeat+1, string(msg))
conn.Write(msg)
}
res, ok := <-feedback
if !ok {
glog.Info("QMP command result chan closed")
return
}
switch res.MessageType() {
case QMP_RESULT:
success = true
break
//success
case QMP_ERROR:
glog.Warning("got one qmp error")
qe = res.(*QmpError)
time.Sleep(1000 * time.Millisecond)
case QMP_INTERNAL_ERROR:
glog.Info("QMP quit... commander quit... ")
return
}
}
if !success {
handler <- qe.Finish(session.respond)
return
}
}
handler <- session.Finish()
return
}
示例2: Get
// Get receives file descriptors from a Unix domain socket.
//
// Num specifies the expected number of file descriptors in one message.
// Internal files' names to be assigned are specified via optional filenames
// argument.
//
// You need to close all files in the returned slice. The slice can be
// non-empty even if this function returns an error.
//
// Use net.FileConn() if you're receiving a network connection.
func Get(via *net.UnixConn, num int, filenames []string) ([]*os.File, error) {
if num < 1 {
return nil, nil
}
// get the underlying socket
viaf, err := via.File()
if err != nil {
return nil, err
}
socket := int(viaf.Fd())
defer viaf.Close()
// recvmsg
buf := make([]byte, syscall.CmsgSpace(num*4))
_, _, _, _, err = syscall.Recvmsg(socket, nil, buf, 0)
if err != nil {
return nil, err
}
// parse control msgs
var msgs []syscall.SocketControlMessage
msgs, err = syscall.ParseSocketControlMessage(buf)
// convert fds to files
res := make([]*os.File, 0, len(msgs))
for i := 0; i < len(msgs) && err == nil; i++ {
var fds []int
fds, err = syscall.ParseUnixRights(&msgs[i])
for fi, fd := range fds {
var filename string
if fi < len(filenames) {
filename = filenames[fi]
}
res = append(res, os.NewFile(uintptr(fd), filename))
}
}
return res, err
}
示例3: Put
// Put sends file descriptors to Unix domain socket.
//
// Please note that the number of descriptors in one message is limited
// and is rather small.
// Use conn.File() to get a file if you want to put a network connection.
func Put(via *net.UnixConn, files ...*os.File) error {
if len(files) == 0 {
return nil
}
viaf, err := via.File()
if err != nil {
return err
}
socket := int(viaf.Fd())
defer viaf.Close()
fds := make([]int, len(files))
for i := range files {
fds[i] = int(files[i].Fd())
}
rights := syscall.UnixRights(fds...)
return syscall.Sendmsg(socket, nil, rights, nil, 0)
}