Ejemplo n.º 1
0
// readRequest
func (server *Server) readTCPRequest(rr *bufio.Reader, proto *Proto) (err error) {
	var (
		packLen   int32
		headerLen int16
		bodyLen   int
	)
	if packLen, err = ioutil.ReadBigEndianInt32(rr); err != nil {
		return
	}
	if Conf.Debug {
		log.Debug("packLen: %d", packLen)
	}
	if packLen > maxPackLen {
		return ErrProtoPackLen
	}
	if headerLen, err = ioutil.ReadBigEndianInt16(rr); err != nil {
		return
	}
	if Conf.Debug {
		log.Debug("headerLen: %d", headerLen)
	}
	if headerLen != rawHeaderLen {
		return ErrProtoHeaderLen
	}
	if proto.Ver, err = ioutil.ReadBigEndianInt16(rr); err != nil {
		return
	}
	if Conf.Debug {
		log.Debug("protoVer: %d", proto.Ver)
	}
	if proto.Operation, err = ioutil.ReadBigEndianInt32(rr); err != nil {
		return
	}
	if Conf.Debug {
		log.Debug("operation: %d", proto.Operation)
	}
	if proto.SeqId, err = ioutil.ReadBigEndianInt32(rr); err != nil {
		return
	}
	if Conf.Debug {
		log.Debug("seqId: %d", proto.SeqId)
	}
	bodyLen = int(packLen - int32(headerLen))
	if Conf.Debug {
		log.Debug("read body len: %d", bodyLen)
	}
	if bodyLen > 0 {
		proto.Body = proto.Buf[0:bodyLen]
		if err = ioutil.ReadAll(rr, proto.Body); err != nil {
			log.Error("body: ReadAll() error(%v)", err)
			return
		}
	} else {
		proto.Body = nil
	}
	if Conf.Debug {
		log.Debug("read proto: %v", proto)
	}
	return
}
Ejemplo n.º 2
0
Archivo: tcp.go Proyecto: james4e/goim
// readRequest
func (server *Server) readTCPRequest(rr *bufio.Reader, pb []byte, proto *Proto) (err error) {
	var (
		packLen   int32
		headerLen int16
		bodyLen   int
	)
	if err = ioutil.ReadAll(rr, pb[:packLenSize]); err != nil {
		return
	}
	packLen = BigEndian.Int32(pb[:packLenSize])
	if Conf.Debug {
		log.Debug("packLen: %d", packLen)
	}
	if packLen > maxPackLen {
		return ErrProtoPackLen
	}
	if err = ioutil.ReadAll(rr, pb[:headerLenSize]); err != nil {
		return
	}
	headerLen = BigEndian.Int16(pb[:headerLenSize])
	if Conf.Debug {
		log.Debug("headerLen: %d", headerLen)
	}
	if headerLen != rawHeaderLen {
		return ErrProtoHeaderLen
	}
	if err = ioutil.ReadAll(rr, pb[:VerSize]); err != nil {
		return
	}
	proto.Ver = BigEndian.Int16(pb[:VerSize])
	if Conf.Debug {
		log.Debug("protoVer: %d", proto.Ver)
	}
	if err = ioutil.ReadAll(rr, pb[:OperationSize]); err != nil {
		return
	}
	proto.Operation = BigEndian.Int32(pb[:OperationSize])
	if Conf.Debug {
		log.Debug("operation: %d", proto.Operation)
	}
	if err = ioutil.ReadAll(rr, pb[:SeqIdSize]); err != nil {
		return
	}
	proto.SeqId = BigEndian.Int32(pb[:SeqIdSize])
	if Conf.Debug {
		log.Debug("seqId: %d", proto.SeqId)
	}
	bodyLen = int(packLen - int32(headerLen))
	if Conf.Debug {
		log.Debug("read body len: %d", bodyLen)
	}
	if bodyLen > 0 {
		// TODO reuse buf
		proto.Body = make([]byte, bodyLen)
		if err = ioutil.ReadAll(rr, proto.Body); err != nil {
			log.Error("body: ReadAll() error(%v)", err)
			return
		}
	} else {
		proto.Body = nil
	}
	if Conf.Debug {
		log.Debug("read proto: %v", proto)
	}
	return
}