当前位置: 首页>>代码示例>>Golang>>正文


Golang UnixConn.Read方法代码示例

本文整理汇总了Golang中net.UnixConn.Read方法的典型用法代码示例。如果您正苦于以下问题:Golang UnixConn.Read方法的具体用法?Golang UnixConn.Read怎么用?Golang UnixConn.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在net.UnixConn的用法示例。


在下文中一共展示了UnixConn.Read方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: handleConnection

func (ch *ctlSockHandler) handleConnection(conn *net.UnixConn) {
	buf := make([]byte, ReadBufSize)
	for {
		n, err := conn.Read(buf)
		if err == io.EOF {
			conn.Close()
			return
		} else if err != nil {
			tlog.Warn.Printf("ctlsock: Read error: %#v", err)
			conn.Close()
			return
		}
		if n == ReadBufSize {
			tlog.Warn.Printf("ctlsock: request too big (max = %d bytes)", ReadBufSize-1)
			conn.Close()
			return
		}
		buf = buf[:n]
		var in RequestStruct
		err = json.Unmarshal(buf, &in)
		if err != nil {
			tlog.Warn.Printf("ctlsock: Unmarshal error: %#v", err)
			errorMsg := ResponseStruct{
				ErrNo:   int32(syscall.EINVAL),
				ErrText: err.Error(),
			}
			sendResponse(&errorMsg, conn)
		}
		ch.handleRequest(&in, conn)
		// Restore original size.
		buf = buf[:cap(buf)]
	}
}
开发者ID:rfjakob,项目名称:gocryptfs,代码行数:33,代码来源:ctlsock_serve.go

示例2: readFrom

func (a *Array) readFrom(c *net.UnixConn) error {
	var l uint32
	if err := binary.Read(c, HostOrder, l); err != nil {
		return err
	}
	*a = make([]byte, l)
	_, err := c.Read(*a)
	return err
}
开发者ID:vasiliyl,项目名称:playwand,代码行数:9,代码来源:types.go

示例3: 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

示例4: 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

示例5: 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

示例6: socketRead

// socket read, we run this in a goroutine so we can block on read
// on data read send through the channel
func socketRead(conn *net.UnixConn, ch chan []byte) {
	buf := make([]byte, 512)
	for {
		n, err := conn.Read(buf)
		if nil != err {
			log.Fatal(err)
		}

		if n > 0 {
			ch <- buf[:n]
		}
	}
}
开发者ID:jmore-reachtech,项目名称:io,代码行数:15,代码来源:sio.go

示例7: ReceiveResponse

func ReceiveResponse(c *net.UnixConn) (*server.Response, 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 && err != io.EOF {
		return nil, err
	} else if n != length {
		return nil, fmt.Errorf("Read %d bytes and expected %d bytes", n, length)
	}
	var resp server.Response
	err = json.Unmarshal(payload, &resp)
	if err != nil {
		return nil, err
	}
	return &resp, nil
}
开发者ID:akramer,项目名称:lateral,代码行数:21,代码来源:client.go

示例8: ReadWaylandMessage

func ReadWaylandMessage(conn *net.UnixConn) (*Message, error) {
	var buf [8]byte
	msg := Message{}
	control := make([]byte, 24)

	n, oobn, _, _, err := conn.ReadMsgUnix(buf[:], control)
	if err != nil {
		return nil, err
	}
	if n != 8 {
		return nil, errors.New("Unable to read message header.")
	}
	if oobn > 0 {
		if oobn > len(control) {
			panic("Unsufficient control msg buffer")
		}
		msg.control_msgs, err = syscall.ParseSocketControlMessage(control)
		if err != nil {
			panic(fmt.Sprintf("Control message parse error: %s", err.Error()))
		}
	}

	msg.Id = ProxyId(binary.LittleEndian.Uint32(buf[0:4]))
	msg.Opcode = uint32(binary.LittleEndian.Uint16(buf[4:6]))
	msg.size = uint32(binary.LittleEndian.Uint16(buf[6:8]))

	// subtract 8 bytes from header
	data := make([]byte, msg.size-8)

	n, err = conn.Read(data)
	if err != nil {
		return nil, err
	}
	if n != int(msg.size)-8 {
		return nil, errors.New("Invalid message size.")
	}
	msg.data = bytes.NewBuffer(data)

	return &msg, nil
}
开发者ID:stanluk,项目名称:wayland-client,代码行数:40,代码来源:message.go

