當前位置: 首頁>>代碼示例>>Golang>>正文


Golang net.UnixConn類代碼示例

本文整理匯總了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
}
開發者ID:vasiliyl,項目名稱:playwand,代碼行數:30,代碼來源:types.go

示例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
		}
	}
}
開發者ID:akramer,項目名稱:lateral,代碼行數:27,代碼來源:server.go

示例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
	}
}
開發者ID:julz,項目名稱:garden-runc,代碼行數:26,代碼來源:listener.go

示例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
}
開發者ID:jessevdk,項目名稱:autobuild,代碼行數:29,代碼來源:remote_linux.go

示例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
}
開發者ID:akramer,項目名稱:lateral,代碼行數:31,代碼來源:client.go

示例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)
	}
}
開發者ID:EaterOfCode,項目名稱:ClueGetter,代碼行數:28,代碼來源:daemon.go

示例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
		}
	}
}
開發者ID:jmore-reachtech,項目名稱:io,代碼行數:30,代碼來源:sio.go

示例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
}
開發者ID:ChaosCloud,項目名稱:docker,代碼行數:9,代碼來源:unix.go

示例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]))
	}
}
開發者ID:CiscoKorea,項目名稱:haproxy-rest,代碼行數:10,代碼來源:logListener.go

示例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()
}
開發者ID:bboreham,項目名稱:ambergris,代碼行數:10,代碼來源:server.go

示例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
}
開發者ID:vasiliyl,項目名稱:playwand,代碼行數:10,代碼來源:types.go

示例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
}
開發者ID:akramer,項目名稱:lateral,代碼行數:54,代碼來源:socketfuncs.go

示例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
}
開發者ID:sunfmin,項目名稱:learn_go_the_hard_way,代碼行數:11,代碼來源:main.go

示例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]
	}
}
開發者ID:postfix,項目名稱:sdnotify-proxy,代碼行數:12,代碼來源:main.go

示例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
}
開發者ID:jessevdk,項目名稱:autobuild,代碼行數:12,代碼來源:remote_linux.go


注:本文中的net.UnixConn類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。