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 | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 69 | addHandler(id: string, handler: (data: Uint8Array) => void): void { |
| 70 | this.handlerFuncs.set(id, handler); |
| 71 | } |
| 72 | |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 73 | connect(): void { |
| 74 | this.webSocketConnection = new WebSocket(this.webSocketUrl); |
| 75 | this.webSocketConnection.binaryType = 'arraybuffer'; |
| 76 | this.webSocketConnection.addEventListener( |
| 77 | 'open', () => this.onWebSocketOpen()); |
| 78 | this.webSocketConnection.addEventListener( |
| 79 | 'message', (e) => this.onWebSocketMessage(e)); |
| 80 | } |
| 81 | |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 82 | get config() { |
| 83 | return this.config_internal; |
| 84 | } |
| 85 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 86 | // Handle messages on the DataChannel. Handles the Configuration message as |
| 87 | // all other messages are sent on specific DataChannels. |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 88 | onDataChannelMessage(e: MessageEvent): void { |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 89 | const fbBuffer = new flatbuffers.ByteBuffer(new Uint8Array(e.data)); |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 90 | this.configInternal = Configuration.getRootAsConfiguration(fbBuffer); |
| 91 | for (handler of this.configHandlers) { |
| 92 | handler(this.configInternal); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | onDataChannel(ev: RTCDataChannelEvent): void { |
| 97 | const channel = ev.channel; |
| 98 | const name = channel.label; |
| 99 | const channelType = name.split('/').pop(); |
| 100 | const handlerFunc = this.handlerFuncs.get(channelType); |
| 101 | this.handlers.add(new Handler(handlerFunc, channel)); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | onIceCandidate(e: RTCPeerConnectionIceEvent): void { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 105 | if (!e.candidate) { |
| 106 | return; |
| 107 | } |
| 108 | const candidate = e.candidate; |
| 109 | const builder = new flatbuffers.Builder(512); |
| 110 | const candidateString = builder.createString(candidate.candidate); |
| 111 | const sdpMidString = builder.createString(candidate.sdpMid); |
| 112 | |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 113 | const iceFb = WebProxy.WebSocketIce.createWebSocketIce( |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 114 | builder, candidateString, sdpMidString, candidate.sdpMLineIndex); |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 115 | const messageFb = WebProxy.WebSocketMessage.createWebSocketMessage( |
| 116 | builder, WebProxy.Payload.WebSocketIce, iceFb); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 117 | builder.finish(messageFb); |
| 118 | const array = builder.asUint8Array(); |
| 119 | this.webSocketConnection.send(array.buffer.slice(array.byteOffset)); |
| 120 | } |
| 121 | |
| 122 | // Called for new SDPs. Make sure to set it locally and remotely. |
| 123 | onOfferCreated(description: RTCSessionDescription): void { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 124 | this.rtcPeerConnection.setLocalDescription(description); |
| 125 | const builder = new flatbuffers.Builder(512); |
| 126 | const offerString = builder.createString(description.sdp); |
| 127 | |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 128 | const webSocketSdp = WebProxy.WebSocketSdp.createWebSocketSdp( |
| 129 | builder, WebProxy.SdpType.OFFER, offerString); |
| 130 | const message = WebProxy.WebSocketMessage.createWebSocketMessage( |
| 131 | builder, WebProxy.Payload.WebSocketSdp, webSocketSdp); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 132 | builder.finish(message); |
| 133 | const array = builder.asUint8Array(); |
| 134 | this.webSocketConnection.send(array.buffer.slice(array.byteOffset)); |
| 135 | } |
| 136 | |
| 137 | // We now have a websocket, so start setting up the peer connection. We only |
| 138 | // want a DataChannel, so create it and then create an offer to send. |
| 139 | onWebSocketOpen(): void { |
| 140 | this.rtcPeerConnection = new RTCPeerConnection({}); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 141 | this.rtcPeerConnection.addEventListener( |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 142 | 'datachannel', (e) => this.onDataChannel(e)); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 143 | this.dataChannel = this.rtcPeerConnection.createDataChannel('signalling'); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 144 | this.dataChannel.addEventListener( |
| 145 | 'message', (e) => this.onDataChannelMessage(e)); |
| 146 | window.dc = this.dataChannel; |
| 147 | this.rtcPeerConnection.addEventListener( |
| 148 | 'icecandidate', (e) => this.onIceCandidate(e)); |
| 149 | this.rtcPeerConnection.createOffer().then( |
| 150 | (offer) => this.onOfferCreated(offer)); |
| 151 | } |
| 152 | |
| 153 | // When we receive a websocket message, we need to determine what type it is |
| 154 | // and handle appropriately. Either by setting the remote description or |
| 155 | // adding the remote ice candidate. |
| 156 | onWebSocketMessage(e: MessageEvent): void { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 157 | const buffer = new Uint8Array(e.data) |
| 158 | const fbBuffer = new flatbuffers.ByteBuffer(buffer); |
| 159 | const message = |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 160 | WebProxy.WebSocketMessage.getRootAsWebSocketMessage(fbBuffer); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 161 | switch (message.payloadType()) { |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 162 | case WebProxy.Payload.WebSocketSdp: |
| 163 | const sdpFb = message.payload(new WebProxy.WebSocketSdp()); |
| 164 | if (sdpFb.type() !== WebProxy.SdpType.ANSWER) { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 165 | console.log('got something other than an answer back'); |
| 166 | break; |
| 167 | } |
| 168 | this.rtcPeerConnection.setRemoteDescription(new RTCSessionDescription( |
| 169 | {'type': 'answer', 'sdp': sdpFb.payload()})); |
| 170 | break; |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 171 | case WebProxy.Payload.WebSocketIce: |
| 172 | const iceFb = message.payload(new WebProxy.WebSocketIce()); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 173 | const candidate = {} as RTCIceCandidateInit; |
| 174 | candidate.candidate = iceFb.candidate(); |
| 175 | candidate.sdpMid = iceFb.sdpMid(); |
| 176 | candidate.sdpMLineIndex = iceFb.sdpMLineIndex(); |
| 177 | this.rtcPeerConnection.addIceCandidate(candidate); |
| 178 | break; |
| 179 | default: |
| 180 | console.log('got an unknown message'); |
| 181 | break; |
| 182 | } |
| 183 | } |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * Subscribes to messages. |
| 187 | * @param a Finished flatbuffer.Builder containing a Connect message to send. |
| 188 | */ |
| 189 | sendConnectMessage(builder: any) { |
| 190 | const array = builder.assUint8Array(); |
| 191 | this.dataChannel.send(array.buffer.slice(array.byteOffset)); |
| 192 | } |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 193 | } |