HexDump() public static method

public static HexDump ( byte buf ) : string
buf byte
return string
Esempio n. 1
0
        /// <summary>
        /// Waits until any of the strings in the <paramref name="untils"/> array matched.
        /// </summary>
        /// <param name="untils">string of arrays to be wait for</param>
        /// <returns></returns>
        Response ReadUntil(String[] untils)
        {
            NetworkStream ns          = m_tcp.GetStream();
            MemoryStream  ms          = new MemoryStream();
            bool          found       = false;
            int           matchLength = 0;
            string        match       = "";

            while (!found)
            {
                int ch = -1;
                try
                {
                    ch = ns.ReadByte();
                }
                catch (Exception e)
                {
                    log.Error("ReadUntil ReadByte() error: " + e.Message);
                }
                if (ch == -1)
                {
                    log.Error("Connection closed");
                    m_tcp.Close();
                    log.Info("Reconnecting");
                    // Save last saved command, because Connect() will overwrite it
                    byte[] currentLastCmd = m_lastCmd;
                    Connect();
                    log.Info("Rewriting last command");
                    // Restore attempted command and resend it after a succesful connection.
                    m_lastCmd = currentLastCmd;
                    Rewrite();
                    return(ReadUntil(untils));
                }
                ms.WriteByte((byte)ch);
                foreach (string until in untils)
                {
                    matchLength = until.Length;
                    match       = until;
                    if (ms.Position > (matchLength - 1))
                    {
                        byte[] buf = new byte[matchLength];
                        ms.Seek(-matchLength, SeekOrigin.Current);
                        ms.Read(buf, 0, buf.Length);
                        string end = ASCIIEncoding.ASCII.GetString(buf);
                        if (end == until)
                        {
                            found = true;
                            break;
                        }
                    }
                }
            }

            log.Debug("Recv: \r\n" + Utils.HexDump(ms.GetBuffer(), ms.Length));
            ms.SetLength(ms.Length - matchLength);
            ms.Position = 0;
            return(new Response(ms, match));
        }
Esempio n. 2
0
        void Send(byte[] data)
        {
            if (!m_tcp.Connected)
            {
                log.Warning("Write attempt while disconnected, trying to reconnect");
                Connect();
            }
            log.Debug("Sending \r\n" + Utils.HexDump(data));
            m_lastCmd = data;
            NetworkStream ns = m_tcp.GetStream();

            try
            {
                ns.Write(data, 0, data.Length);
            }
            catch (Exception e)
            {
                log.Error("An error occured while writing data: " + e.Message);
            }
        }