Ejemplo n.º 1
0
func (d *directChannel) read() {
	defer d.Close()
	for {
		c := d.conn
		if nil == c {
			return
		}
		c.SetReadDeadline(time.Now().Add(d.ReadTimeout()))
		b := make([]byte, 1500)
		n, err := c.Read(b)
		if n > 0 {
			var ev event.Event
			if !d.udpProxyConn {
				ev = &event.TCPChunkEvent{Content: b[0:n]}
			} else {
				ev = &event.UDPEvent{Content: b[0:n]}
			}
			//log.Printf("######recv %T", ev)
			ev.SetId(d.sid)
			proxy.HandleEvent(ev)
		}
		if nil != err {
			if !d.udpProxyConn {
				closeEv := &event.ConnCloseEvent{}
				closeEv.SetId(d.sid)
				proxy.HandleEvent(closeEv)
			}
			return
		}
	}
}
Ejemplo n.º 2
0
func (p *ProxySession) publish(ev event.Event) {
	ev.SetId(p.Id.Id)
	start := time.Now()
	timeout := start.Add(60 * time.Second)
	for !p.closeByClient && time.Now().Before(timeout) {
		queue := getEventQueue(p.Id.ConnId, false)
		if nil != queue {
			err := queue.Publish(ev, 10*time.Second)
			if nil != err {
				continue
			}
			return
		}
		time.Sleep(5 * time.Millisecond)
	}
	if !p.closeByClient {
		log.Printf("Session[%s:%d] publish event timeout after %v", p.Id.User, p.Id.Id, time.Now().Sub(start))
		p.forceClose()
	}
}