Program architecture
Client
Reads keystrokes from server. Creates screenshots, detects changes in them and sends changed tiles (128x128px by default) to the server.
Two threads: GUI thread and ClientWorker, communicating with queues.
Queue<byte[]> received
filled by ClientWorker with messages from the server
-
contains keystrokes and mouse instructions, they are to be performed
Queue<byte[]> tosend
ClientWorker thread:
perform SSL handshake
enter an infinite loop:
GUI thread:
on timer1.tick():
if some data (keystrokes or mouse instructions) are available in the “received” queue, process them
if “tosend” queue is empty (this is to prevent network congestion)
create screenshot
break it to 128x128px tiles
compare with previous screenshot
add changed tiles to “tosend” queue
Server
Displays tiles received from client. Forwards KeyPress and MouseClick events to the client.
Two threads: GUI thread and ServerWorker, communicating with queues.
Queue<byte[]> received
Queue<byte[]> tosend
ServerWorker thread:
listen on socket, perform SSL handshake
read data from socket asynchronously, put them to “readData” queue
enter infinite loop:
read “readData” queue, put messages to “received” queue
if “tosend” queue is not empty, send data from it to client
GUI thread:
Protocol specification
Datagram format:
4B magic value “0x95400954”, 4B length, 4B type, lengthB-4 payload
Message type 0x02: Client -> Server Rectangle
payload: 2B format, 2B xstart, 2B ystart, 2B width, 2B height, data
format: 0x1 = JPEG
Contains image data to be displayed.
Message type 0x01: Server -> Client Hello
Message type 0x02: Server -> Client Mouse Event
payload: 2B X, 2B Y, 4B flags
Message type 0x03: Server -> Client Text Input (type 0x3)
payload: text to be typed on the keyboard
Message type 0x04: Server -> Client Force Redraw (type 0x4)
payload: nothing
Forces client to resend all tiles again.
Sources used