Пример #1
0
func (r *req) receiver(ep mangos.Endpoint) {
	rq := r.sock.RecvChannel()
	cq := r.sock.CloseChannel()

	for {
		m := ep.RecvMsg()
		if m == nil {
			break
		}

		if len(m.Body) < 4 {
			m.Free()
			continue
		}
		m.Header = append(m.Header, m.Body[:4]...)
		m.Body = m.Body[4:]

		select {
		case rq <- m:
		case <-cq:
			m.Free()
			break
		}
	}
}
Пример #2
0
func (r *rep) receiver(ep mangos.Endpoint) {
	for {

		m := ep.RecvMsg()
		if m == nil {
			return
		}

		v := ep.GetID()
		m.Header = append(m.Header,
			byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
		// Move backtrace from body to header.
		for {
			if len(m.Body) < 4 {
				m.Free() // ErrGarbled
				return
			}
			m.Header = append(m.Header, m.Body[:4]...)
			m.Body = m.Body[4:]
			// Check for high order bit set (0x80000000, big endian)
			if m.Header[len(m.Header)-4]&0x80 != 0 {
				break
			}
		}

		select {
		case r.sock.RecvChannel() <- m:
		case <-r.sock.CloseChannel():
			m.Free()
			return
		}
	}
}
Пример #3
0
func (s *sub) receiver(ep mangos.Endpoint) {
	for {
		var matched = false

		m := ep.RecvMsg()
		if m == nil {
			return
		}

		s.Lock()
		for _, sub := range s.subs {
			if bytes.HasPrefix(m.Body, sub) {
				// Matched, send it up.  Best effort.
				matched = true
				break
			}
		}
		s.Unlock()

		if !matched {
			m.Free()
			continue
		}

		select {
		case s.sock.RecvChannel() <- m:
		case <-s.sock.CloseChannel():
			m.Free()
			return
		default: // no room, drop it
			m.Free()
		}
	}
}
Пример #4
0
func (x *surveyor) AddEndpoint(ep mangos.Endpoint) {
	peer := &surveyorP{ep: ep, x: x, q: make(chan *mangos.Message, 1)}
	x.Lock()
	x.peers[ep.GetID()] = peer
	go peer.receiver()
	go peer.sender()
	x.Unlock()
}
Пример #5
0
func (x *star) RemoveEndpoint(ep mangos.Endpoint) {
	x.Lock()
	if peer := x.eps[ep.GetID()]; peer != nil {
		delete(x.eps, ep.GetID())
		close(peer.q)
	}
	x.Unlock()
}
Пример #6
0
func (x *bus) RemoveEndpoint(ep mangos.Endpoint) {
	x.Lock()
	if peer := x.peers[ep.GetID()]; peer != nil {
		close(peer.q)
		delete(x.peers, ep.GetID())
	}
	x.Unlock()
}
Пример #7
0
func (r *req) AddEndpoint(ep mangos.Endpoint) {
	r.Lock()
	r.eps[ep.GetID()] = ep
	r.Unlock()
	r.senders.Add()
	go r.receiver(ep)
	go r.sender(ep)
}
Пример #8
0
func (x *surveyor) RemoveEndpoint(ep mangos.Endpoint) {
	x.Lock()
	defer x.Unlock()
	peer := x.peers[ep.GetID()]
	if peer == nil {
		return
	}
	delete(x.peers, ep.GetID())
}
Пример #9
0
func (x *push) AddEndpoint(ep mangos.Endpoint) {
	pe := &pushEp{ep: ep, cq: make(chan struct{})}
	x.Lock()
	x.eps[ep.GetID()] = pe
	x.Unlock()
	x.w.Add()
	go x.sender(pe)
	go mangos.NullRecv(ep)
}
Пример #10
0
func (p *pub) RemoveEndpoint(ep mangos.Endpoint) {
	id := ep.GetID()
	p.Lock()
	pe := p.eps[id]
	delete(p.eps, id)
	p.Unlock()
	if pe != nil {
		close(pe.q)
	}
}
Пример #11
0
func (x *push) RemoveEndpoint(ep mangos.Endpoint) {
	id := ep.GetID()
	x.Lock()
	pe := x.eps[id]
	delete(x.eps, id)
	x.Unlock()
	if pe != nil {
		close(pe.cq)
	}
}
Пример #12
0
func (r *rep) AddEndpoint(ep mangos.Endpoint) {
	pe := &repEp{ep: ep, r: r, q: make(chan *mangos.Message, 2)}
	pe.w.Init()
	r.Lock()
	r.eps[ep.GetID()] = pe
	r.Unlock()
	pe.w.Add()
	go r.receiver(ep)
	go pe.sender()
}
Пример #13
0
func (r *req) RemoveEndpoint(ep mangos.Endpoint) {
	id := ep.GetID()
	r.Lock()
	pe := r.eps[id]
	delete(r.eps, id)
	r.Unlock()
	if pe != nil {
		close(pe.cq)
	}
}
Пример #14
0
func (x *push) receiver(ep mangos.Endpoint) {
	// In order for us to detect a dropped connection, we need to poll
	// on the socket.  We don't care about the results and discard them,
	// but this allows the disconnect to be noticed.  Note that we will
	// be blocked in this call forever, until the connection is dropped.
	for {
		if m := ep.RecvMsg(); m == nil {
			break
		}
	}
}
Пример #15
0
func (x *surveyor) RemoveEndpoint(ep mangos.Endpoint) {
	id := ep.GetID()

	x.Lock()
	peer := x.peers[id]
	delete(x.peers, id)
	x.Unlock()

	if peer != nil {
		close(peer.q)
	}
}
Пример #16
0
func (x *star) AddEndpoint(ep mangos.Endpoint) {
	depth := 16
	if i, err := x.sock.GetOption(mangos.OptionWriteQLen); err == nil {
		depth = i.(int)
	}
	pe := &starEp{ep: ep, x: x, q: make(chan *mangos.Message, depth)}
	x.Lock()
	x.eps[ep.GetID()] = pe
	x.Unlock()
	x.senders.Add()
	go pe.sender()
	go pe.receiver()
}
Пример #17
0
func (x *resp) AddEndpoint(ep mangos.Endpoint) {
	x.init.Do(func() {
		x.w.Add()
		go x.sender()
	})
	peer := &respPeer{ep: ep, x: x, q: make(chan *mangos.Message, 1)}

	x.Lock()
	x.peers[ep.GetID()] = peer
	x.Unlock()

	go x.receiver(ep)
	go peer.sender()
}
Пример #18
0
func (x *pair) AddEndpoint(ep mangos.Endpoint) {
	x.Lock()
	if x.peer != nil {
		x.Unlock()
		ep.Close()
		return
	}
	x.peer = ep
	x.Unlock()

	x.senders.Add()
	go x.receiver(ep)
	go x.sender(ep)
}
Пример #19
0
func (x *resp) AddEndpoint(ep mangos.Endpoint) {
	x.Lock()
	if x.peer != nil {
		x.Unlock()
		ep.Close()
		return
	}
	peer := &respPeer{ep: ep, x: x, q: make(chan *mangos.Message, 1)}
	x.peer = peer
	x.Unlock()

	x.senders.Add()
	go peer.receiver()
	go peer.sender()
}
Пример #20
0
func (r *req) AddEndpoint(ep mangos.Endpoint) {

	r.init.Do(func() {
		r.w.Add()
		go r.resender()
	})

	pe := &reqEp{cq: make(chan struct{}), ep: ep}
	r.Lock()
	r.eps[ep.GetID()] = pe
	r.Unlock()
	go r.receiver(ep)
	r.w.Add()
	go r.sender(pe)
}
Пример #21
0
func (p *pub) AddEndpoint(ep mangos.Endpoint) {
	depth := 16
	if i, err := p.sock.GetOption(mangos.OptionWriteQLen); err == nil {
		depth = i.(int)
	}
	pe := &pubEp{ep: ep, p: p, q: make(chan *mangos.Message, depth)}
	pe.w.Init()
	p.Lock()
	p.eps[ep.GetID()] = pe
	p.Unlock()

	pe.w.Add()
	go pe.sender()
	go pe.receiver()
}
Пример #22
0
func (x *pair) AddEndpoint(ep mangos.Endpoint) {
	peer := &pairEp{cq: make(chan struct{}), ep: ep}
	x.Lock()
	if x.peer != nil {
		// We already have a connection, reject this one.
		x.Unlock()
		ep.Close()
		return
	}
	x.peer = peer
	x.Unlock()

	x.w.Add()
	go x.receiver(peer)
	go x.sender(peer)
}
Пример #23
0
func (x *bus) AddEndpoint(ep mangos.Endpoint) {
	// Set our broadcast depth to match upper depth -- this should
	// help avoid dropping when bursting, if we burst before we
	// context switch.
	depth := 16
	if i, err := x.sock.GetOption(mangos.OptionWriteQLen); err == nil {
		depth = i.(int)
	}
	pe := &busEp{ep: ep, x: x, q: make(chan *mangos.Message, depth)}
	x.Lock()
	x.eps[ep.GetID()] = pe
	x.Unlock()
	x.senders.Add()
	go pe.sender()
	go pe.receiver()
}
Пример #24
0
func (x *push) sender(ep mangos.Endpoint) {
	sq := x.sock.SendChannel()

	for {
		m := <-sq
		if m == nil {
			break
		}

		if ep.SendMsg(m) != nil {
			m.Free()
			break
		}
	}

	x.senders.Done()
}
Пример #25
0
func (x *pull) receiver(ep mangos.Endpoint) {
	rq := x.sock.RecvChannel()
	cq := x.sock.CloseChannel()
	for {

		m := ep.RecvMsg()
		if m == nil {
			return
		}

		select {
		case rq <- m:
		case <-cq:
			return
		}
	}
}
Пример #26
0
func (x *pair) sender(ep mangos.Endpoint) {

	sq := x.sock.SendChannel()

	// This is pretty easy because we have only one peer at a time.
	// If the peer goes away, we'll just drop the message on the floor.
	for {
		m := <-sq
		if m == nil {
			break
		}

		if ep.SendMsg(m) != nil {
			m.Free()
			break
		}
	}

	x.senders.Done()
}
Пример #27
0
func (x *resp) receiver(ep mangos.Endpoint) {

	rq := x.sock.RecvChannel()
	cq := x.sock.CloseChannel()

	for {
		m := ep.RecvMsg()
		if m == nil {
			return
		}

		v := ep.GetID()
		m.Header = append(m.Header,
			byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
		hops := 0

		for {
			if hops >= x.ttl {
				m.Free() // ErrTooManyHops
				continue
			}
			hops++
			if len(m.Body) < 4 {
				m.Free()
				continue
			}
			m.Header = append(m.Header, m.Body[:4]...)
			m.Body = m.Body[4:]
			if m.Header[len(m.Header)-4]&0x80 != 0 {
				break
			}
		}

		select {
		case rq <- m:
		case <-cq:
			m.Free()
			return
		}
	}
}
Пример #28
0
func (r *req) sender(ep mangos.Endpoint) {

	sq := r.sock.SendChannel()

	for {
		var m *mangos.Message

		select {
		case m = <-r.resend:
		case m = <-sq:
		}
		if m == nil {
			break
		}

		if ep.SendMsg(m) != nil {
			r.resend <- m
			break
		}
	}

	r.senders.Done()
}
Пример #29
0
func (p *pub) RemoveEndpoint(ep mangos.Endpoint) {
	p.Lock()
	delete(p.eps, ep.GetID())
	p.Unlock()
}
Пример #30
0
func (x *bus) RemoveEndpoint(ep mangos.Endpoint) {
	x.Lock()
	delete(x.eps, ep.GetID())
	x.Unlock()
}