示例#1
0
func sender(conn *net.TCPConn, send <-chan []byte, sig <-chan int, raise func()) {
	defer raise()
	for {
		select {
		case <-sig:
			return
		case data := <-send:
			if len(data) == 0 {
				continue
			}
			if err := writeData(conn, data); err != nil {
				log.Printf("[tcp]: send error = '%v'\n", err)
				return
			}
			xlog.TcpLog.Printf("tcp.send:\n%s", utils.Formatted(data))
		}
	}
}
示例#2
0
func recver(conn *net.TCPConn, recv chan<- []byte, sig <-chan int, raise func()) {
	defer raise()
	for {
		select {
		case <-sig:
			return
		default:
			if data, err := readData(conn); err != nil {
				log.Printf("[tcp]: recv error = '%v'\n", err)
				return
			} else if len(data) != 0 {
				select {
				case <-sig:
					return
				case recv <- data:
				}
				xlog.TcpLog.Printf("tcp.recv:\n%s", utils.Formatted(data))
			}
		}
	}
}
示例#3
0
func HandlePacket(lport uint16, raddr *net.UDPAddr, data []byte) {
	h := getHandshake()
	if h == nil {
		return
	}
	defer putHandshake(h)

	var err error
	if data, err = rtmfp.DecodePacket(h, data); err != nil {
		counts.Count("handshake.decode.error", 1)
		xlog.ErrLog.Printf("[handshake]: decode error = '%v'\n", err)
		return
	}
	xlog.OutLog.Printf("[handshake]: recv addr = [%s], data.len = %d\n%s\n", raddr, len(data), utils.Formatted(data))

	h.lport, h.raddr = lport, raddr

	var pkt *packet
	if pkt, err = h.handle(xio.NewPacketReader(data[6:])); err != nil {
		counts.Count("handshake.handle.error", 1)
		xlog.ErrLog.Printf("[handshake]: handle error = '%v'\n", err)
		return
	} else if pkt == nil {
		xlog.OutLog.Printf("[handshake]: response packet is empty\n")
		return
	}

	if data, err = rtmfp.PacketToBytes(pkt); err != nil {
		counts.Count("handshake.tobytes.error", 1)
		xlog.ErrLog.Printf("[handshake]: packet to bytes error = '%v'\n", err)
		return
	}
	xlog.OutLog.Printf("[handshake]: send addr = [%s], data.len = %d\n%s\n", raddr, len(data), utils.Formatted(data))

	if data, err = rtmfp.EncodePacket(h, pkt.yid, data); err != nil {
		counts.Count("handshake.encode.error", 1)
		xlog.ErrLog.Printf("[handshake]: encode packet error = '%v'\n", err)
		return
	}
	udp.Send(lport, raddr, data)
}
示例#4
0
func HandlePacket(lport uint16, raddr *net.UDPAddr, xid uint32, data []byte) {
	s := FindByXid(xid)
	if s == nil {
		counts.Count("session.notfound", 1)
		return
	}
	s.Lock()
	defer s.Unlock()
	if s.closed {
		counts.Count("session.hasclosed", 1)
		return
	}
	defer s.flush()

	var err error
	if data, err = rtmfp.DecodePacket(s, data); err != nil {
		counts.Count("session.decode.error", 1)
		xlog.ErrLog.Printf("[session]: decode error = '%v'\n", err)
		return
	}
	xlog.OutLog.Printf("[session]: recv addr = [%s], data.len = %d\n%s\n", raddr, len(data), utils.Formatted(data))

	s.lport, s.raddr = lport, raddr

	if len(s.cookie) != 0 {
		cookies.Commit(s.cookie)
		s.cookie = ""
		rpc.Join(s.xid, s.raddr)
	}

	s.manage.cnt, s.manage.lasttime = 0, time.Now().UnixNano()

	if err = s.handle(xio.NewPacketReader(data[6:])); err != nil {
		counts.Count("session.handle.error", 1)
		xlog.ErrLog.Printf("[session]: handle error = '%v'\n", err)
	}
}
示例#5
0
func flush(s *Session, msgs []rtmfp.ResponseMessage) {
	lport, raddr := s.lport, s.raddr
	if data, err := rtmfp.PacketToBytes(&packet{s.yid, s.manage.lasttime, s.stmptime, msgs}); err != nil {
		counts.Count("session.tobytes.error", 1)
		xlog.ErrLog.Printf("[session]: packet to bytes error = '%v'\n", err)
		return
	} else {
		xlog.OutLog.Printf("[session]: send addr = [%s], data.len = %d\n%s\n", raddr, len(data), utils.Formatted(data))
		if data, err = rtmfp.EncodePacket(s, s.yid, data); err != nil {
			counts.Count("session.encode.error", 1)
			xlog.ErrLog.Printf("[session]: encode packet error = '%v'\n", err)
			return
		}
		udp.Send(lport, raddr, data)
	}
}