// 读取Frame的完整的数据,包含
func (p *TBufferedFramedTransport) ReadFrame() (frame []byte, err error) {

	if p.FrameSize != 0 {
		err = thrift.NewTTransportExceptionFromError(
			fmt.Errorf("Unexpected frame size: %d", p.FrameSize))
		return nil, err
	}
	var frameSize int

	// Reader来自transport, 中间被封装了多长
	frameSize, err = p.readFrameHeader()
	if err != nil {
		err1, ok := err.(thrift.TTransportException)

		if ok {
			err = thrift.NewTTransportException(err1.TypeId(),
				fmt.Sprintf("Frame Header Read Error: %s", err1.Error()))
		} else {
			err = thrift.NewTTransportExceptionFromError(
				fmt.Errorf("Frame Header Read Error: %s", err.Error()))
		}
		return
	}

	bytes := getSlice(frameSize, frameSize)

	// 什么时候会出现?
	// 1. 如果tcp package比较大,则容易出现package的一部分先到,另一部分随后再到
	// 2. 在同一个机器上的两个进程之间出现的概率低(Time Delay小); 跨机器访问,出现概率高
	var l int
	l, err = io.ReadFull(p.Reader, bytes)
	//	l, err = p.Reader.Read(bytes)
	if l != frameSize {
		log.Warnf(Red("<==== ReadFrame frame size: %d, Got: %d"), frameSize, l)
	}

	if err != nil {
		err1, ok := err.(thrift.TTransportException)
		if ok {
			err = thrift.NewTTransportException(err1.TypeId(),
				fmt.Sprintf("Frame Data Read Error: %s", err1.Error()))
		} else {
			err = thrift.NewTTransportExceptionFromError(
				fmt.Errorf("Frame Data Read Error: %s", err.Error()))
		}
		return nil, err
	}

	return bytes, nil

}
func (p *TBufferedFramedTransport) Read(buf []byte) (l int, err error) {

	// 1. 首先读取Frame Header
	if p.FrameSize == 0 {
		p.FrameSize, err = p.readFrameHeader()
		if err != nil {
			return
		}
	}

	// 2. 异常处理
	if p.FrameSize < len(buf) {
		frameSize := p.FrameSize
		tmp := make([]byte, p.FrameSize)
		l, err = p.Read(tmp)
		copy(buf, tmp)
		if err == nil {
			err = thrift.NewTTransportExceptionFromError(
				fmt.Errorf("Not enough frame size %d to read %d bytes",
					frameSize, len(buf)))
			return
		}
	}

	// 3. 读取剩下的数据
	got, err := p.Reader.Read(buf)
	p.FrameSize = p.FrameSize - got
	//sanity check
	if p.FrameSize < 0 {
		return 0, thrift.NewTTransportException(thrift.UNKNOWN_TRANSPORT_EXCEPTION,
			"Negative frame size")
	}
	return got, thrift.NewTTransportExceptionFromError(err)
}
Пример #3
0
func (p *TUnixDomain) Write(buf []byte) (int, error) {
	if !p.IsOpen() {
		return 0, thrift.NewTTransportException(thrift.NOT_OPEN, "Connection not open")
	}
	p.pushDeadline(false, true)
	return p.conn.Write(buf)
}
Пример #4
0
func (p *TUnixDomain) Read(buf []byte) (int, error) {
	if !p.IsOpen() {
		return 0, thrift.NewTTransportException(thrift.NOT_OPEN, "Connection not open")
	}

	p.pushDeadline(true, false)
	n, err := p.conn.Read(buf)
	return n, thrift.NewTTransportExceptionFromError(err)
}
Пример #5
0
// Connects the socket, creating a new socket object if necessary.
func (p *TUnixDomain) Open() error {
	if p.IsOpen() {
		return thrift.NewTTransportException(thrift.ALREADY_OPEN, "Socket already connected.")
	}
	if p.addr == nil {
		return thrift.NewTTransportException(thrift.NOT_OPEN, "Cannot open nil address.")
	}
	if len(p.addr.Network()) == 0 {
		return thrift.NewTTransportException(thrift.NOT_OPEN, "Cannot open bad network name.")
	}
	if len(p.addr.String()) == 0 {
		return thrift.NewTTransportException(thrift.NOT_OPEN, "Cannot open bad address.")
	}
	var err error
	if p.conn, err = net.DialTimeout(p.addr.Network(), p.addr.String(), p.timeout); err != nil {
		return thrift.NewTTransportException(thrift.NOT_OPEN, err.Error())
	}
	return nil
}
Пример #6
0
func (p *TFramedTransport) readFrameHeader() (int, error) {
	buf := p.buffer[:4]
	if _, err := io.ReadFull(p.reader, buf); err != nil {
		return 0, err
	}
	size := int(binary.BigEndian.Uint32(buf))
	if size < 0 || size > p.maxLength {
		return 0, thrift.NewTTransportException(thrift.UNKNOWN_TRANSPORT_EXCEPTION, fmt.Sprintf("Incorrect frame size (%d)", size))
	}
	return size, nil
}
// Connects the socket, creating a new socket object if necessary.
func (p *TServerUnixDomain) Open() error {
	if p.IsListening() {
		return thrift.NewTTransportException(thrift.ALREADY_OPEN,
			"Server socket already open")
	}
	if l, err := net.Listen(p.addr.Network(), p.addr.String()); err != nil {
		return err
	} else {
		p.listener = l
	}
	return nil
}
func (p *TServerUnixDomain) Accept() (thrift.TTransport, error) {
	p.mu.RLock()
	interrupted := p.interrupted
	p.mu.RUnlock()

	if interrupted {
		return nil, errors.New("Transport Interrupted")
	}
	if p.listener == nil {
		return nil, thrift.NewTTransportException(thrift.NOT_OPEN,
			"No underlying server socket")
	}
	conn, err := p.listener.Accept()
	if err != nil {
		return nil, thrift.NewTTransportExceptionFromError(err)
	}
	return thrift.NewTSocketFromConnTimeout(conn, p.clientTimeout), nil
}
Пример #9
0
func (p *TFramedTransport) Read(buf []byte) (l int, err error) {
	// 确保Transport是打开的
	if !p.IsOpen() {
		err = p.Open()
		if err != nil {
			return 0, err
		}
	}

	if p.frameSize == 0 {
		p.frameSize, err = p.readFrameHeader()
		if err != nil {
			p.Close()
			return
		}
	}
	if p.frameSize < len(buf) {
		frameSize := p.frameSize
		tmp := make([]byte, p.frameSize)
		l, err = p.Read(tmp)
		copy(buf, tmp)
		if err == nil {
			p.Close()
			err = thrift.NewTTransportExceptionFromError(fmt.Errorf("Not enough frame size %d to read %d bytes", frameSize, len(buf)))
			return
		}
	}
	got, err := p.reader.Read(buf)
	p.frameSize = p.frameSize - got
	//sanity check
	if p.frameSize < 0 {
		p.Close()
		return 0, thrift.NewTTransportException(thrift.UNKNOWN_TRANSPORT_EXCEPTION, "Negative frame size")
	}
	if err != nil {
		p.Close()
	}
	return got, thrift.NewTTransportExceptionFromError(err)
}