WriteArrayHeader() public method

public WriteArrayHeader ( int N ) : void
N int
return void
        public Dictionary<object, object> Execute(string method, params object[] args)
        {
            if (string.IsNullOrEmpty (_host))
                throw new Exception ("Host null or empty");

            if (method != "auth.login" && string.IsNullOrEmpty (_token))
                throw new Exception ("Not authenticated.");

            BoxingPacker boxingPacker = new BoxingPacker ();
            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => {return true;}; //dis be bad, no ssl check

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create (_host);
            request.ContentType = "binary/message-pack";
            request.Method = "POST";

            Stream requestStream = request.GetRequestStream ();
            MsgPackWriter msgpackWriter = new MsgPackWriter (requestStream);

            msgpackWriter.WriteArrayHeader (args.Length + 1 + (string.IsNullOrEmpty (_token) ? 0 : 1));

            msgpackWriter.Write (method);

            if (!string.IsNullOrEmpty (_token))
                msgpackWriter.Write (_token);

            foreach (object arg in args)
                Pack(msgpackWriter, arg);

            requestStream.Close ();

            Stream responseStream = request.GetResponse ().GetResponseStream ();

            //everything is a bunch of bytes, needs to be typed
            Dictionary<object, object > resp = boxingPacker.Unpack (responseStream) as Dictionary<object, object>;

            //This is me trying to type the response for the user....
            Dictionary<object, object > returnDictionary = TypifyDictionary(resp);

            return returnDictionary;
        }
 private void Pack(MsgPackWriter writer, object o)
 {
     if (o == null)
     {
         writer.WriteNil();
     }
     else
     {
         Type type = o.GetType();
         if (type.IsPrimitive)
         {
             if (type.Equals(typeof(int)))
             {
                 writer.Write((int)o);
             }
             else if (type.Equals(typeof(uint)))
             {
                 writer.Write((uint)o);
             }
             else if (type.Equals(typeof(float)))
             {
                 writer.Write((float)o);
             }
             else if (type.Equals(typeof(double)))
             {
                 writer.Write((double)o);
             }
             else if (type.Equals(typeof(long)))
             {
                 writer.Write((long)o);
             }
             else if (type.Equals(typeof(ulong)))
             {
                 writer.Write((ulong)o);
             }
             else if (type.Equals(typeof(bool)))
             {
                 writer.Write((bool)o);
             }
             else if (type.Equals(typeof(byte)))
             {
                 writer.Write((byte)o);
             }
             else if (type.Equals(typeof(sbyte)))
             {
                 writer.Write((sbyte)o);
             }
             else if (type.Equals(typeof(short)))
             {
                 writer.Write((short)o);
             }
             else
             {
                 if (!type.Equals(typeof(ushort)))
                 {
                     throw new NotSupportedException();
                 }
                 writer.Write((ushort)o);
             }
         }
         else
         {
             IDictionary dictionary = o as IDictionary;
             if (dictionary != null)
             {
                 writer.WriteMapHeader(dictionary.Count);
                 IDictionaryEnumerator enumerator = dictionary.GetEnumerator();
                 try
                 {
                     while (enumerator.MoveNext())
                     {
                         DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current;
                         Pack(writer, dictionaryEntry.Key);
                         Pack(writer, dictionaryEntry.Value);
                     }
                 }
                 finally
                 {
                     (enumerator as IDisposable)?.Dispose();
                 }
             }
             else if (type.IsArray)
             {
                 Array array       = (Array)o;
                 Type  elementType = type.GetElementType();
                 if (elementType.IsGenericType && elementType.GetGenericTypeDefinition().Equals(KeyValuePairDefinitionType))
                 {
                     PropertyInfo property  = elementType.GetProperty("Key");
                     PropertyInfo property2 = elementType.GetProperty("Value");
                     writer.WriteMapHeader(array.Length);
                     for (int i = 0; i < array.Length; i++)
                     {
                         object value = array.GetValue(i);
                         Pack(writer, property.GetValue(value, null));
                         Pack(writer, property2.GetValue(value, null));
                     }
                 }
                 else
                 {
                     writer.WriteArrayHeader(array.Length);
                     for (int j = 0; j < array.Length; j++)
                     {
                         Pack(writer, array.GetValue(j));
                     }
                 }
             }
         }
     }
 }
