Move announce into session

This commit is contained in:
Asim Aslam 2019-09-04 11:58:03 +01:00
parent b9c437fbfe
commit 66db0ac52c
2 changed files with 28 additions and 24 deletions

View File

@ -27,33 +27,13 @@ func (t *tunListener) announce() {
tick := time.NewTicker(time.Minute) tick := time.NewTicker(time.Minute)
defer tick.Stop() defer tick.Stop()
announce := func() {
msg := &message{
typ: "announce",
tunnel: t.session.tunnel,
channel: t.session.channel,
session: t.session.session,
outbound: t.session.outbound,
loopback: t.session.loopback,
multicast: t.session.multicast,
}
select {
case t.session.send <- msg:
case <-t.session.closed:
return
case <-t.closed:
return
}
}
// first announcement // first announcement
announce() t.session.Announce()
for { for {
select { select {
case <-tick.C: case <-tick.C:
announce() t.session.Announce()
case <-t.closed: case <-t.closed:
return return
} }

View File

@ -91,7 +91,7 @@ func (s *session) Channel() string {
return s.channel return s.channel
} }
// Open will fire the open message for the session // Open will fire the open message for the session. This is called by the dialler.
func (s *session) Open() error { func (s *session) Open() error {
msg := &message{ msg := &message{
typ: "open", typ: "open",
@ -144,6 +144,7 @@ func (s *session) Open() error {
return nil return nil
} }
// Accept sends the accept response to an open message from a dialled connection
func (s *session) Accept() error { func (s *session) Accept() error {
msg := &message{ msg := &message{
typ: "accept", typ: "accept",
@ -178,6 +179,27 @@ func (s *session) Accept() error {
return nil return nil
} }
// Announce sends an announcement to notify that this session exists. This is primarily used by the listener.
func (s *session) Announce() error {
msg := &message{
typ: "announce",
tunnel: s.tunnel,
channel: s.channel,
session: s.session,
outbound: s.outbound,
loopback: s.loopback,
multicast: s.multicast,
}
select {
case s.send <- msg:
return nil
case <-s.closed:
return io.EOF
}
}
// Send is used to send a message
func (s *session) Send(m *transport.Message) error { func (s *session) Send(m *transport.Message) error {
select { select {
case <-s.closed: case <-s.closed:
@ -219,6 +241,7 @@ func (s *session) Send(m *transport.Message) error {
} }
log.Debugf("Appending %+v to send backlog", msg) log.Debugf("Appending %+v to send backlog", msg)
// send the actual message
s.send <- msg s.send <- msg
// wait for an error response // wait for an error response
@ -232,6 +255,7 @@ func (s *session) Send(m *transport.Message) error {
return nil return nil
} }
// Recv is used to receive a message
func (s *session) Recv(m *transport.Message) error { func (s *session) Recv(m *transport.Message) error {
select { select {
case <-s.closed: case <-s.closed:
@ -256,7 +280,7 @@ func (s *session) Recv(m *transport.Message) error {
return nil return nil
} }
// Close closes the session // Close closes the session by sending a close message
func (s *session) Close() error { func (s *session) Close() error {
select { select {
case <-s.closed: case <-s.closed: