示例#1
0
func (h *Handshake) handle(r *xio.PacketReader) (*packet, error) {
	if marker, err := r.Read8(); err != nil {
		return nil, errors.New("packet.read marker")
	} else {
		if _, err := r.Read16(); err != nil {
			return nil, errors.New("packet.read time")
		}
		if marker != 0x0b {
			counts.Count("handshake.marker.unknown", 1)
			return nil, errors.New(fmt.Sprintf("packet.unknown marker = 0x%02x", marker))
		}
	}
	if msg, err := rtmfp.ParseRequestMessage(r); err != nil {
		return nil, err
	} else {
		switch msg.Code {
		default:
			counts.Count("handshake.code.unknown", 1)
			return nil, errors.New(fmt.Sprintf("message.unknown code = 0x%02x", msg.Code))
		case 0x30:
			if rsp, err := h.handleHello(msg.PacketReader); err != nil {
				counts.Count("handshake.hello.error", 1)
				return nil, err
			} else {
				return &packet{0, rsp}, nil
			}
		case 0x38:
			if rsp, yid, err := h.handleAssign(msg.PacketReader); err != nil {
				counts.Count("handshake.assign.error", 1)
				return nil, err
			} else {
				return &packet{yid, rsp}, nil
			}
		}
	}
}
示例#2
0
文件: session.go 项目: Yuhang/xserver
func (s *Session) handle(r *xio.PacketReader) error {
	if marker, err := r.Read8(); err != nil {
		return errors.New("packet.read marker")
	} else {
		if s.stmptime, err = r.Read16(); err != nil {
			return errors.New("packet.read time")
		}
		switch marker | 0xf0 {
		default:
			counts.Count("session.marker.unknown", 1)
			return errors.New(fmt.Sprintf("packet.unknown marker = 0x%02x", marker))
		case 0xfd:
			if _, err = r.Read16(); err != nil {
				return errors.New("packet.read ping time")
			}
		case 0xf9:
		}
	}

	msglist := list.New()
	for r.Len() != 0 {
		if msg, err := rtmfp.ParseRequestMessage(r); err != nil {
			if err != rtmfp.EOP {
				return err
			}
			break
		} else {
			msglist.PushBack(msg)
		}
	}

	var lastreq *flowRequest = nil
	for e := msglist.Front(); e != nil; e = e.Next() {
		msg := e.Value.(*rtmfp.RequestMessage)
		if msg.Code != 0x11 && lastreq != nil {
			if err := s.handleFlowRequest(lastreq); err != nil {
				return err
			}
			lastreq = nil
		}
		switch msg.Code {
		default:
			s.Close()
			counts.Count("session.code.unknown", 1)
			return errors.New(fmt.Sprintf("message.close code = 0x%02x", msg.Code))
		case 0x4c:
			s.Close()
			counts.Count("session.code.close", 1)
			return nil
		case 0x01:
			s.send(newKeepAliveResponse(true))
		case 0x41:
		case 0x5e:
			if req, err := parseFlowErrorRequest(msg.PacketReader); err != nil {
				counts.Count("session.parse5e.error", 1)
				return err
			} else if fw := s.writers[req.fid]; fw != nil {
				fw.reader.handler.OnClose()
			} else {
				xlog.OutLog.Printf("[session]: xid = %d, writer.fid = %d, flow not found 0x5e\n", s.xid, req.fid)
			}
		case 0x51:
			if req, err := parseFlowAckRequest(msg.PacketReader); err != nil {
				counts.Count("session.parse51.error", 1)
				return err
			} else if fw := s.writers[req.fid]; fw != nil {
				fw.CommitAck(req.cnt, req.ack)
			} else {
				xlog.OutLog.Printf("[session]: xid = %d, writer.fid = %d, flow not found 0x51\n", s.xid, req.fid)
			}
		case 0x10:
			if req, err := parseFlowRequest(msg.PacketReader); err != nil {
				counts.Count("session.parse10.error", 1)
				return err
			} else {
				lastreq = req
			}
		case 0x11:
			if req, err := parseFlowRequestSlice(msg.PacketReader); err != nil {
				counts.Count("session.parse11.error", 1)
				return err
			} else if lastreq != nil {
				lastreq.AddSlice(req)
			} else {
				xlog.OutLog.Printf("[session]: xid = %d, not following message\n", s.xid)
			}
		}
	}
	if lastreq != nil {
		return s.handleFlowRequest(lastreq)
	}
	return nil
}