Esempio n. 3
0
        void Pack(MsgPackWriter writer, object o)
        {
            if (o == null)
            {
                writer.WriteNil();
                return;
            }

            Type t = o.GetType();

            if (t.IsPrimitive)
            {
                if (t.Equals(typeof(int)))
                {
                    writer.Write((int)o);
                }
                else if (t.Equals(typeof(uint)))
                {
                    writer.Write((uint)o);
                }
                else if (t.Equals(typeof(float)))
                {
                    writer.Write((float)o);
                }
                else if (t.Equals(typeof(double)))
                {
                    writer.Write((double)o);
                }
                else if (t.Equals(typeof(long)))
                {
                    writer.Write((long)o);
                }
                else if (t.Equals(typeof(ulong)))
                {
                    writer.Write((ulong)o);
                }
                else if (t.Equals(typeof(bool)))
                {
                    writer.Write((bool)o);
                }
                else if (t.Equals(typeof(byte)))
                {
                    writer.Write((byte)o);
                }
                else if (t.Equals(typeof(sbyte)))
                {
                    writer.Write((sbyte)o);
                }
                else if (t.Equals(typeof(short)))
                {
                    writer.Write((short)o);
                }
                else if (t.Equals(typeof(ushort)))
                {
                    writer.Write((ushort)o);
                }
                else if (t.Equals(typeof(char)))
                {
                    writer.Write((ushort)(char)o);
                }
                else
                {
                    throw new NotSupportedException();
                }
                return;
            }

            PackDelegate packer;

            if (PackerMapping.TryGetValue(t, out packer))
            {
                packer(this, writer, o);
                return;
            }

            if (t.IsArray)
            {
                Array ary = (Array)o;
                writer.WriteArrayHeader(ary.Length);
                for (int i = 0; i < ary.Length; i++)
                {
                    Pack(writer, ary.GetValue(i));
                }
                return;
            }

            ReflectionCacheEntry entry = ReflectionCache.Lookup(t);

            writer.WriteMapHeader(entry.FieldMap.Count);
            foreach (KeyValuePair <string, FieldInfo> pair in entry.FieldMap)
            {
                writer.Write(pair.Key, _buf);
                object v = pair.Value.GetValue(o);
                if (pair.Value.FieldType.IsInterface && v != null)
                {
                    writer.WriteArrayHeader(2);
                    writer.Write(v.GetType().FullName);
                }
                Pack(writer, v);
            }
        }
