コード例 #1
0
ファイル: NetClient.cs プロジェクト: moto2002/DinaGameClient
        private void HandlerMessage(byte[] recvBuffer, int nRecvSize)
        {
            MemoryStream stream = new MemoryStream(recvBuffer);
            BinaryReader read = new BinaryReader(stream);

            while (stream.Position != stream.Length && stream.Position < nRecvSize)
            {
                int protocolID = read.PeekChar();
                //log.Debug("receive protocolID=" + protocolID);

                ProcessInfo processInfo = mapping.GetProcessInfo(protocolID);
                log.Assert(processInfo != null);

                Type type = processInfo.type;

                
                if (type == null)
                {
                    log.Error("协议没解释=" + protocolID + "  LastProtocolId: " + (lastProcessInfo != null?lastProcessInfo.protocolId:0));
					return;
                }

                KProtoBuf protoBuf = (KProtoBuf)Activator.CreateInstance(type);
                protoBuf.unpack(read);
                if (processInfo == null)
                {
                    log.Error("未处理的消息 " + protocolID);
                }
				
				if (ConfigManager.GetInstance().DebugMode)
				{
					protoBuf.AddHandler(processInfo);
	                protoBuf.DispatchDelegate();
	                protoBuf.RemoveHandler(processInfo);
				}
				else
				{
					try
	                {
	                    protoBuf.AddHandler(processInfo);
	                    protoBuf.DispatchDelegate();
	                    protoBuf.RemoveHandler(processInfo);
	                }
	                catch (System.Exception e)
	                {
	                    Debug.LogError("处理协议 " + (KS2C_Protocol)protocolID + " 异常" + e.ToString());
	                }
					
				}
                

                lastProcessInfo = processInfo;
            }
        }
コード例 #2
0
ファイル: NetClient.cs プロジェクト: moto2002/DinaGameClient
 public void SendMessage( KProtoBuf proto)
 {
     long begin = sender.Seek(0, SeekOrigin.Begin);
     sender.Write((short)0);
     proto.pack(sender);
     long end = sender.Seek(0, SeekOrigin.Current); 
     long len = end - begin;
     sender.Seek(0, SeekOrigin.Begin);
     sender.Write((short)len); 
     sender.Seek((int)len, SeekOrigin.Current);
     sender.Flush();
     clientSocket.Send(sendBuffer, (int)len, SocketFlags.None);
 }
