cleanup new message creation

This commit is contained in:
Asim Aslam 2019-09-04 12:16:31 +01:00
parent c718b8bf93
commit d5be2136ad
3 changed files with 32 additions and 63 deletions

View File

@ -830,15 +830,13 @@ func (t *tun) Dial(channel string, opts ...DialOption) (Session, error) {
// shit fuck // shit fuck
if !c.discovered { if !c.discovered {
t.send <- &message{ msg := c.newMessage("discover")
typ: "discover", msg.broadcast = true
tunnel: t.id, msg.outbound = true
channel: channel, msg.link = ""
session: c.session,
broadcast: true, // send the discovery message
outbound: true, t.send <- msg
errChan: c.errChan,
}
select { select {
case err := <-c.errChan: case err := <-c.errChan:

View File

@ -91,10 +91,9 @@ func (s *session) Channel() string {
return s.channel return s.channel
} }
// Open will fire the open message for the session. This is called by the dialler. func (s *session) newMessage(typ string) *message {
func (s *session) Open() error { return &message{
msg := &message{ typ: typ,
typ: "open",
tunnel: s.tunnel, tunnel: s.tunnel,
channel: s.channel, channel: s.channel,
session: s.session, session: s.session,
@ -104,6 +103,12 @@ func (s *session) Open() error {
link: s.link, link: s.link,
errChan: s.errChan, errChan: s.errChan,
} }
}
// Open will fire the open message for the session. This is called by the dialler.
func (s *session) Open() error {
// create a new message
msg := s.newMessage("open")
// send open message // send open message
s.send <- msg s.send <- msg
@ -146,17 +151,7 @@ func (s *session) Open() error {
// Accept sends the accept response to an open message from a dialled connection // 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 := s.newMessage("accept")
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 // send the accept message
select { select {
@ -181,15 +176,11 @@ func (s *session) Accept() error {
// Announce sends an announcement to notify that this session exists. This is primarily used by the listener. // Announce sends an announcement to notify that this session exists. This is primarily used by the listener.
func (s *session) Announce() error { func (s *session) Announce() error {
msg := &message{ msg := s.newMessage("announce")
typ: "announce", // we don't need an error back
tunnel: s.tunnel, msg.errChan = nil
channel: s.channel, // we don't need the link
session: s.session, msg.link = ""
outbound: s.outbound,
loopback: s.loopback,
multicast: s.multicast,
}
select { select {
case s.send <- msg: case s.send <- msg:
@ -218,26 +209,14 @@ func (s *session) Send(m *transport.Message) error {
data.Header[k] = v data.Header[k] = v
} }
// append to backlog // create a new message
msg := &message{ msg := s.newMessage("session")
typ: "session", // set the data
tunnel: s.tunnel, msg.data = data
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 multicast don't set the link
if !s.multicast { if s.multicast {
msg.link = s.link msg.link = ""
} }
log.Debugf("Appending %+v to send backlog", msg) log.Debugf("Appending %+v to send backlog", msg)
@ -289,16 +268,9 @@ func (s *session) Close() error {
close(s.closed) close(s.closed)
// append to backlog // append to backlog
msg := &message{ msg := s.newMessage("close")
typ: "close", // no error response on close
tunnel: s.tunnel, msg.errChan = nil
channel: s.channel,
session: s.session,
outbound: s.outbound,
loopback: s.loopback,
multicast: s.multicast,
link: s.link,
}
// send the close message // send the close message
select { select {

View File

@ -15,7 +15,6 @@ var (
DefaultDialTimeout = time.Second * 5 DefaultDialTimeout = time.Second * 5
) )
// Tunnel creates a gre tunnel on top of the go-micro/transport. // Tunnel creates a gre tunnel on top of the go-micro/transport.
// It establishes multiple streams using the Micro-Tunnel-Channel header // It establishes multiple streams using the Micro-Tunnel-Channel header
// and Micro-Tunnel-Session header. The tunnel id is a hash of // and Micro-Tunnel-Session header. The tunnel id is a hash of