Esempio n. 4
0
		void Pack (MsgPackWriter writer, object o)
		{
			if (o == null) {
				writer.WriteNil ();
				return;
			}

			Type t = o.GetType ();
			if (t.IsPrimitive) {
				if (t.Equals (typeof (int))) writer.Write ((int)o);
				else if (t.Equals (typeof (uint))) writer.Write ((uint)o);
				else if (t.Equals (typeof (float))) writer.Write ((float)o);
				else if (t.Equals (typeof (double))) writer.Write ((double)o);
				else if (t.Equals (typeof (long))) writer.Write ((long)o);
				else if (t.Equals (typeof (ulong))) writer.Write ((ulong)o);
				else if (t.Equals (typeof (bool))) writer.Write ((bool)o);
				else if (t.Equals (typeof (byte))) writer.Write ((byte)o);
				else if (t.Equals (typeof (sbyte))) writer.Write ((sbyte)o);
				else if (t.Equals (typeof (short))) writer.Write ((short)o);
				else if (t.Equals (typeof (ushort))) writer.Write ((ushort)o);
				else throw new NotSupportedException ();  // char?
				return;
			}

			IDictionary dic = o as IDictionary;
			if (dic != null) {
				writer.WriteMapHeader (dic.Count);
				foreach (System.Collections.DictionaryEntry e in dic) {
					Pack (writer, e.Key);
					Pack (writer, e.Value);
				}
				return;
			}
			
			if (t.IsArray) {
				Array ary = (Array)o;
				Type et = t.GetElementType ();

				// KeyValuePair<K,V>[] (Map Type)
				if (et.IsGenericType && et.GetGenericTypeDefinition ().Equals (KeyValuePairDefinitionType)) {
					PropertyInfo propKey = et.GetProperty ("Key");
					PropertyInfo propValue = et.GetProperty ("Value");
					writer.WriteMapHeader (ary.Length);
					for (int i = 0; i < ary.Length; i ++) {
						object e = ary.GetValue (i);
						Pack (writer, propKey.GetValue (e, null));
						Pack (writer, propValue.GetValue (e, null));
					}
					return;
				}

				// Array
				writer.WriteArrayHeader (ary.Length);
				for (int i = 0; i < ary.Length; i ++)
					Pack (writer, ary.GetValue (i));
				return;
			}
		}
        void Pack(MsgPackWriter writer, object o)
        {
            if (o == null)
            {
                writer.WriteNil();
                return;
            }

            Type t = o.GetType();

            if (t.IsPrimitive)
            {
                if (t.Equals(typeof(int)))
                {
                    writer.Write((int)o);
                }
                else if (t.Equals(typeof(uint)))
                {
                    writer.Write((uint)o);
                }
                else if (t.Equals(typeof(float)))
                {
                    writer.Write((float)o);
                }
                else if (t.Equals(typeof(double)))
                {
                    writer.Write((double)o);
                }
                else if (t.Equals(typeof(long)))
                {
                    writer.Write((long)o);
                }
                else if (t.Equals(typeof(ulong)))
                {
                    writer.Write((ulong)o);
                }
                else if (t.Equals(typeof(bool)))
                {
                    writer.Write((bool)o);
                }
                else if (t.Equals(typeof(byte)))
                {
                    writer.Write((byte)o);
                }
                else if (t.Equals(typeof(sbyte)))
                {
                    writer.Write((sbyte)o);
                }
                else if (t.Equals(typeof(short)))
                {
                    writer.Write((short)o);
                }
                else if (t.Equals(typeof(ushort)))
                {
                    writer.Write((ushort)o);
                }
                else
                {
                    throw new NotSupportedException();                    // char?
                }
                return;
            }

            IDictionary dic = o as IDictionary;

            if (dic != null)
            {
                writer.WriteMapHeader(dic.Count);
                foreach (System.Collections.DictionaryEntry e in dic)
                {
                    Pack(writer, e.Key);
                    Pack(writer, e.Value);
                }
                return;
            }

            if (t.IsArray)
            {
                Array ary = (Array)o;
                Type  et  = t.GetElementType();

                // KeyValuePair<K,V>[] (Map Type)
                if (et.IsGenericType && et.GetGenericTypeDefinition().Equals(KeyValuePairDefinitionType))
                {
                    PropertyInfo propKey   = et.GetProperty("Key");
                    PropertyInfo propValue = et.GetProperty("Value");
                    writer.WriteMapHeader(ary.Length);
                    for (int i = 0; i < ary.Length; i++)
                    {
                        object e = ary.GetValue(i);
                        Pack(writer, propKey.GetValue(e, null));
                        Pack(writer, propValue.GetValue(e, null));
                    }
                    return;
                }

                // Array
                writer.WriteArrayHeader(ary.Length);
                for (int i = 0; i < ary.Length; i++)
                {
                    Pack(writer, ary.GetValue(i));
                }
                return;
            }
        }
