Added ControlChannel tunnel.Listener to process incoming messages

This commit is contained in:
Milos Gajdos 2019-08-22 13:17:32 +01:00
parent db89fc4efe
commit e53484302c
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F

View File

@ -136,15 +136,40 @@ func (n *network) resolve() {
}
}
func (n *network) process(client transport.Client) {
func (n *network) handleConn(conn tunnel.Conn, msg chan *transport.Message) {
for {
m := new(transport.Message)
if err := client.Recv(m); err != nil {
if err := conn.Recv(m); err != nil {
// TODO: should we bail here?
log.Debugf("Network advert receive error: %v", err)
log.Debugf("Network tunnel advert receive error: %v", err)
return
}
select {
case msg <- m:
case <-n.closed:
return
}
}
}
func (n *network) process(l tunnel.Listener) {
// receive control message queue
recv := make(chan *transport.Message, 128)
// accept a connection
conn, err := l.Accept()
if err != nil {
// TODO: handle this
log.Debugf("Network tunnel accept error: %v", err)
return
}
go n.handleConn(conn, recv)
for {
select {
case m := <-recv:
// switch on type of message and take action
switch m.Header["Micro-Method"] {
case "advert":
@ -183,6 +208,9 @@ func (n *network) process(client transport.Client) {
continue
}
}
case <-n.closed:
return
}
}
}
@ -268,7 +296,12 @@ func (n *network) Connect() error {
// dial into ControlChannel to send route adverts
client, err := n.Tunnel.Dial(ControlChannel)
if err != nil {
// TODO: should we stop the tunnel here?
return err
}
// listen on ControlChannel
listener, err := n.Tunnel.Listen(ControlChannel)
if err != nil {
return err
}
@ -291,8 +324,8 @@ func (n *network) Connect() error {
// advertise routes
go n.advertise(client, advertChan)
// process routes
go n.process(client)
// accept and process routes
go n.process(listener)
// start the server
if err := n.srv.Start(); err != nil {