Beispiel #1
0
        private void Connected_Callback(IAsyncResult ar)
        {
            try
            {
                TcpClient client = (TcpClient)ar.AsyncState;
                if (client.Connected)
                {
                    client.EndConnect(ar);
                    //if (_iridium.ShowPackageInformation)
                    {
                        ShowHistory("Iridium server has connected.", true);
                    }
                    ClientState cs = new ClientState();
                    cs.IsConnected = true;
                    cs.client      = client;
                    cs.length      = 0;
                    cs.received    = null;
                    client.GetStream().BeginRead(cs.buffer, 0, ClientState.SIZE, new AsyncCallback(Read_Callback), cs);
                    // 组包并发送
                    // 数据内容
                    MTPayload mtp = new MTPayload();
                    mtp.Payload = SendContent;
                    mtp.Package();

                    // Header
                    MTHeader mth = new MTHeader();
                    mth.UniqueID         = SendMTMSN;
                    mth.IMEI             = SendIMEI;
                    mth.DispositionFlags = (ushort)(FlushMTQueue ? 1 : 32);
                    mth.Package();
                    // 整包
                    Iridium iridium = new Iridium();
                    iridium.Content = new byte[mth.Content.Length + (FlushMTQueue ? 0 : mtp.Content.Length)];
                    Buffer.BlockCopy(mth.Content, 0, iridium.Content, 0, mth.Content.Length);
                    if (!FlushMTQueue)
                    {
                        // 如果不是删除队列标记则添加payload数据
                        Buffer.BlockCopy(mtp.Content, 0, iridium.Content, mth.Content.Length, mtp.Content.Length);
                    }
                    iridium.Package();

                    client.GetStream().Write(iridium.PackageContent, 0, iridium.PackageContent.Length);
                    var buffer = new IridiumBuffer();
                    buffer.length = iridium.PackageContent.Length;
                    buffer.buffer = new byte[buffer.length];
                    Buffer.BlockCopy(iridium.PackageContent, 0, buffer.buffer, 0, buffer.length);
                    _iridium.AddQueue(buffer);
                }
                else
                {
                    ShowHistory("Can not establish connect to Iridium server.", true);
                    client.Close();
                    client = null;
                }
            }
            catch (Exception e)
            {
                ShowHistory("Connect_Callback error: " + e.Message + Environment.NewLine + e.StackTrace, true);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 处理接收到的铱星数据包
        /// </summary>
        private void HandleIridiumPackage()
        {
            int           sleep = 100;
            int           index = 0;
            ushort        momsn = 0;
            IridiumBuffer obj;
            bool          NeedConfirmation = false;

            while (!IsStop)
            {
                Thread.Sleep(sleep);

                obj = null;
                if (!_pool.TryDequeue(out obj))
                {
                    obj = null;
                }

                if (null != obj)
                {
                    bool showPayload = false;
                    try
                    {
                        NeedConfirmation = false;
                        momsn            = 0;

                        _history.Add(Now + "Iridium package(length: " + obj.length + "): " + Utilities.CustomConvert.GetHex(obj.buffer));

                        // 分析整包铱星数据
                        Iridium iridum = new Iridium();
                        iridum.PackageContent = obj.buffer;
                        iridum.Unpackage();
                        try
                        {
                            // 从铱星数据体中分别解析各个IE字段
                            index = 0;
                            IridiumDataEvent data = new IridiumDataEvent();
                            int length = 0, total = iridum.OverallMessageLength;
                            while (index < iridum.OverallMessageLength)
                            {
                                IE ie = new IE();
                                ie.Content = new byte[total - index];
                                Buffer.BlockCopy(iridum.Content, index, ie.Content, 0, total - index);
                                ie.Unpackage();
                                length = ie.Length + Iridium.HEADER_SIZE;
                                switch (ie.IEI)
                                {
                                case IE.MO_HEADER:
                                    MOHeader moh = new MOHeader();
                                    moh.Content = new byte[length];
                                    Buffer.BlockCopy(ie.Content, 0, moh.Content, 0, length);
                                    moh.Unpackage();
                                    _history.Add(Now + moh.ToString());
                                    data.Data = new IridiumData();
                                    // 默认为终端接收数据的状态
                                    data.Data.Type   = IridiumDataType.MTModelReceiveStatus;
                                    data.Data.IMEI   = moh.IMEI;
                                    data.Data.MTMSN  = moh.MTMSN;
                                    momsn            = moh.MTMSN;
                                    data.Data.Time   = moh.Time;
                                    data.Data.Status = moh.SessionStatus;
                                    moh = null;
                                    NeedConfirmation = true;
                                    break;

                                case IE.MO_LOCATION:
                                    MOLocation mol = new MOLocation();
                                    mol.Content = new byte[length];
                                    Buffer.BlockCopy(ie.Content, 0, mol.Content, 0, length);
                                    mol.Unpackage();
                                    _history.Add(Now + mol.ToString());
                                    mol = null;
                                    NeedConfirmation = true;
                                    break;

                                case IE.MO_PAYLOAD:
                                    MOPayload mop = new MOPayload();
                                    mop.Content = new byte[length];
                                    Buffer.BlockCopy(ie.Content, 0, mop.Content, 0, length);
                                    mop.Unpackage();
                                    _history.Add(Now + mop.ToString());
                                    // 终端发送了数据上来
                                    data.Data.Type    = IridiumDataType.MOPayload;
                                    data.Data.Payload = mop.Payload;
                                    mop              = null;
                                    showPayload      = true;
                                    NeedConfirmation = true;
                                    break;

                                case IE.MO_CONFIRMATION:
                                    MOConfirmation moc = new MOConfirmation();
                                    moc.Content = new byte[length];
                                    Buffer.BlockCopy(ie.Content, 0, moc.Content, 0, length);
                                    moc.Unpackage();
                                    _history.Add(Now + moc.ToString());
                                    moc = null;
                                    break;

                                case IE.MT_CONFIRMATION:
                                    MTConfirmation mtc = new MTConfirmation();
                                    mtc.Content = new byte[length];
                                    Buffer.BlockCopy(ie.Content, 0, mtc.Content, 0, length);
                                    mtc.Unpackage();
                                    // MTConfirmation消息没有Header结构
                                    data.Data        = new IridiumData();
                                    data.Data.Type   = IridiumDataType.MTServerSendStatus;
                                    data.Data.MTMSN  = (ushort)mtc.UniqueID;
                                    momsn            = (ushort)mtc.UniqueID;
                                    data.Data.IMEI   = mtc.IMEI;
                                    data.Data.Status = mtc.Status;
                                    _history.Add(Now + mtc.ToString());
                                    mtc = null;
                                    break;

                                case IE.MT_HEADER:
                                    MTHeader mth = new MTHeader();
                                    mth.Content = new byte[length];
                                    Buffer.BlockCopy(ie.Content, 0, mth.Content, 0, length);
                                    mth.Unpackage();
                                    momsn = (ushort)mth.UniqueID;
                                    _history.Add(Now + mth.ToString());
                                    mth = null;
                                    break;

                                case IE.MT_PAYLOAD:
                                    MTPayload mtp = new MTPayload();
                                    mtp.Content = new byte[length];
                                    Buffer.BlockCopy(ie.Content, 0, mtp.Content, 0, length);
                                    mtp.Unpackage();
                                    _history.Add(Now + mtp.ToString());
                                    mtp = null;
                                    break;

                                case IE.MT_PRIORITY:
                                    break;
                                }
                                ie     = null;
                                index += length;
                            }
                            // 分析完毕之后看是否为终端发送的数据
                            if (null != data)
                            {
                                if (null != data.Data)
                                {
                                    OnIridiumReceive?.Invoke(this, data);
                                }
                                else
                                {
                                    data = null;
                                }
                            }
                        }
                        finally
                        {
                            // 反馈MO Confirmation包
                            if (NeedConfirmation)
                            {
                                MOConfirmation confirm = new MOConfirmation();
                                confirm.Package();
                                iridum.OverallMessageLength = 0;
                                iridum.Content = confirm.Content;
                                iridum.Package();
                                try
                                {
                                    obj.socket.Send(iridum.PackageContent);
                                    _history.Add(Now + "Send MO Confirmation: " + Utilities.CustomConvert.GetHex(iridum.PackageContent));
                                }
                                catch (Exception send)
                                {
                                    HandleOnMessage(format("{0} Send iridiu package error: {1}, Trace: {2}", Now, send.Message, send.StackTrace));
                                }
                                AddQueue(iridum.PackageContent);
                                //HandleOnMessage(Now + "MO confirmation: " + Wbs.Utilities.CustomConvert.GetHex(iridum.PackageContent));
                                iridum = null;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        HandleOnMessage(format("{0} HandleIridiumPackage error: {1}, Trace: {2}", Now, e.Message, e.StackTrace));
                    }
                    finally
                    {
                        obj.Dispose();
                        obj = null;
                    }
                    // 检测是否可以显示当前分析的数据包
                    string text = "";
                    lock (lock_momsn)
                    {
                        if (!_showPackage)
                        {
                            var m = MOMSNs.FirstOrDefault(f => f.mtmsn == momsn);
                            if (null != m)
                            {
                                showPayload = true;
                                m.HandleTimes++;
                            }
                        }
                        else
                        {
                            // 如果显示数据包数据则直接显示
                            showPayload = true;
                        }
                        if (showPayload)
                        {
                            foreach (var s in _history)
                            {
                                text += s + Environment.NewLine;
                            }
                        }
                        // 移除超时的MTMSN监控记录
                        MOMSNs.RemoveAll(r => r.HandleTimes >= 3 || r.time < DateTime.Now.AddDays(-7));
                    }
                    if (!string.IsNullOrEmpty(text))
                    {
                        HandleOnMessage(text);
                    }

                    // 清空分析的历史记录
                    _history.Clear();
                }
            }
        }