private void CommThread(object o) {
byte[] buf=new byte[256];
bool escChar=false;
int cnt=-1;
int len=-1;
MsMessage msg;
DateTime busyTime=DateTime.Now;
try {
while(_port!=null && _port.IsOpen) {
if(GetPacket(_port, ref len, buf, ref cnt, ref escChar)) {
if(len==5 && buf[1]==(byte)MsMessageType.SUBSCRIBE) {
_advTick=DateTime.Now.AddMilliseconds(100); // Send Advertise
}
MsDevice.ProcessInPacket(this, _gateAddr, buf, 0, len);
cnt=-1;
msg=null;
continue;
}
msg=null;
if(busyTime>DateTime.Now) {
Thread.Sleep(0);
continue;
}
lock(_sendQueue) {
if(_sendQueue.Count>0) {
msg=_sendQueue.Dequeue();
}
}
if(msg!=null) {
SendRaw(this, msg, _sndBuf);
busyTime=DateTime.Now.AddMilliseconds(msg.IsRequest?20:5);
continue;
}
if(_gwTopic!=null && _gwTopic.value!=null && (_gwTopic.value.state==State.Disconnected || _gwTopic.value.state==State.Lost)) {
_gwTopic=null;
Thread.Sleep(500);
this.Dispose();
Thread.Sleep(1000);
_startScan.Set();
return;
}
if(_advTick<DateTime.Now) {
SendRaw(this, new MsAdvertise(gwIdx, 900), _sndBuf);
_advTick=DateTime.Now.AddMinutes(15);
}
Thread.Sleep(15);
}
}
catch(IOException ex) {
if(_verbose.value) {
Log.Error("MsGSerial({0}).CommThread() - {1}", gwIdx, ex.ToString());
}
}
catch(Exception ex) {
Log.Error("MsGSerial({0}).CommThread() - {1}", gwIdx, ex.ToString());
}
if(_verbose.value) {
Log.Debug("MsGSerial({0}).CommThread - exit", gwIdx);
}
this.Dispose();
}
private void Dispose() {