示例9: readTtyMessage

func readTtyMessage(conn *net.UnixConn) (*hyperstartapi.TtyMessage, error) {
	needRead := 12
	length := 0
	read := 0
	buf := make([]byte, 512)
	res := []byte{}
	for read < needRead {
		want := needRead - read
		if want > 512 {
			want = 512
		}
		glog.V(1).Infof("tty: trying to read %d bytes", want)
		nr, err := conn.Read(buf[:want])
		if err != nil {
			glog.Error("read tty data failed")
			return nil, err
		}

		res = append(res, buf[:nr]...)
		read = read + nr

		glog.V(1).Infof("tty: read %d/%d [length = %d]", read, needRead, length)

		if length == 0 && read >= 12 {
			length = int(binary.BigEndian.Uint32(res[8:12]))
			glog.V(1).Infof("data length is %d", length)
			if length > 12 {
				needRead = length
			}
		}
	}

	return &hyperstartapi.TtyMessage{
		Session: binary.BigEndian.Uint64(res[:8]),
		Message: res[12:],
	}, nil
}
开发者ID:gnawux,项目名称:runv,代码行数:37,代码来源:tty.go

示例10: ReadVmMessage

func ReadVmMessage(conn *net.UnixConn) (*DecodedMessage, error) {
	needRead := 8
	length := 0
	read := 0
	buf := make([]byte, 512)
	res := []byte{}
	for read < needRead {
		want := needRead - read
		if want > 512 {
			want = 512
		}
		glog.V(1).Infof("trying to read %d bytes", want)
		nr, err := conn.Read(buf[:want])
		if err != nil {
			glog.Error("read init data failed")
			return nil, err
		}

		res = append(res, buf[:nr]...)
		read = read + nr

		glog.V(1).Infof("read %d/%d [length = %d]", read, needRead, length)

		if length == 0 && read >= 8 {
			length = int(binary.BigEndian.Uint32(res[4:8]))
			glog.V(1).Infof("data length is %d", length)
			if length > 8 {
				needRead = length
			}
		}
	}

	return &DecodedMessage{
		Code:    binary.BigEndian.Uint32(res[:4]),
		Message: res[8:],
	}, nil
}
开发者ID:carmark,项目名称:runv,代码行数:37,代码来源:init_comm.go

示例11: readResp

// read command response/output
func readResp(c *net.UnixConn) *response {
	buf := make([]byte, 1024)

	var output []byte

	var read int
	var ret uint16

	n, err := c.Read(buf[:4])
	if err != nil {
		return &response{err: err}
	}

	// start read
	if n != 4 {
		return &response{err: fmt.Errorf("invalid response data: %s", buf[0:n])}
	}

	// length of output
	toRead := int(binary.BigEndian.Uint32(buf[0:n]))

	for read < toRead {
		min := min(toRead-read, 1024)

		n, err := c.Read(buf[:min])
		if err != nil {
			return &response{err: err}
		}

		output = append(output, buf[0:n]...)
		read += n
	}

	n, err = c.Read(buf[:2])
	if err != nil {
		return &response{err: err}
	}

	if n != 2 {
		return &response{err: fmt.Errorf("invalid response data: %s", buf[0:n])}
	}

	ret = binary.BigEndian.Uint16(buf[0:n])

	return &response{output: string(output), ret: ret}
}
开发者ID:mikkeloscar,项目名称:conprox,代码行数:47,代码来源:client.go


注:本文中的net.UnixConn.Read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。