support dial to remote readline

This commit is contained in:
Cheney 2016-01-31 12:18:46 +08:00
parent cbab1f1566
commit 8853e17195
1 changed files with 49 additions and 0 deletions

49
remote.go Normal file
View File

@ -0,0 +1,49 @@
package readline
import (
"io"
"net"
"os"
)
type Conn struct {
Conn net.Conn
Terminal *Terminal
runChan chan error
}
func NewConn(conn net.Conn, t *Terminal) (*Conn, error) {
return &Conn{
Conn: conn,
Terminal: t,
runChan: make(chan error),
}, nil
}
func (c *Conn) Run() error {
c.Terminal.EnterRawMode()
go func() {
_, err := io.Copy(c.Conn, os.Stdin)
c.runChan <- err
}()
go func() {
_, err := io.Copy(os.Stdout, c.Conn)
c.runChan <- err
}()
err := <-c.runChan
c.Terminal.ExitRawMode()
return err
}
func Dial(network string, address string) (*Remote, error) {
conn, err := net.Dial(network, address)
if err != nil {
return nil, err
}
var cfg Config
t, err := NewTerminal(&cfg)
if err != nil {
return nil, err
}
return NewRemote(conn, t)
}