用 iodine 处理 AVL 消息
handle AVL messages with iodine
现在我正在为 Skypatrol TT8750+ 的消息开发某种解析器,我的线程 TCP 服务器正在工作。问题是,如果同时连接许多设备,这不是一个好方法。我正在使用碘,但我无法使用提供给我的一些代码。我的目标是首先接收一条 33 字节的消息来识别设备,然后开始接收 86 字节的消息,其中包含车辆信息。
require 'iodine'
# define the protocol for our service
class TT8750plus
@timeout = 10
def on_open
puts "New Connection Accepted."
# this file is just for testing purposes.
t = Time.now.strftime("%d-%m-%Y %H%M")
file_name = t + '.txt'
@out_file = File.new(file_name, "w+")
# a rolling buffer for fragmented messages
@expecting = 33
@msg = ""
end
def on_message buffer
length = buffer.length
pos = 0
while length >= @expecting
@msg << (buffer[pos, @expecting])
@out_file.puts(@msg.unpack('H*')[0])
length -= @expecting
po += @expecting
@expecting = 86
@msg.clear
end
if(length > 0)
@msg << (buffer[pos, length])
@expecting = 86 - length
end
puts @msg
end
def on_close
@out_file.close
end
end
# create the service instance
Iodine.listen 12050, TT8750plus
# start the service
Iodine.start
每封邮件都会出现这个错误
New Connection Accepted.
Iodine caught an unprotected exception - NoMethodError: undefined method `+' for nil:NilClass
iodineServer.rb:26:in `on_message'
iodineServer.rb:1:in `on_data'Iodine caught an unprotected exception - NoMethodError: undefined method `+' for nil:NilClass
而且这个实现没有得到我需要的消息
这些是我从这个实现中得到的前两行:
0021000a0800000000000120202020202038363332383630323034333433373020
0021000a08000000000001202020202020383633323836303230343334333730200056000a08100020202020202038363332383630323034333433373020014b0000
这是线程实现的前两行
0021000a0800000000000120202020202038363332383630323034333433373020
0056000a08100020202020202038363332383630323034333433373020000b00000013090044709bfb8109e400000000001100000000000067eb11090c1512012e970020000000000005000000000005000000000007
0056000a08100020202020202038363332383630323034333433373020010b00000013090044709bfb8109e400000000001200000000000067eb11090c1512042e970020000000000005000000000005000000000008
这看起来像是我之前发布的未经测试的代码的变体。
问题似乎出在代码中的拼写错误(可能是我的?)。
po += ...
应该是pos += ...
require 'iodine'
# define the protocol for our service
class TT8750plus
@timeout = 10
def on_open
puts "New Connection Accepted."
# this file is just for testing purposes.
t = Time.now.strftime("%d-%m-%Y %H%M")
file_name = t + '.txt'
@out_file = File.new(file_name, "w+")
# a rolling buffer for fragmented messages
@expecting = 33
@msg = ""
end
def on_message buffer
length = buffer.length
pos = 0
while length >= @expecting
@msg << (buffer[pos, @expecting])
@out_file.puts(@msg.unpack('H*')[0])
length -= @expecting
pos += @expecting # the spelling mistake was here
@expecting = 86
puts "wrote:", @msg
@msg.clear
end
if(length > 0)
@msg << (buffer[pos, length])
@expecting = 86 - length
end
puts("Waiting for more data:", @msg) unless @msg.empty?
end
def on_close
@out_file.close
end
end
# create the service instance
Iodine.listen 12050, TT8750plus
# start the service
Iodine.start
同样,缺少 Skypatrol TT8750+ 的仿真,我无法测试代码。但是应该可以按照错误消息慢慢追踪这些类型的问题。
P.S.
为了防止出现异常,请考虑使用 Ruby 的:
begin
# code
rescue => e
# oops something happened. i.e.
puts e.message, e.backtrace
end
即 on_message
方法可能如下所示:
def on_message buffer
begin
length = buffer.length
pos = 0
while length >= @expecting
@msg << (buffer[pos, @expecting])
@out_file.puts(@msg.unpack('H*')[0])
length -= @expecting
pos += @expecting # the spelling mistake was here
@expecting = 86
@msg.clear
end
if(length > 0)
@msg << (buffer[pos, length])
@expecting = 86 - length
end
puts @msg unless @msg.empty? # print leftovers for testing...?
rescue => e
# oops something happened. React
puts e.message, e.backtrace
end
end
此外,仅供参考,iodine 允许您控制正在使用的进程(工作程序)和线程的数量。有关详细信息,请参阅 the documentation。
现在我正在为 Skypatrol TT8750+ 的消息开发某种解析器,我的线程 TCP 服务器正在工作。问题是,如果同时连接许多设备,这不是一个好方法。我正在使用碘,但我无法使用提供给我的一些代码。我的目标是首先接收一条 33 字节的消息来识别设备,然后开始接收 86 字节的消息,其中包含车辆信息。
require 'iodine'
# define the protocol for our service
class TT8750plus
@timeout = 10
def on_open
puts "New Connection Accepted."
# this file is just for testing purposes.
t = Time.now.strftime("%d-%m-%Y %H%M")
file_name = t + '.txt'
@out_file = File.new(file_name, "w+")
# a rolling buffer for fragmented messages
@expecting = 33
@msg = ""
end
def on_message buffer
length = buffer.length
pos = 0
while length >= @expecting
@msg << (buffer[pos, @expecting])
@out_file.puts(@msg.unpack('H*')[0])
length -= @expecting
po += @expecting
@expecting = 86
@msg.clear
end
if(length > 0)
@msg << (buffer[pos, length])
@expecting = 86 - length
end
puts @msg
end
def on_close
@out_file.close
end
end
# create the service instance
Iodine.listen 12050, TT8750plus
# start the service
Iodine.start
每封邮件都会出现这个错误
New Connection Accepted.
Iodine caught an unprotected exception - NoMethodError: undefined method `+' for nil:NilClass
iodineServer.rb:26:in `on_message'
iodineServer.rb:1:in `on_data'Iodine caught an unprotected exception - NoMethodError: undefined method `+' for nil:NilClass
而且这个实现没有得到我需要的消息 这些是我从这个实现中得到的前两行:
0021000a0800000000000120202020202038363332383630323034333433373020
0021000a08000000000001202020202020383633323836303230343334333730200056000a08100020202020202038363332383630323034333433373020014b0000
这是线程实现的前两行
0021000a0800000000000120202020202038363332383630323034333433373020
0056000a08100020202020202038363332383630323034333433373020000b00000013090044709bfb8109e400000000001100000000000067eb11090c1512012e970020000000000005000000000005000000000007
0056000a08100020202020202038363332383630323034333433373020010b00000013090044709bfb8109e400000000001200000000000067eb11090c1512042e970020000000000005000000000005000000000008
这看起来像是我之前发布的未经测试的代码的变体。
问题似乎出在代码中的拼写错误(可能是我的?)。
po += ...
应该是pos += ...
require 'iodine'
# define the protocol for our service
class TT8750plus
@timeout = 10
def on_open
puts "New Connection Accepted."
# this file is just for testing purposes.
t = Time.now.strftime("%d-%m-%Y %H%M")
file_name = t + '.txt'
@out_file = File.new(file_name, "w+")
# a rolling buffer for fragmented messages
@expecting = 33
@msg = ""
end
def on_message buffer
length = buffer.length
pos = 0
while length >= @expecting
@msg << (buffer[pos, @expecting])
@out_file.puts(@msg.unpack('H*')[0])
length -= @expecting
pos += @expecting # the spelling mistake was here
@expecting = 86
puts "wrote:", @msg
@msg.clear
end
if(length > 0)
@msg << (buffer[pos, length])
@expecting = 86 - length
end
puts("Waiting for more data:", @msg) unless @msg.empty?
end
def on_close
@out_file.close
end
end
# create the service instance
Iodine.listen 12050, TT8750plus
# start the service
Iodine.start
同样,缺少 Skypatrol TT8750+ 的仿真,我无法测试代码。但是应该可以按照错误消息慢慢追踪这些类型的问题。
P.S.
为了防止出现异常,请考虑使用 Ruby 的:
begin
# code
rescue => e
# oops something happened. i.e.
puts e.message, e.backtrace
end
即 on_message
方法可能如下所示:
def on_message buffer
begin
length = buffer.length
pos = 0
while length >= @expecting
@msg << (buffer[pos, @expecting])
@out_file.puts(@msg.unpack('H*')[0])
length -= @expecting
pos += @expecting # the spelling mistake was here
@expecting = 86
@msg.clear
end
if(length > 0)
@msg << (buffer[pos, length])
@expecting = 86 - length
end
puts @msg unless @msg.empty? # print leftovers for testing...?
rescue => e
# oops something happened. React
puts e.message, e.backtrace
end
end
此外,仅供参考,iodine 允许您控制正在使用的进程(工作程序)和线程的数量。有关详细信息,请参阅 the documentation。