Skip to content

Commit

Permalink
Add a way to notify the sender when new receivers connect
Browse files Browse the repository at this point in the history
Also try to "refresh" the terminal, when new receivers connect, so the
content is fully rendered on the receiver side.
At the moment, the "refresh" is faked with making the window smaller and
then back bigger, after a short time (there doesn't seem to be a way to
tell the app to re-draw itself. Or is there?).
  • Loading branch information
elisescu committed May 19, 2018
1 parent 4e34ba8 commit 885e5f1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
30 changes: 24 additions & 6 deletions common/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,22 @@ import (
type ProtocolMessageIDType string

const (
MsgIDSenderInitRequest = "SenderInitRequest"
MsgIDSenderInitReply = "SenderInitReply"
MsgIDReceiverInitRequest = "ReceiverInitRequest"
MsgIDReceiverInitReply = "ReceiverInitReply"
MsgIDWrite = "Write"
MsgIDWinSize = "WinSize"
MsgIDSenderInitRequest = "SenderInitRequest"
MsgIDSenderInitReply = "SenderInitReply"
MsgIDSenderNewReceiverConnected = "SenderNewReceiverConnected"
MsgIDReceiverInitRequest = "ReceiverInitRequest"
MsgIDReceiverInitReply = "ReceiverInitReply"
MsgIDWrite = "Write"
MsgIDWinSize = "WinSize"
)

// Message used to encapsulate the rest of the bessages bellow
type MsgAll struct {
Type ProtocolMessageIDType
Data []byte
}

// These messages are used between the server and the sender/receiver
type MsgTTYSenderInitRequest struct {
Salt string
PasswordVerifierA string
Expand All @@ -32,13 +35,19 @@ type MsgTTYSenderInitReply struct {
ReceiverURLWebReadWrite string
}

type MsgTTYSenderNewReceiverConnected struct {
Name string
}

type MsgTTYReceiverInitRequest struct {
ChallengeReply string
}

type MsgTTYReceiverInitReply struct {
}

// These messages are not intended for the server, so they are just forwarded by it to the remote
// side.
type MsgTTYWrite struct {
Data []byte
Size int
Expand Down Expand Up @@ -107,6 +116,15 @@ func MarshalMsg(aMessage interface{}) (_ []byte, err error) {
return json.Marshal(msg)
}

if newRcvMsg, ok := aMessage.(MsgTTYSenderNewReceiverConnected); ok {
msg.Type = MsgIDSenderNewReceiverConnected
msg.Data, err = json.Marshal(newRcvMsg)
if err != nil {
return
}
return json.Marshal(msg)
}

return nil, nil
}

Expand Down
7 changes: 7 additions & 0 deletions tty-sender/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ func main() {
json.Unmarshal(msg.Data, &msgWrite)
ptyMaster.Write(msgWrite.Data[:msgWrite.Size])
}
if msg.Type == common.MsgIDSenderNewReceiverConnected {
var msgReceiverConnected common.MsgTTYSenderNewReceiverConnected
json.Unmarshal(msg.Data, &msgReceiverConnected)

ptyMaster.Refresh()
fmt.Printf("New receiver connected: %s ", msgReceiverConnected.Name)
}
}
}()

Expand Down
21 changes: 20 additions & 1 deletion tty-sender/pty_master.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os/exec"
"os/signal"
"syscall"
"time"

ptyDevice "github.com/elisescu/pty"
"golang.org/x/crypto/ssh/terminal"
Expand Down Expand Up @@ -61,7 +62,8 @@ func (pty *ptyMaster) Start(command string, args []string, winChangedCB onWindow
}

func (pty *ptyMaster) GetWinSize() (int, int, error) {
return terminal.GetSize(0)
cols, rows, err := terminal.GetSize(0)
return cols, rows, err
}

func (pty *ptyMaster) Write(b []byte) (int, error) {
Expand All @@ -76,6 +78,23 @@ func (pty *ptyMaster) SetWinSize(rows, cols int) {
ptyDevice.Setsize(pty.ptyFile, rows, cols)
}

func (pty *ptyMaster) Refresh() {
// We wanna force the app to re-draw itself, but there doesn't seem to be a way to do that
// so we fake it by resizing the window quickly, making it smaller and then back big
cols, rows, err := pty.GetWinSize()

if err != nil {
return
}

pty.SetWinSize(rows-1, cols)

go func() {
time.Sleep(time.Millisecond * 50)
pty.SetWinSize(rows, cols)
}()
}

func (pty *ptyMaster) Wait() (err error) {
err = pty.command.Wait()
// The terminal has to be restored from the RAW state, to its initial state
Expand Down
10 changes: 10 additions & 0 deletions tty-server/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ func (session *ttyShareSession) HandleReceiver(rawConn *WSConnection) {
// Sending the initial size of the window, if we have one
rcvProtoConn.WriteRawData(lastWindowSize)

// Notify the tty-sender that we got a new receiver connected
msgRcvConnected, err := MarshalMsg(MsgTTYSenderNewReceiverConnected{
Name: rawConn.Address(),
})
senderConn.WriteRawData(msgRcvConnected)

if err != nil {
log.Errorf("Cannot notify tty-sender. Error: %s", err.Error())
}

// Wait until the TTYReceiver will close the connection on its end
for {
msg, err := rcvProtoConn.ReadMessage()
Expand Down

0 comments on commit 885e5f1

Please sign in to comment.