35 lines
505 B
Go
35 lines
505 B
Go
package tunnel
|
|
|
|
type Connection struct {
|
|
RX <-chan *DataFrame
|
|
TX chan<- *DataFrame
|
|
ID ID
|
|
}
|
|
|
|
func (c *Connection) Read(p []byte) (int, error) {
|
|
df, ok := <-c.RX
|
|
if !ok {
|
|
return 0, c.Close()
|
|
}
|
|
return df.Read(p)
|
|
}
|
|
|
|
func (c *Connection) Write(p []byte) (int, error) {
|
|
defer recover()
|
|
df := &DataFrame{
|
|
ID: c.ID,
|
|
Type: TypeNormal,
|
|
}
|
|
n, err := df.Write(p)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
c.TX <- df
|
|
return n, nil
|
|
}
|
|
|
|
func (c *Connection) Close() error {
|
|
defer close(c.TX)
|
|
return nil
|
|
}
|