2019-07-10 17:36:04 +01:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/transport"
|
|
|
|
)
|
|
|
|
|
|
|
|
// socket is our pseudo socket for transport.Socket
|
|
|
|
type socket struct {
|
|
|
|
// socket id based on Micro-Tunnel
|
|
|
|
id string
|
2019-07-10 18:24:03 +01:00
|
|
|
// the session id based on Micro.Tunnel-Session
|
|
|
|
session string
|
2019-07-10 17:36:04 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// message is sent over the send channel
|
|
|
|
type message struct {
|
2019-07-10 18:24:03 +01:00
|
|
|
// tunnel id
|
2019-07-10 17:36:04 +01:00
|
|
|
id string
|
2019-07-10 18:24:03 +01:00
|
|
|
// the session id
|
|
|
|
session string
|
2019-07-10 17:36:04 +01:00
|
|
|
// transport data
|
|
|
|
data *transport.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *socket) Remote() string {
|
|
|
|
return s.remote
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *socket) Local() string {
|
|
|
|
return s.local
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *socket) Id() string {
|
|
|
|
return s.id
|
|
|
|
}
|
|
|
|
|
2019-07-10 18:26:11 +01:00
|
|
|
func (s *socket) Session() string {
|
|
|
|
return s.session
|
|
|
|
}
|
|
|
|
|
2019-07-10 17:36:04 +01:00
|
|
|
func (s *socket) Send(m *transport.Message) error {
|
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
return errors.New("socket is closed")
|
|
|
|
default:
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
// append to backlog
|
2019-07-10 18:24:03 +01:00
|
|
|
s.send <- &message{id: s.id, session: s.session, data: m}
|
2019-07-10 17:36:04 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
// set message
|
|
|
|
*m = *msg.data
|
|
|
|
// return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *socket) Close() error {
|
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
// no op
|
|
|
|
default:
|
|
|
|
close(s.closed)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|