-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(studio connections): Support for binary serialization of messages (
#853)
- Loading branch information
1 parent
13446fe
commit 0fd75b8
Showing
25 changed files
with
852 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,164 @@ | ||
import {StorageType, binaryToObject, createObjectToBinaryOptions, objectToBinary} from "../../util/binarySerialization.js"; | ||
import {deserializeErrorHook, serializeErrorHook} from "../../util/TypedMessenger/errorSerialization.js"; | ||
import {TypedMessenger} from "../../util/TypedMessenger/TypedMessenger.js"; | ||
|
||
const typedMessengerSendDataBinaryOpts = createObjectToBinaryOptions({ | ||
structure: [ | ||
StorageType.UNION_ARRAY, | ||
{ | ||
id: StorageType.UINT16, | ||
type: StorageType.STRING, | ||
args: StorageType.ARRAY_BUFFER, | ||
}, | ||
{ | ||
id: StorageType.UINT16, | ||
type: StorageType.STRING, | ||
returnValue: StorageType.ARRAY_BUFFER, | ||
didThrow: StorageType.BOOL, | ||
}, | ||
{ | ||
json: StorageType.STRING, | ||
}, | ||
], | ||
nameIds: { | ||
id: 1, | ||
type: 2, | ||
args: 3, | ||
returnValue: 4, | ||
didThrow: 5, | ||
json: 6, | ||
}, | ||
}); | ||
|
||
/** | ||
* @template {import("../../mod.js").TypedMessengerSignatures} TReliableRespondHandlers | ||
* @template {import("../../mod.js").TypedMessengerSignatures} TReliableRequestHandlers | ||
*/ | ||
export class StudioConnection { | ||
#messageHandler; | ||
|
||
/** | ||
* @param {import("./messageHandlers/MessageHandler.js").MessageHandler} messageHandler | ||
* @param {TReliableRespondHandlers} reliableResponseHandlers | ||
* @param {import("./DiscoveryManager.js").ConnectionRequestAcceptOptions<TReliableRespondHandlers>} options | ||
*/ | ||
constructor(messageHandler, reliableResponseHandlers) { | ||
/** @private */ | ||
this.messageHandler = messageHandler; | ||
constructor(messageHandler, options) { | ||
this.#messageHandler = messageHandler; | ||
|
||
/** @type {TypedMessenger<TReliableRespondHandlers, TReliableRequestHandlers>} */ | ||
this.messenger = new TypedMessenger(); | ||
this.messenger.setResponseHandlers(reliableResponseHandlers); | ||
this.messenger.setSendHandler(data => { | ||
messageHandler.send(data.sendData, {transfer: data.transfer}); | ||
this.messenger = new TypedMessenger({ | ||
deserializeErrorHook, | ||
serializeErrorHook, | ||
}); | ||
this.messenger.setResponseHandlers(options.reliableResponseHandlers || /** @type {TReliableRespondHandlers} */ ({})); | ||
this.messenger.setSendHandler(async data => { | ||
if (messageHandler.supportsSerialization) { | ||
await messageHandler.send(data.sendData, {transfer: data.transfer}); | ||
} else { | ||
const castType = /** @type {string} */ (data.sendData.type); | ||
/** @type {ArrayBuffer} */ | ||
let buffer; | ||
if (data.sendData.direction == "request" && options.requestSerializers && options.requestSerializers[castType]) { | ||
const serializedArguments = await options.requestSerializers[castType](...data.sendData.args); | ||
buffer = objectToBinary({ | ||
id: data.sendData.id, | ||
type: castType, | ||
args: serializedArguments, | ||
}, typedMessengerSendDataBinaryOpts); | ||
} else if (data.sendData.direction == "response" && options.responseSerializers && options.responseSerializers[castType] && !data.sendData.didThrow) { | ||
const serializedReturnType = await options.responseSerializers[castType](data.sendData.returnValue); | ||
buffer = objectToBinary({ | ||
id: data.sendData.id, | ||
type: castType, | ||
didThrow: data.sendData.didThrow, | ||
returnValue: serializedReturnType, | ||
}, typedMessengerSendDataBinaryOpts); | ||
} else { | ||
buffer = objectToBinary({ | ||
json: JSON.stringify(data.sendData), | ||
}, typedMessengerSendDataBinaryOpts); | ||
} | ||
await messageHandler.send(buffer); | ||
} | ||
}); | ||
messageHandler.onMessage(data => { | ||
const castData = /** @type {import("../../mod.js").TypedMessengerMessageSendData<TReliableRespondHandlers, TReliableRequestHandlers>} */ (data); | ||
this.messenger.handleReceivedMessage(castData); | ||
messageHandler.onMessage(async data => { | ||
/** @type {import("../../mod.js").TypedMessengerMessageSendData<any, any>} */ | ||
let decodedData; | ||
if (messageHandler.supportsSerialization) { | ||
decodedData = /** @type {import("../../mod.js").TypedMessengerMessageSendData<any, any>} */ (data); | ||
} else { | ||
if (!(data instanceof ArrayBuffer)) { | ||
throw new Error("This message handler is expected to only receive ArrayBuffer messages."); | ||
} | ||
const decoded = binaryToObject(data, typedMessengerSendDataBinaryOpts); | ||
if ("args" in decoded) { | ||
if (!options.requestDeserializers || !options.requestDeserializers[decoded.type]) { | ||
throw new Error(`Unexpected serialized request message was received for "${decoded.type}". The message was serialized by the sender in the 'requestSerializers' object, but no deserializer was defined in the 'requestDeserializers' object.`); | ||
} | ||
const decodedArgs = await options.requestDeserializers[decoded.type](decoded.args); | ||
decodedData = { | ||
direction: "request", | ||
type: decoded.type, | ||
id: decoded.id, | ||
args: /** @type {any} */ (decodedArgs), | ||
}; | ||
} else if ("returnValue" in decoded) { | ||
if (!options.responseDeserializers || !options.responseDeserializers[decoded.type]) { | ||
throw new Error(`Unexpected serialized response message was received for "${decoded.type}". The message was serialized by the sender in the 'responseSerializers' object, but no deserializer was defined in the 'responseDeserializers' object.`); | ||
} | ||
const decodedReturnValue = await options.responseDeserializers[decoded.type](decoded.returnValue); | ||
decodedData = { | ||
direction: "response", | ||
type: decoded.type, | ||
id: decoded.id, | ||
didThrow: decoded.didThrow, | ||
returnValue: decodedReturnValue, | ||
}; | ||
} else if ("json" in decoded) { | ||
decodedData = JSON.parse(decoded.json); | ||
} else { | ||
throw new Error("An error occurred while deserializing the message."); | ||
} | ||
} | ||
const castData = /** @type {import("../../mod.js").TypedMessengerMessageSendData<TReliableRespondHandlers, TReliableRequestHandlers>} */ (decodedData); | ||
await this.messenger.handleReceivedMessage(castData); | ||
}); | ||
} | ||
|
||
get otherClientUuid() { | ||
return this.messageHandler.otherClientUuid; | ||
return this.#messageHandler.otherClientUuid; | ||
} | ||
|
||
get clientType() { | ||
return this.messageHandler.clientType; | ||
return this.#messageHandler.clientType; | ||
} | ||
|
||
get connectionType() { | ||
return this.messageHandler.connectionType; | ||
return this.#messageHandler.connectionType; | ||
} | ||
|
||
/** | ||
* True when the connection was initiated by our client (i.e. the client that holds the instance of this class in memory). | ||
*/ | ||
get initiatedByMe() { | ||
return this.messageHandler.initiatedByMe; | ||
return this.#messageHandler.initiatedByMe; | ||
} | ||
|
||
get projectMetadata() { | ||
return this.messageHandler.projectMetadata; | ||
return this.#messageHandler.projectMetadata; | ||
} | ||
|
||
close() { | ||
this.messageHandler.close(); | ||
this.#messageHandler.close(); | ||
} | ||
|
||
/** | ||
* @param {import("./messageHandlers/MessageHandler.js").OnStatusChangeCallback} cb | ||
*/ | ||
onStatusChange(cb) { | ||
this.messageHandler.onStatusChange(cb); | ||
this.#messageHandler.onStatusChange(cb); | ||
} | ||
|
||
get status() { | ||
return this.messageHandler.status; | ||
return this.#messageHandler.status; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.