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