Esempio n. 6
0
        private void Pack(MsgPackWriter writer, object o)
        {
            if (o == null)
            {
                writer.WriteNil();
                return;
            }

            if (o is string s)
            {
                writer.Write(Encoding.UTF8.GetBytes((string)o));
                return;
            }

            Type type = o.GetType();

            if (type.IsPrimitive)
            {
                if (type.Equals(typeof(int)))
                {
                    writer.Write((int)o);
                    return;
                }
                if (type.Equals(typeof(uint)))
                {
                    writer.Write((uint)o);
                    return;
                }
                if (type.Equals(typeof(float)))
                {
                    writer.Write((float)o);
                    return;
                }
                if (type.Equals(typeof(double)))
                {
                    writer.Write((double)o);
                    return;
                }
                if (type.Equals(typeof(long)))
                {
                    writer.Write((long)o);
                    return;
                }
                if (type.Equals(typeof(ulong)))
                {
                    writer.Write((ulong)o);
                    return;
                }
                if (type.Equals(typeof(bool)))
                {
                    writer.Write((bool)o);
                    return;
                }
                if (type.Equals(typeof(byte)))
                {
                    writer.Write((byte)o);
                    return;
                }
                if (type.Equals(typeof(sbyte)))
                {
                    writer.Write((sbyte)o);
                    return;
                }
                if (type.Equals(typeof(short)))
                {
                    writer.Write((short)o);
                    return;
                }
                if (type.Equals(typeof(ushort)))
                {
                    writer.Write((ushort)o);
                    return;
                }
                throw new NotSupportedException();
            }

            IDictionary dictionary = o as IDictionary;

            if (dictionary != null)
            {
                writer.WriteMapHeader(dictionary.Count);
                foreach (DictionaryEntry item in dictionary)
                {
                    Pack(writer, item.Key);
                    Pack(writer, item.Value);
                }
            }
            else
            {
                if (!type.IsArray)
                {
                    return;
                }
                Array array       = (Array)o;
                Type  elementType = type.GetElementType();
                if (elementType.IsGenericType && elementType.GetGenericTypeDefinition().Equals(KeyValuePairDefinitionType))
                {
                    PropertyInfo property  = elementType.GetProperty("Key");
                    PropertyInfo property2 = elementType.GetProperty("Value");
                    writer.WriteMapHeader(array.Length);
                    for (int i = 0; i < array.Length; i++)
                    {
                        object value = array.GetValue(i);
                        Pack(writer, property.GetValue(value, null));
                        Pack(writer, property2.GetValue(value, null));
                    }
                }
                else
                {
                    writer.WriteArrayHeader(array.Length);
                    for (int j = 0; j < array.Length; j++)
                    {
                        Pack(writer, array.GetValue(j));
                    }
                }
            }
        }
        //Yay, fun method!
        public Dictionary<object, object> Execute(string method, object[] args)
        {
            if (string.IsNullOrEmpty(_host))
                throw new Exception("Host null or empty");

            if (method != "auth.login" && string.IsNullOrEmpty(_token))
                throw new Exception("Not authenticated.");

            BoxingPacker boxingPacker = new BoxingPacker();
            CompiledPacker compiledPacker = new CompiledPacker(true);
            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => {return true;}; //dis be bad, no ssl check

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_host);
            request.ContentType = "binary/message-pack";
            request.Method = "POST";

            Stream requestStream = request.GetRequestStream();
            MsgPackWriter msgpackWriter = new MsgPackWriter(requestStream);

            msgpackWriter.WriteArrayHeader(args.Length + 1 + (string.IsNullOrEmpty(_token) ? 0 : 1));

            msgpackWriter.Write(method);

            if (!string.IsNullOrEmpty(_token))
                msgpackWriter.Write(_token);

            foreach (object arg in args)
            {
                if (arg is string)
                    msgpackWriter.Write(arg as string);
                else if (arg is Dictionary<object, object>)
                    msgpackWriter.Write(compiledPacker.Pack<Dictionary<object, object>>(arg as Dictionary<object, object>));
            }

            requestStream.Close();

            Stream responseStream = request.GetResponse().GetResponseStream();

            //everything is a bunch of bytes, needs to be typed
            Dictionary<object, object> resp = boxingPacker.Unpack(responseStream) as Dictionary<object, object>;

            //This is me trying to type the response for the user....
            Dictionary<object, object> returnDictionary = new Dictionary<object, object>();

            System.Text.Encoding enc = System.Text.Encoding.UTF8;
            foreach (KeyValuePair<object, object> pair in resp)
            {
                string keyType = pair.Key.GetType().ToString();
                string valueType = pair.Value.GetType().ToString();

                if (pair.Value.GetType() == typeof(bool))
                    returnDictionary.Add(enc.GetString(pair.Key as byte[]), ((bool)pair.Value).ToString());
                else if (pair.Value.GetType() == typeof(byte[]))
                    returnDictionary.Add(enc.GetString(pair.Key as byte[]), enc.GetString(pair.Value as byte[]));
                else if (pair.Value.GetType() == typeof(object[]))
                    returnDictionary.Add(enc.GetString(pair.Key as byte[]), pair.Value);
                else if (pair.Value.GetType() == typeof(UInt32))
                    returnDictionary.Add(enc.GetString(pair.Key as byte[]), ((UInt32)pair.Value).ToString());
                else if (pair.Value.GetType() == typeof(Int32))
                    returnDictionary.Add(enc.GetString(pair.Key as byte[]), ((Int32)pair.Value).ToString());
                else
                    throw new Exception("key type: " + keyType + ", value type: " + valueType);
            }

            return returnDictionary;
        }
