MCPcopy
hub / github.com/1Panel-dev/KubePi / Read

Method Read

pkg/terminal/shell.go:73–100  ·  view source on GitHub ↗

Read handles pty->process messages (stdin, resize) Called in a loop from remotecommand as long as the process is running

(p []byte)

Source from the content-addressed store, hash-verified

71// Read handles pty->process messages (stdin, resize)
72// Called in a loop from remotecommand as long as the process is running
73func (t TerminalSession) Read(p []byte) (int, error) {
74 session := TerminalSessions.Get(t.Id)
75 if session.TimeOut.Before(time.Now()) {
76 _ = TerminalSessions.Sessions[session.Id].sockJSSession.Close(2, "the connection has been disconnected. Please reconnect")
77 return 0, errors.New("the connection has been disconnected. Please reconnect")
78 }
79 TerminalSessions.Set(session.Id, session)
80 m, err := session.sockJSSession.Recv()
81 if err != nil {
82 // Send terminated signal to process to avoid resource leak
83 return copy(p, END_OF_TRANSMISSION), err
84 }
85
86 var msg TerminalMessage
87 if err := json.Unmarshal([]byte(m), &msg); err != nil {
88 return copy(p, END_OF_TRANSMISSION), err
89 }
90
91 switch msg.Op {
92 case "stdin":
93 return copy(p, msg.Data), nil
94 case "resize":
95 session.SizeChan <- remotecommand.TerminalSize{Width: msg.Cols, Height: msg.Rows}
96 return 0, nil
97 default:
98 return copy(p, END_OF_TRANSMISSION), fmt.Errorf("unknown message type '%s'", msg.Op)
99 }
100}
101
102// Write handles process->pty stdout
103// Called from remotecommand whenever there is any output

Callers 5

GenLoggingSessionIdFunction · 0.45
startLogProcessFunction · 0.45
GenTerminalSessionIdFunction · 0.45
CopyFileFromPodMethod · 0.45
CopyFolderFromPodMethod · 0.45

Calls 4

GetMethod · 0.65
CloseMethod · 0.65
NewMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected