[remote] fix remote

This commit is contained in:
chzyer 2016-04-11 01:44:19 +08:00
parent e3e573aa21
commit d4c46a49e6
2 changed files with 36 additions and 9 deletions

View File

@ -69,5 +69,8 @@ func (c *SegmentComplete) Do(line []rune, pos int) (newLine [][]rune, offset int
cands := c.DoSegment(segment, idx) cands := c.DoSegment(segment, idx)
newLine, offset = RetSegment(segment, cands, idx) newLine, offset = RetSegment(segment, cands, idx)
for idx := range newLine {
newLine[idx] = append(newLine[idx], ' ')
}
return newLine, offset return newLine, offset
} }

View File

@ -287,6 +287,8 @@ type RemoteCli struct {
conn net.Conn conn net.Conn
raw RawMode raw RawMode
receiveChan chan struct{} receiveChan chan struct{}
inited int32
isTerminal *bool
data bytes.Buffer data bytes.Buffer
dataM sync.Mutex dataM sync.Mutex
@ -297,13 +299,18 @@ func NewRemoteCli(conn net.Conn) (*RemoteCli, error) {
conn: conn, conn: conn,
receiveChan: make(chan struct{}), receiveChan: make(chan struct{}),
} }
if err := r.init(); err != nil {
return nil, err
}
return r, nil return r, nil
} }
func (r *RemoteCli) MarkIsTerminal(is bool) {
r.isTerminal = &is
}
func (r *RemoteCli) init() error { func (r *RemoteCli) init() error {
if !atomic.CompareAndSwapInt32(&r.inited, 0, 1) {
return nil
}
if err := r.reportIsTerminal(); err != nil { if err := r.reportIsTerminal(); err != nil {
return err return err
} }
@ -329,9 +336,9 @@ func (r *RemoteCli) writeMsg(m *Message) error {
func (r *RemoteCli) Write(b []byte) (int, error) { func (r *RemoteCli) Write(b []byte) (int, error) {
m := NewMessage(T_DATA, b) m := NewMessage(T_DATA, b)
r.dataM.Lock() r.dataM.Lock()
n, err := m.WriteTo(r.conn) _, err := m.WriteTo(r.conn)
r.dataM.Unlock() r.dataM.Unlock()
return n, err return len(b), err
} }
func (r *RemoteCli) reportWidth() error { func (r *RemoteCli) reportWidth() error {
@ -347,7 +354,12 @@ func (r *RemoteCli) reportWidth() error {
} }
func (r *RemoteCli) reportIsTerminal() error { func (r *RemoteCli) reportIsTerminal() error {
isTerminal := DefaultIsTerminal() var isTerminal bool
if r.isTerminal != nil {
isTerminal = *r.isTerminal
} else {
isTerminal = DefaultIsTerminal()
}
data := make([]byte, 2) data := make([]byte, 2)
if isTerminal { if isTerminal {
binary.BigEndian.PutUint16(data, 1) binary.BigEndian.PutUint16(data, 1)
@ -379,12 +391,16 @@ func (r *RemoteCli) readLoop() {
} }
} }
func (r *RemoteCli) Serve() error { func (r *RemoteCli) ServeBy(source io.Reader) error {
if err := r.init(); err != nil {
return err
}
go func() { go func() {
defer r.Close()
for { for {
n, _ := io.Copy(r, os.Stdin) n, _ := io.Copy(r, source)
if n == 0 { if n == 0 {
r.writeMsg(NewMessage(T_EOF, nil))
break break
} }
} }
@ -394,6 +410,14 @@ func (r *RemoteCli) Serve() error {
return nil return nil
} }
func (r *RemoteCli) Close() {
r.writeMsg(NewMessage(T_EOF, nil))
}
func (r *RemoteCli) Serve() error {
return r.ServeBy(os.Stdin)
}
func ListenRemote(n, addr string, cfg *Config, h func(*Instance), onListen ...func(net.Listener) error) error { func ListenRemote(n, addr string, cfg *Config, h func(*Instance), onListen ...func(net.Listener) error) error {
ln, err := net.Listen(n, addr) ln, err := net.Listen(n, addr)
if err != nil { if err != nil {