Ejemplo n.º 1
0
 //public static List<KNetSerializedField> DEBUG_SEND_BUFFERED_VALUES() {
 //    return SendBufferedValues();
 //}
 internal void SendBufferedValues(KNetConnection newClient)
 {
     //List<KNetSerializedField> serializedFields = new List<KNetSerializedField>();
     foreach (var view in KlivenNet.Views)
     {
         int buffFieldId = -1;
         foreach (var bufferedField in view.BufferedFields)
         {
             buffFieldId++;
             var fieldType  = bufferedField.FieldType;
             var bufferable = KNetUtils.IsSerializable(fieldType);
             if (bufferable == SerializableType.nonSerializable)
             {
                 KNetLogger.LogError($"KNetServer: could not serialize field {bufferedField.Name} on KNetView {view.Id}: does {bufferedField.DeclaringType.Name} implement KNetSerializable interface?");
                 continue;
             }
             // Console.WriteLine(bufferedField.Name + " is bufferable: " + bufferable);
             var bytes = KNetUtils.Serialize(bufferedField.GetValue(view), bufferable, out int count);
             if (bytes == null)
             {
                 continue;
             }
             var serializedField = new KNetSerializedField(view.Id, buffFieldId, bytes);
             serializedField.count = count;
             var packet = KNetUtils.ConstructPacket(KNetUtils.PacketType.bufferedObject, serializedField);
             SendBytes(newClient, packet);
         }
     }
 }
Ejemplo n.º 2
0
 public void Execute()
 {
     if (RpcInfo == null)
     {
         KNetLogger.LogError("KNetRpc: Cound not execute: RpcInfo is null.");
     }
     RpcInfo.methodInfo.Invoke(RpcInfo.kNetView, Arguments);
 }
Ejemplo n.º 3
0
 protected virtual void OnConnectedToServer()
 {
     if (KlivenNet.IsConnected || KlivenNet.IsServer)
     {
         //KNetLogger.LogError("KlivenNetworking: Cannot start client instance, because other instance (server or client) is already running.");
         throw new Exception("KlivenNetworking: Cannot start client instance, because other instance (server or client) is already running on that application. Note that KlivenNetworking does not support Host mode (client & server at once)");
     }
     KlivenNet.ClientInstnace = this;
     KNetLogger.Log("KlivenNetwroking: Connected to the server.");
 }
Ejemplo n.º 4
0
        internal static byte[] Serialize(object kNetSerializable, SerializableType serializableType, out int count)
        {
            count = 1;
            var bufferable = (int)serializableType;

            if (bufferable == 0)
            {
                KNetLogger.LogWarning($"KNetUtils: could not serialize {kNetSerializable} : Non supported type");
                return(null);
            }
            if (kNetSerializable == null)
            {
                // Console.WriteLine($"{fieldType} is null, not buffering.");
                return(null);
            }
            byte[] buffer = null;
            if (bufferable == 1)
            {
                MemoryStream    ms = new MemoryStream();
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, kNetSerializable);
                return(ms.GetBuffer());
            }
            else if (bufferable == 2)
            {
                buffer = ((IKNetSerializable)kNetSerializable).KNetSerialize();
                if (buffer != null && buffer.Length > 0)
                {
                    return(buffer);
                }
            }
            else if (bufferable == 3 || bufferable == 4)
            {
                List <byte[]> serialized = new List <byte[]>();
                foreach (var element in (IEnumerable <IKNetSerializable>)kNetSerializable)
                {
                    if (element != null)
                    {
                        serialized.Add(element.KNetSerialize());
                    }
                }

                MemoryStream    ms = new MemoryStream();
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, serialized);
                buffer = ms.GetBuffer();
                if (buffer != null && buffer.Length > 0)
                {
                    count = serialized.Count;
                    return(buffer);
                }
            }
            return(null);
        }
Ejemplo n.º 5
0
 public static KNetPlayer FindPlayer(int connectionId)
 {
     for (int i = 0; i < Players.Count; i++)
     {
         if (Players[i].Connection.Id == connectionId)
         {
             return(Players[i]);
         }
     }
     KNetLogger.LogWarning($"No KNetPlayer of connectionId = {connectionId} was found.");
     return(null);
 }
