-
Notifications
You must be signed in to change notification settings - Fork 0
Remote Client
The RemoteClient class is a base class that the two example clients, WorkbenchClient and AudioPatchbayClient, are derived from. It contains general purpose functions that are shared between the two example clients.
Below is an explanation of each function, and where applicable, a sample block of JSON code to show how a command sent by the function would look, and how a response to the command would look.
Where applicable, variables will be explained as well.
The uint32 variable 'magicNumber' is the magic number written to the start of the packet that has to match at both ends of the connection. The derived classes have their own magic numbers that they use for this purpose.
The bool variable 'hostCurrentlyChangingProperty' is used to determine whether the host is currently changing a property. Using a bool variable this way works due to the InterprocessConnection class being set to send callbacks on the message thread.
the LinkedListPointer variable 'inflightCommands' is used to store a linked list of inflightCommand pointers.
The connectionMade function is called when the socket successfully connects to a remote application. It then generates a notification that the socket has changed.
The connectionLost function is called when the socket has disconnected. It then generates a notification that the socket has changed.
The messageReceived function is called when a message comes in over the socket.
The MemoryBlock parameter is just JSON without the magic number or byte count.
First, this function parses the MemoryBlock into JSON, and displays it in the appropriate window in the user interface.
It then starts reading properties out of the JSON data. It first does some error checking to make sure this is valid JSON data. It checks to see if the message has a sequence number, and if it has a status message.
It then checks to see if the sequence number matches a previously sent command's sequence number. If it does not match, it checks to see if the incoming message is a PropertyChanged message. These can be sent unsolicited by the remote program, so it's okay if the sequence numbers do not match a previously sent command.
Finally, it checks to see if the status's message is set to "OK".
If any of these checks fail, the function returns.
After error checking, the function then checks to see if the incoming message is of a type that it can handle. There are currently two types; PropertyChanged and GetResponse. In both cases, the function handlePropertyChangedMessage is called.
If the incoming message does not match a type that it can handle, the function returns.
The getProperty function creates a message object out of the passed in parameters, which is then sent to the remote program via the socket with the function sendJSONToSocket.
In this particular case, the message being sent is a request for a specific property value, which is determined by the passed in parameters.
The ID variable is an Identifier, which is similar to a string variable. It determines which property is being requested.
the parameter variable is used as part of the construction of the message being sent; specifically, it is being used along with the ID variable as a JSON object in the message.
Once sendJSONToSocket is called, the function returns a Result which contains the success or failure of the message being sent across the socket.
The getSystemInfo function is a wrapper function that calls getProperty, with the necessary variables to request the system information property values.
In this case, the ID variable is set to Identifiers::System, and the parameter is a new DynamicObject.
It returns a Result on whether sending the message succeeded or failed.
The sendJSONToSocket function has a messageObject passed in, which it converts into a JSON string by writing the messageObject to a MemoryOutputStream.
It updates the user interface to show the outgoing message, then sends the JSON string across the socket. If it succeeds, it locks for a short interval to create and store the InflightCommand, which is a derived class of RemoteClient. It then returns a Result stating that the message was sent successfully.
If sending the message fails, the function returns an error message stating that the message failed to send properly.
The inflightCommandFound function checks through a list of inflightCommands to see if any of them match the current received message's sequence number. It locks during the process.
If it finds an inflightCommand that matches the sequence number, it removes that inflightCommand from the array, deletes it, and returns true.
If it does not find an inflightCommand that matches the sequence number, it returns false.
InflightCommand is a derived class that is used to store several variables to keep track of in-flight commands that have been sent across the socket.
The sequence variable is used to check incoming messages to see if they match previously sent message sequence numbers, for error checking purposes.
The time variable is used to store the time the message was sent.
The nextListItem variable is used to point to the next inflightCommand in the linked list.