예제 #1
0
        /// <summary>
        /// Should be executed when bytes are recieved from the Server. You have to call it manually.
        /// </summary>
        /// <param name="bytes">Data recieved</param>
        public void OnBytesRecieved(byte[] bytes)
        {
            KNetUtils.PacketType     packetType;
            KNetUtils.PacketDataType dataType;
            short senderId;

            KNetUtils.DeconstructPacket(ref bytes, out packetType, out dataType, out senderId);

            MemoryStream    ms = new MemoryStream(bytes);
            BinaryFormatter bf = new BinaryFormatter();

            //KNetUtils.RecievePacket(packetType)
            switch (packetType)
            {
            case KNetUtils.PacketType.welcome:
                RecieveWelcomePacket(bytes);
                break;

            case KNetUtils.PacketType.bufferedObject:
                RecieveBufferedObject((KNetSerializedField)bf.Deserialize(ms));
                break;

            case KNetUtils.PacketType.rpc:
                KNetRpc rpc = (KNetRpc) new KNetRpc().KNetDeserialize(bytes);
                rpc.Execute();
                break;
            }
        }
예제 #2
0
        internal void SendWelcomePacket(KNetConnection newClient)
        {
            object[] welcomePacket = new object[2];
            string[] viewTypeNames = new string[KlivenNet.Views.Count];
            int[]    viewIds       = new int[KlivenNet.Views.Count];
            // byte[][] viewOwnersData = new byte[KlivenNet.Views.Count][];
            for (int i = 0; i < KlivenNet.Views.Count; i++)
            {
                var view = KlivenNet.Views[i];
                viewTypeNames[i] = view.GetType().FullName;
                viewIds[i]       = view.Id;
                // viewOwnersData[i] = view.Owner.KNetSerializeReference();
            }
            //  welcomePacket[0] = KlivenNet.NextPlayerId;
            welcomePacket[0] = viewTypeNames;
            welcomePacket[1] = viewIds;
            // welcomePacket[2] = viewOwnersData;
            MemoryStream    ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();

            bf.Serialize(ms, welcomePacket);
            var packet = KNetUtils.ConstructPacket(KNetUtils.PacketType.welcome, ms.GetBuffer());

            SendBytes(newClient, packet);
            SendBufferedValues(newClient);

            /*KlivenNet.ZeroView.RPC("JebnijSobie", newClient, "monsterka kurwa", new int[] { 1, 2, 3 }, new KNetPlayer(), 2137);
             * KlivenNet.ZeroView.RPC("JebnijSobie", newClient, "monsterka kurwa2", new int[] { 3, 21, 3222 }, new KNetPlayer(), 2167);*/
            KlivenNet.ZeroView.RPC("ZaszczekajPajacu", new KNetConnection[] { newClient }, "paulina");
        }
예제 #3
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);
         }
     }
 }
예제 #4
0
 public void GetObjectData(SerializationInfo info, StreamingContext context)
 {
     info.AddValue("counts", Counts);
     info.AddValue("rpcInfo", RpcInfo.KNetSerializeReference());
     byte[][] serializedArgs = new byte[Arguments.Length][];
     for (int i = 0; i < serializedArgs.Length; i++)
     {
         serializedArgs[i] = KNetUtils.Serialize(Arguments[i], RpcInfo.ArgSerializationTypes[i], out Counts[i]);
     }
     info.AddValue("args", serializedArgs);
 }
예제 #5
0
        public KNetRpc(SerializationInfo info, StreamingContext context)
        {
            Counts    = (int[])info.GetValue("counts", typeof(int[]));
            RpcInfo   = (KNetRpcInfo) new KNetRpcInfo().KNetDeserializeReference((byte[])info.GetValue("rpcInfo", typeof(byte[])));
            Arguments = new object[RpcInfo.ArgumentsTypes.Length];

            byte[][] serializedArgs = (byte[][])info.GetValue("args", typeof(byte[][]));
            for (int i = 0; i < serializedArgs.Length; i++)
            {
                Arguments[i] = KNetUtils.Deserialize(serializedArgs[i], Counts[i], RpcInfo.ArgSerializationTypes[i], RpcInfo.ArgumentsTypes[i]);
            }
        }
예제 #6
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));
        }
예제 #7
0
        public static KNetRpcInfo CreateRpcInfo(short id, KNetView view, MethodInfo methodInfo)
        {
            KNetRpcInfo info = new KNetRpcInfo();
            var         args = methodInfo.GetParameters().OrderBy(e => e.Position);

            if (args.Where(e => KNetUtils.IsSerializable(e.ParameterType) == SerializableType.nonSerializable).Any())
            {
                return(null);
            }
            info.methodInfo            = methodInfo;
            info.Name                  = methodInfo.Name;
            info.ArgumentsInfo         = args.ToArray();
            info.ArgumentsTypes        = new Type[info.ArgumentsInfo.Length];
            info.ArgSerializationTypes = new SerializableType[info.ArgumentsInfo.Length];
            for (int i = 0; i < info.ArgumentsInfo.Length; i++)
            {
                info.ArgumentsTypes[i]        = info.ArgumentsInfo[i].ParameterType;
                info.ArgSerializationTypes[i] = KNetUtils.IsSerializable(info.ArgumentsTypes[i]);
            }
            info.Id       = id;
            info.kNetView = view;
            return(info);
        }