Ejemplo n.º 6
0
 public static KNetView FindView(int id)
 {
     for (int i = 0; i < Views.Count; i++)
     {
         if (Views[i].Id == id)
         {
             return(Views[i]);
         }
     }
     KNetLogger.LogWarning($"No KNetView of id {id} was found.");
     return(null);
 }
Ejemplo n.º 7
0
        internal static object Deserialize(byte[] kNetSerialized, int count, SerializableType serializedType, Type objRealType)
        {
            MemoryStream    ms = new MemoryStream(kNetSerialized);
            BinaryFormatter bf = new BinaryFormatter();

            if (serializedType == 0)
            {
                KNetLogger.LogError($"KNetClient: could not Recieve buffered object: could not deserialize field {objRealType.Name}: Type not supported");
                return(null);
            }
            if (serializedType == SerializableType.primitive)
            {
                return(bf.Deserialize(ms));
            }
            else if (serializedType == SerializableType.kNetSerializable)
            {
                return(typeof(IKNetSerializable).GetMethod("KNetDeserialize")
                       .Invoke(Activator.CreateInstance(objRealType), new object[] { kNetSerialized }));
            }
            else if (serializedType == SerializableType.array)
            {
                List <byte[]> serialized   = (List <byte[]>)bf.Deserialize(ms);
                var           elemType     = objRealType.GetElementType();
                var           deserialized = Array.CreateInstance(elemType, count);
                int           index        = 0;
                for (int i = 0; i < count; i++)
                {
                    object deserializedObject = typeof(IKNetSerializable).GetMethod("KNetDeserialize")
                                                .Invoke(Activator.CreateInstance(elemType), new object[] { serialized[i] });

                    deserialized.SetValue(deserializedObject, index);
                    index++;
                }
                return(deserialized);
            }
            else if (serializedType == SerializableType.list)
            {
                List <byte[]> serialized   = (List <byte[]>)bf.Deserialize(ms);
                var           genArgType   = objRealType.GetGenericArguments()[0];
                var           deserialized = Activator.CreateInstance(typeof(List <>).MakeGenericType(genArgType), count);
                var           listType     = deserialized.GetType();
                for (int i = 0; i < count; i++)
                {
                    object deserializedObject = typeof(IKNetSerializable).GetMethod("KNetDeserialize")
                                                .Invoke(Activator.CreateInstance(genArgType), new object[] { serialized[i] });

                    listType.GetMethod("Add").Invoke(deserialized, new object[] { deserializedObject });
                }
                return(deserialized);
            }
            return(null);
        }
Ejemplo n.º 8
0
        public static int AddView(KNetView view)
        {
            if (IsServer == false)
            {
                KNetLogger.LogError("KlivenNet.AddView() is only available on Server. On clients Views are synchronized automatically.");
                return(-1);
            }
            int id = Views.Count;

            view.Init(id);
            Views.Add(view);
            return(id);
        }
Ejemplo n.º 9
0
        internal static void RecieveBufferedObject(KNetSerializedField serializedField)
        {
            KNetView view = KlivenNet.FindView(serializedField.viewId);

            if (view == null)
            {
                KNetLogger.LogError($"KNetClient: could not Recieve buffered object: KNetView of id {serializedField.viewId}: view does not exist.");
                return;
            }
            var field = serializedField.fieldId < view.BufferedFields.Length ? view.BufferedFields[serializedField.fieldId] : null;

            if (field == null)
            {
                KNetLogger.LogError($"KNetClient: could not Recieve buffered object: KNetView of id {serializedField.viewId}: field id {serializedField.fieldId} is not defined.");
                return;
            }
            field.SetValue(view, KNetUtils.Deserialize(serializedField.data, serializedField.count
                                                       , KNetUtils.IsSerializable(field.FieldType), field.FieldType));
        }
Ejemplo n.º 10
0
 void JebnijSobie(string monsterka, int[] raz, KNetPlayer dwa, int trzy)
 {
     KNetLogger.Log($"jebnij sobie {monsterka}, {raz.Length}, {dwa}, {trzy}");
     KNetLogger.Log(string.Join(", ", raz));
 }