Your Universal Remote Control Center
RemoteCentral.com
Philips Pronto Professional Forum - View Post
Up level
Up level
The following page was printed from RemoteCentral.com:

Login:
Pass:
 
 

Original thread:
Post 2 made on Saturday August 22, 2020 at 20:04
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,994
The queue is definitely the best approach.

Use an array for the queue. Do .push(entry) to add things to the end and .shift() to remove the first entry.

This is one way to do it. The queue just guarantees an ordered write.

var queue = [];
var buffer = '';
var activeCommand = null;

function sendSomething(toSend) {
queue.push(toSend);
sendNext();
}

function sendNext() {
if (activeCommand) {
return;
}

if (socket && socket.connected && queue.length) {
activeCommand = queue.shift();
socket.write(activeCommand);
}
}


function onConnect() {
// if any commands have been queued
sendNext();
}

function cleanupSocket() {
if (socket) {
if (socket.connected) {
try{socket.close();}catch(e){}
}
socket.onData = null;
socket.onIOError = null;
socket.onConnect = null;
socket = null;
}
}

function onIOError() {
cleanupSocket();
}

function onData() {
// within onData, 'this' and socket are one in the same.
buffer += this.read();
// process any data that was read.
// blah blah blah
// assume that if you received a message, server is ready for next command.
activeCommand = null;
// send next command.
sendNext();
}

Note that ideally, you might want to use a scheduleAfter() to clear activeCommand and then call sendNext Periodically in case the server does not respond.

Finally, the code above does not manage reconnects should the server close the socket. I leave that exercise up to you. I would recommend that in onIOError that you use scheduleAfter to do a reconnect.

Pete,

I edited the above after a re-read and also added onConnect and onIOError as an example.

I do what you ask specifically in my Denon TCP/IP module but as I use 2 queues (priority and background), the code is more complicated than needed for this purpose.

If you will be so kind as to email me at my address on profile, I can help you work out the details of how to use a single socket that reconnects as needed and also uses a queue to manage commands. Email me your PS Library or your config I will have a look. If you are in the USA, I don't mind having a phone call (or facetime) to talk through the details.

Last edited by Lyndel McGee on August 23, 2020 09:49.
Lyndel McGee
Philips Pronto Addict/Beta Tester


Hosting Services by ipHouse