private TranMsg CombineMsg(TranMsgType type) { var queue = this._msgQueue[type]; TranMsg ms = TranMsg.Combine(queue.ToArray()); queue.Clear(); return(ms); }
public TranMsg[] Splite(byte[] buff, int len) { TranMsg msg = new TranMsg(); msg.Data = new byte[len]; Array.Copy(buff, msg.Data, len); msg.LoadSize = (ushort)len; msg.PackageIndex = 0; msg.PackageCount = 1; msg.Type = TranMsgType.Dac; return(new TranMsg[] { msg }); }
public void OnDataReceived(IDtuConnection c, DtuMsg msg) { if (msg == null) { Log.ErrorFormat("Received a null msg from {0}.", c); return; } if (!msg.IsOK()) { Log.DebugFormat("Invalid Msg, dropped."); return; } TranMsg tm = new TranMsg(msg.Databuffer); // Send ack. TranMsg ack = new HeartBeatTranMsg(Convert.ToInt32(msg.DtuId), tm.LoadSize); // ACK ack.ID = tm.ID; // SAME. c.Asend(ack.Marshall()); if (TranMsgType.HeartBeat == tm.Type) { // HeartBeat. Log.DebugFormat("heartBeat, send ack and continue"); return; } Log.DebugFormat("OnDataReceived: id={4},result={0}, len={1}, pkg={2}/{3}", msg.ErrorCode, msg.Databuffer != null ? msg.Databuffer.Length : 0, tm.PackageIndex, tm.PackageCount, tm.ID); if (!tm.IsLastPackage()) { // 入列. EnqueueMsg(tm); return; } // 最后一包已到达, 或仅一包. TranMsg outMsg = null; if (tm.PackageCount > 1) { Log.DebugFormat("Last package, combine them."); outMsg = CombineMsg(tm.Type); //组包 } else { outMsg = tm; //单包. } // 委托调用. if (OnTranMsgReceived != null) { OnTranMsgReceived(tm.Type, outMsg); } }
public bool SSend(TranMsg req, int timeout, out TranAckMsg resp) { DtuMsg msg = _dtuConnection.Ssend(req.Marshall(), timeout); resp = new TranAckMsg(); if (msg.IsOK()) { resp.Unmarshall(msg.Databuffer); return(true); } else { resp.ErrorMsg = msg.ErrorMsg; resp.ErrorCode = msg.ErrorCode; return(false); } }
private void EnqueueMsg(TranMsg tm) { TranMsgType t = tm.Type; Queue <TranMsg> queue = null; if (!this._msgQueue.ContainsKey(t)) { this._msgQueue[t] = queue = new Queue <TranMsg>(); } else { queue = this._msgQueue[t]; if (tm.PackageIndex == 0 && queue.Count > 0) { Log.ErrorFormat("Invalid msg idx: {0}, purge old packages.", tm.PackageIndex); queue.Clear(); } } queue.Enqueue(tm); }
public TranMsg[] Splite(byte[] buff, int len) { int packCnt = (int)Math.Ceiling(len / (MAX_PACKAGE_SIZE * 1.0)); IList <TranMsg> msgs = new List <TranMsg>(); int restSize = len; for (int i = 0; i < packCnt; i++) { int pLen = (i == packCnt - 1) ? restSize : MAX_PACKAGE_SIZE; TranMsg msg = new TranMsg(); msg.Data = new byte[pLen]; Array.Copy(buff, i * 1014, msg.Data, 0, pLen); msg.LoadSize = (ushort)pLen; msg.PackageIndex = (byte)i; msg.PackageCount = (byte)packCnt; msg.Type = TranMsgType.Dac; msgs.Add(msg); restSize -= pLen; } return(msgs.ToArray()); }
public static TranMsg Combine(TranMsg[] msgs) { TranMsg msg = new TranMsg(); int len = 0; foreach (TranMsg tm in msgs) { len += tm.LoadSize; } msg.Data = new byte[len]; msg.PackageIndex = 0; msg.PackageCount = 1; msg.LoadSize = (ushort)len; int offset = 0; foreach (TranMsg tm in msgs) { Array.Copy(tm.Data, 0, msg.Data, offset, tm.LoadSize); offset += tm.LoadSize; } return(msg); }
private void _run() { while (!_source.IsCancellationRequested) { if (!this._delegator.IsConnected()) { Log.InfoFormat("Reconnecting to delegate"); this._delegator.Connect(); Thread.Sleep(200); if (!this._delegator.IsConnected()) { Log.InfoFormat("Connecting error, retry after 1 second."); Thread.Sleep(1000); continue; } else { Log.InfoFormat("Connected."); } } foreach (ITranDataProvider _provider in _providers) { if (_source.IsCancellationRequested) { break; } if (_provider.HasMoreData()) //未发送完毕 { if (_source.IsCancellationRequested) { break; } int len = 0; TranMsg[] msgs = _provider.NextPackages(out len); int sendingIdx = 0; bool allMsgSent = false; long started = System.DateTime.Now.Ticks; while (sendingIdx < msgs.Length) // 循环发送 { if (_source.IsCancellationRequested) { break; } TranMsg m = msgs[sendingIdx]; Log.DebugFormat("Sending package: {0}: {1}/{2}, len={3}", m.ID, sendingIdx, msgs.Length, m.LoadSize); try { TranAckMsg ack; if (_delegator.SSend(m, DTU_SEND_TIMEOUT, out ack)) { if (!IsValidAck(m, ack)) { { Log.ErrorFormat("Sent failed, ack error, {0}!={1}", m.LoadSize, ack.Received); continue; // resent it. } } sendingIdx++; //发送成功. if (OnMessageSent != null) { OnMessageSent.Invoke(m, ack); //异步 } allMsgSent = sendingIdx == msgs.Length; Thread.Sleep(100); } else { Log.ErrorFormat("Sending error: {0}, retry after 2 second: ", ack.ErrorMsg); Thread.Sleep(1000); } } catch (Exception e) { Log.ErrorFormat("Sending exception: {0}: ", e.Message); Thread.Sleep(1000); } } if (allMsgSent) { Log.DebugFormat("Package Group sent in {0} seconds.", (System.DateTime.Now.Ticks - started) / 10000); _provider.OnPackageSent(); } Thread.Sleep(10); } else { Log.InfoFormat("No more data. wait {0} seconds.", NO_DATA_WAIT_SECONDS); Thread.Sleep(NO_DATA_WAIT_SECONDS * 1000); } } } }
private bool IsValidAck(TranMsg req, TranAckMsg resp) { return(resp.Received == req.LoadSize || resp.ID != req.ID); }