コード例 #3
0
ファイル: KProtoBuf.cs プロジェクト: moto2002/DinaGameClient
        public void unpack(BinaryReader buffer)
        {
            KSign  sign   = null;
            int    j      = 0;
            object reader = 0;

            for (int i = 0; i < signs.Count; i++)
            {
                sign = signs[i];
                int count = sign.count;
                if (count == 0)                //0表示是变长
                {
                    count = buffer.ReadInt16();
                }

                switch (sign.type)
                {
                case "System.Boolean":                         //bool  1个字节
                {
                    if (sign.count == 1)
                    {
                        SetPropertyValue(sign.name, buffer.ReadBoolean());
                    }
                    else if (sign.count == 0)
                    {
                        List <bool> list = new List <bool>();
                        for (j = 0; j < count; j++)
                        {
                            list.Add(buffer.ReadBoolean());
                        }
                        SetPropertyValue(sign.name, list);
                    }
                    else
                    {
                        bool[] list = new bool[count];
                        for (j = 0; j < count; j++)
                        {
                            list[j] = buffer.ReadBoolean();
                        }
                        SetPropertyValue(sign.name, list);
                    }
                    break;
                }

                case "System.Int32":
                {
                    if (sign.count == 1)
                    {
                        switch (sign.bytes)
                        {
                        case 1:
                            reader = buffer.ReadSByte();
                            break;

                        case 2:
                            reader = buffer.ReadInt16();
                            break;

                        case 4:
                            reader = buffer.ReadInt32();
                            break;

                        case 8:
                            reader = buffer.ReadInt64();
                            break;

                        default:
                            log.Assert(false);
                            break;
                        }
                        SetPropertyValue(sign.name, reader);
                    }
                    else if (sign.count == 0)
                    {
                        switch (sign.bytes)
                        {
                        case 1:
                            List <sbyte> sbyteList = new List <sbyte>();
                            for (j = 0; j < count; j++)
                            {
                                sbyteList.Add(buffer.ReadSByte());
                            }
                            SetPropertyValue(sign.name, sbyteList);
                            break;

                        case 2:
                            List <short> shortList = new List <short>();
                            for (j = 0; j < count; j++)
                            {
                                shortList.Add(buffer.ReadInt16());
                            }
                            SetPropertyValue(sign.name, shortList);
                            break;

                        case 4:
                            List <int> intList = new List <int>();
                            for (j = 0; j < count; j++)
                            {
                                intList.Add(buffer.ReadInt32());
                            }
                            SetPropertyValue(sign.name, intList);
                            break;

                        case 8:
                            List <long> longList = new List <long>();
                            for (j = 0; j < count; j++)
                            {
                                longList.Add(buffer.ReadInt64());
                            }
                            SetPropertyValue(sign.name, longList);
                            break;

                        default:
                            log.Assert(false);
                            break;
                        }
                    }
                    else
                    {
                        switch (sign.bytes)
                        {
                        case 1:
                            sbyte[] sbyteList = new sbyte[count];
                            for (j = 0; j < count; j++)
                            {
                                sbyteList[j] = buffer.ReadSByte();
                            }
                            SetPropertyValue(sign.name, sbyteList);
                            break;

                        case 2:
                            short[] shortList = new short[count];
                            for (j = 0; j < count; j++)
                            {
                                shortList[j] = buffer.ReadInt16();
                            }
                            SetPropertyValue(sign.name, shortList);
                            break;

                        case 4:
                            int[] intList = new int[count];
                            for (j = 0; j < count; j++)
                            {
                                intList[j] = buffer.ReadInt32();
                            }
                            SetPropertyValue(sign.name, intList);
                            break;

                        case 8:
                            long[] longList = new long[count];
                            for (j = 0; j < count; j++)
                            {
                                longList[j] = buffer.ReadInt64();
                            }
                            SetPropertyValue(sign.name, longList);
                            break;

                        default:
                            log.Assert(false);
                            break;
                        }
                    }
                    break;
                }

                case "System.UInt32":
                {
                    if (sign.count == 1)
                    {
                        switch (sign.bytes)
                        {
                        case 1:
                            reader = buffer.ReadByte();
                            break;

                        case 2:
                            reader = buffer.ReadUInt16();
                            break;

                        case 4:
                            reader = buffer.ReadUInt32();
                            break;

                        case 8:
                            reader = buffer.ReadUInt64();
                            break;

                        default:
                            log.Assert(false);
                            break;
                        }
                        SetPropertyValue(sign.name, reader);
                    }
                    else if (sign.count == 0)
                    {
                        switch (sign.bytes)
                        {
                        case 1:
                            List <byte> byteList = new List <byte>();
                            for (j = 0; j < count; j++)
                            {
                                byteList.Add(buffer.ReadByte());
                            }
                            SetPropertyValue(sign.name, byteList);
                            break;

                        case 2:
                            List <ushort> ushortList = new List <ushort>();
                            for (j = 0; j < count; j++)
                            {
                                ushortList.Add(buffer.ReadUInt16());
                            }
                            SetPropertyValue(sign.name, ushortList);
                            break;

                        case 4:
                            List <uint> uintList = new List <uint>();
                            for (j = 0; j < count; j++)
                            {
                                uintList.Add(buffer.ReadUInt32());
                            }
                            SetPropertyValue(sign.name, uintList);
                            break;

                        case 8:
                            List <ulong> ulongList = new List <ulong>();
                            for (j = 0; j < count; j++)
                            {
                                ulongList.Add(buffer.ReadUInt64());
                            }
                            SetPropertyValue(sign.name, ulongList);
                            break;

                        default:
                            log.Assert(false);
                            break;
                        }
                    }
                    else
                    {
                        switch (sign.bytes)
                        {
                        case 1:
                            byte[] byteList = new byte[count];
                            for (j = 0; j < count; j++)
                            {
                                byteList[j] = buffer.ReadByte();
                            }
                            SetPropertyValue(sign.name, byteList);
                            break;

                        case 2:
                            ushort[] ushortList = new ushort[count];
                            for (j = 0; j < count; j++)
                            {
                                ushortList[j] = buffer.ReadUInt16();
                            }
                            SetPropertyValue(sign.name, ushortList);
                            break;

                        case 4:
                            uint[] uintList = new uint[count];
                            for (j = 0; j < count; j++)
                            {
                                uintList[j] = buffer.ReadUInt32();
                            }
                            SetPropertyValue(sign.name, uintList);
                            break;

                        case 8:
                            ulong[] ulongList = new ulong[count];
                            for (j = 0; j < count; j++)
                            {
                                ulongList[j] = buffer.ReadUInt64();
                            }
                            SetPropertyValue(sign.name, ulongList);
                            break;

                        default:
                            log.Assert(false);
                            break;
                        }
                    }
                    break;
                }

                case "System.Double":
                {
                    if (sign.bytes != 8)
                    {
                        log.Assert(false);
                    }
                    if (sign.count == 1)
                    {
                        reader = buffer.ReadDouble();
                        SetPropertyValue(sign.name, reader);
                    }
                    else if (sign.count == 0)
                    {
                        List <double> doubleList = new List <double>();
                        for (j = 0; j < count; j++)
                        {
                            doubleList.Add(buffer.ReadDouble());
                        }
                        SetPropertyValue(sign.name, doubleList);
                    }
                    else
                    {
                        double[] doubleList = new double[count];
                        for (j = 0; j < count; j++)
                        {
                            doubleList[j] = buffer.ReadDouble();
                        }
                        SetPropertyValue(sign.name, doubleList);
                    }
                    break;
                }

                case "System.Single":
                {
                    if (sign.bytes != 4)
                    {
                        log.Assert(false);
                    }
                    if (sign.count == 1)
                    {
                        reader = buffer.ReadSingle();
                        SetPropertyValue(sign.name, reader);
                    }
                    else if (sign.count == 0)
                    {
                        List <float> floatList = new List <float>();
                        for (j = 0; j < count; j++)
                        {
                            floatList.Add(buffer.ReadSingle());
                        }
                        SetPropertyValue(sign.name, floatList);
                    }
                    else
                    {
                        float[] floatList = new float[count];
                        for (j = 0; j < count; j++)
                        {
                            floatList[j] = buffer.ReadSingle();
                        }
                        SetPropertyValue(sign.name, floatList);
                    }
                    break;
                }

                case "System.String":
                {
                    if (sign.count == 1)
                    {
                        SetPropertyValue(sign.name, ReadString(buffer, sign.bytes));
                    }
                    else if (sign.count == 0)
                    {
                        List <string> list = new List <string>();
                        for (j = 0; j < count; j++)
                        {
                            list.Add(ReadString(buffer, sign.bytes));
                        }
                        SetPropertyValue(sign.name, list);
                    }
                    else
                    {
                        string[] list = new string[count];
                        for (j = 0; j < count; j++)
                        {
                            list[j] = ReadString(buffer, sign.bytes);
                        }
                        SetPropertyValue(sign.name, list);
                    }
                    break;
                }

                case "System.IO.MemoryStream":
                {
                    SetPropertyValue(sign.name, ReadMemoryStream(buffer, sign.bytes));
                    break;
                }

                default:
                {
                    Type type = Type.GetType(sign.type);
                    log.Assert(sign.bytes == 0);
                    if (sign.count == 1)
                    {
                        KProtoBuf obj = Activator.CreateInstance(type) as KProtoBuf;
                        obj.unpack(buffer);
                        SetPropertyValue(sign.name, obj);
                    }
                    else if (sign.count == 0)
                    {
                        log.Assert(type != null, "undefine objClass: ");
                        Assembly   _Assembly  = Assembly.Load("mscorlib");
                        Type       _TypeList  = _Assembly.GetType("System.Collections.Generic.List`1[[" + type.FullName + "," + type.Assembly.FullName + "]]");
                        object     _List      = System.Activator.CreateInstance(_TypeList);
                        MethodInfo methodinfo = _TypeList.GetMethod("Add");
                        for (j = 0; j < count; j++)
                        {
                            KProtoBuf obj = Activator.CreateInstance(type) as KProtoBuf;
                            obj.unpack(buffer);
                            object[] parameters = new object[1];
                            parameters[0] = obj;
                            methodinfo.Invoke(_List, parameters);
                        }
                        SetPropertyValue(sign.name, _List);
                    }
                    else
                    {
                        log.Assert(type != null, "undefine objClass: ");
                        Assembly _Assembly  = Assembly.Load("mscorlib");
                        Type     _TypeArray = Type.GetType(type.FullName + "[]");

                        object     _Array     = System.Activator.CreateInstance(_TypeArray, count);
                        MethodInfo methodinfo = _TypeArray.GetMethod("SetValue", new Type[] { typeof(object), typeof(int) });
                        for (j = 0; j < count; j++)
                        {
                            KProtoBuf obj = Activator.CreateInstance(type) as KProtoBuf;
                            obj.unpack(buffer);
                            object[] parameters = new object[2];
                            parameters[0] = obj;
                            parameters[1] = j;
                            methodinfo.Invoke(_Array, parameters);
                        }
                        SetPropertyValue(sign.name, _Array);
                    }
                    break;
                }
                }
            }
        }
コード例 #4
0
 /// <summary>
 /// 构造器
 /// </summary>
 /// <param name="protoID"></param>
 /// <param name="type"></param>
 public ProcessInfo(int protoID, Type type)
 {
     protocolID   = protoID;
     protocolData = (KProtoBuf)Activator.CreateInstance(type);
 }