Skip to content

Commit c99a680

Browse files
committed
Fix message handling when sending multiple commands in short succession (#20)
1 parent 55c18c3 commit c99a680

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/DOC
22
/.idea
3+
/luacov.report.out
4+
/luacov.stats.out

libvisca.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1058,8 +1058,8 @@ function Visca.Connection:__transmissions_process()
10581058
local transmit_size = 0
10591059
local transmit_data
10601060

1061-
local remove_transmission = false
10621061
for i,t in pairs(self.transmission_queue) do
1062+
local remove_transmission = false
10631063
if t:timed_out() then
10641064
self:__exec_callback('timeout', t)
10651065
remove_transmission = true

test/libvisca_test.lua

+42
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,46 @@ function test_send_raw_command()
410410
lunit.assert_table_equal({0x81, 0x01, 0x7E, 0x01, 0x0A, 0x00, 0x02, 0xFF}, msg_tally.payload)
411411
end
412412

413+
function test_transaction_processing()
414+
-- Adding first item should yield immediate send, thus send bytes > 0
415+
local size, message_data = connection:Cam_Pantilt_Position_Inquiry()
416+
lunit.assert_equal(13, size)
417+
local msg = Visca.Message.new():from_data(message_data):dump("Cam_Pantilt_Position_Inquiry")
418+
lunit.assert_equal(0, msg.seq_nr)
419+
420+
-- Queue should contain 1 item
421+
lunit.assert_equal(1, #connection.transmission_queue)
422+
-- Reprocessing should not yield any effect
423+
lunit.assert_equal(0, connection:__transmissions_process())
424+
-- Adding second item should not cause sending of data, since reply to first command is not received
425+
lunit.assert_equal(0, connection:Cam_Zoom_Position_Inquiry())
426+
-- Queue should contain 2 items
427+
lunit.assert_equal(2, #connection.transmission_queue)
428+
-- Polling receive yields no effect
429+
lunit.assert_nil(connection:receive())
430+
lunit.assert_equal(2, #connection.transmission_queue)
431+
432+
lunit.assert_not_nil(connection.transmission_queue[1].send_timestamp)
433+
lunit.assert_nil(connection.transmission_queue[2].send_timestamp)
434+
435+
local is_completed_calls = 0
436+
connection:register_on_completion_callback(0, function(t) is_completed_calls = is_completed_calls + 1 end)
437+
438+
-- Inject a reply in the ReplyServer queue
439+
table.insert(Visca.ReplyServer.replies, {'127.1.2.101', "\x90\x50\x00\x00\x0E\x00\x0F\x0E\x09\x0C\xFF" })
440+
441+
msg, err, _ = connection:receive()
442+
lunit.assert_not_nil(msg)
443+
lunit.assert_not_nil(msg.message.reply)
444+
lunit.assert_true(msg.message.reply:is_completion())
445+
lunit.assert_nil(err)
446+
447+
lunit.assert_equal(1, is_completed_calls)
448+
449+
-- first message should be processed (and nil), second should be send
450+
lunit.assert_equal(2, #connection.transmission_queue)
451+
lunit.assert_nil(connection.transmission_queue[1])
452+
lunit.assert_not_nil(connection.transmission_queue[2].send_timestamp)
453+
end
454+
413455
lunit.main(...)

test/stubs/ljsocket.lua

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ function stub_socket.bind(_address, _port)
1212
return true
1313
end
1414

15+
function stub_socket.receive_from(_address, _length)
16+
return nil
17+
end
18+
1519
local M = {}
1620

1721
function M.connect()

0 commit comments

Comments
 (0)