done
This commit is contained in:
parent
3bf499a5dc
commit
ed15f041ce
@ -1,5 +1,10 @@
|
|||||||
package tunnel
|
package tunnel
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
type Connection struct {
|
type Connection struct {
|
||||||
RX <-chan *DataFrame
|
RX <-chan *DataFrame
|
||||||
TX chan<- *DataFrame
|
TX chan<- *DataFrame
|
||||||
@ -16,7 +21,14 @@ func (c *Connection) Read(p []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) Write(p []byte) (int, error) {
|
func (c *Connection) Write(p []byte) (int, error) {
|
||||||
defer recover()
|
var lock sync.Mutex
|
||||||
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
|
|
||||||
|
if c.closed {
|
||||||
|
return 0, io.ErrClosedPipe
|
||||||
|
}
|
||||||
|
|
||||||
df := &DataFrame{
|
df := &DataFrame{
|
||||||
ID: c.ID,
|
ID: c.ID,
|
||||||
Type: TypeNormal,
|
Type: TypeNormal,
|
||||||
@ -30,6 +42,7 @@ func (c *Connection) Write(p []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) Close() error {
|
func (c *Connection) Close() error {
|
||||||
|
var lock sync.Mutex
|
||||||
defer close(c.TX)
|
defer close(c.TX)
|
||||||
if !c.closed {
|
if !c.closed {
|
||||||
c.TX <- &DataFrame{
|
c.TX <- &DataFrame{
|
||||||
@ -37,6 +50,8 @@ func (c *Connection) Close() error {
|
|||||||
Type: TypeClosed,
|
Type: TypeClosed,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
lock.Lock()
|
||||||
c.closed = true
|
c.closed = true
|
||||||
|
lock.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
12
manager.go
12
manager.go
@ -29,6 +29,7 @@ func NewManager(tun io.ReadWriter) *Manager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) Run() {
|
func (m *Manager) Run() {
|
||||||
|
onReceiveQueue := make(chan bool)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-m.closed:
|
case <-m.closed:
|
||||||
@ -40,15 +41,18 @@ func (m *Manager) Run() {
|
|||||||
case connection := <-m.delConnection:
|
case connection := <-m.delConnection:
|
||||||
connection.Close()
|
connection.Close()
|
||||||
delete(m.Connections, connection.ID)
|
delete(m.Connections, connection.ID)
|
||||||
default:
|
case onReceiveQueue <- true:
|
||||||
go m.onReceive()
|
go m.onReceive(onReceiveQueue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) onReceive() {
|
func (m *Manager) onReceive(ch chan bool) {
|
||||||
|
<-ch
|
||||||
df := new(DataFrame)
|
df := new(DataFrame)
|
||||||
df.Decode(m.Tunnel)
|
if err := df.Decode(m.Tunnel); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
switch df.Type {
|
switch df.Type {
|
||||||
case TypeRequest:
|
case TypeRequest:
|
||||||
m.accept <- df
|
m.accept <- df
|
||||||
|
Reference in New Issue
Block a user