Example #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);
            }
        }
Example #2
0
        public void AddQueue(byte[] buffer)
        {
            IridiumBuffer b = new IridiumBuffer();

            b.length = buffer.Length;
            b.buffer = new byte[b.length];
            Buffer.BlockCopy(buffer, 0, b.buffer, 0, b.length);
            b.socket = null;
            _pool.Enqueue(b);
        }
Example #3
0
        /// <summary>
        /// 手动分析指定的铱星数据包
        /// </summary>
        private void IridiumManualAnalysePackage()
        {
            //uint shor = IririumMTMSN.CalculateMTMSN(DateTime.Now, mtmsn++);
            //byte[] b = BitConverter.GetBytes(shor);
            //ShowHistory("short: " + shor + ", byte: " + CustomConvert.GetHex(b) + ", to: " + CustomConvert.GetHex(CustomConvert.reserve(b)), false);
            //if (shor == 0)
            //{ shor = 1; }
            var temp   = CustomConvert.GetBytes(tstbData.Text.Trim());
            var len    = temp.Length;
            var buffer = new IridiumBuffer();

            buffer.socket = null;
            buffer.length = len;
            buffer.buffer = new byte[len];
            Buffer.BlockCopy(temp, 0, buffer.buffer, 0, len);
            _iridium.AddQueue(buffer);
            temp = null;
        }
Example #4
0
 /// <summary>
 /// 将数据加入待处理队列
 /// </summary>
 /// <param name="buffer"></param>
 public void AddQueue(IridiumBuffer buffer)
 {
     _pool.Enqueue(buffer);
 }
Example #5
0
        private void Read_Callback(IAsyncResult ar)
        {
            try
            {
                StateObject so = (StateObject)ar.AsyncState;
                if (null == so)
                {
                    return;
                }
                Socket s = so.socket;
                // 增加判断socket是否还正常连接的判断 2015/09/16 12:40
                if (null == s)
                {
                    return;
                }
                if (!s.Connected)
                {
                    return;
                }

                int read = s.EndReceive(ar);
                if (read > 0)
                {
                    if (_showPackage)
                    {
                        HandleOnMessage(format("{0}Received data(length:{1}) from {2}:{3}", Now, read, so.point.Address.ToString(), so.point.Port));
                    }
                    if (null == so.Received)
                    {
                        so.Received = new byte[read];
                        so.length   = read;
                    }
                    else
                    {
                        so.length  += read;
                        so.Received = Utilities.CustomConvert.expand(so.Received, so.length);
                    }
                    Buffer.BlockCopy(so.buffer, 0, so.Received, so.length - read, read);
                    s.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE, SocketFlags.None, new AsyncCallback(Read_Callback), so);
                    if (so.length >= so.Received[2])
                    {
                        // 加入队列并等待处理
                        var b = new IridiumBuffer();
                        b.length = so.length;
                        b.buffer = new byte[so.length];
                        Buffer.BlockCopy(so.Received, 0, b.buffer, 0, so.length);
                        b.socket    = s;
                        so.length   = 0;
                        so.Received = null;
                        _pool.Enqueue(b);
                    }
                }
                else
                {
                    if (_showPackage)
                    {
                        HandleOnMessage(format("{0}Iridium server {1}:{2} disconnected.", Now, so.point.Address.ToString(), so.point.Port));
                    }
                    //so.socket.Shutdown(SocketShutdown.Both);
                    //so.socket.Close();
                }
            }
            catch (Exception e)
            {
                if (e.Message.IndexOf("forcibly") < 0)
                {
                    HandleOnMessage(format("{0} Read_Callback error: {1}, Trace: {2}", Now, e.Message, e.StackTrace));
                }
            }
        }
Example #6
0
        private void Read_Callback(IAsyncResult ar)
        {
            try
            {
                ClientState cs = (ClientState)ar.AsyncState;
                if (null == cs)
                {
                    return;
                }
                if (null == cs.client)
                {
                    return;
                }
                if (!cs.client.Connected)
                {
                    cs.Dispose();
                    try
                    {
                        cs.client.Close();
                        //cs.client.GetStream().Close();
                    }
                    catch { }
                    cs.client = null;
                    return;
                }
                if (cs.IsConnected == false)
                {
                    return;
                }

                var len = cs.client.GetStream().EndRead(ar);
                if (len > 0)
                {
                    if (null == cs.received)
                    {
                        cs.received = new byte[len];
                        cs.length   = len;
                    }
                    else
                    {
                        cs.length  += len;
                        cs.received = CustomConvert.expand(cs.received, cs.length);
                    }
                    Buffer.BlockCopy(cs.buffer, 0, cs.received, cs.length - len, len);
                    cs.client.GetStream().BeginRead(cs.buffer, 0, ClientState.SIZE, new AsyncCallback(Read_Callback), cs);
                    if (cs.length >= cs.received[2])
                    {
                        var buffer = new IridiumBuffer();
                        buffer.socket = null;
                        buffer.length = cs.length;
                        buffer.buffer = new byte[cs.length];
                        Buffer.BlockCopy(cs.received, 0, buffer.buffer, 0, cs.length);
                        cs.length   = 0;
                        cs.received = null;
                        _iridium.AddQueue(buffer);
                        try
                        {
                            cs.Dispose();
                            cs.client.Close();
                            //if (_iridium.ShowPackageInformation)
                            ShowHistory("MT operation complete, client disconnected.", true);
                        }
                        catch (Exception e)
                        {
                            ShowHistory("Handle Iridium package error: " + e.Message + Environment.NewLine + e.StackTrace, true);
                        }
                        finally
                        {
                            cs.client = null;
                            cs        = null;
                        }
                    }
                }
                else
                {
                    try
                    {
                        cs.Dispose();
                        cs.client.GetStream().Close();
                        cs.client.Close();
                    }
                    catch (Exception ee)
                    {
                        ShowHistory("Handle Iridium package error(len<=0): " + ee.Message + Environment.NewLine + ee.StackTrace, true);
                    }
                    finally
                    {
                        cs = null;
                    }
                }
            }
            catch (Exception error)
            { ShowHistory("Read_Callback error: " + error.Message + Environment.NewLine + error.StackTrace, true); }
        }