Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 1 | import {ConfigHandler} from './config_handler'; |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 2 | import {Configuration} from 'aos/configuration_generated'; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 3 | |
| 4 | // There is one handler for each DataChannel, it maintains the state of |
| 5 | // multi-part messages and delegates to a callback when the message is fully |
| 6 | // assembled. |
| 7 | export class Handler { |
| 8 | private dataBuffer: Uint8Array|null = null; |
| 9 | private receivedMessageLength: number = 0; |
| 10 | constructor( |
| 11 | private readonly handlerFunc: (data: Uint8Array) => void, |
| 12 | private readonly channel: RTCPeerConnection) { |
| 13 | channel.addEventListener('message', (e) => this.handleMessage(e)); |
| 14 | } |
| 15 | |
| 16 | handleMessage(e: MessageEvent): void { |
| 17 | const fbBuffer = new flatbuffers.ByteBuffer(new Uint8Array(e.data)); |
| 18 | const messageHeader = |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 19 | WebProxy.MessageHeader.getRootAsMessageHeader(fbBuffer); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 20 | // Short circuit if only one packet |
| 21 | if (messageHeader.packetCount === 1) { |
| 22 | this.handlerFunc(messageHeader.dataArray()); |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | if (messageHeader.packetIndex() === 0) { |
| 27 | this.dataBuffer = new Uint8Array(messageHeader.length()); |
| 28 | } |
| 29 | this.dataBuffer.set( |
| 30 | messageHeader.dataArray(), |
| 31 | this.receivedMessageLength); |
| 32 | this.receivedMessageLength += messageHeader.dataLength(); |
| 33 | |
| 34 | if (messageHeader.packetIndex() === messageHeader.packetCount() - 1) { |
| 35 | this.handlerFunc(this.dataBuffer); |
| 36 | } |
| 37 | } |
| 38 | } |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 39 | |
| 40 | // Analogous to the Connection class in //aos/network/web_proxy.h. Because most |
| 41 | // of the apis are native in JS, it is much simpler. |
| 42 | export class Connection { |
| 43 | private webSocketConnection: WebSocket|null = null; |
| 44 | private rtcPeerConnection: RTCPeerConnection|null = null; |
| 45 | private dataChannel: DataChannel|null = null; |
| 46 | private webSocketUrl: string; |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 47 | private configHandler: ConfigHandler|null = null; |
| 48 | private config: Configuration|null = null; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 49 | private readonly handlerFuncs = new Map<string, (data: Uint8Array) => void>(); |
| 50 | private readonly handlers = new Set<Handler>(); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 51 | |
| 52 | constructor() { |
| 53 | const server = location.host; |
| 54 | this.webSocketUrl = `ws://${server}/ws`; |
| 55 | } |
| 56 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 57 | addHandler(id: string, handler: (data: Uint8Array) => void): void { |
| 58 | this.handlerFuncs.set(id, handler); |
| 59 | } |
| 60 | |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 61 | connect(): void { |
| 62 | this.webSocketConnection = new WebSocket(this.webSocketUrl); |
| 63 | this.webSocketConnection.binaryType = 'arraybuffer'; |
| 64 | this.webSocketConnection.addEventListener( |
| 65 | 'open', () => this.onWebSocketOpen()); |
| 66 | this.webSocketConnection.addEventListener( |
| 67 | 'message', (e) => this.onWebSocketMessage(e)); |
| 68 | } |
| 69 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 70 | // Handle messages on the DataChannel. Handles the Configuration message as |
| 71 | // all other messages are sent on specific DataChannels. |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 72 | onDataChannelMessage(e: MessageEvent): void { |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 73 | const fbBuffer = new flatbuffers.ByteBuffer(new Uint8Array(e.data)); |
| 74 | // TODO(alex): handle config updates if/when required |
| 75 | if (!this.configHandler) { |
| 76 | const config = aos.Configuration.getRootAsConfiguration(fbBuffer); |
| 77 | this.config = config; |
| 78 | this.configHandler = new ConfigHandler(config, this.dataChannel); |
| 79 | this.configHandler.printConfig(); |
| 80 | return; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | onDataChannel(ev: RTCDataChannelEvent): void { |
| 85 | const channel = ev.channel; |
| 86 | const name = channel.label; |
| 87 | const channelType = name.split('/').pop(); |
| 88 | const handlerFunc = this.handlerFuncs.get(channelType); |
| 89 | this.handlers.add(new Handler(handlerFunc, channel)); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | onIceCandidate(e: RTCPeerConnectionIceEvent): void { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 93 | if (!e.candidate) { |
| 94 | return; |
| 95 | } |
| 96 | const candidate = e.candidate; |
| 97 | const builder = new flatbuffers.Builder(512); |
| 98 | const candidateString = builder.createString(candidate.candidate); |
| 99 | const sdpMidString = builder.createString(candidate.sdpMid); |
| 100 | |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 101 | const iceFb = WebProxy.WebSocketIce.createWebSocketIce( |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 102 | builder, candidateString, sdpMidString, candidate.sdpMLineIndex); |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 103 | const messageFb = WebProxy.WebSocketMessage.createWebSocketMessage( |
| 104 | builder, WebProxy.Payload.WebSocketIce, iceFb); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 105 | builder.finish(messageFb); |
| 106 | const array = builder.asUint8Array(); |
| 107 | this.webSocketConnection.send(array.buffer.slice(array.byteOffset)); |
| 108 | } |
| 109 | |
| 110 | // Called for new SDPs. Make sure to set it locally and remotely. |
| 111 | onOfferCreated(description: RTCSessionDescription): void { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 112 | this.rtcPeerConnection.setLocalDescription(description); |
| 113 | const builder = new flatbuffers.Builder(512); |
| 114 | const offerString = builder.createString(description.sdp); |
| 115 | |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 116 | const webSocketSdp = WebProxy.WebSocketSdp.createWebSocketSdp( |
| 117 | builder, WebProxy.SdpType.OFFER, offerString); |
| 118 | const message = WebProxy.WebSocketMessage.createWebSocketMessage( |
| 119 | builder, WebProxy.Payload.WebSocketSdp, webSocketSdp); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 120 | builder.finish(message); |
| 121 | const array = builder.asUint8Array(); |
| 122 | this.webSocketConnection.send(array.buffer.slice(array.byteOffset)); |
| 123 | } |
| 124 | |
| 125 | // We now have a websocket, so start setting up the peer connection. We only |
| 126 | // want a DataChannel, so create it and then create an offer to send. |
| 127 | onWebSocketOpen(): void { |
| 128 | this.rtcPeerConnection = new RTCPeerConnection({}); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 129 | this.rtcPeerConnection.addEventListener( |
| 130 | 'datachannel', (e) => this.onDataCnannel(e)); |
| 131 | this.dataChannel = this.rtcPeerConnection.createDataChannel('signalling'); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 132 | this.dataChannel.addEventListener( |
| 133 | 'message', (e) => this.onDataChannelMessage(e)); |
| 134 | window.dc = this.dataChannel; |
| 135 | this.rtcPeerConnection.addEventListener( |
| 136 | 'icecandidate', (e) => this.onIceCandidate(e)); |
| 137 | this.rtcPeerConnection.createOffer().then( |
| 138 | (offer) => this.onOfferCreated(offer)); |
| 139 | } |
| 140 | |
| 141 | // When we receive a websocket message, we need to determine what type it is |
| 142 | // and handle appropriately. Either by setting the remote description or |
| 143 | // adding the remote ice candidate. |
| 144 | onWebSocketMessage(e: MessageEvent): void { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 145 | const buffer = new Uint8Array(e.data) |
| 146 | const fbBuffer = new flatbuffers.ByteBuffer(buffer); |
| 147 | const message = |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 148 | WebProxy.WebSocketMessage.getRootAsWebSocketMessage(fbBuffer); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 149 | switch (message.payloadType()) { |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 150 | case WebProxy.Payload.WebSocketSdp: |
| 151 | const sdpFb = message.payload(new WebProxy.WebSocketSdp()); |
| 152 | if (sdpFb.type() !== WebProxy.SdpType.ANSWER) { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 153 | console.log('got something other than an answer back'); |
| 154 | break; |
| 155 | } |
| 156 | this.rtcPeerConnection.setRemoteDescription(new RTCSessionDescription( |
| 157 | {'type': 'answer', 'sdp': sdpFb.payload()})); |
| 158 | break; |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 159 | case WebProxy.Payload.WebSocketIce: |
| 160 | const iceFb = message.payload(new WebProxy.WebSocketIce()); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 161 | const candidate = {} as RTCIceCandidateInit; |
| 162 | candidate.candidate = iceFb.candidate(); |
| 163 | candidate.sdpMid = iceFb.sdpMid(); |
| 164 | candidate.sdpMLineIndex = iceFb.sdpMLineIndex(); |
| 165 | this.rtcPeerConnection.addIceCandidate(candidate); |
| 166 | break; |
| 167 | default: |
| 168 | console.log('got an unknown message'); |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | } |