-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9a06af
commit 16e7a60
Showing
6 changed files
with
251 additions
and
398 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package start | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"syscall" | ||
"time" | ||
|
||
pty "github.com/MCSManager/pty/console" | ||
) | ||
|
||
func runControl(fifo string, con pty.Console) error { | ||
err := os.Remove(fifo) | ||
if err != nil { | ||
if !os.IsNotExist(err) { | ||
return fmt.Errorf("remove fifo error: %w", err) | ||
} | ||
} | ||
if err := syscall.Mkfifo(fifo, 0666); err != nil { | ||
return fmt.Errorf("create fifo error: %w", err) | ||
} | ||
|
||
if testFifoResize { | ||
go func() { | ||
time.Sleep(time.Second * 5) | ||
_ = testUnixResize(fifo) | ||
}() | ||
} | ||
|
||
for { | ||
f, err := os.OpenFile(fifo, os.O_RDONLY, os.ModeNamedPipe) | ||
if err != nil { | ||
return fmt.Errorf("open fifo error: %w", err) | ||
} | ||
defer f.Close() | ||
u := newConnUtils(f, f) | ||
_ = handleConn(u, con) | ||
} | ||
} | ||
|
||
func testUnixResize(fifo string) error { | ||
n, err := os.OpenFile(fifo, os.O_WRONLY, os.ModeNamedPipe) | ||
if err != nil { | ||
return fmt.Errorf("open fifo error: %w", err) | ||
} | ||
defer n.Close() | ||
u := newConnUtils(n, n) | ||
return testResize(u) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package start | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
pty "github.com/MCSManager/pty/console" | ||
"github.com/zijiren233/stream" | ||
) | ||
|
||
type connUtils struct { | ||
r *stream.Reader | ||
w *stream.Writer | ||
} | ||
|
||
func newConnUtils(r io.Reader, w io.Writer) *connUtils { | ||
return &connUtils{ | ||
r: stream.NewReader(r, stream.BigEndian), | ||
w: stream.NewWriter(w, stream.BigEndian), | ||
} | ||
} | ||
|
||
func (cu *connUtils) ReadMessage() (uint8, []byte, error) { | ||
var ( | ||
length uint16 | ||
msgType uint8 | ||
) | ||
data, err := cu.r.U8(&msgType).U16(&length).ReadBytes(int(length)) | ||
return msgType, data, err | ||
} | ||
|
||
func (cu *connUtils) SendMessage(msgType uint8, data any) error { | ||
b, err := json.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
return cu.w.U8(msgType).U16(uint16(len(b))).Bytes(b).Error() | ||
} | ||
|
||
func handleConn(u *connUtils, con pty.Console) error { | ||
for { | ||
t, msg, err := u.ReadMessage() | ||
if err != nil { | ||
return fmt.Errorf("read message error: %w", err) | ||
} | ||
switch t { | ||
case RESIZE: | ||
resize := resizeMsg{} | ||
err := json.Unmarshal(msg, &resize) | ||
if err != nil { | ||
_ = u.SendMessage( | ||
ERROR, | ||
&errorMsg{ | ||
Msg: fmt.Sprintf("unmarshal resize message error: %s", err), | ||
}, | ||
) | ||
continue | ||
} | ||
err = con.SetSize(resize.Width, resize.Height) | ||
if err != nil { | ||
_ = u.SendMessage( | ||
ERROR, | ||
&errorMsg{ | ||
Msg: fmt.Sprintf("resize error: %s", err), | ||
}, | ||
) | ||
continue | ||
} | ||
} | ||
} | ||
} | ||
|
||
func testResize(u *connUtils) error { | ||
err := u.SendMessage( | ||
RESIZE, | ||
&resizeMsg{ | ||
Width: 20, | ||
Height: 20, | ||
}, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("send resize message error: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package start | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
pty "github.com/MCSManager/pty/console" | ||
|
||
winio "github.com/Microsoft/go-winio" | ||
) | ||
|
||
// \\.\pipe\mypipe | ||
func runControl(fifo string, con pty.Console) error { | ||
n, err := winio.ListenPipe(fifo, &winio.PipeConfig{}) | ||
if err != nil { | ||
return fmt.Errorf("open fifo error: %w", err) | ||
} | ||
defer n.Close() | ||
|
||
if testFifoResize { | ||
go func() { | ||
time.Sleep(time.Second * 5) | ||
_ = testWinResize(fifo) | ||
}() | ||
} | ||
|
||
for { | ||
conn, err := n.Accept() | ||
if err != nil { | ||
return fmt.Errorf("accept fifo error: %w", err) | ||
} | ||
go func() { | ||
defer conn.Close() | ||
u := newConnUtils(conn, conn) | ||
_ = handleConn(u, con) | ||
}() | ||
} | ||
} | ||
|
||
func testWinResize(fifo string) error { | ||
n, err := winio.DialPipe(fifo, nil) | ||
if err != nil { | ||
return fmt.Errorf("open fifo error: %w", err) | ||
} | ||
u := newConnUtils(n, n) | ||
return testResize(u) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.