Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
7662ab4e08 | ||
|
8c2b78e30b | ||
|
6002e20080 | ||
|
afc125cc1c |
@ -22,6 +22,7 @@ func (c *Connection) Read(p []byte) (int, error) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return 0, c.Close()
|
return 0, c.Close()
|
||||||
}
|
}
|
||||||
|
c.logger.Trace("dataframe received: ", df)
|
||||||
return df.Read(p)
|
return df.Read(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,11 +43,13 @@ func (c *Connection) Write(p []byte) (int, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
c.logger.Trace("dataframe send: ", df)
|
||||||
c.TX <- df
|
c.TX <- df
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) Close() error {
|
func (c *Connection) Close() error {
|
||||||
|
defer c.logger.Tracef("connection closed")
|
||||||
var lock sync.Mutex
|
var lock sync.Mutex
|
||||||
defer close(c.TX)
|
defer close(c.TX)
|
||||||
if !c.closed {
|
if !c.closed {
|
||||||
|
29
logger.go
29
logger.go
@ -22,22 +22,17 @@ type Logger interface {
|
|||||||
|
|
||||||
type logger struct{}
|
type logger struct{}
|
||||||
|
|
||||||
func (l *logger) Trace(args ...interface{})
|
func (l *logger) Trace(args ...interface{}) {}
|
||||||
func (l *logger) Tracef(format string, args ...interface{})
|
func (l *logger) Tracef(format string, args ...interface{}) {}
|
||||||
|
func (l *logger) Debug(args ...interface{}) {}
|
||||||
func (l *logger) Debug(args ...interface{})
|
func (l *logger) Debugf(format string, args ...interface{}) {}
|
||||||
func (l *logger) Debugf(format string, args ...interface{})
|
func (l *logger) Info(args ...interface{}) {}
|
||||||
|
func (l *logger) Infof(format string, args ...interface{}) {}
|
||||||
func (l *logger) Info(args ...interface{})
|
func (l *logger) Error(args ...interface{}) {}
|
||||||
func (l *logger) Infof(format string, args ...interface{})
|
func (l *logger) Errorf(format string, args ...interface{}) {}
|
||||||
|
func (l *logger) Fatal(args ...interface{}) {}
|
||||||
func (l *logger) Error(args ...interface{})
|
func (l *logger) Fatalf(format string, args ...interface{}) {}
|
||||||
func (l *logger) Errorf(format string, args ...interface{})
|
func (l *logger) Panic(args ...interface{}) {}
|
||||||
|
func (l *logger) Panicf(format string, args ...interface{}) {}
|
||||||
func (l *logger) Fatal(args ...interface{})
|
|
||||||
func (l *logger) Fatalf(format string, args ...interface{})
|
|
||||||
|
|
||||||
func (l *logger) Panic(args ...interface{})
|
|
||||||
func (l *logger) Panicf(format string, args ...interface{})
|
|
||||||
|
|
||||||
var DefaultLogger = &logger{}
|
var DefaultLogger = &logger{}
|
||||||
|
13
manager.go
13
manager.go
@ -25,6 +25,7 @@ func NewManager(tun io.ReadWriter) *Manager {
|
|||||||
accept: make(chan *DataFrame, 1024),
|
accept: make(chan *DataFrame, 1024),
|
||||||
newConnection: make(chan *Connection),
|
newConnection: make(chan *Connection),
|
||||||
delConnection: make(chan *Connection),
|
delConnection: make(chan *Connection),
|
||||||
|
closed: make(chan bool),
|
||||||
logger: DefaultLogger,
|
logger: DefaultLogger,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -34,18 +35,24 @@ func (m *Manager) SetLogger(logger Logger) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) Run() {
|
func (m *Manager) Run() {
|
||||||
|
m.logger.Debug("manager run")
|
||||||
onReceiveQueue := make(chan bool)
|
onReceiveQueue := make(chan bool)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-m.closed:
|
case <-m.closed:
|
||||||
return
|
return
|
||||||
case df := <-m.incoming:
|
case df := <-m.incoming:
|
||||||
|
m.logger.Trace("dataframe->tunnel: ", df)
|
||||||
go df.Encode(m.Tunnel)
|
go df.Encode(m.Tunnel)
|
||||||
case connection := <-m.newConnection:
|
case connection := <-m.newConnection:
|
||||||
m.Connections[connection.ID] = connection
|
m.Connections[connection.ID] = connection
|
||||||
|
m.logger.Tracef("connection '%s' registered", connection.ID)
|
||||||
case connection := <-m.delConnection:
|
case connection := <-m.delConnection:
|
||||||
connection.Close()
|
if !connection.closed {
|
||||||
delete(m.Connections, connection.ID)
|
connection.Close()
|
||||||
|
delete(m.Connections, connection.ID)
|
||||||
|
m.logger.Tracef("connection '%s' unregistered", connection.ID)
|
||||||
|
}
|
||||||
case onReceiveQueue <- true:
|
case onReceiveQueue <- true:
|
||||||
go m.onReceive(onReceiveQueue)
|
go m.onReceive(onReceiveQueue)
|
||||||
}
|
}
|
||||||
@ -58,6 +65,8 @@ func (m *Manager) onReceive(ch chan bool) {
|
|||||||
if err := df.Decode(m.Tunnel); err != nil {
|
if err := df.Decode(m.Tunnel); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
m.logger.Trace("dataframe<-tunnel: ", df)
|
||||||
|
|
||||||
switch df.Type {
|
switch df.Type {
|
||||||
case TypeRequest:
|
case TypeRequest:
|
||||||
m.accept <- df
|
m.accept <- df
|
||||||
|
Reference in New Issue
Block a user