micro/tunnel/listener.go

208 lines
4.1 KiB
Go
Raw Normal View History

2019-08-07 20:44:33 +03:00
package tunnel
import (
"io"
"time"
2019-08-11 20:11:33 +03:00
"github.com/micro/go-micro/util/log"
2019-08-07 20:44:33 +03:00
)
type tunListener struct {
// address of the listener
2019-08-30 22:05:00 +03:00
channel string
// token is the tunnel token
token string
2019-08-07 20:44:33 +03:00
// the accept channel
2019-08-30 22:05:00 +03:00
accept chan *session
2019-08-07 20:44:33 +03:00
// the channel to close
closed chan bool
// the tunnel closed channel
tunClosed chan bool
2019-08-30 22:05:00 +03:00
// the listener session
session *session
// del func to kill listener
delFunc func()
}
// periodically announce self the channel being listened on
func (t *tunListener) announce() {
tick := time.NewTicker(time.Second * 30)
defer tick.Stop()
// first announcement
2019-09-04 13:58:03 +03:00
t.session.Announce()
for {
select {
case <-tick.C:
2019-09-04 13:58:03 +03:00
t.session.Announce()
case <-t.closed:
return
}
}
2019-08-07 20:44:33 +03:00
}
func (t *tunListener) process() {
// our connection map for session
2019-08-30 22:05:00 +03:00
conns := make(map[string]*session)
2019-08-07 20:44:33 +03:00
defer func() {
// close the sessions
for id, conn := range conns {
conn.Close()
delete(conns, id)
}
// unassign
conns = nil
}()
2019-08-07 20:44:33 +03:00
for {
select {
case <-t.closed:
return
case <-t.tunClosed:
t.Close()
return
2019-08-07 20:44:33 +03:00
// receive a new message
2019-08-30 22:05:00 +03:00
case m := <-t.session.recv:
var sessionId string
// get the session id
switch m.mode {
case Multicast, Broadcast:
// use channel name if multicast/broadcast
sessionId = m.channel
log.Tracef("Tunnel listener using session %s for real session %s", sessionId, m.session)
default:
// use session id if unicast
sessionId = m.session
}
2019-08-30 22:05:00 +03:00
// get a session
sess, ok := conns[sessionId]
log.Debugf("Tunnel listener received channel %s session %s type %s exists: %t", m.channel, sessionId, m.typ, ok)
2019-08-07 20:44:33 +03:00
if !ok {
// we only process open and session types
switch m.typ {
case "open", "session":
default:
continue
}
2019-08-30 22:05:00 +03:00
// create a new session session
sess = &session{
// the id of the remote side
2019-09-03 17:56:37 +03:00
tunnel: m.tunnel,
2019-08-30 22:05:00 +03:00
// the channel
channel: m.channel,
2019-08-07 20:44:33 +03:00
// the session id
session: sessionId,
// tunnel token
token: t.token,
// is loopback conn
loopback: m.loopback,
2019-08-29 14:42:27 +03:00
// the link the message was received on
link: m.link,
2019-10-15 17:40:04 +03:00
// set the connection mode
mode: m.mode,
2019-08-07 20:44:33 +03:00
// close chan
closed: make(chan bool),
// recv called by the acceptor
recv: make(chan *message, 128),
// use the internal send buffer
2019-08-30 22:05:00 +03:00
send: t.session.send,
// error channel
errChan: make(chan error, 1),
2019-08-07 20:44:33 +03:00
}
2019-08-30 22:05:00 +03:00
// save the session
conns[sessionId] = sess
2019-08-07 20:44:33 +03:00
select {
case <-t.closed:
return
// send to accept chan
2019-08-30 22:05:00 +03:00
case t.accept <- sess:
2019-08-07 20:44:33 +03:00
}
}
// an existing session was found
switch m.typ {
case "close":
// received a close message
select {
// check if the session is closed
case <-sess.closed:
// no op
delete(conns, sessionId)
default:
if sess.mode == Unicast {
// only close if unicast session
// close and delete session
close(sess.closed)
delete(conns, sessionId)
}
}
// continue
continue
case "session":
// operate on this
default:
// non operational type
continue
2019-08-07 20:44:33 +03:00
}
// send this to the accept chan
select {
2019-08-30 22:05:00 +03:00
case <-sess.closed:
delete(conns, sessionId)
2019-08-30 22:05:00 +03:00
case sess.recv <- m:
log.Debugf("Tunnel listener sent to recv chan channel %s session %s type %s", m.channel, sessionId, m.typ)
2019-08-07 20:44:33 +03:00
}
}
}
}
2019-08-30 22:05:00 +03:00
func (t *tunListener) Channel() string {
return t.channel
2019-08-07 20:44:33 +03:00
}
// Close closes tunnel listener
2019-08-07 20:44:33 +03:00
func (t *tunListener) Close() error {
select {
case <-t.closed:
return nil
default:
// close and delete
t.delFunc()
t.session.Close()
2019-08-07 20:44:33 +03:00
close(t.closed)
}
return nil
}
// Everytime accept is called we essentially block till we get a new connection
2019-08-30 22:05:00 +03:00
func (t *tunListener) Accept() (Session, error) {
2019-08-07 20:44:33 +03:00
select {
2019-08-30 22:05:00 +03:00
// if the session is closed return
2019-08-07 20:44:33 +03:00
case <-t.closed:
return nil, io.EOF
case <-t.tunClosed:
// close the listener when the tunnel closes
return nil, io.EOF
2019-08-07 20:44:33 +03:00
// wait for a new connection
case c, ok := <-t.accept:
// check if the accept chan is closed
2019-08-07 20:44:33 +03:00
if !ok {
return nil, io.EOF
}
// send back the accept
if err := c.Accept(); err != nil {
return nil, err
}
2019-08-07 20:44:33 +03:00
return c, nil
}
}