Simplify structure of command messages

The first byte of a input message shows the type of that message.
0: normal keystrokes
1: resize window command
This commit is contained in:
Iwasaki Yudai 2015-08-21 13:40:39 +09:00
parent ce96943af2
commit 5eea5067db
3 changed files with 35 additions and 53 deletions

View File

@ -158,7 +158,7 @@ func (app *App) generateHandler() func(w http.ResponseWriter, r *http.Request) {
}
switch data[0] {
case '0':
case Input:
if !app.options.PermitWrite {
break
}
@ -168,41 +168,22 @@ func (app *App) generateHandler() func(w http.ResponseWriter, r *http.Request) {
return
}
case '1':
var remoteCmd command
err = json.Unmarshal(data[1:], &remoteCmd)
case ResizeTerminal:
var args argResizeTerminal
err = json.Unmarshal(data[1:], &args)
if err != nil {
log.Print("Malformed remote command")
return
}
switch remoteCmd.Name {
case "resize_terminal":
rows := remoteCmd.Arguments["rows"]
switch rows.(type) {
case float64:
default:
log.Print("Malformed remote command")
return
}
cols := remoteCmd.Arguments["columns"]
switch cols.(type) {
case float64:
default:
log.Print("Malformed remote command")
return
}
window := struct {
row uint16
col uint16
x uint16
y uint16
}{
uint16(rows.(float64)),
uint16(cols.(float64)),
uint16(args.Rows),
uint16(args.Columns),
0,
0,
}
@ -212,7 +193,6 @@ func (app *App) generateHandler() func(w http.ResponseWriter, r *http.Request) {
syscall.TIOCSWINSZ,
uintptr(unsafe.Pointer(&window)),
)
}
default:
log.Print("Unknown message type")
@ -230,9 +210,14 @@ func (app *App) generateHandler() func(w http.ResponseWriter, r *http.Request) {
}
}
type command struct {
Name string `json:"name"`
Arguments map[string]interface{} `json:"arguments"`
const (
Input = '0'
ResizeTerminal = '1'
)
type argResizeTerminal struct {
Columns float64
Rows float64
}
func generateRandomString(length int) string {

File diff suppressed because one or more lines are too long

View File

@ -24,11 +24,8 @@
ws.send(
"1" + JSON.stringify(
{
name: "resize_terminal",
arguments: {
columns: columns,
rows: rows,
},
}
)
)