2019-08-06 11:45:25 +01:00
|
|
|
package tunnel
|
|
|
|
|
2019-08-07 18:44:33 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
2019-08-06 11:45:25 +01:00
|
|
|
|
2019-08-07 18:44:33 +01:00
|
|
|
"github.com/micro/go-micro/transport"
|
2019-08-11 18:11:33 +01:00
|
|
|
"github.com/micro/go-micro/util/log"
|
2019-08-07 18:44:33 +01:00
|
|
|
)
|
2019-08-06 11:45:25 +01:00
|
|
|
|
2019-08-07 18:44:33 +01:00
|
|
|
// socket is our pseudo socket for transport.Socket
|
|
|
|
type socket struct {
|
|
|
|
// socket id based on Micro-Tunnel
|
|
|
|
id string
|
|
|
|
// the session id based on Micro.Tunnel-Session
|
|
|
|
session string
|
|
|
|
// closed
|
|
|
|
closed chan bool
|
|
|
|
// remote addr
|
|
|
|
remote string
|
|
|
|
// local addr
|
|
|
|
local string
|
|
|
|
// send chan
|
|
|
|
send chan *message
|
|
|
|
// recv chan
|
|
|
|
recv chan *message
|
|
|
|
// wait until we have a connection
|
|
|
|
wait chan bool
|
2019-08-28 23:12:22 +01:00
|
|
|
// outbound marks the socket as outbound dialled connection
|
2019-08-14 17:14:39 +01:00
|
|
|
outbound bool
|
2019-08-28 23:12:22 +01:00
|
|
|
// lookback marks the socket as a loopback on the inbound
|
|
|
|
loopback bool
|
2019-08-29 12:42:27 +01:00
|
|
|
// the link on which this message was received
|
|
|
|
link string
|
2019-08-06 11:45:25 +01:00
|
|
|
}
|
|
|
|
|
2019-08-07 18:44:33 +01:00
|
|
|
// message is sent over the send channel
|
|
|
|
type message struct {
|
2019-08-29 12:42:27 +01:00
|
|
|
// type of message
|
|
|
|
typ string
|
2019-08-07 18:44:33 +01:00
|
|
|
// tunnel id
|
|
|
|
id string
|
|
|
|
// the session id
|
|
|
|
session string
|
2019-08-14 17:14:39 +01:00
|
|
|
// outbound marks the message as outbound
|
|
|
|
outbound bool
|
2019-08-28 23:12:22 +01:00
|
|
|
// loopback marks the message intended for loopback
|
|
|
|
loopback bool
|
2019-08-29 12:42:27 +01:00
|
|
|
// the link to send the message on
|
|
|
|
link string
|
2019-08-07 18:44:33 +01:00
|
|
|
// transport data
|
|
|
|
data *transport.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *socket) Remote() string {
|
|
|
|
return s.remote
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *socket) Local() string {
|
|
|
|
return s.local
|
2019-08-06 11:45:25 +01:00
|
|
|
}
|
|
|
|
|
2019-08-07 18:44:33 +01:00
|
|
|
func (s *socket) Id() string {
|
|
|
|
return s.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *socket) Session() string {
|
|
|
|
return s.session
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *socket) Send(m *transport.Message) error {
|
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
return errors.New("socket is closed")
|
|
|
|
default:
|
|
|
|
// no op
|
|
|
|
}
|
2019-08-11 18:11:33 +01:00
|
|
|
|
|
|
|
// make copy
|
|
|
|
data := &transport.Message{
|
|
|
|
Header: make(map[string]string),
|
|
|
|
Body: m.Body,
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range m.Header {
|
|
|
|
data.Header[k] = v
|
|
|
|
}
|
|
|
|
|
2019-08-07 18:44:33 +01:00
|
|
|
// append to backlog
|
2019-08-14 17:14:39 +01:00
|
|
|
msg := &message{
|
2019-08-29 12:42:27 +01:00
|
|
|
typ: "message",
|
2019-08-14 17:14:39 +01:00
|
|
|
id: s.id,
|
|
|
|
session: s.session,
|
|
|
|
outbound: s.outbound,
|
2019-08-28 23:12:22 +01:00
|
|
|
loopback: s.loopback,
|
2019-08-14 17:14:39 +01:00
|
|
|
data: data,
|
2019-08-29 12:42:27 +01:00
|
|
|
// specify the link on which to send this
|
|
|
|
// it will be blank for dialled sockets
|
|
|
|
link: s.link,
|
2019-08-14 17:14:39 +01:00
|
|
|
}
|
2019-08-11 18:11:33 +01:00
|
|
|
log.Debugf("Appending %+v to send backlog", msg)
|
|
|
|
s.send <- msg
|
2019-08-06 11:45:25 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-07 18:44:33 +01:00
|
|
|
func (s *socket) Recv(m *transport.Message) error {
|
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
return errors.New("socket is closed")
|
|
|
|
default:
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
// recv from backlog
|
|
|
|
msg := <-s.recv
|
2019-08-11 18:11:33 +01:00
|
|
|
log.Debugf("Received %+v from recv backlog", msg)
|
2019-08-07 18:44:33 +01:00
|
|
|
// set message
|
|
|
|
*m = *msg.data
|
|
|
|
// return nil
|
|
|
|
return nil
|
2019-08-06 11:45:25 +01:00
|
|
|
}
|
|
|
|
|
2019-08-08 15:20:53 +01:00
|
|
|
// Close closes the socket
|
2019-08-07 18:44:33 +01:00
|
|
|
func (s *socket) Close() error {
|
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
// no op
|
|
|
|
default:
|
|
|
|
close(s.closed)
|
|
|
|
}
|
|
|
|
return nil
|
2019-08-06 11:45:25 +01:00
|
|
|
}
|