void IMaslConnectionObserver.OnMaslUserDataArrival(byte[] maslUserData) { try { var frame = SaiFrame.Parse(maslUserData); if (this.CheckFrame(frame)) { _seqNoManager.UpdateAckSeq(frame.SequenceNo); _currentState.HandleFrame(frame); if (this.SaiFrameReceived != null) { this.SaiFrameReceived(null, new SaiFrameIncomingEventArgs(frame)); } } else if (_seqNoManager.IsBeyondRange(frame.SequenceNo)) { throw new Exception(string.Format("检测到丢包,当前确认序号={0},收到的发送序号={1}。", _seqNoManager.AckSeq, frame.SequenceNo)); } else { // 序号值为旧值,丢弃即可。 } } catch (System.Exception ex) { LogUtility.Error(string.Format("Sai: {0} \r\n{1}", _rsspEndPoint.ID, ex)); this.Disconnect(MaslErrorCode.NotDefined, 0); } }
public static SaiFrame Parse(byte[] bytes) { SaiFrame theFrame = null; var theFrameType = (SaiFrameType)bytes[0]; if (theFrameType == SaiFrameType.TTS_OffsetStart) { theFrame = new SaiTtsFrameOffsetStart(); } else if (theFrameType == SaiFrameType.TTS_OffsetAnswer1) { theFrame = new SaiTtsFrameOffsetAnswer1(); } else if (theFrameType == SaiFrameType.TTS_OffsetAnswer2) { theFrame = new SaiTtsFrameOffsetAnswer2(); } else if (theFrameType == SaiFrameType.TTS_OffsetEstimate) { theFrame = new SaiTtsFrameEstimate(); } else if (theFrameType == SaiFrameType.TTS_OffsetEnd) { theFrame = new SaiTtsFrameOffsetEnd(); } else if (theFrameType == SaiFrameType.TTS_AppData) { theFrame = new SaiTtsFrameAppData(); } else if (theFrameType == SaiFrameType.EC_Start) { theFrame = new SaiEcFrameStart(); } else if (theFrameType == SaiFrameType.EC_AppData) { theFrame = new SaiEcFrameApplication(); } else if (theFrameType == SaiFrameType.EC_AppDataAskForAck) { theFrame = new SaiEcFrameAskForAck(); } else if (theFrameType == SaiFrameType.EC_AppDataAcknowlegment) { theFrame = new SaiEcFrameAcknowlegment(); } else { throw new InvalidOperationException(string.Format("无法解析指定的Sai帧,不可识别的类型{0}。", theFrameType)); } theFrame.ParseBytes(bytes); return(theFrame); }
/// <summary> /// 计算指定Sai协议帧的时延。(单位:10毫秒) /// </summary> /// <param name="saiFrame"></param> /// <returns></returns> public long CalcTimeDelay(SaiFrame saiFrame) { if (SaiFrame.IsEcFrame(saiFrame.FrameType)) { return(this.CalcEcTimeDelay(saiFrame as SaiEcFrame)); } else if (SaiFrame.IsTtsFrame(saiFrame.FrameType)) { return(this.CalcTtsTimeDelay(saiFrame as SaiTtsFrame)); } else { throw new InvalidOperationException("指定的SaiFrame不可识别,无法计算时延。"); } }
public void HandleFrame(SaiFrame saiFrame) { if (SaiFrame.IsEcFrame(saiFrame.FrameType)) { this.HandleEcFrame(saiFrame as SaiEcFrame); } else if (SaiFrame.IsTtsFrame(saiFrame.FrameType)) { this.HandleTtsFrame(saiFrame as SaiTtsFrame); } else { throw new NotImplementedException("指定的SaiFrame不可识别,SaiState无法处理。"); } }
private bool CheckFrame(SaiFrame saiFrame) { if (this.Connected) { return(_seqNoManager.IsExpected(saiFrame.SequenceNo)); } else { if (saiFrame.FrameType == SaiFrameType.EC_Start || saiFrame.FrameType == SaiFrameType.TTS_OffsetStart || saiFrame.FrameType == SaiFrameType.TTS_OffsetAnswer1) { return(true); } else { return(_seqNoManager.IsExpected(saiFrame.SequenceNo)); } } }
void ISaiFrameTransport.SendSaiFrame(SaiFrame saiFrame) { var bytes = saiFrame.GetBytes(); _maslConnection.SendUserData(bytes); }