Ejemplo n.º 1
0
        private void ReciveCallBack(IAsyncResult ar)
        {
            ZTcpClient client = (ZTcpClient)ar.AsyncState;

            if (client.NetWork.Connected)
            {
                try
                {
                    NetworkStream ns  = client.NetWork.GetStream();
                    int           len = ns.EndRead(ar);
                    if (len > 0)
                    {
                        ReciveData(client, len);
                        ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(ReciveCallBack), client);
                    }
                    else
                    {
                        DisConnect();
                    }
                }
                catch (Exception ex)
                {
                    DisConnect();
                }
            }
        }
Ejemplo n.º 2
0
        private void ReciveData(ZTcpClient client, int len)
        {
            byte[] ds = client.buffer;
            //如果接收数据大于最大数据接收长度,丢弃
            if (client.BufferStream.Length + len > MaxReciveLength)
            {
                client.BufferStream.SetLength(0);
            }
            client.RefreshLastReciveTime();
            if (isDataEscape)
            {
                for (int i = 0; i < len; i++)
                {
                    if (ds[i] == EscapeCharDefine.StartChar)
                    {
                        client.BufferStream.SetLength(0);
                        client.BufferStream.WriteByte(ds[i]);
                    }
                    else if (ds[i] == EscapeCharDefine.EndChar)
                    {
                        client.BufferStream.WriteByte(ds[i]);
                        byte[] d = EscapeCharDefine.UnEscape(client.BufferStream.ToArray());
                        client.BufferStream.SetLength(0);
                        client.BufferStream.Write(d, 0, d.Length);
                        if (OnReciveData != null)
                        {
                            OnReciveData(this, new ZTcpClientEventArgs(client, client.NetWork));
                        }

                        //发出指令收到事件
                        if (IsCmdInvoke && (OnCmdInvoke != null))
                        {
                            string str = Encoding.GetString(d);
                            NameValueCollection cmd = StringEscape.StrToCollection(str);
                            OnCmdInvoke(this, new ZTcpClientCmdEventArgs(this, client.NetWork, cmd));
                        }

                        client.BufferStream.SetLength(0);
                    }
                    else
                    {
                        client.BufferStream.WriteByte(ds[i]);
                    }
                }
            }
            else
            {
                if (OnReciveData != null)
                {
                    client.BufferStream.SetLength(0);
                    client.BufferStream.Write(ds, 0, len);
                    OnReciveData(this, new ZTcpClientEventArgs(client, client.NetWork));
                }
            }
        }
Ejemplo n.º 3
0
 public ZTcpClientCmdEventArgs(ZTcpClient zclient, TcpClient client, NameValueCollection data)
     : base(zclient, client)
 {
     _Data = data;
 }
Ejemplo n.º 4
0
 public ZTcpClientEventArgs(ZTcpClient zclient, TcpClient client)
 {
     _ZTcpClient = zclient;
     _Client     = client;
 }