Ros_CSharp.Header.Parse C# (CSharp) Method

Parse() public method

public Parse ( byte buffer, int size, string &error_msg ) : bool
buffer byte
size int
error_msg string
return bool
        public bool Parse(byte[] buffer, int size, ref string error_msg)
        {
            int i = 0;
            while (i < size)
            {
                int thispiece = BitConverter.ToInt32(buffer, i);
                i += 4;
                byte[] line = new byte[thispiece];
                Array.Copy(buffer, i, line, 0, thispiece);
                string thisheader = Encoding.ASCII.GetString(line);
                string[] chunks = thisheader.Split('=');
                if (chunks.Length != 2)
                {
                    i += thispiece;
                    continue;
                }
                Values[chunks[0].Trim()] = chunks[1].Trim();
                i += thispiece;
            }
            bool res = (i == size);
            if (!res)
                EDB.WriteLine("OH NOES CONNECTION HEADER FAILED TO PARSE!");
            return res;
        }

Usage Example

Example #1
0
        private bool onHeaderRead(Connection conn, byte[] data, int size, bool success)
        {
            if (conn != this)
            {
                throw new Exception("THAT EVENT IS NOT FOR MEEE!");
            }
            if (!success)
            {
                return(false);
            }
            string error_msg = "";

            if (!header.Parse(data, (int)size, ref error_msg))
            {
                drop(DropReason.HeaderError);
                return(false);
            }
            else
            {
                string error_val = "";
                if (header.Values.Contains("error"))
                {
                    error_val = (string)header.Values["error"];
                    EDB.WriteLine("Received error message in header for connection to [{0}]: [{1}]",
                                  "TCPROS connection to [" + transport.cached_remote_host + "]", error_val);
                    drop(DropReason.HeaderError);
                    return(false);
                }
                else
                {
                    if (header_func == null)
                    {
                        throw new Exception("AMG YOUR HEADERFUNC SUCKS");
                    }
                    transport.parseHeader(header);
                    header_func(conn, header);
                }
            }
            return(true);
        }