Пример #1
0
func (this *PacketSlicesHandler) Process(s *netlib.Session, data interface{}) error {
	if packetslices, ok := data.(*protocol.SSPacketSlices); ok {
		seqNo := int(packetslices.GetSeqNo())
		if seqNo < 1 {
			return errors.New("PacketSlicesHandler unexpect packet seq:" + strconv.Itoa(seqNo))
		}
		totalSize := int(packetslices.GetTotalSize())
		if totalSize > s.GetSessionConfig().MaxPacket {
			return errors.New("PacketSlicesHandler exceed MaxPacket size:" + strconv.Itoa(s.GetSessionConfig().MaxPacket) + " size=" + strconv.Itoa(totalSize))
		}
		attr := s.GetAttribute(SessionAttributeBigBuf)
		if seqNo == 1 {
			if attr == nil {
				attr = bytes.NewBuffer(make([]byte, 0, packetslices.GetTotalSize()))
				s.SetAttribute(SessionAttributeBigBuf, attr)
			}
		}
		if seqNo > 1 {
			if attr == nil {
				return errors.New("PacketSlicesHandler Incorrect packet seq, expect seq=1")
			}
		} else if attr == nil {
			return errors.New("PacketSlicesHandler get bytesbuf failed")
		}

		buf := attr.(*bytes.Buffer)
		if seqNo == 1 {
			buf.Reset()
		}
		if buf.Len() != int(packetslices.GetOffset()) {
			return errors.New("PacketSlicesHandler get next packet offset error")
		}
		buf.Write(packetslices.GetPacketData())
		if buf.Len() == totalSize {
			packetid, pck, err := netlib.UnmarshalPacket(buf.Bytes())
			if err != nil {
				return err
			}
			h := netlib.GetHandler(packetid)
			if h != nil {
				h.Process(s, pck)
			}
		}
	}
	return nil
}
Пример #2
0
func (this *PacketRedirectHandler) Process(s *netlib.Session, data interface{}) error {
	logger.Trace("PacketRedirectHandler.Process")
	if pr, ok := data.(*protocol.SSPacketRedirect); ok {
		packid, pack, err := netlib.UnmarshalPacket(pr.GetData())
		if err != nil {
			return err
		}
		h := srvlib.GetHandler(packid)
		if h != nil {
			return h.Process(s, pack, pr.GetClientSid(), pr.GetSrvRoutes())
		} else {
			nh := netlib.GetHandler(packid)
			if nh != nil {
				return nh.Process(s, pack)
			}
		}
	}
	return nil
}