Tunnel discover/announce/open/session/close
This commit is contained in:
@@ -3,6 +3,7 @@ package tunnel
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/micro/go-micro/transport"
|
||||
"github.com/micro/go-micro/util/log"
|
||||
@@ -28,10 +29,20 @@ type session struct {
|
||||
recv chan *message
|
||||
// wait until we have a connection
|
||||
wait chan bool
|
||||
// if the discovery worked
|
||||
discovered bool
|
||||
// if the session was accepted
|
||||
accepted bool
|
||||
// outbound marks the session as outbound dialled connection
|
||||
outbound bool
|
||||
// lookback marks the session as a loopback on the inbound
|
||||
loopback bool
|
||||
// if the session is multicast
|
||||
multicast bool
|
||||
// if the session is broadcast
|
||||
broadcast bool
|
||||
// the timeout
|
||||
timeout time.Duration
|
||||
// the link on which this message was received
|
||||
link string
|
||||
// the error response
|
||||
@@ -52,6 +63,10 @@ type message struct {
|
||||
outbound bool
|
||||
// loopback marks the message intended for loopback
|
||||
loopback bool
|
||||
// whether to send as multicast
|
||||
multicast bool
|
||||
// broadcast sets the broadcast type
|
||||
broadcast bool
|
||||
// the link to send the message on
|
||||
link string
|
||||
// transport data
|
||||
@@ -76,10 +91,97 @@ func (s *session) Channel() string {
|
||||
return s.channel
|
||||
}
|
||||
|
||||
// Open will fire the open message for the session
|
||||
func (s *session) Open() error {
|
||||
msg := &message{
|
||||
typ: "open",
|
||||
tunnel: s.tunnel,
|
||||
channel: s.channel,
|
||||
session: s.session,
|
||||
outbound: s.outbound,
|
||||
loopback: s.loopback,
|
||||
multicast: s.multicast,
|
||||
link: s.link,
|
||||
errChan: s.errChan,
|
||||
}
|
||||
|
||||
// send open message
|
||||
s.send <- msg
|
||||
|
||||
// wait for an error response for send
|
||||
select {
|
||||
case err := <-msg.errChan:
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case <-s.closed:
|
||||
return io.EOF
|
||||
}
|
||||
|
||||
// we don't wait on multicast
|
||||
if s.multicast {
|
||||
s.accepted = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// now wait for the accept
|
||||
select {
|
||||
case msg = <-s.recv:
|
||||
if msg.typ != "accept" {
|
||||
log.Debugf("Received non accept message in Open %s", msg.typ)
|
||||
return errors.New("failed to connect")
|
||||
}
|
||||
// set to accepted
|
||||
s.accepted = true
|
||||
// set link
|
||||
s.link = msg.link
|
||||
case <-time.After(s.timeout):
|
||||
return ErrDialTimeout
|
||||
case <-s.closed:
|
||||
return io.EOF
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *session) Accept() error {
|
||||
msg := &message{
|
||||
typ: "accept",
|
||||
tunnel: s.tunnel,
|
||||
channel: s.channel,
|
||||
session: s.session,
|
||||
outbound: s.outbound,
|
||||
loopback: s.loopback,
|
||||
multicast: s.multicast,
|
||||
link: s.link,
|
||||
errChan: s.errChan,
|
||||
}
|
||||
|
||||
// send the accept message
|
||||
select {
|
||||
case <-s.closed:
|
||||
return io.EOF
|
||||
case s.send <- msg:
|
||||
return nil
|
||||
}
|
||||
|
||||
// wait for send response
|
||||
select {
|
||||
case err := <-s.errChan:
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case <-s.closed:
|
||||
return io.EOF
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *session) Send(m *transport.Message) error {
|
||||
select {
|
||||
case <-s.closed:
|
||||
return errors.New("session is closed")
|
||||
return io.EOF
|
||||
default:
|
||||
// no op
|
||||
}
|
||||
@@ -96,19 +198,26 @@ func (s *session) Send(m *transport.Message) error {
|
||||
|
||||
// append to backlog
|
||||
msg := &message{
|
||||
typ: "session",
|
||||
tunnel: s.tunnel,
|
||||
channel: s.channel,
|
||||
session: s.session,
|
||||
outbound: s.outbound,
|
||||
loopback: s.loopback,
|
||||
data: data,
|
||||
typ: "session",
|
||||
tunnel: s.tunnel,
|
||||
channel: s.channel,
|
||||
session: s.session,
|
||||
outbound: s.outbound,
|
||||
loopback: s.loopback,
|
||||
multicast: s.multicast,
|
||||
data: data,
|
||||
// specify the link on which to send this
|
||||
// it will be blank for dialled sessions
|
||||
link: s.link,
|
||||
// error chan
|
||||
errChan: s.errChan,
|
||||
}
|
||||
|
||||
// if not multicast then set link
|
||||
if !s.multicast {
|
||||
msg.link = s.link
|
||||
}
|
||||
|
||||
log.Debugf("Appending %+v to send backlog", msg)
|
||||
s.send <- msg
|
||||
|
||||
@@ -154,6 +263,25 @@ func (s *session) Close() error {
|
||||
// no op
|
||||
default:
|
||||
close(s.closed)
|
||||
|
||||
// append to backlog
|
||||
msg := &message{
|
||||
typ: "close",
|
||||
tunnel: s.tunnel,
|
||||
channel: s.channel,
|
||||
session: s.session,
|
||||
outbound: s.outbound,
|
||||
loopback: s.loopback,
|
||||
multicast: s.multicast,
|
||||
link: s.link,
|
||||
}
|
||||
|
||||
// send the close message
|
||||
select {
|
||||
case s.send <- msg:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user