Esempio n. 8
0
        private void Pack(MsgPackWriter writer, object o)
        {
            if (o == null)
            {
                writer.WriteNil();
                return;
            }
            Type         type = o.GetType();
            PackDelegate value;

            if (type.IsPrimitive)
            {
                if (type.Equals(typeof(int)))
                {
                    writer.Write((int)o);
                    return;
                }
                if (type.Equals(typeof(uint)))
                {
                    writer.Write((uint)o);
                    return;
                }
                if (type.Equals(typeof(float)))
                {
                    writer.Write((float)o);
                    return;
                }
                if (type.Equals(typeof(double)))
                {
                    writer.Write((double)o);
                    return;
                }
                if (type.Equals(typeof(long)))
                {
                    writer.Write((long)o);
                    return;
                }
                if (type.Equals(typeof(ulong)))
                {
                    writer.Write((ulong)o);
                    return;
                }
                if (type.Equals(typeof(bool)))
                {
                    writer.Write((bool)o);
                    return;
                }
                if (type.Equals(typeof(byte)))
                {
                    writer.Write((byte)o);
                    return;
                }
                if (type.Equals(typeof(sbyte)))
                {
                    writer.Write((sbyte)o);
                    return;
                }
                if (type.Equals(typeof(short)))
                {
                    writer.Write((short)o);
                    return;
                }
                if (type.Equals(typeof(ushort)))
                {
                    writer.Write((ushort)o);
                    return;
                }
                if (!type.Equals(typeof(char)))
                {
                    throw new NotSupportedException();
                }
                writer.Write((ushort)(char)o);
            }
            else if (PackerMapping.TryGetValue(type, out value))
            {
                value(this, writer, o);
            }
            else if (type.IsArray)
            {
                Array array = (Array)o;
                writer.WriteArrayHeader(array.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    Pack(writer, array.GetValue(i));
                }
            }
            else
            {
                ReflectionCacheEntry reflectionCacheEntry = ReflectionCache.Lookup(type);
                writer.WriteMapHeader(reflectionCacheEntry.FieldMap.Count);
                foreach (KeyValuePair <string, FieldInfo> item in reflectionCacheEntry.FieldMap)
                {
                    writer.Write(item.Key, _buf);
                    object value2 = item.Value.GetValue(o);
                    if (item.Value.FieldType.IsInterface && value2 != null)
                    {
                        writer.WriteArrayHeader(2);
                        writer.Write(value2.GetType().FullName);
                    }
                    Pack(writer, value2);
                }
            }
        }
