本文整理汇总了Golang中net.UnixConn类的典型用法代码示例。如果您正苦于以下问题:Golang UnixConn类的具体用法?Golang UnixConn怎么用?Golang UnixConn使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UnixConn类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: readFrom
func (fd *Fd) readFrom(c *net.UnixConn) error {
var b []byte
oob := make([]byte, 16)
_, oobn, _, _, err := c.ReadMsgUnix(b, oob)
if err != nil {
return err
}
if oobn == 0 {
return errors.New("error reading oob")
}
oob = oob[:oobn]
scms, err := syscall.ParseSocketControlMessage(oob)
if err != nil {
return err
}
if len(scms) != 1 {
return fmt.Errorf("expected 1 SocketControlMessage, got %d", len(scms))
}
scm := scms[0]
fds, err := syscall.ParseUnixRights(&scm)
if err != nil {
return nil
}
if len(fds) != 1 {
return fmt.Errorf("expected 1 fd, got %d", len(fds))
}
*fd = Fd(fds[0])
return nil
}
示例2: connectionHandler
func (i *instance) connectionHandler(c *net.UnixConn) {
defer c.Close()
for {
req, err := readRequest(c)
if err == io.EOF {
return // Client closed the connection.
}
if err != nil {
glog.Errorln("Failed to read a message from socket:", err)
}
f, t := funcMap[req.Type]
if t != true {
sendError(c, fmt.Errorf("unknown request type"))
continue
}
resp, err := f(i, req)
if err != nil {
sendError(c, err)
continue
}
err = writeResponse(c, resp)
if err != nil {
glog.Errorln("Failed to write a message to socket:", err)
return
}
}
}
示例3: writeData
func writeData(conn *net.UnixConn, files []*os.File, pid int, responseErr error) {
var errMsg string = ""
if responseErr != nil {
errMsg = responseErr.Error()
}
response := &Response{
Pid: pid,
ErrMessage: errMsg,
}
responseJson, _ := json.Marshal(response) // Ignore error
args := make([]int, len(files))
for i, f := range files {
args[i] = int(f.Fd())
}
resp := syscall.UnixRights(args...)
conn.WriteMsgUnix(responseJson, resp, nil) // Ignore error
// Close the files whose descriptors have been sent to the host to ensure that
// a close on the host takes effect in a timely fashion.
for _, file := range files {
file.Close() // Ignore error
}
}
示例4: RemoteRecvCredentials
func RemoteRecvCredentials(conn *net.UnixConn) (uint32, uint32, error) {
err := syscall.SetsockoptInt(sysfd(conn), syscall.SOL_SOCKET, syscall.SO_PASSCRED, 1)
if err != nil {
return 0, 0, err
}
oob := make([]byte, len(syscall.UnixCredentials(&syscall.Ucred{})))
_, _, _, _, err = conn.ReadMsgUnix(nil, oob)
if err != nil {
return 0, 0, err
}
scm, err := syscall.ParseSocketControlMessage(oob)
if err != nil {
return 0, 0, err
}
ucred, err := syscall.ParseUnixCredentials(&scm[0])
if err != nil {
return 0, 0, err
}
return ucred.Uid, ucred.Gid, nil
}
示例5: SendRequest
func SendRequest(c *net.UnixConn, req *server.Request) error {
payload, err := json.Marshal(req)
if err != nil {
return err
}
err = binary.Write(c, binary.BigEndian, uint32(len(payload)))
if err != nil {
return err
}
n, err := c.Write(payload)
if err != nil {
return err
} else if n != len(payload) {
return fmt.Errorf("Failed to write full payload, expected %v, wrote %v", len(payload), n)
}
if !req.HasFds {
return nil
}
// Send filedescriptors with a 1 byte message.
var oob []byte
oob = syscall.UnixRights(req.Fds...)
payload = make([]byte, 1)
n, oobn, err := c.WriteMsgUnix(payload, oob, nil)
if err != nil {
return err
} else if n != len(payload) || oobn != len(oob) {
return fmt.Errorf("Error writing to socket, expected n=%v got %v, oob=%v got %v", len(payload), n, len(oob), oobn)
}
return nil
}
示例6: daemonIpcHandleConn
func daemonIpcHandleConn(conn *net.UnixConn) {
defer cluegetterRecover("daemonIpcHandleConn")
defer conn.Close()
for {
message, err := bufio.NewReader(conn).ReadString('\x00')
if err != nil {
if message != "" {
Log.Info("Got %s on IPC Socket. Ignoring.", err.Error())
}
return
}
kv := strings.SplitN(message, " ", 2)
handle := strings.TrimRightFunc(kv[0], func(v rune) bool { return v == '\x00' })
callback := ipcHandlers[handle]
v := ""
if len(kv) > 1 {
v = strings.TrimRightFunc(kv[1], func(v rune) bool { return v == '\x00' })
}
if callback == nil {
Log.Debug("Received IPC message but no such pattern was registered, ignoring: <%s>%s", handle, v)
return
}
callback(v)
}
}
示例7: handleSocket
// handle the socket connection. When data comes in on the socket write it
// to the channel so the serial port can see it. When data comes in over the
// channel translate the message and write it to the socket
func handleSocket(conn *net.UnixConn, ch chan []byte) {
defer conn.Close()
readCh := make(chan []byte)
go socketRead(conn, readCh)
for {
select {
case s := <-ch:
{
// map micro -> gui
trans := mapMicro.ItemTranslate(string(s))
_, err := conn.Write([]byte(trans))
if err != nil {
fmt.Println(err)
}
}
case r := <-readCh:
{
ch <- r
}
case <-time.After(timeout):
continue
}
}
}
示例8: receiveUnix
func receiveUnix(conn *net.UnixConn) ([]byte, []int, error) {
buf := make([]byte, 4096)
oob := make([]byte, 4096)
bufn, oobn, _, _, err := conn.ReadMsgUnix(buf, oob)
if err != nil {
return nil, nil, err
}
return buf[:bufn], extractFds(oob[:oobn]), nil
}
示例9: logPrinter
func logPrinter(c *net.UnixConn) {
for {
var buf [1024]byte
nr, err := c.Read(buf[:])
if err != nil {
panic(err)
}
fmt.Printf("%s\n", string(buf[:nr]))
}
}
示例10: handleConn
func (srv *Server) handleConn(conn *net.UnixConn) {
err := srv.doRequest(conn)
resp := "Ok\n"
if err != nil {
resp = err.Error()
}
conn.Write(([]byte)(resp))
conn.Close()
}
示例11: writeTo
func (s String) writeTo(c *net.UnixConn) error {
l := s.len()
if err := binary.Write(c, HostOrder, l); err != nil {
return err
}
d := make([]byte, l)
copy(d, s)
_, err := c.Write(d)
return err
}
示例12: readRequest
func readRequest(c *net.UnixConn) (*Request, error) {
var l uint32
err := binary.Read(c, binary.BigEndian, &l)
length := int(l)
if err != nil {
return nil, err
}
payload := make([]byte, length)
n, err := c.Read(payload)
if err != nil {
return nil, err
} else if n != length {
return nil, fmt.Errorf("Payload was %d bytes rather than reported size of %d", n, length)
}
req := &Request{}
err = json.Unmarshal(payload, req)
if err != nil {
return nil, err
}
if !req.HasFds {
return req, nil
}
payload = make([]byte, 1)
// TODO: does this buffer need to be configurable?
oob := make([]byte, 8192)
n, oobn, _, _, err := c.ReadMsgUnix(payload, oob)
if err != nil && err != io.EOF {
return nil, err
}
if n != 1 {
return nil, fmt.Errorf("Error reading OOB filedescriptors")
}
oob = oob[0:oobn]
scm, err := syscall.ParseSocketControlMessage(oob)
if err != nil {
return nil, fmt.Errorf("Error parsing socket control message: %v", err)
}
var fds []int
for i := 0; i < len(scm); i++ {
tfds, err := syscall.ParseUnixRights(&scm[i])
if err == syscall.EINVAL {
continue // Wasn't a UnixRights Control Message
} else if err != nil {
return nil, fmt.Errorf("Error parsing unix rights: %v", err)
}
fds = append(fds, tfds...)
}
if len(fds) == 0 {
return nil, fmt.Errorf("Failed to receive any FDs on a request with HasFds == true")
}
req.ReceivedFds = fds
return req, nil
}
示例13: WriteFile
func WriteFile(c *net.UnixConn, file *os.File, timeout time.Duration) error {
if timeout > 0 {
deadline := time.Now().Add(timeout)
if err := c.SetWriteDeadline(deadline); err != nil {
return err
}
}
oob := syscall.UnixRights(int(file.Fd()))
_, _, err := c.WriteMsgUnix(nil, oob, nil)
return err
}
示例14: readUnixConn
func readUnixConn(conn *net.UnixConn, msgs chan []byte) {
for {
msg := make([]byte, maxPktSize)
nread, err := conn.Read(msg)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read from unix socket: %v\n", err)
return
}
msgs <- msg[:nread]
}
}
示例15: RemoteSendCredentials
func RemoteSendCredentials(conn *net.UnixConn) error {
ucred := &syscall.Ucred{
Pid: int32(os.Getpid()),
Uid: uint32(os.Getuid()),
Gid: uint32(os.Getgid()),
}
oob := syscall.UnixCredentials(ucred)
_, _, err := conn.WriteMsgUnix(nil, oob, nil)
return err
}