Esempio n. 9
0
        void Pack(MsgPackWriter writer, object o)
        {
            if (o == null)
            {
                writer.WriteNil();
                return;
            }

            Type t = o.GetType();

            if (t.IsPrimitive)
            {
                if (t.Equals(typeof(int)))
                {
                    writer.Write((int)o);
                }
                else if (t.Equals(typeof(uint)))
                {
                    writer.Write((uint)o);
                }
                else if (t.Equals(typeof(float)))
                {
                    writer.Write((float)o);
                }
                else if (t.Equals(typeof(double)))
                {
                    writer.Write((double)o);
                }
                else if (t.Equals(typeof(long)))
                {
                    writer.Write((long)o);
                }
                else if (t.Equals(typeof(ulong)))
                {
                    writer.Write((ulong)o);
                }
                else if (t.Equals(typeof(bool)))
                {
                    writer.Write((bool)o);
                }
                else if (t.Equals(typeof(byte)))
                {
                    writer.Write((byte)o);
                }
                else if (t.Equals(typeof(sbyte)))
                {
                    writer.Write((sbyte)o);
                }
                else if (t.Equals(typeof(short)))
                {
                    writer.Write((short)o);
                }
                else if (t.Equals(typeof(ushort)))
                {
                    writer.Write((ushort)o);
                }
                else if (t.Equals(typeof(char)))
                {
                    writer.Write((ushort)(char)o);
                }
                else
                {
                    throw new NotSupportedException();
                }
                return;
            }

            PackDelegate packer;

            if (PackerMapping.TryGetValue(t, out packer))
            {
                packer(this, writer, o);
                return;
            }

            if (o is Packable)
            {
                Packable p = o as Packable;
                p.Pack(this, writer);
                return;
            }

            if (t.IsArray)
            {
                Array ary = (Array)o;
                writer.WriteArrayHeader(ary.Length);
                for (int i = 0; i < ary.Length; i++)
                {
                    Pack(writer, ary.GetValue(i));
                }
                return;
            }

            if (typeof(IList).IsAssignableFrom(t))
            {
                IList list = (IList)o;
                writer.WriteArrayHeader(list.Count);

                for (int i = 0; i < list.Count; i++)
                {
                    Pack(writer, list[i]);
                }
                return;
            }

            if (t.IsGenericType)
            {
                var setType = typeof(ISet <>).MakeGenericType(t.GetGenericArguments()[0]);
                if (setType.IsAssignableFrom(t))
                {
                    IEnumerable list = (IEnumerable)o;
                    writer.WriteArrayHeader((int)t.GetProperty("Count").GetValue(o, null));

                    foreach (object e in list)
                    {
                        Pack(writer, e);
                    }
                    return;
                }
            }

            if (typeof(IDictionary).IsAssignableFrom(t))
            {
                IDictionary dict = (IDictionary)o;
                writer.WriteArrayHeader(dict.Count);

                IDictionaryEnumerator enumerator = dict.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    Pack(writer, enumerator.Key);
                    Pack(writer, enumerator.Value);
                }

                return;
            }

            ReflectionCacheEntry entry = ReflectionCache.Lookup(t);

            writer.WriteMapHeader(entry.FieldMap.Count);
            foreach (KeyValuePair <string, FieldInfo> pair in entry.FieldMap)
            {
                writer.Write(pair.Key, _buf);
                object v = pair.Value.GetValue(o);
                if (pair.Value.FieldType.IsInterface && v != null)
                {
                    writer.WriteArrayHeader(2);
                    writer.Write(v.GetType().FullName);
                }
                Pack(writer, v);
            }
        }